Skip to content

Commit

Permalink
[PCH] Support writing BuiltinBitCastExprs to PCHs
Browse files Browse the repository at this point in the history
eee944e adds the new BuiltinBitCastExpr, but does not set the Code member of
ASTStmtWriter. This is not correct and causes an assertion failue in
ASTStmtWriter::emit() when building PCHs that contain __builtin_bit_cast.  This
commit adds serialization::EXPR_BUILTIN_BIT_CAST and handles
ASTStmtWriter::Code properly.

Differential revision: https://reviews.llvm.org/D80360
  • Loading branch information
hyd-dev authored and epilk committed Jun 11, 2020
1 parent eedd8fe commit 95d7ccb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/AST/ExprCXX.h
Original file line number Diff line number Diff line change
Expand Up @@ -4821,6 +4821,8 @@ class BuiltinBitCastExpr final
: ExplicitCastExpr(BuiltinBitCastExprClass, T, VK, CK, SrcExpr, 0,
DstType),
KWLoc(KWLoc), RParenLoc(RParenLoc) {}
BuiltinBitCastExpr(EmptyShell Empty)
: ExplicitCastExpr(BuiltinBitCastExprClass, Empty, 0) {}

SourceLocation getBeginLoc() const LLVM_READONLY { return KWLoc; }
SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Serialization/ASTBitCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,9 @@ class TypeIdx {
/// A CXXFunctionalCastExpr record.
EXPR_CXX_FUNCTIONAL_CAST,

/// A BuiltinBitCastExpr record.
EXPR_BUILTIN_BIT_CAST,

/// A UserDefinedLiteral record.
EXPR_USER_DEFINED_LITERAL,

Expand Down
5 changes: 5 additions & 0 deletions clang/lib/Serialization/ASTReaderStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3618,6 +3618,11 @@ Stmt *ASTReader::ReadStmtFromStream(ModuleFile &F) {
/*PathSize*/ Record[ASTStmtReader::NumExprFields]);
break;

case EXPR_BUILTIN_BIT_CAST:
assert(Record[ASTStmtReader::NumExprFields] == 0 && "Wrong PathSize!");
S = new (Context) BuiltinBitCastExpr(Empty);
break;

case EXPR_USER_DEFINED_LITERAL:
S = UserDefinedLiteral::CreateEmpty(
Context, /*NumArgs=*/Record[ASTStmtReader::NumExprFields], Empty);
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Serialization/ASTWriterStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,7 @@ void ASTStmtWriter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *E) {
VisitExplicitCastExpr(E);
Record.AddSourceLocation(E->getBeginLoc());
Record.AddSourceLocation(E->getEndLoc());
Code = serialization::EXPR_BUILTIN_BIT_CAST;
}

void ASTStmtWriter::VisitUserDefinedLiteral(UserDefinedLiteral *E) {
Expand Down
19 changes: 19 additions & 0 deletions clang/test/PCH/builtin-bit-cast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %clang_cc1 -emit-pch -o %t %s
// RUN: %clang_cc1 -include-pch %t -fsyntax-only -verify %s
// expected-no-diagnostics

#ifndef HEADER
#define HEADER

template <class T, class U>
constexpr T BuiltinBitCastWrapper(const U &Arg) {
return __builtin_bit_cast(T, Arg);
}

#else

int main() {
return BuiltinBitCastWrapper<int>(0);
}

#endif

0 comments on commit 95d7ccb

Please sign in to comment.