Skip to content

Commit a443333

Browse files
committed
contrib/llvm-project: fix clang crash compiling modules
clang++ may crash when compiling certain C++20 modules. Backport the fix from LLVM upstream. This fixes LLVM bug 102684: llvm/llvm-project#102684. PR: 287803 MFC after: 3 days Obtained from: llvm/llvm-project#102855 Reviewed by: kevans, dim Approved by: kevans (mentor) Differential Revision: https://reviews.freebsd.org/D51041 (cherry picked from commit 55dfaea)
1 parent e0c9d85 commit a443333

File tree

7 files changed

+91
-18
lines changed

7 files changed

+91
-18
lines changed

contrib/llvm-project/clang/include/clang/Frontend/MultiplexConsumer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class MultiplexASTDeserializationListener : public ASTDeserializationListener {
3636
void MacroRead(serialization::MacroID ID, MacroInfo *MI) override;
3737
void TypeRead(serialization::TypeIdx Idx, QualType T) override;
3838
void DeclRead(GlobalDeclID ID, const Decl *D) override;
39+
void PredefinedDeclBuilt(PredefinedDeclIDs ID, const Decl *D) override;
3940
void SelectorRead(serialization::SelectorID iD, Selector Sel) override;
4041
void MacroDefinitionRead(serialization::PreprocessedEntityID,
4142
MacroDefinitionRecord *MD) override;

contrib/llvm-project/clang/include/clang/Serialization/ASTDeserializationListener.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class ASTDeserializationListener {
4545
virtual void TypeRead(serialization::TypeIdx Idx, QualType T) { }
4646
/// A decl was deserialized from the AST file.
4747
virtual void DeclRead(GlobalDeclID ID, const Decl *D) {}
48+
/// A predefined decl was built during the serialization.
49+
virtual void PredefinedDeclBuilt(PredefinedDeclIDs ID, const Decl *D) {}
4850
/// A selector was read from the AST file.
4951
virtual void SelectorRead(serialization::SelectorID iD, Selector Sel) {}
5052
/// A macro definition was read from the AST file.

contrib/llvm-project/clang/include/clang/Serialization/ASTReader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,9 @@ class ASTReader
15411541
std::pair<ModuleFile *, unsigned>
15421542
translateTypeIDToIndex(serialization::TypeID ID) const;
15431543

1544+
/// Get a predefined Decl from ASTContext.
1545+
Decl *getPredefinedDecl(PredefinedDeclIDs ID);
1546+
15441547
public:
15451548
/// Load the AST file and validate its contents against the given
15461549
/// Preprocessor.

contrib/llvm-project/clang/include/clang/Serialization/ASTWriter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,7 @@ class ASTWriter : public ASTDeserializationListener,
869869
void IdentifierRead(serialization::IdentifierID ID, IdentifierInfo *II) override;
870870
void MacroRead(serialization::MacroID ID, MacroInfo *MI) override;
871871
void TypeRead(serialization::TypeIdx Idx, QualType T) override;
872+
void PredefinedDeclBuilt(PredefinedDeclIDs ID, const Decl *D) override;
872873
void SelectorRead(serialization::SelectorID ID, Selector Sel) override;
873874
void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
874875
MacroDefinitionRecord *MD) override;

contrib/llvm-project/clang/lib/Frontend/MultiplexConsumer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ void MultiplexASTDeserializationListener::DeclRead(GlobalDeclID ID,
5858
Listeners[i]->DeclRead(ID, D);
5959
}
6060

61+
void MultiplexASTDeserializationListener::PredefinedDeclBuilt(PredefinedDeclIDs ID, const Decl *D) {
62+
for (size_t i = 0, e = Listeners.size(); i != e; ++i)
63+
Listeners[i]->PredefinedDeclBuilt(ID, D);
64+
}
65+
6166
void MultiplexASTDeserializationListener::SelectorRead(
6267
serialization::SelectorID ID, Selector Sel) {
6368
for (size_t i = 0, e = Listeners.size(); i != e; ++i)

contrib/llvm-project/clang/lib/Serialization/ASTReader.cpp

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7741,7 +7741,10 @@ SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
77417741
return Loc;
77427742
}
77437743

7744-
static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7744+
Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
7745+
assert(ContextObj && "reading predefined decl without AST context");
7746+
ASTContext &Context = *ContextObj;
7747+
Decl *NewLoaded = nullptr;
77457748
switch (ID) {
77467749
case PREDEF_DECL_NULL_ID:
77477750
return nullptr;
@@ -7750,54 +7753,106 @@ static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
77507753
return Context.getTranslationUnitDecl();
77517754

77527755
case PREDEF_DECL_OBJC_ID_ID:
7753-
return Context.getObjCIdDecl();
7756+
if (Context.ObjCIdDecl)
7757+
return Context.ObjCIdDecl;
7758+
NewLoaded = Context.getObjCIdDecl();
7759+
break;
77547760

77557761
case PREDEF_DECL_OBJC_SEL_ID:
7756-
return Context.getObjCSelDecl();
7762+
if (Context.ObjCSelDecl)
7763+
return Context.ObjCSelDecl;
7764+
NewLoaded = Context.getObjCSelDecl();
7765+
break;
77577766

77587767
case PREDEF_DECL_OBJC_CLASS_ID:
7759-
return Context.getObjCClassDecl();
7768+
if (Context.ObjCClassDecl)
7769+
return Context.ObjCClassDecl;
7770+
NewLoaded = Context.getObjCClassDecl();
7771+
break;
77607772

77617773
case PREDEF_DECL_OBJC_PROTOCOL_ID:
7762-
return Context.getObjCProtocolDecl();
7774+
if (Context.ObjCProtocolClassDecl)
7775+
return Context.ObjCProtocolClassDecl;
7776+
NewLoaded = Context.getObjCProtocolDecl();
7777+
break;
77637778

77647779
case PREDEF_DECL_INT_128_ID:
7765-
return Context.getInt128Decl();
7780+
if (Context.Int128Decl)
7781+
return Context.Int128Decl;
7782+
NewLoaded = Context.getInt128Decl();
7783+
break;
77667784

77677785
case PREDEF_DECL_UNSIGNED_INT_128_ID:
7768-
return Context.getUInt128Decl();
7786+
if (Context.UInt128Decl)
7787+
return Context.UInt128Decl;
7788+
NewLoaded = Context.getUInt128Decl();
7789+
break;
77697790

77707791
case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7771-
return Context.getObjCInstanceTypeDecl();
7792+
if (Context.ObjCInstanceTypeDecl)
7793+
return Context.ObjCInstanceTypeDecl;
7794+
NewLoaded = Context.getObjCInstanceTypeDecl();
7795+
break;
77727796

77737797
case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7774-
return Context.getBuiltinVaListDecl();
7798+
if (Context.BuiltinVaListDecl)
7799+
return Context.BuiltinVaListDecl;
7800+
NewLoaded = Context.getBuiltinVaListDecl();
7801+
break;
77757802

77767803
case PREDEF_DECL_VA_LIST_TAG:
7777-
return Context.getVaListTagDecl();
7804+
if (Context.VaListTagDecl)
7805+
return Context.VaListTagDecl;
7806+
NewLoaded = Context.getVaListTagDecl();
7807+
break;
77787808

77797809
case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7780-
return Context.getBuiltinMSVaListDecl();
7810+
if (Context.BuiltinMSVaListDecl)
7811+
return Context.BuiltinMSVaListDecl;
7812+
NewLoaded = Context.getBuiltinMSVaListDecl();
7813+
break;
77817814

77827815
case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7816+
// ASTContext::getMSGuidTagDecl won't create MSGuidTagDecl conditionally.
77837817
return Context.getMSGuidTagDecl();
77847818

77857819
case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7786-
return Context.getExternCContextDecl();
7820+
if (Context.ExternCContext)
7821+
return Context.ExternCContext;
7822+
NewLoaded = Context.getExternCContextDecl();
7823+
break;
77877824

77887825
case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7789-
return Context.getMakeIntegerSeqDecl();
7826+
if (Context.MakeIntegerSeqDecl)
7827+
return Context.MakeIntegerSeqDecl;
7828+
NewLoaded = Context.getMakeIntegerSeqDecl();
7829+
break;
77907830

77917831
case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7792-
return Context.getCFConstantStringDecl();
7832+
if (Context.CFConstantStringTypeDecl)
7833+
return Context.CFConstantStringTypeDecl;
7834+
NewLoaded = Context.getCFConstantStringDecl();
7835+
break;
77937836

77947837
case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7795-
return Context.getCFConstantStringTagDecl();
7838+
if (Context.CFConstantStringTagDecl)
7839+
return Context.CFConstantStringTagDecl;
7840+
NewLoaded = Context.getCFConstantStringTagDecl();
7841+
break;
77967842

77977843
case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7798-
return Context.getTypePackElementDecl();
7844+
if (Context.TypePackElementDecl)
7845+
return Context.TypePackElementDecl;
7846+
NewLoaded = Context.getTypePackElementDecl();
7847+
break;
77997848
}
7800-
llvm_unreachable("PredefinedDeclIDs unknown enum value");
7849+
7850+
assert(NewLoaded && "Failed to load predefined decl?");
7851+
7852+
if (DeserializationListener)
7853+
DeserializationListener->PredefinedDeclBuilt(ID, NewLoaded);
7854+
7855+
return NewLoaded;
78017856
}
78027857

