Skip to content

Commit 7dad94d

Browse files
committed
Update
1 parent 57cfb2b commit 7dad94d

File tree

6 files changed

+61
-51
lines changed

6 files changed

+61
-51
lines changed

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,10 @@ using DeclID = DeclIDBase::DeclID;
7272
/// An ID number that refers to a type in an AST file.
7373
///
7474
/// The ID of a type is partitioned into three parts:
75-
/// - the lower
76-
/// three bits are used to store the const/volatile/restrict
77-
/// qualifiers (as with QualType).
78-
/// - the upper 29 bits provide a type index in the corresponding
79-
/// module file.
75+
/// - the lower three bits are used to store the const/volatile/restrict
76+
/// qualifiers (as with QualType).
77+
/// - the next 29 bits provide a type index in the corresponding
78+
/// module file.
8079
/// - the upper 32 bits provide a module file index.
8180
///
8281
/// The type index values are partitioned into two
@@ -95,7 +94,6 @@ class TypeIdx {
9594

9695
public:
9796
TypeIdx() = default;
98-
explicit TypeIdx(uint32_t Idx) : ModuleFileIndex(0), Idx(Idx) {}
9997

10098
explicit TypeIdx(uint32_t ModuleFileIdx, uint32_t Idx)
10199
: ModuleFileIndex(ModuleFileIdx), Idx(Idx) {}
@@ -114,7 +112,7 @@ class TypeIdx {
114112

115113
static TypeIdx fromTypeID(TypeID ID) {
116114
if (ID == TypeID(-1))
117-
return TypeIdx(-1);
115+
return TypeIdx(0, -1);
118116

119117
return TypeIdx(ID >> 32, (ID & llvm::maskTrailingOnes<TypeID>(32)) >>
120118
Qualifiers::FastWidth);

clang/include/clang/Serialization/ASTReader.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,9 @@ class ASTReader
18811881
QualType GetType(serialization::TypeID ID);
18821882

18831883
/// Resolve a local type ID within a given AST file into a type.
1884+
///
1885+
/// A local type ID is only meaningful with the corresponding module file.
1886+
/// See the implementation of getGlobalTypeID for details.
18841887
QualType getLocalType(ModuleFile &F, serialization::TypeID LocalID);
18851888

18861889
/// Map a local type ID within a given AST file into a global type ID.
@@ -1907,7 +1910,6 @@ class ASTReader
19071910
/// if the declaration is not from a module file.
19081911
ModuleFile *getOwningModuleFile(const Decl *D) const;
19091912
ModuleFile *getOwningModuleFile(GlobalDeclID ID) const;
1910-
ModuleFile *getOwningModuleFile(serialization::TypeID ID) const;
19111913

19121914
/// Returns the source location for the decl \p ID.
19131915
SourceLocation getSourceLocationForDeclID(GlobalDeclID ID);

clang/lib/Serialization/ASTCommon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
278278
break;
279279
}
280280

281-
return TypeIdx(ID);
281+
return TypeIdx(0, ID);
282282
}
283283

284284
unsigned serialization::ComputeHash(Selector Sel) {

clang/lib/Serialization/ASTReader.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7084,15 +7084,34 @@ TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
70847084
return TInfo;
70857085
}
70867086

7087+
static unsigned getIndexForTypeID(serialization::TypeID ID) {
7088+
return (ID & llvm::maskTrailingOnes<TypeID>(32)) >> Qualifiers::FastWidth;
7089+
;
7090+
}
7091+
7092+
static unsigned getModuleFileIndexForTypeID(serialization::TypeID ID) {
7093+
return ID >> 32;
7094+
}
7095+
7096+
static bool isPredefinedTypes(serialization::TypeID ID) {
7097+
// We don't need to erase the higher bits since if these bits are not 0,
7098+
// it must be larger than NUM_PREDEF_TYPE_IDS.
7099+
return (ID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS;
7100+
}
7101+
70877102
std::pair<ModuleFile *, unsigned>
70887103
ASTReader::translateTypeIDToIndex(serialization::TypeID ID) const {
7089-
unsigned Index =
7090-
(ID & llvm::maskTrailingOnes<TypeID>(32)) >> Qualifiers::FastWidth;
7104+
assert(!isPredefinedTypes(ID) &&
7105+
"Predefined type shouldn't be in TypesLoaded");
7106+
unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID);
7107+
assert(ModuleFileIndex && "Untranslated Local Decl?");
70917108

7092-
ModuleFile *OwningModuleFile = getOwningModuleFile(ID);
7109+
ModuleFile *OwningModuleFile = &getModuleManager()[ModuleFileIndex - 1];
70937110
assert(OwningModuleFile &&
70947111
"untranslated type ID or local type ID shouldn't be in TypesLoaded");
7095-
return {OwningModuleFile, OwningModuleFile->BaseTypeIndex + Index};
7112+
7113+
return {OwningModuleFile,
7114+
OwningModuleFile->BaseTypeIndex + getIndexForTypeID(ID)};
70967115
}
70977116

70987117
QualType ASTReader::GetType(TypeID ID) {
@@ -7101,9 +7120,9 @@ QualType ASTReader::GetType(TypeID ID) {
71017120

71027121
unsigned FastQuals = ID & Qualifiers::FastMask;
71037122

7104-
if (uint64_t Index = ID >> Qualifiers::FastWidth;
7105-
Index < NUM_PREDEF_TYPE_IDS) {
7123+
if (isPredefinedTypes(ID)) {
71067124
QualType T;
7125+
unsigned Index = getIndexForTypeID(ID);
71077126
switch ((PredefinedTypeIDs)Index) {
71087127
case PREDEF_TYPE_LAST_ID:
71097128
// We should never use this one.
@@ -7394,13 +7413,13 @@ QualType ASTReader::getLocalType(ModuleFile &F, TypeID LocalID) {
73947413

73957414
serialization::TypeID ASTReader::getGlobalTypeID(ModuleFile &F,
73967415
TypeID LocalID) const {
7397-
if ((LocalID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS)
7416+
if (isPredefinedTypes(LocalID))
73987417
return LocalID;
73997418

74007419
if (!F.ModuleOffsetMap.empty())
74017420
ReadModuleOffsetMap(F);
74027421

7403-
unsigned ModuleFileIndex = LocalID >> 32;
7422+
unsigned ModuleFileIndex = getModuleFileIndexForTypeID(LocalID);
74047423
LocalID &= llvm::maskTrailingOnes<TypeID>(32);
74057424

74067425
if (ModuleFileIndex == 0)
@@ -7647,16 +7666,6 @@ ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {
76477666
return &getModuleManager()[ModuleFileIndex - 1];
76487667
}
76497668

7650-
ModuleFile *ASTReader::getOwningModuleFile(TypeID ID) const {
7651-
if (ID < NUM_PREDEF_TYPE_IDS)
7652-
return nullptr;
7653-
7654-
uint64_t ModuleFileIndex = ID >> 32;
7655-
assert(ModuleFileIndex && "Untranslated Local Decl?");
7656-
7657-
return &getModuleManager()[ModuleFileIndex - 1];
7658-
}
7659-
76607669
ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) const {
76617670
if (!D->isFromASTFile())
76627671
return nullptr;

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3263,7 +3263,7 @@ void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
32633263
void ASTWriter::WriteType(QualType T) {
32643264
TypeIdx &IdxRef = TypeIdxs[T];
32653265
if (IdxRef.getValue() == 0) // we haven't seen this type before.
3266-
IdxRef = TypeIdx(NextTypeID++);
3266+
IdxRef = TypeIdx(0, NextTypeID++);
32673267
TypeIdx Idx = IdxRef;
32683268

32693269
assert(Idx.getModuleFileIndex() == 0 && "Re-writing a type from a prior AST");
@@ -6106,9 +6106,9 @@ static TypeID MakeTypeID(ASTContext &Context, QualType T,
61066106
return TypeIdxFromBuiltin(BT).asTypeID(FastQuals);
61076107

61086108
if (T == Context.AutoDeductTy)
6109-
return TypeIdx(PREDEF_TYPE_AUTO_DEDUCT).asTypeID(FastQuals);
6109+
return TypeIdx(0, PREDEF_TYPE_AUTO_DEDUCT).asTypeID(FastQuals);
61106110
if (T == Context.AutoRRefDeductTy)
6111-
return TypeIdx(PREDEF_TYPE_AUTO_RREF_DEDUCT).asTypeID(FastQuals);
6111+
return TypeIdx(0, PREDEF_TYPE_AUTO_RREF_DEDUCT).asTypeID(FastQuals);
61126112

61136113
return IdxForType(T).asTypeID(FastQuals);
61146114
}
@@ -6129,7 +6129,7 @@ TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
61296129

61306130
// We haven't seen this type before. Assign it a new ID and put it
61316131
// into the queue of types to emit.
6132-
Idx = TypeIdx(NextTypeID++);
6132+
Idx = TypeIdx(0, NextTypeID++);
61336133
DeclTypesToEmit.push(T);
61346134
}
61356135
return Idx;
@@ -6659,18 +6659,19 @@ void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
66596659
// This copes with an interesting
66606660
// case for chained AST writing where we schedule writing the type and then,
66616661
// later, deserialize the type from another AST. In this case, we want to
6662-
// keep the just writing entry so that we can properly write it out to
6662+
// keep the entry from a later module so that we can properly write it out to
66636663
// the AST file.
66646664
TypeIdx &StoredIdx = TypeIdxs[T];
66656665

66666666
// Ignore it if the type comes from the current being written module file.
6667+
// Since the current being written module file
66676668
unsigned ModuleFileIndex = StoredIdx.getModuleFileIndex();
66686669
if (ModuleFileIndex == 0 && StoredIdx.getValue())
66696670
return;
66706671

66716672
// Otherwise, keep the highest ID since the module file comes later has
66726673
// higher module file indexes.
6673-
if (Idx.getValue() >= StoredIdx.getValue())
6674+
if (Idx.getModuleFileIndex() >= StoredIdx.getModuleFileIndex())
66746675
StoredIdx = Idx;
66756676
}
66766677

clang/test/Modules/pr59999.cppm

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,44 @@
1212
// RUN: -fmodule-file=Module=%t/Module.pcm -emit-llvm -o - | FileCheck %t/Object.cppm
1313

1414
// Test again with reduced BMI.
15-
// RUNX: rm -rf %t
16-
// RUNX: mkdir -p %t
17-
// RUNX: split-file %s %t
15+
// RUN: rm -rf %t
16+
// RUN: mkdir -p %t
17+
// RUN: split-file %s %t
1818
//
19-
// RUNX: %clang_cc1 -std=c++20 -triple %itanium_abi_triple %t/Module.cppm \
20-
// RUNX: -emit-reduced-module-interface -o %t/Module.pcm
21-
// RUNX: %clang_cc1 -std=c++20 -triple %itanium_abi_triple %t/Object.cppm \
22-
// RUNX: -fmodule-file=Module=%t/Module.pcm -emit-module-interface -o %t/Object.pcm
23-
// RUNX: %clang_cc1 -std=c++20 -triple %itanium_abi_triple %t/Object.pcm \
24-
// RUNX: -fmodule-file=Module=%t/Module.pcm -emit-llvm -o - | FileCheck %t/Object.cppm
19+
// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple %t/Module.cppm \
20+
// RUN: -emit-reduced-module-interface -o %t/Module.pcm
21+
// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple %t/Object.cppm \
22+
// RUN: -fmodule-file=Module=%t/Module.pcm -emit-module-interface -o %t/Object.pcm
23+
// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple %t/Object.pcm \
24+
// RUN: -fmodule-file=Module=%t/Module.pcm -emit-llvm -o - | FileCheck %t/Object.cppm
2525

2626

2727
//--- Module.cppm
2828
export module Module;
2929

3030
export template <class ObjectType> bool ModuleRegister() { return true; };
3131

32-
// export struct ModuleEntry {
33-
// static const bool bRegistered;
34-
// };
32+
export struct ModuleEntry {
33+
static const bool bRegistered;
34+
};
3535

36-
// const bool ModuleEntry::bRegistered = ModuleRegister<ModuleEntry>();
36+
const bool ModuleEntry::bRegistered = ModuleRegister<ModuleEntry>();
3737

3838
//--- Object.cppm
3939
export module Object;
4040

4141
import Module;
4242

43-
// export template <class ObjectType> bool ObjectRegister() { return true; }
44-
// export struct NObject {
45-
// static const bool bRegistered;
46-
// };
43+
export template <class ObjectType> bool ObjectRegister() { return true; }
44+
export struct NObject {
45+
static const bool bRegistered;
46+
};
4747
export struct ObjectModuleEntry {
4848
static const bool bRegistered;
4949
};
5050

5151
// This function is also required for crash
52-
// const bool NObject::bRegistered = ObjectRegister<NObject>();
52+
const bool NObject::bRegistered = ObjectRegister<NObject>();
5353
// One another function, that helps clang crash
5454
const bool ObjectModuleEntry::bRegistered = ModuleRegister<ObjectModuleEntry>();
5555

0 commit comments

Comments
 (0)