From: Chandler Carruth Date: Thu, 26 Mar 2015 08:32:49 +0000 (+0000) Subject: [Modules] Make "#pragma weak" undeclared identifiers be tracked X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f8cf68746c538b47c26416525520cc8a4d67c9d7;p=clang [Modules] Make "#pragma weak" undeclared identifiers be tracked deterministically. This fixes a latent issue where even Clang's Sema (and diagnostics) were non-deterministic in the face of this pragma. The fix is super simple -- just use a MapVector so we track the order in which these are parsed (or imported). Especially considering how rare they are, this seems like the perfect tradeoff. I've also simplified the client code with judicious use of auto and range based for loops. I've added some pretty hilarious code to my stress test which now survives the binary diff without issue. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@233261 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 9e07ad533f..dde928ff91 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -592,7 +592,7 @@ public: /// WeakUndeclaredIdentifiers - Identifiers contained in /// \#pragma weak before declared. rare. may alias another /// identifier, declared or undeclared - llvm::DenseMap WeakUndeclaredIdentifiers; + llvm::MapVector WeakUndeclaredIdentifiers; /// ExtnameUndeclaredIdentifiers - Identifiers contained in /// \#pragma redefine_extname before declared. Used in Solaris system headers diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp index a1a9b9d0b8..6825dfa41f 100644 --- a/lib/Sema/Sema.cpp +++ b/lib/Sema/Sema.cpp @@ -524,14 +524,8 @@ void Sema::LoadExternalWeakUndeclaredIdentifiers() { SmallVector, 4> WeakIDs; ExternalSource->ReadWeakUndeclaredIdentifiers(WeakIDs); - for (unsigned I = 0, N = WeakIDs.size(); I != N; ++I) { - llvm::DenseMap::iterator Pos - = WeakUndeclaredIdentifiers.find(WeakIDs[I].first); - if (Pos != WeakUndeclaredIdentifiers.end()) - continue; - - WeakUndeclaredIdentifiers.insert(WeakIDs[I]); - } + for (auto &WeakID : WeakIDs) + WeakUndeclaredIdentifiers.insert(WeakID); } @@ -694,16 +688,13 @@ void Sema::ActOnEndOfTranslationUnit() { } // Check for #pragma weak identifiers that were never declared - // FIXME: This will cause diagnostics to be emitted in a non-determinstic - // order! Iterating over a densemap like this is bad. LoadExternalWeakUndeclaredIdentifiers(); - for (llvm::DenseMap::iterator - I = WeakUndeclaredIdentifiers.begin(), - E = WeakUndeclaredIdentifiers.end(); I != E; ++I) { - if (I->second.getUsed()) continue; + for (auto WeakID : WeakUndeclaredIdentifiers) { + if (WeakID.second.getUsed()) + continue; - Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared) - << I->first; + Diag(WeakID.second.getLocation(), diag::warn_weak_identifier_undeclared) + << WeakID.first; } if (LangOpts.CPlusPlus11 && diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 336d222f17..4ef62b9bb5 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -5019,8 +5019,7 @@ void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { ND = FD; if (ND) { if (IdentifierInfo *Id = ND->getIdentifier()) { - llvm::DenseMap::iterator I - = WeakUndeclaredIdentifiers.find(Id); + auto I = WeakUndeclaredIdentifiers.find(Id); if (I != WeakUndeclaredIdentifiers.end()) { WeakInfo W = I->second; DeclApplyPragmaWeak(S, ND, W); diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp index 9f0626f060..7227a97e7b 100644 --- a/lib/Serialization/ASTWriter.cpp +++ b/lib/Serialization/ASTWriter.cpp @@ -4359,15 +4359,13 @@ void ASTWriter::WriteASTCore(Sema &SemaRef, // entire table, since later PCH files in a PCH chain are only interested in // the results at the end of the chain. RecordData WeakUndeclaredIdentifiers; - if (!SemaRef.WeakUndeclaredIdentifiers.empty()) { - for (llvm::DenseMap::iterator - I = SemaRef.WeakUndeclaredIdentifiers.begin(), - E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) { - AddIdentifierRef(I->first, WeakUndeclaredIdentifiers); - AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers); - AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers); - WeakUndeclaredIdentifiers.push_back(I->second.getUsed()); - } + for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) { + IdentifierInfo *II = WeakUndeclaredIdentifier.first; + WeakInfo &WI = WeakUndeclaredIdentifier.second; + AddIdentifierRef(II, WeakUndeclaredIdentifiers); + AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers); + AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers); + WeakUndeclaredIdentifiers.push_back(WI.getUsed()); } // Build a record containing all of the ext_vector declarations. diff --git a/test/Modules/Inputs/stress1/common.h b/test/Modules/Inputs/stress1/common.h index d2fff1098d..b158a8e6c1 100644 --- a/test/Modules/Inputs/stress1/common.h +++ b/test/Modules/Inputs/stress1/common.h @@ -35,4 +35,18 @@ struct S01 {}; struct S02 {}; } +#pragma weak pragma_weak00 +#pragma weak pragma_weak01 +#pragma weak pragma_weak02 +#pragma weak pragma_weak03 +#pragma weak pragma_weak04 +#pragma weak pragma_weak05 + +extern "C" int pragma_weak00(); +extern "C" int pragma_weak01(); +extern "C" int pragma_weak02(); +extern "C" int pragma_weak03; +extern "C" int pragma_weak04; +extern "C" int pragma_weak05; + #endif diff --git a/test/Modules/Inputs/stress1/merge00.h b/test/Modules/Inputs/stress1/merge00.h index c6dccdf530..b769b0877d 100644 --- a/test/Modules/Inputs/stress1/merge00.h +++ b/test/Modules/Inputs/stress1/merge00.h @@ -1,6 +1,14 @@ #ifndef STRESS1_MERGE00_H #define STRESS1_MERGE00_H +// These don't match the imported declarations because we import them from +// modules which are built in isolation of the current header's pragma state +// much like they are built in isolation of the incoming macro state. +// FIXME: We should expect warnings here but we can't because verify doesn't +// work for modules. +//#pragma weak pragma_weak01 // expected-warning {{weak identifier 'pragma_weak01' never declared}} +//#pragma weak pragma_weak04 // expected-warning {{weak identifier 'pragma_waek04' never declared}} + #include "m00.h" #include "m01.h" #include "m02.h" @@ -8,4 +16,10 @@ inline int g() { return N00::S00('a').method00('b') + (int)N00::S00(42) + function00(42); } +#pragma weak pragma_weak02 +#pragma weak pragma_weak05 + +extern "C" int pragma_weak02(); +int pragma_weak05; + #endif diff --git a/test/Modules/stress1.cpp b/test/Modules/stress1.cpp index 55d9533b7a..e03e538a3c 100644 --- a/test/Modules/stress1.cpp +++ b/test/Modules/stress1.cpp @@ -98,3 +98,8 @@ #include "merge00.h" int f() { return N01::S00('a').method00('b') + (int)N00::S00(42) + function00(42) + g(); } + +int f2() { + return pragma_weak00() + pragma_weak01() + pragma_weak02() + + pragma_weak03 + pragma_weak04 + pragma_weak05; +}