78037858
unsigned ASTReader::translateGlobalDeclIDToIndex(GlobalDeclID GlobalID) const {
@@ -7814,7 +7869,7 @@ Decl *ASTReader::GetExistingDecl(GlobalDeclID ID) {
78147869
assert(ContextObj && "reading decl with no AST context");
78157870

78167871
if (ID < NUM_PREDEF_DECL_IDS) {
7817-
Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7872+
Decl *D = getPredefinedDecl((PredefinedDeclIDs)ID);
78187873
if (D) {
78197874
// Track that we have merged the declaration with ID \p ID into the
78207875
// pre-existing predefined declaration \p D.

contrib/llvm-project/clang/lib/Serialization/ASTWriter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6757,6 +6757,12 @@ void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
67576757
StoredIdx = Idx;
67586758
}
67596759

6760+
void ASTWriter::PredefinedDeclBuilt(PredefinedDeclIDs ID, const Decl *D) {
6761+
assert(D->isCanonicalDecl() && "predefined decl is not canonical");
6762+
DeclIDs[D] = LocalDeclID(ID);
6763+
PredefinedDecls.insert(D);
6764+
}
6765+
67606766
void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
67616767
// Always keep the highest ID. See \p TypeRead() for more information.
67626768
SelectorID &StoredID = SelectorIDs[S];

0 commit comments

Comments
 (0)