Skip to content

Commit c915cb9

Browse files
committed
Avoid including Module.h from ExternalASTSource.h
Module.h takes 86ms to parse, mostly parsing the class itself. Avoid it if possible. ASTContext.h depends on ExternalASTSource.h. A few NFC changes were needed to make this possible: - Move ASTSourceDescriptor to Module.h. This needs Module to be complete, and seems more related to modules and AST files than external AST sources. - Move "import complete" bit from Module* pointer int pair to NextLocalImport pointer. Required because PointerIntPair<Module*,...> requires Module to be complete, and now it may not be. Reviewed By: aaron.ballman, hans Differential Revision: https://reviews.llvm.org/D75784
1 parent a0cacb6 commit c915cb9

File tree

17 files changed

+115
-87
lines changed

17 files changed

+115
-87
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class Preprocessor;
121121
class Stmt;
122122
class StoredDeclsMap;
123123
class TargetAttr;
124+
class TargetInfo;
124125
class TemplateDecl;
125126
class TemplateParameterList;
126127
class TemplateTemplateParmDecl;
@@ -881,7 +882,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
881882
void addedLocalImportDecl(ImportDecl *Import);
882883

883884
static ImportDecl *getNextLocalImport(ImportDecl *Import) {
884-
return Import->NextLocalImport;
885+
return Import->getNextLocalImport();
885886
}
886887

887888
using import_range = llvm::iterator_range<import_iterator>;
@@ -909,13 +910,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
909910

910911
/// Get the additional modules in which the definition \p Def has
911912
/// been merged.
912-
ArrayRef<Module*> getModulesWithMergedDefinition(const NamedDecl *Def) {
913-
auto MergedIt =
914-
MergedDefModules.find(cast<NamedDecl>(Def->getCanonicalDecl()));
915-
if (MergedIt == MergedDefModules.end())
916-
return None;
917-
return MergedIt->second;
918-
}
913+
ArrayRef<Module*> getModulesWithMergedDefinition(const NamedDecl *Def);
919914

920915
/// Add a declaration to the list of declarations that are initialized
921916
/// for a module. This will typically be a global variable (with internal

clang/include/clang/AST/Decl.h

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4340,17 +4340,18 @@ class ImportDecl final : public Decl,
43404340
friend class ASTReader;
43414341
friend TrailingObjects;
43424342

4343-
/// The imported module, along with a bit that indicates whether
4344-
/// we have source-location information for each identifier in the module
4345-
/// name.
4346-
///
4347-
/// When the bit is false, we only have a single source location for the
4348-
/// end of the import declaration.
4349-
llvm::PointerIntPair<Module *, 1, bool> ImportedAndComplete;
4343+
/// The imported module.
4344+
Module *ImportedModule = nullptr;
43504345

43514346
/// The next import in the list of imports local to the translation
43524347
/// unit being parsed (not loaded from an AST file).
4353-
ImportDecl *NextLocalImport = nullptr;
4348+
///
4349+
/// Includes a bit that indicates whether we have source-location information
4350+
/// for each identifier in the module name.
4351+
///
4352+
/// When the bit is false, we only have a single source location for the
4353+
/// end of the import declaration.
4354+
llvm::PointerIntPair<ImportDecl *, 1, bool> NextLocalImportAndComplete;
43544355

43554356
ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported,
43564357
ArrayRef<SourceLocation> IdentifierLocs);
@@ -4360,6 +4361,20 @@ class ImportDecl final : public Decl,
43604361

43614362
ImportDecl(EmptyShell Empty) : Decl(Import, Empty) {}
43624363

4364+
bool isImportComplete() const { return NextLocalImportAndComplete.getInt(); }
4365+
4366+
void setImportComplete(bool C) { NextLocalImportAndComplete.setInt(C); }
4367+
4368+
/// The next import in the list of imports local to the translation
4369+
/// unit being parsed (not loaded from an AST file).
4370+
ImportDecl *getNextLocalImport() const {
4371+
return NextLocalImportAndComplete.getPointer();
4372+
}
4373+
4374+
void setNextLocalImport(ImportDecl *Import) {
4375+
NextLocalImportAndComplete.setPointer(Import);
4376+
}
4377+
43634378
public:
43644379
/// Create a new module import declaration.
43654380
static ImportDecl *Create(ASTContext &C, DeclContext *DC,
@@ -4377,7 +4392,7 @@ class ImportDecl final : public Decl,
43774392
unsigned NumLocations);
43784393

43794394
/// Retrieve the module that was imported by the import declaration.
4380-
Module *getImportedModule() const { return ImportedAndComplete.getPointer(); }
4395+
Module *getImportedModule() const { return ImportedModule; }
43814396

43824397
/// Retrieves the locations of each of the identifiers that make up
43834398
/// the complete module name in the import declaration.

