@@ -3728,6 +3728,29 @@ static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3728
3728
3729
3729
namespace {
3730
3730
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
+
3731
3754
class ASTIdentifierTableTrait {
3732
3755
ASTWriter &Writer;
3733
3756
Preprocessor &PP;
@@ -3741,17 +3764,8 @@ class ASTIdentifierTableTrait {
3741
3764
// / doesn't check whether the name has macros defined; use PublicMacroIterator
3742
3765
// / to check that.
3743
3766
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 );
3755
3769
}
3756
3770
3757
3771
public:
@@ -3782,10 +3796,6 @@ class ASTIdentifierTableTrait {
3782
3796
return isInterestingIdentifier (II, MacroOffset);
3783
3797
}
3784
3798
3785
- bool isInterestingNonMacroIdentifier (const IdentifierInfo *II) {
3786
- return isInterestingIdentifier (II, 0 );
3787
- }
3788
-
3789
3799
std::pair<unsigned , unsigned >
3790
3800
EmitKeyDataLength (raw_ostream &Out, const IdentifierInfo *II, IdentifierID ID) {
3791
3801
// Record the location of the identifier data. This is used when generating
@@ -3887,21 +3897,6 @@ void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3887
3897
ASTIdentifierTableTrait Trait (*this , PP, IdResolver, IsModule,
3888
3898
IsModule ? &InterestingIdents : nullptr );
3889
3899
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
-
3905
3900
// Create the on-disk hash table representation. We only store offsets
3906
3901
// for identifiers that appear here for the first time.
3907
3902
IdentifierOffsets.resize (NextIdentID - FirstIdentID);
@@ -4125,16 +4120,24 @@ bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
4125
4120
DC->hasNeedToReconcileExternalVisibleStorage ();
4126
4121
}
4127
4122
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) {
4130
4129
for (auto *D : Result.getLookupResult ()) {
4131
- auto *LocalD = getDeclForLocalLookup (getLangOpts (), D);
4130
+ auto *LocalD = getDeclForLocalLookup (Writer. getLangOpts (), D);
4132
4131
if (LocalD->isFromASTFile ())
4133
4132
continue ;
4134
4133
4135
4134
// We can only be sure whether the local declaration is reachable
4136
4135
// 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))
4138
4141
continue ;
4139
4142
4140
4143
return false ;
@@ -4182,11 +4185,11 @@ ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
4182
4185
// that entirely external or unreachable.
4183
4186
//
4184
4187
// FIMXE: It looks sufficient to test
4185
- // isLookupResultEntirelyExternalOrUnreachable here. But due to bug we have
4188
+ // isLookupResultNotInteresting here. But due to bug we have
4186
4189
// to test isLookupResultExternal here. See
4187
4190
// https://github.com/llvm/llvm-project/issues/61065 for details.
4188
4191
if ((GeneratingReducedBMI || isLookupResultExternal (Result, DC)) &&
4189
- isLookupResultEntirelyExternalOrUnreachable (Result, DC ))
4192
+ isLookupResultNotInteresting (* this , Result ))
4190
4193
continue ;
4191
4194
4192
4195
// We also skip empty results. If any of the results could be external and
@@ -5007,6 +5010,7 @@ void ASTWriter::PrepareWritingSpecialDecls(Sema &SemaRef) {
5007
5010
if (D) {
5008
5011
assert (D->isCanonicalDecl () && " predefined decl is not canonical" );
5009
5012
DeclIDs[D] = ID;
5013
+ PredefinedDecls.insert (D);
5010
5014
}
5011
5015
};
5012
5016
RegisterPredefDecl (Context.getTranslationUnitDecl (),
@@ -5355,6 +5359,24 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
5355
5359
5356
5360
writeUnhashedControlBlock (PP, Context);
5357
5361
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
+
5358
5380
// Write the set of weak, undeclared identifiers. We always write the
5359
5381
// entire table, since later PCH files in a PCH chain are only interested in
5360
5382
// the results at the end of the chain.
0 commit comments