Skip to content

Commit ebe084f

Browse files
authored
[Modules] Fix the inconsistency of which Decl should be serialized for an identifier. (llvm#135887)
Fixes the assertion failure > Assertion failed: (DeclIDs.contains(D) && "Declaration not emitted!"), function getDeclID, file ASTWriter.cpp, line 6873. We prepare to serialize a `Decl` by adding it to `DeclIDs` in `ASTWriter::GetDeclRef`. But the checks before this call aren't the same as when we are actually serializing a `Decl` in `ASTIdentifierTableTrait::EmitData` and `ASTWriter::WriteIdentifierTable`. That's how we can end up serializing a `Decl` not present in `DeclIDs` and hitting the assertion. With the assertions disabled clang crashes when trying to use a deserialized null `Decl`. Fix by making the code checks before `ASTWriter::GetDeclRef` call similar to those we have before the serialization. rdar://139319683
1 parent 62b9cbd commit ebe084f

File tree

2 files changed

+105
-3
lines changed

2 files changed

+105
-3
lines changed

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5670,14 +5670,16 @@ void ASTWriter::PrepareWritingSpecialDecls(Sema &SemaRef) {
56705670
llvm::SmallVector<const IdentifierInfo*, 256> IIs;
56715671
for (const auto &ID : SemaRef.PP.getIdentifierTable()) {
56725672
const IdentifierInfo *II = ID.second;
5673-
if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
5673+
if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization() ||
5674+
II->hasFETokenInfoChangedSinceDeserialization())
56745675
IIs.push_back(II);
56755676
}
56765677
// Sort the identifiers to visit based on their name.
56775678
llvm::sort(IIs, llvm::deref<std::less<>>());
5679+
const LangOptions &LangOpts = getLangOpts();
56785680
for (const IdentifierInfo *II : IIs)
5679-
for (const Decl *D : SemaRef.IdResolver.decls(II))
5680-
GetDeclRef(D);
5681+
for (NamedDecl *D : SemaRef.IdResolver.decls(II))
5682+
GetDeclRef(getDeclForLocalLookup(LangOpts, D));
56815683
}
56825684

56835685
// Write all of the DeclsToCheckForDeferredDiags.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
// RUN: %clang_cc1 -fsyntax-only -I %t/include %t/test.c \
4+
// RUN: -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/modules.cache
5+
6+
// Test when a decl is present in multiple modules through an inclusion of
7+
// a non-modular header. Make sure such decl is serialized correctly and can be
8+
// used after deserialization.
9+
10+
//--- include/non-modular.h
11+
#ifndef NON_MODULAR_H
12+
#define NON_MODULAR_H
13+
14+
union TestUnion {
15+
int x;
16+
float y;
17+
};
18+
19+
struct ReferenceUnion1 {
20+
union TestUnion name;
21+
unsigned versionMajor;
22+
};
23+
struct ReferenceUnion2 {
24+
union TestUnion name;
25+
unsigned versionMinor;
26+
};
27+
28+
// Test another kind of RecordDecl.
29+
struct TestStruct {
30+
int p;
31+
float q;
32+
};
33+
34+
struct ReferenceStruct1 {
35+
unsigned fieldA;
36+
struct TestStruct fieldB;
37+
};
38+
39+
struct ReferenceStruct2 {
40+
unsigned field1;
41+
struct TestStruct field2;
42+
};
43+
44+
#endif
45+
46+
//--- include/piecewise1-empty.h
47+
//--- include/piecewise1-initially-hidden.h
48+
#include <non-modular.h>
49+
50+
//--- include/piecewise2-empty.h
51+
//--- include/piecewise2-initially-hidden.h
52+
#include <non-modular.h>
53+
54+
//--- include/with-multiple-decls.h
55+
#include <piecewise1-empty.h>
56+
// Include the non-modular header and resolve a name duplication between decl
57+
// in non-modular.h and in piecewise1-initially-hidden.h, create a
58+
// redeclaration chain.
59+
#include <non-modular.h>
60+
// Include a decl with a duplicate name again to add more to IdentifierResolver.
61+
#include <piecewise2-empty.h>
62+
63+
//--- include/module.modulemap
64+
module Piecewise1 {
65+
module Empty {
66+
header "piecewise1-empty.h"
67+
}
68+
module InitiallyHidden {
69+
header "piecewise1-initially-hidden.h"
70+
export *
71+
}
72+
}
73+
74+
module Piecewise2 {
75+
module Empty {
76+
header "piecewise2-empty.h"
77+
}
78+
module InitiallyHidden {
79+
header "piecewise2-initially-hidden.h"
80+
export *
81+
}
82+
}
83+
84+
module WithMultipleDecls {
85+
header "with-multiple-decls.h"
86+
export *
87+
}
88+
89+
//--- test.c
90+
#include <with-multiple-decls.h>
91+
92+
struct Test {
93+
int x;
94+
union TestUnion name;
95+
};
96+
97+
struct Test2 {
98+
struct TestStruct name;
99+
float y;
100+
};

0 commit comments

Comments
 (0)