From: Nico Weber Date: Wed, 2 Mar 2016 19:28:54 +0000 (+0000) Subject: Serialize `#pragma detect_mismatch`. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5493a102487f4b1d819e4a9c58f714a64811dd07;p=clang Serialize `#pragma detect_mismatch`. This is like r262493, but for pragma detect_mismatch instead of pragma comment. The two pragmas have similar behavior, so use the same approach for both. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@262506 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/ASTConsumer.h b/include/clang/AST/ASTConsumer.h index 7aea66bd06..7b5dd9be8a 100644 --- a/include/clang/AST/ASTConsumer.h +++ b/include/clang/AST/ASTConsumer.h @@ -94,12 +94,6 @@ public: /// The default implementation passes it to HandleTopLevelDecl. virtual void HandleImplicitImportDecl(ImportDecl *D); - /// \brief Handle a pragma that emits a mismatch identifier and value to the - /// object file for the linker to work with. Currently, this only exists to - /// support Microsoft's #pragma detect_mismatch. - virtual void HandleDetectMismatch(llvm::StringRef Name, - llvm::StringRef Value) {} - /// CompleteTentativeDefinition - Callback invoked at the end of a translation /// unit to notify the consumer that the given tentative definition should be /// completed. diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index 569c23a678..314e8f9f04 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -138,6 +138,39 @@ public: static bool classofKind(Kind K) { return K == PragmaComment; } }; +/// \brief Represents a `#pragma detect_mismatch` line. Always a child of +/// TranslationUnitDecl. +class PragmaDetectMismatchDecl final + : public Decl, + private llvm::TrailingObjects { + virtual void anchor(); + + size_t ValueStart; + + friend TrailingObjects; + friend class ASTDeclReader; + friend class ASTDeclWriter; + + PragmaDetectMismatchDecl(TranslationUnitDecl *TU, SourceLocation Loc, + size_t ValueStart) + : Decl(PragmaDetectMismatch, TU, Loc), ValueStart(ValueStart) {} + +public: + static PragmaDetectMismatchDecl *Create(const ASTContext &C, + TranslationUnitDecl *DC, + SourceLocation Loc, StringRef Name, + StringRef Value); + static PragmaDetectMismatchDecl * + CreateDeserialized(ASTContext &C, unsigned ID, unsigned NameValueSize); + + StringRef getName() const { return getTrailingObjects(); } + StringRef getValue() const { return getTrailingObjects() + ValueStart; } + + // Implement isa/cast/dyncast/etc. + static bool classof(const Decl *D) { return classofKind(D->getKind()); } + static bool classofKind(Kind K) { return K == PragmaDetectMismatch; } +}; + /// \brief Declaration context for names declared as extern "C" in C++. This /// is neither the semantic nor lexical context for such declarations, but is /// used to check for conflicts with other extern "C" declarations. Example: diff --git a/include/clang/AST/RecursiveASTVisitor.h b/include/clang/AST/RecursiveASTVisitor.h index 76e870a4ee..a7d0c6a79b 100644 --- a/include/clang/AST/RecursiveASTVisitor.h +++ b/include/clang/AST/RecursiveASTVisitor.h @@ -1361,6 +1361,8 @@ DEF_TRAVERSE_DECL( DEF_TRAVERSE_DECL(PragmaCommentDecl, {}) +DEF_TRAVERSE_DECL(PragmaDetectMismatchDecl, {}) + DEF_TRAVERSE_DECL(ExternCContextDecl, {}) DEF_TRAVERSE_DECL(NamespaceAliasDecl, { diff --git a/include/clang/Basic/DeclNodes.td b/include/clang/Basic/DeclNodes.td index 9f4296132d..950eff8f7a 100644 --- a/include/clang/Basic/DeclNodes.td +++ b/include/clang/Basic/DeclNodes.td @@ -12,6 +12,7 @@ class DeclContext { } def TranslationUnit : Decl, DeclContext; def PragmaComment : Decl; +def PragmaDetectMismatch : Decl; def ExternCContext : Decl, DeclContext; def Named : Decl<1>; def Namespace : DDecl, DeclContext; diff --git a/include/clang/Frontend/MultiplexConsumer.h b/include/clang/Frontend/MultiplexConsumer.h index 2021a7676c..da14526633 100644 --- a/include/clang/Frontend/MultiplexConsumer.h +++ b/include/clang/Frontend/MultiplexConsumer.h @@ -44,8 +44,6 @@ public: void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) override; void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override; void HandleImplicitImportDecl(ImportDecl *D) override; - void HandleDetectMismatch(llvm::StringRef Name, - llvm::StringRef Value) override; void CompleteTentativeDefinition(VarDecl *D) override; void AssignInheritanceModel(CXXRecordDecl *RD) override; void HandleVTable(CXXRecordDecl *RD) override; diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 10fd485096..fc6e56174a 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -7676,7 +7676,8 @@ public: void ActOnPragmaDump(Scope *S, SourceLocation Loc, IdentifierInfo *II); /// ActOnPragmaDetectMismatch - Call on well-formed \#pragma detect_mismatch - void ActOnPragmaDetectMismatch(StringRef Name, StringRef Value); + void ActOnPragmaDetectMismatch(SourceLocation Loc, StringRef Name, + StringRef Value); /// ActOnPragmaUnused - Called on well-formed '\#pragma unused'. void ActOnPragmaUnused(const Token &Identifier, diff --git a/include/clang/Serialization/ASTBitCodes.h b/include/clang/Serialization/ASTBitCodes.h index b58f4157bd..82f0cbf938 100644 --- a/include/clang/Serialization/ASTBitCodes.h +++ b/include/clang/Serialization/ASTBitCodes.h @@ -1166,7 +1166,9 @@ namespace clang { /// \brief An OMPCapturedExprDecl record. DECL_OMP_CAPTUREDEXPR, /// \brief A PragmaCommentDecl record. - DECL_PRAGMA_COMMENT + DECL_PRAGMA_COMMENT, + /// \brief A PragmaDetectMismatchDecl record. + DECL_PRAGMA_DETECT_MISMATCH }; /// \brief Record codes for each kind of statement or expression. diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index 0579bca3fe..4c9ccd7cc9 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -8505,6 +8505,8 @@ bool ASTContext::DeclMustBeEmitted(const Decl *D) { return false; } else if (isa(D)) return true; + else if (isa(D)) + return true; else if (isa(D)) return true; else diff --git a/lib/AST/ASTDumper.cpp b/lib/AST/ASTDumper.cpp index e0ef80d475..872420606b 100644 --- a/lib/AST/ASTDumper.cpp +++ b/lib/AST/ASTDumper.cpp @@ -427,6 +427,7 @@ namespace { void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D); void VisitImportDecl(const ImportDecl *D); void VisitPragmaCommentDecl(const PragmaCommentDecl *D); + void VisitPragmaDetectMismatchDecl(const PragmaDetectMismatchDecl *D); // C++ Decls void VisitNamespaceDecl(const NamespaceDecl *D); @@ -1216,6 +1217,12 @@ void ASTDumper::VisitPragmaCommentDecl(const PragmaCommentDecl *D) { OS << " \"" << Arg << "\""; } +void ASTDumper::VisitPragmaDetectMismatchDecl( + const PragmaDetectMismatchDecl *D) { + OS << " \"" << D->getName() << "\" \"" << D->getValue() << "\""; +} + + //===----------------------------------------------------------------------===// // C++ Declarations //===----------------------------------------------------------------------===// diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index e22f1bcf0f..6451a5c4b1 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -3927,6 +3927,30 @@ PragmaCommentDecl *PragmaCommentDecl::CreateDeserialized(ASTContext &C, PragmaCommentDecl(nullptr, SourceLocation(), PCK_Unknown); } +void PragmaDetectMismatchDecl::anchor() { } + +PragmaDetectMismatchDecl * +PragmaDetectMismatchDecl::Create(const ASTContext &C, TranslationUnitDecl *DC, + SourceLocation Loc, StringRef Name, + StringRef Value) { + size_t ValueStart = Name.size() + 1; + PragmaDetectMismatchDecl *PDMD = + new (C, DC, additionalSizeToAlloc(ValueStart + Value.size() + 1)) + PragmaDetectMismatchDecl(DC, Loc, ValueStart); + memcpy(PDMD->getTrailingObjects(), Name.data(), Name.size()); + PDMD->getTrailingObjects()[Name.size()] = '\0'; + memcpy(PDMD->getTrailingObjects() + ValueStart, Value.data(), + Value.size()); + PDMD->getTrailingObjects()[ValueStart + Value.size()] = '\0'; + return PDMD; +} + +PragmaDetectMismatchDecl * +PragmaDetectMismatchDecl::CreateDeserialized(ASTContext &C, unsigned ID, + unsigned NameValueSize) { + return new (C, ID, additionalSizeToAlloc(NameValueSize + 1)) + PragmaDetectMismatchDecl(nullptr, SourceLocation(), 0); +} void ExternCContextDecl::anchor() { } diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp index 91bc809cf4..a572687afe 100644 --- a/lib/AST/DeclBase.cpp +++ b/lib/AST/DeclBase.cpp @@ -642,6 +642,7 @@ unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) { case StaticAssert: case ObjCPropertyImpl: case PragmaComment: + case PragmaDetectMismatch: case Block: case Captured: case TranslationUnit: diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp index f9a6b06a4a..c218ca41a1 100644 --- a/lib/CodeGen/CGDecl.cpp +++ b/lib/CodeGen/CGDecl.cpp @@ -72,6 +72,7 @@ void CodeGenFunction::EmitDecl(const Decl &D) { case Decl::ObjCProperty: case Decl::ObjCCompatibleAlias: case Decl::PragmaComment: + case Decl::PragmaDetectMismatch: case Decl::AccessSpec: case Decl::LinkageSpec: case Decl::ObjCPropertyImpl: diff --git a/lib/CodeGen/CodeGenAction.cpp b/lib/CodeGen/CodeGenAction.cpp index 8d08114490..c9e188a872 100644 --- a/lib/CodeGen/CodeGenAction.cpp +++ b/lib/CodeGen/CodeGenAction.cpp @@ -205,11 +205,6 @@ namespace clang { Gen->HandleVTable(RD); } - void HandleDetectMismatch(llvm::StringRef Name, - llvm::StringRef Value) override { - Gen->HandleDetectMismatch(Name, Value); - } - static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context, unsigned LocCookie) { SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie); diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 286ccb1595..cf9e29d46b 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -3748,6 +3748,12 @@ void CodeGenModule::EmitTopLevelDecl(Decl *D) { break; } + case Decl::PragmaDetectMismatch: { + const auto *PDMD = cast(D); + AddDetectMismatch(PDMD->getName(), PDMD->getValue()); + break; + } + case Decl::LinkageSpec: EmitLinkageSpec(cast(D)); break; diff --git a/lib/CodeGen/ModuleBuilder.cpp b/lib/CodeGen/ModuleBuilder.cpp index 968cc46f7e..041a15c562 100644 --- a/lib/CodeGen/ModuleBuilder.cpp +++ b/lib/CodeGen/ModuleBuilder.cpp @@ -233,11 +233,6 @@ namespace { Builder->EmitVTable(RD); } - - void HandleDetectMismatch(llvm::StringRef Name, - llvm::StringRef Value) override { - Builder->AddDetectMismatch(Name, Value); - } }; } diff --git a/lib/Frontend/MultiplexConsumer.cpp b/lib/Frontend/MultiplexConsumer.cpp index 95e9bec6a4..31c9b5f23f 100644 --- a/lib/Frontend/MultiplexConsumer.cpp +++ b/lib/Frontend/MultiplexConsumer.cpp @@ -317,11 +317,6 @@ void MultiplexConsumer::HandleImplicitImportDecl(ImportDecl *D) { Consumer->HandleImplicitImportDecl(D); } -void MultiplexConsumer::HandleDetectMismatch(llvm::StringRef Name, llvm::StringRef Value) { - for (auto &Consumer : Consumers) - Consumer->HandleDetectMismatch(Name, Value); -} - void MultiplexConsumer::CompleteTentativeDefinition(VarDecl *D) { for (auto &Consumer : Consumers) Consumer->CompleteTentativeDefinition(D); diff --git a/lib/Parse/ParsePragma.cpp b/lib/Parse/ParsePragma.cpp index 06c1a17bdd..a7ea206d09 100644 --- a/lib/Parse/ParsePragma.cpp +++ b/lib/Parse/ParsePragma.cpp @@ -1722,10 +1722,10 @@ void PragmaMSPragma::HandlePragma(Preprocessor &PP, void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, Token &Tok) { - SourceLocation CommentLoc = Tok.getLocation(); + SourceLocation DetectMismatchLoc = Tok.getLocation(); PP.Lex(Tok); if (Tok.isNot(tok::l_paren)) { - PP.Diag(CommentLoc, diag::err_expected) << tok::l_paren; + PP.Diag(DetectMismatchLoc, diag::err_expected) << tok::l_paren; return; } @@ -1760,10 +1760,10 @@ void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP, // If the pragma is lexically sound, notify any interested PPCallbacks. if (PP.getPPCallbacks()) - PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString, + PP.getPPCallbacks()->PragmaDetectMismatch(DetectMismatchLoc, NameString, ValueString); - Actions.ActOnPragmaDetectMismatch(NameString, ValueString); + Actions.ActOnPragmaDetectMismatch(DetectMismatchLoc, NameString, ValueString); } /// \brief Handle the microsoft \#pragma comment extension. diff --git a/lib/Sema/SemaAttr.cpp b/lib/Sema/SemaAttr.cpp index 1900d80b21..4b7aa2b07e 100644 --- a/lib/Sema/SemaAttr.cpp +++ b/lib/Sema/SemaAttr.cpp @@ -277,9 +277,12 @@ void Sema::ActOnPragmaMSComment(SourceLocation CommentLoc, Consumer.HandleTopLevelDecl(DeclGroupRef(PCD)); } -void Sema::ActOnPragmaDetectMismatch(StringRef Name, StringRef Value) { - // FIXME: Serialize this. - Consumer.HandleDetectMismatch(Name, Value); +void Sema::ActOnPragmaDetectMismatch(SourceLocation Loc, StringRef Name, + StringRef Value) { + auto *PDMD = PragmaDetectMismatchDecl::Create( + Context, Context.getTranslationUnitDecl(), Loc, Name, Value); + Context.getTranslationUnitDecl()->addDecl(PDMD); + Consumer.HandleTopLevelDecl(DeclGroupRef(PDMD)); } void Sema::ActOnPragmaMSPointersToMembers( diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp index 844bdd1fa0..ffb7eeda4f 100644 --- a/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -338,6 +338,11 @@ TemplateDeclInstantiator::VisitPragmaCommentDecl(PragmaCommentDecl *D) { llvm_unreachable("pragma comment cannot be instantiated"); } +Decl *TemplateDeclInstantiator::VisitPragmaDetectMismatchDecl( + PragmaDetectMismatchDecl *D) { + llvm_unreachable("pragma comment cannot be instantiated"); +} + Decl * TemplateDeclInstantiator::VisitExternCContextDecl(ExternCContextDecl *D) { llvm_unreachable("extern \"C\" context cannot be instantiated"); diff --git a/lib/Serialization/ASTCommon.cpp b/lib/Serialization/ASTCommon.cpp index 067653dfbf..aba3923132 100644 --- a/lib/Serialization/ASTCommon.cpp +++ b/lib/Serialization/ASTCommon.cpp @@ -320,6 +320,7 @@ bool serialization::isRedeclarableDeclKind(unsigned Kind) { case Decl::LinkageSpec: case Decl::ObjCPropertyImpl: case Decl::PragmaComment: + case Decl::PragmaDetectMismatch: case Decl::FileScopeAsm: case Decl::AccessSpec: case Decl::Friend: diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp index a7eded1a2f..213f2aa0ec 100644 --- a/lib/Serialization/ASTReaderDecl.cpp +++ b/lib/Serialization/ASTReaderDecl.cpp @@ -243,6 +243,7 @@ namespace clang { void VisitDecl(Decl *D); void VisitPragmaCommentDecl(PragmaCommentDecl *D); + void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D); void VisitTranslationUnitDecl(TranslationUnitDecl *TU); void VisitNamedDecl(NamedDecl *ND); void VisitLabelDecl(LabelDecl *LD); @@ -552,6 +553,20 @@ void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) { D->getTrailingObjects()[Arg.size()] = '\0'; } +void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) { + VisitDecl(D); + D->setLocation(ReadSourceLocation(Record, Idx)); + std::string Name = ReadString(Record, Idx); + memcpy(D->getTrailingObjects(), Name.data(), Name.size()); + D->getTrailingObjects()[Name.size()] = '\0'; + + D->ValueStart = Name.size() + 1; + std::string Value = ReadString(Record, Idx); + memcpy(D->getTrailingObjects() + D->ValueStart, Value.data(), + Value.size()); + D->getTrailingObjects()[D->ValueStart + Value.size()] = '\0'; +} + void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) { llvm_unreachable("Translation units are not serialized"); } @@ -2433,6 +2448,7 @@ static bool isConsumerInterestedIn(Decl *D, bool HasBody) { isa(D) || isa(D) || isa(D) || + isa(D) || isa(D)) return true; if (VarDecl *Var = dyn_cast(D)) @@ -3353,6 +3369,10 @@ Decl *ASTReader::ReadDeclRecord(DeclID ID) { case DECL_PRAGMA_COMMENT: D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record[Idx++]); break; + case DECL_PRAGMA_DETECT_MISMATCH: + D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID, + Record[Idx++]); + break; case DECL_EMPTY: D = EmptyDecl::CreateDeserialized(Context, ID); break; diff --git a/lib/Serialization/ASTWriterDecl.cpp b/lib/Serialization/ASTWriterDecl.cpp index bf36af6bd5..ee693bf2fc 100644 --- a/lib/Serialization/ASTWriterDecl.cpp +++ b/lib/Serialization/ASTWriterDecl.cpp @@ -50,6 +50,7 @@ namespace clang { void VisitDecl(Decl *D); void VisitPragmaCommentDecl(PragmaCommentDecl *D); + void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D); void VisitTranslationUnitDecl(TranslationUnitDecl *D); void VisitNamedDecl(NamedDecl *D); void VisitLabelDecl(LabelDecl *LD); @@ -326,6 +327,18 @@ void ASTDeclWriter::VisitPragmaCommentDecl(PragmaCommentDecl *D) { Code = serialization::DECL_PRAGMA_COMMENT; } +void ASTDeclWriter::VisitPragmaDetectMismatchDecl( + PragmaDetectMismatchDecl *D) { + StringRef Name = D->getName(); + StringRef Value = D->getValue(); + Record.push_back(Name.size() + 1 + Value.size()); + VisitDecl(D); + Writer.AddSourceLocation(D->getLocStart(), Record); + Writer.AddString(Name, Record); + Writer.AddString(Value, Record); + Code = serialization::DECL_PRAGMA_DETECT_MISMATCH; +} + void ASTDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { llvm_unreachable("Translation units aren't directly serialized"); } diff --git a/test/PCH/pragma-detect_mismatch.c b/test/PCH/pragma-detect_mismatch.c new file mode 100644 index 0000000000..b6233ed8a4 --- /dev/null +++ b/test/PCH/pragma-detect_mismatch.c @@ -0,0 +1,23 @@ +// Test this without pch. +// RUN: %clang_cc1 %s -Wunknown-pragmas -Werror -triple x86_64-pc-win32 -fms-extensions -emit-llvm -include %s -o - | FileCheck %s + +// Test with pch. +// RUN: %clang_cc1 %s -Wunknown-pragmas -Werror -triple x86_64-pc-win32 -fms-extensions -emit-pch -o %t +// RUN: %clang_cc1 %s -Wunknown-pragmas -Werror -triple x86_64-pc-win32 -fms-extensions -emit-llvm -include-pch %t -o - | FileCheck %s + +// The first run line creates a pch, and since at that point HEADER is not +// defined, the only thing contained in the pch is the pragma. The second line +// then includes that pch, so HEADER is defined and the actual code is compiled. +// The check then makes sure that the pragma is in effect in the file that +// includes the pch. + +#ifndef HEADER +#define HEADER +#pragma detect_mismatch("FruitKind", "Jaboticaba") + +#else + +// CHECK: "/DEFAULTLIB:foo.lib" +// CHECK: "/FAILIFMISMATCH:\22FruitKind=Jaboticaba\22" + +#endif diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index f4fa07c7fa..f24b62f044 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -5587,6 +5587,7 @@ CXCursor clang_getCursorDefinition(CXCursor C) { case Decl::ObjCTypeParam: case Decl::BuiltinTemplate: case Decl::PragmaComment: + case Decl::PragmaDetectMismatch: return C; // Declaration kinds that don't make any sense here, but are