From 95d7ccb70b9cbd53f1f137c0b2411852c42c122b Mon Sep 17 00:00:00 2001 From: hyd-dev Date: Thu, 11 Jun 2020 12:30:16 -0400 Subject: [PATCH] [PCH] Support writing BuiltinBitCastExprs to PCHs eee944e7f 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 --- clang/include/clang/AST/ExprCXX.h | 2 ++ clang/include/clang/Serialization/ASTBitCodes.h | 3 +++ clang/lib/Serialization/ASTReaderStmt.cpp | 5 +++++ clang/lib/Serialization/ASTWriterStmt.cpp | 1 + clang/test/PCH/builtin-bit-cast.cpp | 19 +++++++++++++++++++ 5 files changed, 30 insertions(+) create mode 100644 clang/test/PCH/builtin-bit-cast.cpp diff --git a/clang/include/clang/AST/ExprCXX.h b/clang/include/clang/AST/ExprCXX.h index 56b27d5..379f762 100644 --- a/clang/include/clang/AST/ExprCXX.h +++ b/clang/include/clang/AST/ExprCXX.h @@ -4821,6 +4821,8 @@ public: : 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; } diff --git a/clang/include/clang/Serialization/ASTBitCodes.h b/clang/include/clang/Serialization/ASTBitCodes.h index 4008f11..c6f9f1d 100644 --- a/clang/include/clang/Serialization/ASTBitCodes.h +++ b/clang/include/clang/Serialization/ASTBitCodes.h @@ -1812,6 +1812,9 @@ public: /// A CXXFunctionalCastExpr record. EXPR_CXX_FUNCTIONAL_CAST, + /// A BuiltinBitCastExpr record. + EXPR_BUILTIN_BIT_CAST, + /// A UserDefinedLiteral record. EXPR_USER_DEFINED_LITERAL, diff --git a/clang/lib/Serialization/ASTReaderStmt.cpp b/clang/lib/Serialization/ASTReaderStmt.cpp index 5c7bc7a..86895c3 100644 --- a/clang/lib/Serialization/ASTReaderStmt.cpp +++ b/clang/lib/Serialization/ASTReaderStmt.cpp @@ -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); diff --git a/clang/lib/Serialization/ASTWriterStmt.cpp b/clang/lib/Serialization/ASTWriterStmt.cpp index 5e445b6..45cd54f 100644 --- a/clang/lib/Serialization/ASTWriterStmt.cpp +++ b/clang/lib/Serialization/ASTWriterStmt.cpp @@ -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) { diff --git a/clang/test/PCH/builtin-bit-cast.cpp b/clang/test/PCH/builtin-bit-cast.cpp new file mode 100644 index 0000000..5755ce9 --- /dev/null +++ b/clang/test/PCH/builtin-bit-cast.cpp @@ -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 +constexpr T BuiltinBitCastWrapper(const U &Arg) { + return __builtin_bit_cast(T, Arg); +} + +#else + +int main() { + return BuiltinBitCastWrapper(0); +} + +#endif -- 2.7.4