Skip to content

Commit 4266533

Browse files
committed
[WIP][Modules] Delay deserialization of preferred_name attribute at record level.
1 parent 85ca551 commit 4266533

File tree

6 files changed

+128
-10
lines changed

6 files changed

+128
-10
lines changed

clang/include/clang/Serialization/ASTReader.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,12 @@ class ASTReader
12051205
/// been completed.
12061206
std::deque<PendingDeclContextInfo> PendingDeclContextInfos;
12071207

1208+
struct PendingPreferredNameAttribute {
1209+
uint64_t RecordIdx;
1210+
Decl *D;
1211+
};
1212+
SmallVector<PendingPreferredNameAttribute> PendingPreferredNameAttributes;
1213+
12081214
template <typename DeclTy>
12091215
using DuplicateObjCDecls = std::pair<DeclTy *, DeclTy *>;
12101216

@@ -1551,6 +1557,8 @@ class ASTReader
15511557
void loadPendingDeclChain(Decl *D, uint64_t LocalOffset);
15521558
void loadObjCCategories(GlobalDeclID ID, ObjCInterfaceDecl *D,
15531559
unsigned PreviousGeneration = 0);
1560+
void loadPreferredNameAttribute(
1561+
const PendingPreferredNameAttribute &PreferredNameAttribute);
15541562

15551563
RecordLocation getLocalBitOffset(uint64_t GlobalOffset);
15561564
uint64_t getGlobalBitOffset(ModuleFile &M, uint64_t LocalOffset);

clang/include/clang/Serialization/ASTRecordReader.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,12 @@ class ASTRecordReader
337337
/// Reads attributes from the current stream position, advancing Idx.
338338
void readAttributes(AttrVec &Attrs);
339339

340+
/// Reads one attribute from the current stream position, advancing Idx.
341+
Attr *readAttr(Decl *D);
342+
343+
/// Reads attributes from the current stream position, advancing Idx.
344+
void readAttributes(AttrVec &Attrs, Decl *D);
345+
340346
/// Read an BTFTypeTagAttr object.
341347
BTFTypeTagAttr *readBTFTypeTagAttr() {
342348
return cast<BTFTypeTagAttr>(readAttr());
@@ -355,6 +361,10 @@ class ASTRecordReader
355361
SwitchCase *getSwitchCaseWithID(unsigned ID) {
356362
return Reader->getSwitchCaseWithID(ID);
357363
}
364+
365+
private:
366+
Attr *readAttrImpl(Decl *D);
367+
void readAttributesImpl(AttrVec &Attrs, Decl *D);
358368
};
359369

360370
/// Helper class that saves the current stream position and

clang/lib/Serialization/ASTReader.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10079,6 +10079,11 @@ void ASTReader::finishPendingActions() {
1007910079
}
1008010080
PendingDeducedVarTypes.clear();
1008110081

10082+
// Load the delayed preferred name attributes.
10083+
for (unsigned I = 0; I != PendingPreferredNameAttributes.size(); ++I)
10084+
loadPreferredNameAttribute(PendingPreferredNameAttributes[I]);
10085+
PendingPreferredNameAttributes.clear();
10086+
1008210087
// For each decl chain that we wanted to complete while deserializing, mark
1008310088
// it as "still needs to be completed".
1008410089
for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ void ASTDeclReader::VisitDecl(Decl *D) {
612612

613613
if (HasAttrs) {
614614
AttrVec Attrs;
615-
Record.readAttributes(Attrs);
615+
Record.readAttributes(Attrs, D);
616616
// Avoid calling setAttrs() directly because it uses Decl::getASTContext()
617617
// internally which is unsafe during derialization.
618618
D->setAttrsImpl(Attrs, Reader.getContext());
@@ -3118,13 +3118,22 @@ class AttrReader {
31183118
return Reader.readVersionTuple();
31193119
}
31203120

3121+
void skipInts(unsigned N) {
3122+
Reader.skipInts(N);
3123+
}
3124+
3125+
unsigned getCurrentIdx() {
3126+
return Reader.getIdx();
3127+
}
3128+
31213129
OMPTraitInfo *readOMPTraitInfo() { return Reader.readOMPTraitInfo(); }
31223130

31233131
template <typename T> T *readDeclAs() { return Reader.readDeclAs<T>(); }
31243132
};
31253133
}
31263134

