[clang] b8e3b2a - [NFC] [Serialization] Turn type alias LocalDeclID into class
Chuanqi Xu via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 23 01:57:15 PDT 2024
Author: Chuanqi Xu
Date: 2024-04-23T16:56:14+08:00
New Revision: b8e3b2ad66cf78ad2b7832577b1d58dc93c5da21
URL: https://github.com/llvm/llvm-project/commit/b8e3b2ad66cf78ad2b7832577b1d58dc93c5da21
DIFF: https://github.com/llvm/llvm-project/commit/b8e3b2ad66cf78ad2b7832577b1d58dc93c5da21.diff
LOG: [NFC] [Serialization] Turn type alias LocalDeclID into class
Previously, the LocalDeclID and GlobalDeclID are defined as:
```
using LocalDeclID = DeclID;
using GlobalDeclID = DeclID;
```
This is more or less concerning that we may misuse LocalDeclID and
GlobalDeclID without understanding it. There is also a FIXME saying
this.
This patch tries to turn LocalDeclID into a class to improve the type
safety here.
Added:
Modified:
clang/include/clang/Serialization/ASTBitCodes.h
clang/include/clang/Serialization/ASTReader.h
clang/include/clang/Serialization/ModuleFile.h
clang/lib/Serialization/ASTReader.cpp
clang/lib/Serialization/ASTReaderDecl.cpp
clang/utils/TableGen/ClangAttrEmitter.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Serialization/ASTBitCodes.h b/clang/include/clang/Serialization/ASTBitCodes.h
index c91a1c1c82edd4..ca51a2dff3d57b 100644
--- a/clang/include/clang/Serialization/ASTBitCodes.h
+++ b/clang/include/clang/Serialization/ASTBitCodes.h
@@ -69,9 +69,18 @@ using IdentifierID = uint32_t;
/// FIXME: Merge with Decl::DeclID
using DeclID = uint32_t;
-// FIXME: Turn these into classes so we can have some type safety when
+class LocalDeclID {
+public:
+ explicit LocalDeclID(DeclID ID) : ID(ID) {}
+
+ DeclID get() const { return ID; }
+
+private:
+ DeclID ID;
+};
+
+// FIXME: Turn GlobalDeclID into class so we can have some type safety when
// we go from local ID to global and vice-versa.
-using LocalDeclID = DeclID;
using GlobalDeclID = DeclID;
/// An ID number that refers to a type in an AST file.
diff --git a/clang/include/clang/Serialization/ASTReader.h b/clang/include/clang/Serialization/ASTReader.h
index fe9644eaca4916..42aecf059907e8 100644
--- a/clang/include/clang/Serialization/ASTReader.h
+++ b/clang/include/clang/Serialization/ASTReader.h
@@ -1924,7 +1924,7 @@ class ASTReader
Decl *GetExistingDecl(serialization::DeclID ID);
/// Reads a declaration with the given local ID in the given module.
- Decl *GetLocalDecl(ModuleFile &F, serialization::DeclID LocalID) {
+ Decl *GetLocalDecl(ModuleFile &F, serialization::LocalDeclID LocalID) {
return GetDecl(getGlobalDeclID(F, LocalID));
}
@@ -1932,7 +1932,7 @@ class ASTReader
///
/// \returns The requested declaration, casted to the given return type.
template <typename T>
- T *GetLocalDeclAs(ModuleFile &F, serialization::DeclID LocalID) {
+ T *GetLocalDeclAs(ModuleFile &F, serialization::LocalDeclID LocalID) {
return cast_or_null<T>(GetLocalDecl(F, LocalID));
}
diff --git a/clang/include/clang/Serialization/ModuleFile.h b/clang/include/clang/Serialization/ModuleFile.h
index 675e1e9bc355c5..492c35dceb08d4 100644
--- a/clang/include/clang/Serialization/ModuleFile.h
+++ b/clang/include/clang/Serialization/ModuleFile.h
@@ -474,7 +474,7 @@ class ModuleFile {
llvm::DenseMap<ModuleFile *, serialization::DeclID> GlobalToLocalDeclIDs;
/// Array of file-level DeclIDs sorted by file.
- const serialization::DeclID *FileSortedDecls = nullptr;
+ const serialization::LocalDeclID *FileSortedDecls = nullptr;
unsigned NumFileSortedDecls = 0;
/// Array of category list location information within this
diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp
index 9764fdc6cd2d49..cfb6ab42c36bd7 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -954,14 +954,16 @@ ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
// Load instance methods
for (unsigned I = 0; I != NumInstanceMethods; ++I) {
if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
- F, endian::readNext<DeclID, llvm::endianness::little>(d)))
+ F,
+ LocalDeclID(endian::readNext<DeclID, llvm::endianness::little>(d))))
Result.Instance.push_back(Method);
}
// Load factory methods
for (unsigned I = 0; I != NumFactoryMethods; ++I) {
if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
- F, endian::readNext<DeclID, llvm::endianness::little>(d)))
+ F,
+ LocalDeclID(endian::readNext<DeclID, llvm::endianness::little>(d))))
Result.Factory.push_back(Method);
}
@@ -1091,7 +1093,8 @@ IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
SmallVector<DeclID, 4> DeclIDs;
for (; DataLen > 0; DataLen -= sizeof(DeclID))
DeclIDs.push_back(Reader.getGlobalDeclID(
- F, endian::readNext<DeclID, llvm::endianness::little>(d)));
+ F,
+ LocalDeclID(endian::readNext<DeclID, llvm::endianness::little>(d))));
Reader.SetGloballyVisibleDecls(II, DeclIDs);
}
@@ -1212,7 +1215,7 @@ void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
using namespace llvm::support;
for (unsigned NumDecls = DataLen / sizeof(DeclID); NumDecls; --NumDecls) {
- DeclID LocalID = endian::readNext<DeclID, llvm::endianness::little>(d);
+ LocalDeclID LocalID(endian::readNext<DeclID, llvm::endianness::little>(d));
Val.insert(Reader.getGlobalDeclID(F, LocalID));
}
}
@@ -1652,7 +1655,7 @@ bool ASTReader::ReadSLocEntry(int ID) {
unsigned NumFileDecls = Record[7];
if (NumFileDecls && ContextObj) {
- const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
+ const LocalDeclID *FirstDecl = F->FileSortedDecls + Record[6];
assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
FileDeclIDs[FID] =
FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
@@ -3458,7 +3461,8 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
// FIXME: Skip reading this record if our ASTConsumer doesn't care
// about "interesting" decls (for instance, if we're building a module).
for (unsigned I = 0, N = Record.size(); I != N; ++I)
- EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
+ EagerlyDeserializedDecls.push_back(
+ getGlobalDeclID(F, LocalDeclID(Record[I])));
break;
case MODULAR_CODEGEN_DECLS:
@@ -3467,7 +3471,8 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
if (F.Kind == MK_MainFile ||
getContext().getLangOpts().BuildingPCHWithObjectFile)
for (unsigned I = 0, N = Record.size(); I != N; ++I)
- EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
+ EagerlyDeserializedDecls.push_back(
+ getGlobalDeclID(F, LocalDeclID(Record[I])));
break;
case SPECIAL_TYPES:
@@ -3499,12 +3504,14 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
case UNUSED_FILESCOPED_DECLS:
for (unsigned I = 0, N = Record.size(); I != N; ++I)
- UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
+ UnusedFileScopedDecls.push_back(
+ getGlobalDeclID(F, LocalDeclID(Record[I])));
break;
case DELEGATING_CTORS:
for (unsigned I = 0, N = Record.size(); I != N; ++I)
- DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
+ DelegatingCtorDecls.push_back(
+ getGlobalDeclID(F, LocalDeclID(Record[I])));
break;
case WEAK_UNDECLARED_IDENTIFIERS:
@@ -3612,7 +3619,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
break;
case FILE_SORTED_DECLS:
- F.FileSortedDecls = (const DeclID *)Blob.data();
+ F.FileSortedDecls = (const LocalDeclID *)Blob.data();
F.NumFileSortedDecls = Record[0];
break;
@@ -3667,7 +3674,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
case EXT_VECTOR_DECLS:
for (unsigned I = 0, N = Record.size(); I != N; ++I)
- ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
+ ExtVectorDecls.push_back(getGlobalDeclID(F, LocalDeclID(Record[I])));
break;
case VTABLE_USES:
@@ -3681,7 +3688,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
VTableUses.clear();
for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
- VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
+ VTableUses.push_back(getGlobalDeclID(F, LocalDeclID(Record[Idx++])));
VTableUses.push_back(
ReadSourceLocation(F, Record, Idx).getRawEncoding());
VTableUses.push_back(Record[Idx++]);
@@ -3700,7 +3707,8 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
"Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
- PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
+ PendingInstantiations.push_back(
+ getGlobalDeclID(F, LocalDeclID(Record[I++])));
PendingInstantiations.push_back(
ReadSourceLocation(F, Record, I).getRawEncoding());
}
@@ -3711,7 +3719,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
return llvm::createStringError(std::errc::illegal_byte_sequence,
"Invalid SEMA_DECL_REFS block");
for (unsigned I = 0, N = Record.size(); I != N; ++I)
- SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
+ SemaDeclRefs.push_back(getGlobalDeclID(F, LocalDeclID(Record[I])));
break;
case PPD_ENTITIES_OFFSETS: {
@@ -3770,7 +3778,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
std::errc::illegal_byte_sequence,
"invalid DECL_UPDATE_OFFSETS block in AST file");
for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
- GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
+ GlobalDeclID ID = getGlobalDeclID(F, LocalDeclID(Record[I]));
DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
// If we've already loaded the decl, perform the updates when we finish
@@ -3788,7 +3796,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
"invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "
"file");
for (unsigned I = 0, N = Record.size(); I != N; I += 3) {
- GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
+ GlobalDeclID ID = getGlobalDeclID(F, LocalDeclID(Record[I]));
uint64_t BaseOffset = F.DeclsBlockStartOffset;
assert(BaseOffset && "Invalid DeclsBlockStartOffset for module file!");
@@ -3823,7 +3831,8 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
// FIXME: Modules will have trouble with this.
CUDASpecialDeclRefs.clear();
for (unsigned I = 0, N = Record.size(); I != N; ++I)
- CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
+ CUDASpecialDeclRefs.push_back(
+ getGlobalDeclID(F, LocalDeclID(Record[I])));
break;
case HEADER_SEARCH_TABLE:
@@ -3864,12 +3873,13 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
case TENTATIVE_DEFINITIONS:
for (unsigned I = 0, N = Record.size(); I != N; ++I)
- TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
+ TentativeDefinitions.push_back(
+ getGlobalDeclID(F, LocalDeclID(Record[I])));
break;
case KNOWN_NAMESPACES:
for (unsigned I = 0, N = Record.size(); I != N; ++I)
- KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
+ KnownNamespaces.push_back(getGlobalDeclID(F, LocalDeclID(Record[I])));
break;
case UNDEFINED_BUT_USED:
@@ -3881,7 +3891,8 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
return llvm::createStringError(std::errc::illegal_byte_sequence,
"invalid undefined-but-used record");
for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
- UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
+ UndefinedButUsed.push_back(
+ getGlobalDeclID(F, LocalDeclID(Record[I++])));
UndefinedButUsed.push_back(
ReadSourceLocation(F, Record, I).getRawEncoding());
}
@@ -3889,7 +3900,8 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
case DELETE_EXPRS_TO_ANALYZE:
for (unsigned I = 0, N = Record.size(); I != N;) {
- DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
+ DelayedDeleteExprs.push_back(
+ getGlobalDeclID(F, LocalDeclID(Record[I++])));
const uint64_t Count = Record[I++];
DelayedDeleteExprs.push_back(Count);
for (uint64_t C = 0; C < Count; ++C) {
@@ -3974,7 +3986,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
for (unsigned I = 0, N = Record.size(); I != N; ++I)
UnusedLocalTypedefNameCandidates.push_back(
- getGlobalDeclID(F, Record[I]));
+ getGlobalDeclID(F, LocalDeclID(Record[I])));
break;
case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
@@ -4030,7 +4042,8 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
for (unsigned I = 0, N = Record.size(); I != N; ++I)
- DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I]));
+ DeclsToCheckForDeferredDiags.insert(
+ getGlobalDeclID(F, LocalDeclID(Record[I])));
break;
}
}
@@ -6010,7 +6023,7 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
break;
SmallVector<DeclID, 16> Inits;
for (auto &ID : Record)
- Inits.push_back(getGlobalDeclID(F, ID));
+ Inits.push_back(getGlobalDeclID(F, LocalDeclID(ID)));
ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
break;
}
@@ -7650,17 +7663,18 @@ CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
serialization::DeclID
ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
- if (LocalID < NUM_PREDEF_DECL_IDS)
- return LocalID;
+ DeclID ID = LocalID.get();
+ if (ID < NUM_PREDEF_DECL_IDS)
+ return ID;
if (!F.ModuleOffsetMap.empty())
ReadModuleOffsetMap(F);
ContinuousRangeMap<DeclID, int, 2>::iterator I =
- F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
+ F.DeclRemap.find(ID - NUM_PREDEF_DECL_IDS);
assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
- return LocalID + I->second;
+ return ID + I->second;
}
bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
@@ -7830,7 +7844,7 @@ serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
return 0;
}
- return getGlobalDeclID(F, Record[Idx++]);
+ return getGlobalDeclID(F, LocalDeclID(Record[Idx++]));
}
/// Resolve the offset of a statement into a statement.
@@ -7877,7 +7891,7 @@ void ASTReader::FindExternalLexicalDecls(
PredefsVisited[ID] = true;
}
- if (Decl *D = GetLocalDecl(*M, ID)) {
+ if (Decl *D = GetLocalDecl(*M, LocalDeclID(ID))) {
assert(D->getKind() == K && "wrong kind for lexical decl");
if (!DC->isDeclInLexicalTraversal(D))
Decls.push_back(D);
@@ -8767,11 +8781,11 @@ void ASTReader::ReadLateParsedTemplates(
RecordDataImpl &LateParsed = LPT.second;
for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
/* In loop */) {
- FunctionDecl *FD =
- cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
+ FunctionDecl *FD = cast<FunctionDecl>(
+ GetLocalDecl(*FMod, LocalDeclID(LateParsed[Idx++])));
auto LT = std::make_unique<LateParsedTemplate>();
- LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
+ LT->D = GetLocalDecl(*FMod, LocalDeclID(LateParsed[Idx++]));
LT->FPO = FPOptions::getFromOpaqueInt(LateParsed[Idx++]);
ModuleFile *F = getOwningModuleFile(LT->D);
diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp
index 68261d6c42a584..7e1b1592c9d0b1 100644
--- a/clang/lib/Serialization/ASTReaderDecl.cpp
+++ b/clang/lib/Serialization/ASTReaderDecl.cpp
@@ -3140,7 +3140,7 @@ class AttrReader {
OMPTraitInfo *readOMPTraitInfo() { return Reader.readOMPTraitInfo(); }
- template <typename T> T *GetLocalDeclAs(DeclID LocalID) {
+ template <typename T> T *GetLocalDeclAs(LocalDeclID LocalID) {
return Reader.GetLocalDeclAs<T>(LocalID);
}
};
@@ -4327,7 +4327,7 @@ void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
// we should instead generate one loop per kind and dispatch up-front?
Decl *MostRecent = FirstLocal;
for (unsigned I = 0, N = Record.size(); I != N; ++I) {
- auto *D = GetLocalDecl(*M, Record[N - I - 1]);
+ auto *D = GetLocalDecl(*M, LocalDeclID(Record[N - I - 1]));
ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
MostRecent = D;
}
@@ -4437,7 +4437,7 @@ namespace {
M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
for (unsigned I = 0; I != N; ++I)
add(cast_or_null<ObjCCategoryDecl>(
- Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
+ Reader.GetLocalDecl(M, LocalDeclID(M.ObjCCategories[Offset++]))));
return true;
}
};
diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp b/clang/utils/TableGen/ClangAttrEmitter.cpp
index 357c4e5e852cf7..0d1365f09291e0 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -107,7 +107,7 @@ static std::string ReadPCHRecord(StringRef type) {
return StringSwitch<std::string>(type)
.EndsWith("Decl *", "Record.GetLocalDeclAs<" +
std::string(type.data(), 0, type.size() - 1) +
- ">(Record.readInt())")
+ ">(LocalDeclID(Record.readInt()))")
.Case("TypeSourceInfo *", "Record.readTypeSourceInfo()")
.Case("Expr *", "Record.readExpr()")
.Case("IdentifierInfo *", "Record.readIdentifier()")
More information about the cfe-commits
mailing list