@@ -3728,6 +3728,29 @@ static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
37283728
37293729namespace {
37303730
3731+ bool IsInterestingIdentifier (const IdentifierInfo *II, uint64_t MacroOffset,
3732+ bool IsModule, bool IsCPlusPlus) {
3733+ bool NeedDecls = !IsModule || !IsCPlusPlus;
3734+
3735+ bool IsInteresting =
3736+ II->getNotableIdentifierID () != tok::NotableIdentifierKind::not_notable ||
3737+ II->getBuiltinID () != Builtin::ID::NotBuiltin ||
3738+ II->getObjCKeywordID () != tok::ObjCKeywordKind::objc_not_keyword;
3739+ if (MacroOffset || II->isPoisoned () || (!IsModule && IsInteresting) ||
3740+ II->hasRevertedTokenIDToIdentifier () ||
3741+ (NeedDecls && II->getFETokenInfo ()))
3742+ return true ;
3743+
3744+ return false ;
3745+ }
3746+
3747+ bool IsInterestingNonMacroIdentifier (const IdentifierInfo *II,
3748+ ASTWriter &Writer) {
3749+ bool IsModule = Writer.isWritingModule ();
3750+ bool IsCPlusPlus = Writer.getLangOpts ().CPlusPlus ;
3751+ return IsInterestingIdentifier (II, /* MacroOffset=*/ 0 , IsModule, IsCPlusPlus);
3752+ }
3753+
37313754class ASTIdentifierTableTrait {
37323755 ASTWriter &Writer;
37333756 Preprocessor &PP;
@@ -3741,17 +3764,8 @@ class ASTIdentifierTableTrait {
37413764 // / doesn't check whether the name has macros defined; use PublicMacroIterator
37423765 // / to check that.
37433766 bool isInterestingIdentifier (const IdentifierInfo *II, uint64_t MacroOffset) {
3744- bool IsInteresting =
3745- II->getNotableIdentifierID () !=
3746- tok::NotableIdentifierKind::not_notable ||
3747- II->getBuiltinID () != Builtin::ID::NotBuiltin ||
3748- II->getObjCKeywordID () != tok::ObjCKeywordKind::objc_not_keyword;
3749- if (MacroOffset || II->isPoisoned () || (!IsModule && IsInteresting) ||
3750- II->hasRevertedTokenIDToIdentifier () ||
3751- (NeedDecls && II->getFETokenInfo ()))
3752- return true ;
3753-
3754- return false ;
3767+ return IsInterestingIdentifier (II, MacroOffset, IsModule,
3768+ Writer.getLangOpts ().CPlusPlus );
37553769 }
37563770
37573771public:
@@ -3782,10 +3796,6 @@ class ASTIdentifierTableTrait {
37823796 return isInterestingIdentifier (II, MacroOffset);
37833797 }
37843798
3785- bool isInterestingNonMacroIdentifier (const IdentifierInfo *II) {
3786- return isInterestingIdentifier (II, 0 );
3787- }
3788-
37893799 std::pair<unsigned , unsigned >
37903800 EmitKeyDataLength (raw_ostream &Out, const IdentifierInfo *II, IdentifierID ID) {
37913801 // Record the location of the identifier data. This is used when generating
@@ -3887,21 +3897,6 @@ void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
38873897 ASTIdentifierTableTrait Trait (*this , PP, IdResolver, IsModule,
38883898 IsModule ? &InterestingIdents : nullptr );
38893899
3890- // Look for any identifiers that were named while processing the
3891- // headers, but are otherwise not needed. We add these to the hash
3892- // table to enable checking of the predefines buffer in the case
3893- // where the user adds new macro definitions when building the AST
3894- // file.
3895- SmallVector<const IdentifierInfo *, 128 > IIs;
3896- for (const auto &ID : PP.getIdentifierTable ())
3897- if (Trait.isInterestingNonMacroIdentifier (ID.second ))
3898- IIs.push_back (ID.second );
3899- // Sort the identifiers lexicographically before getting the references so
3900- // that their order is stable.
3901- llvm::sort (IIs, llvm::deref<std::less<>>());
3902- for (const IdentifierInfo *II : IIs)
3903- getIdentifierRef (II);
3904-
39053900 // Create the on-disk hash table representation. We only store offsets
39063901 // for identifiers that appear here for the first time.
39073902 IdentifierOffsets.resize (NextIdentID - FirstIdentID);
@@ -4125,16 +4120,24 @@ bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
41254120 DC->hasNeedToReconcileExternalVisibleStorage ();
41264121}
41274122
4128- bool ASTWriter::isLookupResultEntirelyExternalOrUnreachable (
4129- StoredDeclsList &Result, DeclContext *DC) {
4123+ // / Returns ture if all of the lookup result are either external, not emitted or
4124+ // / predefined. In such cases, the lookup result is not interesting and we don't
4125+ // / need to record the result in the current being written module. Return false
4126+ // / otherwise.
4127+ static bool isLookupResultNotInteresting (ASTWriter &Writer,
4128+ StoredDeclsList &Result) {
41304129 for (auto *D : Result.getLookupResult ()) {
4131- auto *LocalD = getDeclForLocalLookup (getLangOpts (), D);
4130+ auto *LocalD = getDeclForLocalLookup (Writer. getLangOpts (), D);
41324131 if (LocalD->isFromASTFile ())
41334132 continue ;
41344133
41354134 // We can only be sure whether the local declaration is reachable
41364135 // after we done writing the declarations and types.
4137- if (DoneWritingDeclsAndTypes && !wasDeclEmitted (LocalD))
4136+ if (Writer.getDoneWritingDeclsAndTypes () && !Writer.wasDeclEmitted (LocalD))
4137+ continue ;
4138+
4139+ // We don't need to emit the predefined decls.
4140+ if (Writer.isDeclPredefined (LocalD))
41384141 continue ;
41394142
41404143 return false ;
@@ -4182,11 +4185,11 @@ ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
41824185 // that entirely external or unreachable.
41834186 //
41844187 // FIMXE: It looks sufficient to test
4185- // isLookupResultEntirelyExternalOrUnreachable here. But due to bug we have
4188+ // isLookupResultNotInteresting here. But due to bug we have
41864189 // to test isLookupResultExternal here. See
41874190 // https://github.com/llvm/llvm-project/issues/61065 for details.
41884191 if ((GeneratingReducedBMI || isLookupResultExternal (Result, DC)) &&
4189- isLookupResultEntirelyExternalOrUnreachable (Result, DC ))
4192+ isLookupResultNotInteresting (* this , Result ))
41904193 continue ;
41914194
41924195 // We also skip empty results. If any of the results could be external and
@@ -5007,6 +5010,7 @@ void ASTWriter::PrepareWritingSpecialDecls(Sema &SemaRef) {
50075010 if (D) {
50085011 assert (D->isCanonicalDecl () && " predefined decl is not canonical" );
50095012 DeclIDs[D] = ID;
5013+ PredefinedDecls.insert (D);
50105014 }
50115015 };
50125016 RegisterPredefDecl (Context.getTranslationUnitDecl (),
@@ -5355,6 +5359,24 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
53555359
53565360 writeUnhashedControlBlock (PP, Context);
53575361
5362+ // Look for any identifiers that were named while processing the
5363+ // headers, but are otherwise not needed. We add these to the hash
5364+ // table to enable checking of the predefines buffer in the case
5365+ // where the user adds new macro definitions when building the AST
5366+ // file.
5367+ //
5368+ // We do this before emitting any Decl and Types to make sure the
5369+ // Identifier ID is stable.
5370+ SmallVector<const IdentifierInfo *, 128 > IIs;
5371+ for (const auto &ID : PP.getIdentifierTable ())
5372+ if (IsInterestingNonMacroIdentifier (ID.second , *this ))
5373+ IIs.push_back (ID.second );
5374+ // Sort the identifiers lexicographically before getting the references so
5375+ // that their order is stable.
5376+ llvm::sort (IIs, llvm::deref<std::less<>>());
5377+ for (const IdentifierInfo *II : IIs)
5378+ getIdentifierRef (II);
5379+
53585380 // Write the set of weak, undeclared identifiers. We always write the
53595381 // entire table, since later PCH files in a PCH chain are only interested in
53605382 // the results at the end of the chain.
0 commit comments