3127-
Attr *ASTRecordReader::readAttr() {
3135+
/// Reads one attribute from the current stream position, advancing Idx.
3136+
Attr *ASTRecordReader::readAttrImpl(Decl *D) {
31283137
AttrReader Record(*this);
31293138
auto V = Record.readInt();
31303139
if (!V)
@@ -3134,6 +3143,17 @@ Attr *ASTRecordReader::readAttr() {
31343143
// Kind is stored as a 1-based integer because 0 is used to indicate a null
31353144
// Attr pointer.
31363145
auto Kind = static_cast<attr::Kind>(V - 1);
3146+
if (Kind == attr::PreferredName && D != nullptr) {
3147+
if (D != nullptr) {
3148+
Reader->PendingPreferredNameAttributes.push_back(
3149+
{Record.getCurrentIdx() - 1, D});
3150+
auto SkipCount = Record.readInt();
3151+
Record.skipInts(SkipCount);
3152+
return nullptr;
3153+
}
3154+
// Ignore the skip count when resolving pending actions.
3155+
Record.readInt();
3156+
}
31373157
ASTContext &Context = getContext();
31383158

31393159
IdentifierInfo *AttrName = Record.readIdentifier();
@@ -3159,13 +3179,27 @@ Attr *ASTRecordReader::readAttr() {
31593179
return New;
31603180
}
31613181

3162-
/// Reads attributes from the current stream position.
3163-
void ASTRecordReader::readAttributes(AttrVec &Attrs) {
3182+
void ASTRecordReader::readAttributesImpl(AttrVec &Attrs, Decl *D) {
31643183
for (unsigned I = 0, E = readInt(); I != E; ++I)
3165-
if (auto *A = readAttr())
3184+
if (auto *A = readAttr(D))
31663185
Attrs.push_back(A);
31673186
}
31683187

3188+
Attr *ASTRecordReader::readAttr() { return readAttrImpl(nullptr); }
3189+
3190+
/// Reads attributes from the current stream position.
3191+
void ASTRecordReader::readAttributes(AttrVec &Attrs) {
3192+
readAttributesImpl(Attrs, nullptr);
3193+
}
3194+
3195+
/// Reads one attribute from the current stream position, advancing Idx.
3196+
Attr *ASTRecordReader::readAttr(Decl *D) { return readAttrImpl(D); }
3197+
3198+
/// Reads attributes from the current stream position, advancing Idx.
3199+
void ASTRecordReader::readAttributes(AttrVec &Attrs, Decl *D) {
3200+
readAttributesImpl(Attrs, D);
3201+
}
3202+
31693203
//===----------------------------------------------------------------------===//
31703204
// ASTReader Implementation
31713205
//===----------------------------------------------------------------------===//
@@ -4424,6 +4458,51 @@ void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
44244458
ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
44254459
}
44264460

4461+
void ASTReader::loadPreferredNameAttribute(
4462+
const PendingPreferredNameAttribute &PreferredNameAttribute) {
4463+
Decl *D = PreferredNameAttribute.D;
4464+
ModuleFile *M = getOwningModuleFile(D);
4465+
4466+
unsigned LocalDeclIndex = D->getGlobalID().getLocalDeclIndex();
4467+
const DeclOffset &DOffs = M->DeclOffsets[LocalDeclIndex];
4468+
RecordLocation Loc(M, DOffs.getBitOffset(M->DeclsBlockStartOffset));
4469+
4470+
llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
4471+
SavedStreamPosition SavedPosition(Cursor);
4472+
if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
4473+
Error(std::move(Err));
4474+
}
4475+
4476+
Expected<unsigned> MaybeCode = Cursor.ReadCode();
4477+
if (!MaybeCode) {
4478+
llvm::report_fatal_error(
4479+
Twine("ASTReader::loadPreferredNameAttribute failed reading code: ") +
4480+
toString(MaybeCode.takeError()));
4481+
}
4482+
unsigned Code = MaybeCode.get();
4483+
4484+
ASTRecordReader Record(*this, *Loc.F);
4485+
Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
4486+
if (!MaybeRecCode) {
4487+
llvm::report_fatal_error(
4488+
Twine(
4489+
"ASTReader::loadPreferredNameAttribute failed reading rec code: ") +
4490+
toString(MaybeCode.takeError()));
4491+
}
4492+
unsigned RecCode = MaybeRecCode.get();
4493+
if (RecCode != DECL_CXX_RECORD) {
4494+
llvm::report_fatal_error(
4495+
Twine("ASTReader::loadPreferredNameAttribute failed reading rec code: "
4496+
"expected CXXRecord") +
4497+
toString(MaybeCode.takeError()));
4498+
}
4499+
4500+
Record.skipInts(PreferredNameAttribute.RecordIdx);
4501+
Attr *PreferredNameAttr = Record.readAttr(nullptr);
4502+
AttrVec &Attrs = getContext().getDeclAttrs(D);
4503+
Attrs.push_back(PreferredNameAttr);
4504+
}
4505+
44274506
namespace {
44284507

44294508
/// Given an ObjC interface, goes through the modules and links to the

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "clang/AST/Type.h"
3838
#include "clang/AST/TypeLoc.h"
3939
#include "clang/AST/TypeLocVisitor.h"
40+
#include "clang/Basic/AttrKinds.h"
4041
#include "clang/Basic/Diagnostic.h"
4142
#include "clang/Basic/DiagnosticOptions.h"
4243
#include "clang/Basic/FileEntry.h"
@@ -4909,12 +4910,16 @@ void ASTRecordWriter::AddAttr(const Attr *A) {
49094910
// FIXME: Clang can't handle the serialization/deserialization of
49104911
// preferred_name properly now. See
49114912
// https://github.com/llvm/llvm-project/issues/56490 for example.
4912-
if (!A || (isa<PreferredNameAttr>(A) &&
4913-
Writer->isWritingStdCXXNamedModules()))
4913+
if (!A)
49144914
return Record.push_back(0);
49154915

49164916
Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs
49174917

4918+
auto SkipIdx = Record.size();
4919+
if (A->getKind() == attr::PreferredName)
4920+
// Add placeholder for the size of preferred_name attribute.
4921+
Record.push_back(0);
4922+
49184923
Record.AddIdentifierRef(A->getAttrName());
49194924
Record.AddIdentifierRef(A->getScopeName());
49204925
Record.AddSourceRange(A->getRange());
@@ -4925,6 +4930,11 @@ void ASTRecordWriter::AddAttr(const Attr *A) {
49254930
Record.push_back(A->isRegularKeywordAttribute());
49264931

49274932
#include "clang/Serialization/AttrPCHWrite.inc"
4933+
4934+
if (A->getKind() == attr::PreferredName)
4935+
// Record the actual size of preferred_name attribute (-1 to count the
4936+
// placeholder).
4937+
Record[SkipIdx] = Record.size() - SkipIdx - 1;
49284938
}
49294939

49304940
/// Emit the list of attributes to the specified record.

clang/test/Modules/preferred_name.cppm

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,16 @@ import A;
5353
export using ::foo_templ;
5454

5555
//--- Use1.cpp
56-
import A; // expected-[email protected]:8 {{attribute declaration must precede definition}}
57-
#include "foo.h" // [email protected]:9 {{previous definition is here}}
58-
56+
// expected-no-diagnostics
57+
import A;
58+
#include "foo.h"
5959
//--- Use2.cpp
6060
// expected-no-diagnostics
6161
#include "foo.h"
6262
import A;
63+
64+
//--- Use3.cpp
65+
#include "foo.h"
66+
import A;
67+
foo test;
68+
int size = test.size(); // expected-error {{no member named 'size' in 'foo'}}

0 commit comments

Comments
 (0)