clang/include/clang/AST/ExternalASTSource.h

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "clang/AST/CharUnits.h"
1818
#include "clang/AST/DeclBase.h"
1919
#include "clang/Basic/LLVM.h"
20-
#include "clang/Basic/Module.h"
2120
#include "llvm/ADT/ArrayRef.h"
2221
#include "llvm/ADT/DenseMap.h"
2322
#include "llvm/ADT/IntrusiveRefCntPtr.h"
@@ -39,6 +38,7 @@ namespace clang {
3938

4039
class ASTConsumer;
4140
class ASTContext;
41+
class ASTSourceDescriptor;
4242
class CXXBaseSpecifier;
4343
class CXXCtorInitializer;
4444
class CXXRecordDecl;
@@ -165,31 +165,6 @@ class ExternalASTSource : public RefCountedBase<ExternalASTSource> {
165165
/// object file.
166166
virtual bool DeclIsFromPCHWithObjectFile(const Decl *D) { return false; }
167167

168-
/// Abstracts clang modules and precompiled header files and holds
169-
/// everything needed to generate debug info for an imported module
170-
/// or PCH.
171-
class ASTSourceDescriptor {
172-
StringRef PCHModuleName;
173-
StringRef Path;
174-
StringRef ASTFile;
175-
ASTFileSignature Signature;
176-
const Module *ClangModule = nullptr;
177-
178-
public:
179-
ASTSourceDescriptor() = default;
180-
ASTSourceDescriptor(StringRef Name, StringRef Path, StringRef ASTFile,
181-
ASTFileSignature Signature)
182-
: PCHModuleName(std::move(Name)), Path(std::move(Path)),
183-
ASTFile(std::move(ASTFile)), Signature(Signature) {}
184-
ASTSourceDescriptor(const Module &M);
185-
186-
std::string getModuleName() const;
187-
StringRef getPath() const { return Path; }
188-
StringRef getASTFile() const { return ASTFile; }
189-
ASTFileSignature getSignature() const { return Signature; }
190-
const Module *getModuleOrNull() const { return ClangModule; }
191-
};
192-
193168
/// Return a descriptor for the corresponding module, if one exists.
194169
virtual llvm::Optional<ASTSourceDescriptor> getSourceDescriptor(unsigned ID);
195170

clang/include/clang/Basic/Module.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,32 @@ class VisibleModuleSet {
654654
unsigned Generation = 0;
655655
};
656656

657+
/// Abstracts clang modules and precompiled header files and holds
658+
/// everything needed to generate debug info for an imported module
659+
/// or PCH.
660+
class ASTSourceDescriptor {
661+
StringRef PCHModuleName;
662+
StringRef Path;
663+
StringRef ASTFile;
664+
ASTFileSignature Signature;
665+
const Module *ClangModule = nullptr;
666+
667+
public:
668+
ASTSourceDescriptor() = default;
669+
ASTSourceDescriptor(StringRef Name, StringRef Path, StringRef ASTFile,
670+
ASTFileSignature Signature)
671+
: PCHModuleName(std::move(Name)), Path(std::move(Path)),
672+
ASTFile(std::move(ASTFile)), Signature(Signature) {}
673+
ASTSourceDescriptor(const Module &M);
674+
675+
std::string getModuleName() const;
676+
StringRef getPath() const { return Path; }
677+
StringRef getASTFile() const { return ASTFile; }
678+
ASTFileSignature getSignature() const { return Signature; }
679+
const Module *getModuleOrNull() const { return ClangModule; }
680+
};
681+
682+
657683
} // namespace clang
658684

659685
#endif // LLVM_CLANG_BASIC_MODULE_H

clang/include/clang/StaticAnalyzer/Frontend/CheckerRegistry.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
1414
#include "llvm/ADT/StringMap.h"
1515
#include "llvm/ADT/StringRef.h"
16+
#include "llvm/ADT/SetVector.h"
1617
#include <cstddef>
1718
#include <vector>
1819

clang/lib/AST/ASTContext.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "clang/Basic/LLVM.h"
5656
#include "clang/Basic/LangOptions.h"
5757
#include "clang/Basic/Linkage.h"
58+
#include "clang/Basic/Module.h"
5859
#include "clang/Basic/ObjCRuntime.h"
5960
#include "clang/Basic/SanitizerBlacklist.h"
6061
#include "clang/Basic/SourceLocation.h"
@@ -1103,6 +1104,15 @@ void ASTContext::deduplicateMergedDefinitonsFor(NamedDecl *ND) {
11031104
Merged.erase(std::remove(Merged.begin(), Merged.end(), nullptr), Merged.end());
11041105
}
11051106

1107+
ArrayRef<Module *>
1108+
ASTContext::getModulesWithMergedDefinition(const NamedDecl *Def) {
1109+
auto MergedIt =
1110+
MergedDefModules.find(cast<NamedDecl>(Def->getCanonicalDecl()));
1111+
if (MergedIt == MergedDefModules.end())
1112+
return None;
1113+
return MergedIt->second;
1114+
}
1115+
11061116
void ASTContext::PerModuleInitializers::resolve(ASTContext &Ctx) {
11071117
if (LazyInitializers.empty())
11081118
return;
@@ -1606,15 +1616,15 @@ void ASTContext::getOverriddenMethods(
16061616
}
16071617

16081618
void ASTContext::addedLocalImportDecl(ImportDecl *Import) {
1609-
assert(!Import->NextLocalImport && "Import declaration already in the chain");
1619+
assert(!Import->getNextLocalImport() && "Import declaration already in the chain");
16101620
assert(!Import->isFromASTFile() && "Non-local import declaration");
16111621
if (!FirstLocalImport) {
16121622
FirstLocalImport = Import;
16131623
LastLocalImport = Import;
16141624
return;
16151625
}
16161626

1617-
LastLocalImport->NextLocalImport = Import;
1627+
LastLocalImport->setNextLocalImport(Import);
16181628
LastLocalImport = Import;
16191629
}
16201630

clang/lib/AST/Decl.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4896,7 +4896,8 @@ static unsigned getNumModuleIdentifiers(Module *Mod) {
48964896
ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
48974897
Module *Imported,
48984898
ArrayRef<SourceLocation> IdentifierLocs)
4899-
: Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true) {
4899+
: Decl(Import, DC, StartLoc), ImportedModule(Imported),
4900+
NextLocalImportAndComplete(nullptr, true) {
49004901
assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
49014902
auto *StoredLocs = getTrailingObjects<SourceLocation>();
49024903
std::uninitialized_copy(IdentifierLocs.begin(), IdentifierLocs.end(),
@@ -4905,7 +4906,8 @@ ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
49054906

49064907
ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
49074908
Module *Imported, SourceLocation EndLoc)
4908-
: Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false) {
4909+
: Decl(Import, DC, StartLoc), ImportedModule(Imported),
4910+
NextLocalImportAndComplete(nullptr, false) {
49094911
*getTrailingObjects<SourceLocation>() = EndLoc;
49104912
}
49114913

@@ -4934,7 +4936,7 @@ ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
49344936
}
49354937

49364938
ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
4937-
if (!ImportedAndComplete.getInt())
4939+
if (!isImportComplete())
49384940
return None;
49394941

49404942
const auto *StoredLocs = getTrailingObjects<SourceLocation>();
@@ -4943,7 +4945,7 @@ ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
49434945
}
49444946

49454947
SourceRange ImportDecl::getSourceRange() const {
4946-
if (!ImportedAndComplete.getInt())
4948+
if (!isImportComplete())
49474949
return SourceRange(getLocation(), *getTrailingObjects<SourceLocation>());
49484950

49494951
return SourceRange(getLocation(), getIdentifierLocs().back());

clang/lib/AST/ExternalASTSource.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ char ExternalASTSource::ID;
2929

3030
ExternalASTSource::~ExternalASTSource() = default;
3131

32-
llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
32+
llvm::Optional<ASTSourceDescriptor>
3333
ExternalASTSource::getSourceDescriptor(unsigned ID) {
3434
return None;
3535
}
@@ -39,21 +39,6 @@ ExternalASTSource::hasExternalDefinitions(const Decl *D) {
3939
return EK_ReplyHazy;
4040
}
4141

42-
ExternalASTSource::ASTSourceDescriptor::ASTSourceDescriptor(const Module &M)
43-
: Signature(M.Signature), ClangModule(&M) {
44-
if (M.Directory)
45-
Path = M.Directory->getName();
46-
if (auto *File = M.getASTFile())
47-
ASTFile = File->getName();
48-
}
49-
50-
std::string ExternalASTSource::ASTSourceDescriptor::getModuleName() const {
51-
if (ClangModule)
52-
return ClangModule->Name;
53-
else
54-
return std::string(PCHModuleName);
55-
}
56-
5742
void ExternalASTSource::FindFileRegionDecls(FileID File, unsigned Offset,
5843
unsigned Length,
5944
SmallVectorImpl<Decl *> &Decls) {}

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// http://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling
1414
//
1515
//===----------------------------------------------------------------------===//
16+
1617
#include "clang/AST/Mangle.h"
1718
#include "clang/AST/ASTContext.h"
1819
#include "clang/AST/Attr.h"
@@ -27,6 +28,7 @@
2728
#include "clang/AST/ExprObjC.h"
2829
#include "clang/AST/TypeLoc.h"
2930
#include "clang/Basic/ABI.h"
31+
#include "clang/Basic/Module.h"
3032
#include "clang/Basic/SourceManager.h"
3133
#include "clang/Basic/TargetInfo.h"
3234
#include "llvm/ADT/StringExtras.h"

clang/lib/AST/TextNodeDumper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "clang/AST/DeclOpenMP.h"
1616
#include "clang/AST/DeclTemplate.h"
1717
#include "clang/AST/LocInfoType.h"
18+
#include "clang/Basic/Module.h"
1819
#include "clang/Basic/SourceManager.h"
1920

2021
using namespace clang;

0 commit comments

Comments
 (0)