Skip to content

Commit

Permalink
clang/Modules: Rename CompilerInstance::ModuleManager, NFC
Browse files Browse the repository at this point in the history
Fix the confusing naming of `CompilerInstance::ModuleManager`.  This is
actually an instance of `ASTReader`, which contains an instance of
`ModuleManager`.  I have to assume there was a point in the past where
they were just one class, but it's been pretty confusing for a while.  I
think it's time to fix it.

The new name is `TheASTReader`; the annoying `The` prefix is so that we
don't shadow the `ASTReader` class.  I tried out `ASTRdr` but that
seemed less clear, and this choice matches `ThePCHContainerOperations`
just a couple of declarations below.

Also rename `CompilerInstance::getModuleManager` and
`CompilerInstance::createModuleManager` to `*ASTReader`, making some
cases of `getModuleManager().getModuleManager()` a little more clear.

https://reviews.llvm.org/D70583
  • Loading branch information
dexonsmith committed Nov 23, 2019
1 parent 5cca622 commit 20d51b2
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void ExpandModularHeadersPPCallbacks::handleModuleFile(

// Visit all the input files of this module and mark them to record their
// contents later.
Compiler.getModuleManager()->visitInputFiles(
Compiler.getASTReader()->visitInputFiles(
*MF, true, false,
[this](const serialization::InputFile &IF, bool /*IsSystem*/) {
Recorder->addNecessaryFile(IF.getFile());
Expand Down Expand Up @@ -153,7 +153,7 @@ void ExpandModularHeadersPPCallbacks::InclusionDirective(
const Module *Imported, SrcMgr::CharacteristicKind FileType) {
if (Imported) {
serialization::ModuleFile *MF =
Compiler.getModuleManager()->getModuleManager().lookup(
Compiler.getASTReader()->getModuleManager().lookup(
Imported->getASTFile());
handleModuleFile(MF);
}
Expand Down
6 changes: 3 additions & 3 deletions clang/include/clang/Frontend/CompilerInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class CompilerInstance : public ModuleLoader {
std::unique_ptr<llvm::Timer> FrontendTimer;

/// The ASTReader, if one exists.
IntrusiveRefCntPtr<ASTReader> ModuleManager;
IntrusiveRefCntPtr<ASTReader> TheASTReader;

/// The module dependency collector for crashdumps
std::shared_ptr<ModuleDependencyCollector> ModuleDepCollector;
Expand Down Expand Up @@ -514,7 +514,7 @@ class CompilerInstance : public ModuleLoader {
/// @name Module Management
/// {

IntrusiveRefCntPtr<ASTReader> getModuleManager() const;
IntrusiveRefCntPtr<ASTReader> getASTReader() const;
void setModuleManager(IntrusiveRefCntPtr<ASTReader> Reader);

std::shared_ptr<ModuleDependencyCollector> getModuleDepCollector() const;
Expand Down Expand Up @@ -782,7 +782,7 @@ class CompilerInstance : public ModuleLoader {
}

// Create module manager.
void createModuleManager();
void createASTReader();

bool loadModuleFile(StringRef FileName);

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Frontend/ASTUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,7 @@ void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
CI.setFileManager(nullptr);
if (CI.hasTarget())
Target = &CI.getTarget();
Reader = CI.getModuleManager();
Reader = CI.getASTReader();
HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
}

Expand Down
97 changes: 48 additions & 49 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void CompilerInstance::setInvocation(

bool CompilerInstance::shouldBuildGlobalModuleIndex() const {
return (BuildGlobalModuleIndex ||
(ModuleManager && ModuleManager->isGlobalIndexUnavailable() &&
(TheASTReader && TheASTReader->isGlobalIndexUnavailable() &&
getFrontendOpts().GenerateGlobalModuleIndex)) &&
!ModuleBuildFailed;
}
Expand Down Expand Up @@ -135,13 +135,13 @@ std::unique_ptr<Sema> CompilerInstance::takeSema() {
return std::move(TheSema);
}

IntrusiveRefCntPtr<ASTReader> CompilerInstance::getModuleManager() const {
return ModuleManager;
IntrusiveRefCntPtr<ASTReader> CompilerInstance::getASTReader() const {
return TheASTReader;
}
void CompilerInstance::setModuleManager(IntrusiveRefCntPtr<ASTReader> Reader) {
assert(ModuleCache.get() == &Reader->getModuleManager().getModuleCache() &&
"Expected ASTReader to use the same PCM cache");
ModuleManager = std::move(Reader);
TheASTReader = std::move(Reader);
}

std::shared_ptr<ModuleDependencyCollector>
Expand Down Expand Up @@ -380,7 +380,7 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {
const PreprocessorOptions &PPOpts = getPreprocessorOpts();

// The module manager holds a reference to the old preprocessor (if any).
ModuleManager.reset();
TheASTReader.reset();

// Create the Preprocessor.
HeaderSearch *HeaderInfo =
Expand Down Expand Up @@ -494,7 +494,7 @@ void CompilerInstance::createPCHExternalASTSource(
StringRef Path, bool DisablePCHValidation, bool AllowPCHWithCompilerErrors,
void *DeserializationListener, bool OwnDeserializationListener) {
bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
ModuleManager = createPCHExternalASTSource(
TheASTReader = createPCHExternalASTSource(
Path, getHeaderSearchOpts().Sysroot, DisablePCHValidation,
AllowPCHWithCompilerErrors, getPreprocessor(), getModuleCache(),
getASTContext(), getPCHContainerReader(),
Expand Down Expand Up @@ -1313,7 +1313,7 @@ static bool compileModuleAndReadAST(CompilerInstance &ImportingInstance,

// Try to read the module file, now that we've compiled it.
ASTReader::ASTReadResult ReadResult =
ImportingInstance.getModuleManager()->ReadAST(
ImportingInstance.getASTReader()->ReadAST(
ModuleFileName, serialization::MK_ImplicitModule, ImportLoc,
ModuleLoadCapabilities);

Expand Down Expand Up @@ -1477,8 +1477,8 @@ static void pruneModuleCache(const HeaderSearchOptions &HSOpts) {
}
}

void CompilerInstance::createModuleManager() {
if (ModuleManager)
void CompilerInstance::createASTReader() {
if (TheASTReader)
return;

if (!hasASTContext())
Expand All @@ -1501,29 +1501,28 @@ void CompilerInstance::createModuleManager() {
ReadTimer = std::make_unique<llvm::Timer>("reading_modules",
"Reading modules",
*FrontendTimerGroup);
ModuleManager = new ASTReader(
TheASTReader = new ASTReader(
getPreprocessor(), getModuleCache(), &getASTContext(),
getPCHContainerReader(), getFrontendOpts().ModuleFileExtensions,
Sysroot.empty() ? "" : Sysroot.c_str(), PPOpts.DisablePCHValidation,
/*AllowASTWithCompilerErrors=*/false,
/*AllowConfigurationMismatch=*/false,
HSOpts.ModulesValidateSystemHeaders,
/*AllowConfigurationMismatch=*/false, HSOpts.ModulesValidateSystemHeaders,
HSOpts.ValidateASTInputFilesContent,
getFrontendOpts().UseGlobalModuleIndex, std::move(ReadTimer));
if (hasASTConsumer()) {
ModuleManager->setDeserializationListener(
getASTConsumer().GetASTDeserializationListener());
TheASTReader->setDeserializationListener(
getASTConsumer().GetASTDeserializationListener());
getASTContext().setASTMutationListener(
getASTConsumer().GetASTMutationListener());
}
getASTContext().setExternalSource(ModuleManager);
getASTContext().setExternalSource(TheASTReader);
if (hasSema())
ModuleManager->InitializeSema(getSema());
TheASTReader->InitializeSema(getSema());
if (hasASTConsumer())
ModuleManager->StartTranslationUnit(&getASTConsumer());
TheASTReader->StartTranslationUnit(&getASTConsumer());

for (auto &Listener : DependencyCollectors)
Listener->attachToASTReader(*ModuleManager);
Listener->attachToASTReader(*TheASTReader);
}

bool CompilerInstance::loadModuleFile(StringRef FileName) {
Expand Down Expand Up @@ -1580,8 +1579,8 @@ bool CompilerInstance::loadModuleFile(StringRef FileName) {
};

// If we don't already have an ASTReader, create one now.
if (!ModuleManager)
createModuleManager();
if (!TheASTReader)
createASTReader();

// If -Wmodule-file-config-mismatch is mapped as an error or worse, allow the
// ASTReader to diagnose it, since it can produce better errors that we can.
Expand All @@ -1592,11 +1591,11 @@ bool CompilerInstance::loadModuleFile(StringRef FileName) {

auto Listener = std::make_unique<ReadModuleNames>(*this);
auto &ListenerRef = *Listener;
ASTReader::ListenerScope ReadModuleNamesListener(*ModuleManager,
ASTReader::ListenerScope ReadModuleNamesListener(*TheASTReader,
std::move(Listener));

// Try to load the module file.
switch (ModuleManager->ReadAST(
switch (TheASTReader->ReadAST(
FileName, serialization::MK_ExplicitModule, SourceLocation(),
ConfigMismatchIsRecoverable ? ASTReader::ARR_ConfigurationMismatch : 0)) {
case ASTReader::Success:
Expand Down Expand Up @@ -1696,8 +1695,8 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
}

// Create an ASTReader on demand.
if (!getModuleManager())
createModuleManager();
if (!getASTReader())
createASTReader();

// Time how long it takes to load the module.
llvm::Timer Timer;
Expand All @@ -1714,13 +1713,13 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
: Source == MS_PrebuiltModulePath
? 0
: ASTReader::ARR_ConfigurationMismatch;
switch (getModuleManager()->ReadAST(
ModuleFilename,
Source == MS_PrebuiltModulePath
? serialization::MK_PrebuiltModule
: Source == MS_ModuleBuildPragma ? serialization::MK_ExplicitModule
: serialization::MK_ImplicitModule,
ImportLoc, ARRFlags)) {
switch (getASTReader()->ReadAST(ModuleFilename,
Source == MS_PrebuiltModulePath
? serialization::MK_PrebuiltModule
: Source == MS_ModuleBuildPragma
? serialization::MK_ExplicitModule
: serialization::MK_ImplicitModule,
ImportLoc, ARRFlags)) {
case ASTReader::Success: {
if (M)
return M;
Expand Down Expand Up @@ -1848,8 +1847,8 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
if (ImportLoc.isValid() && LastModuleImportLoc == ImportLoc) {
// Make the named module visible.
if (LastModuleImportResult && ModuleName != getLangOpts().CurrentModule)
ModuleManager->makeModuleVisible(LastModuleImportResult, Visibility,
ImportLoc);
TheASTReader->makeModuleVisible(LastModuleImportResult, Visibility,
ImportLoc);
return LastModuleImportResult;
}

Expand Down Expand Up @@ -2008,7 +2007,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
return ModuleLoadResult();
}

ModuleManager->makeModuleVisible(Module, Visibility, ImportLoc);
TheASTReader->makeModuleVisible(Module, Visibility, ImportLoc);
}

// Check for any configuration macros that have changed.
Expand Down Expand Up @@ -2085,27 +2084,27 @@ void CompilerInstance::createModuleFromSource(SourceLocation ImportLoc,
void CompilerInstance::makeModuleVisible(Module *Mod,
Module::NameVisibilityKind Visibility,
SourceLocation ImportLoc) {
if (!ModuleManager)
createModuleManager();
if (!ModuleManager)
if (!TheASTReader)
createASTReader();
if (!TheASTReader)
return;

ModuleManager->makeModuleVisible(Mod, Visibility, ImportLoc);
TheASTReader->makeModuleVisible(Mod, Visibility, ImportLoc);
}

GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(
SourceLocation TriggerLoc) {
if (getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty())
return nullptr;
if (!ModuleManager)
createModuleManager();
if (!TheASTReader)
createASTReader();
// Can't do anything if we don't have the module manager.
if (!ModuleManager)
if (!TheASTReader)
return nullptr;
// Get an existing global index. This loads it if not already
// loaded.
ModuleManager->loadGlobalIndex();
GlobalModuleIndex *GlobalIndex = ModuleManager->getGlobalIndex();
TheASTReader->loadGlobalIndex();
GlobalModuleIndex *GlobalIndex = TheASTReader->getGlobalIndex();
// If the global index doesn't exist, create it.
if (!GlobalIndex && shouldBuildGlobalModuleIndex() && hasFileManager() &&
hasPreprocessor()) {
Expand All @@ -2121,9 +2120,9 @@ GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(
consumeError(std::move(Err));
return nullptr;
}
ModuleManager->resetForReload();
ModuleManager->loadGlobalIndex();
GlobalIndex = ModuleManager->getGlobalIndex();
TheASTReader->resetForReload();
TheASTReader->loadGlobalIndex();
GlobalIndex = TheASTReader->getGlobalIndex();
}
// For finding modules needing to be imported for fixit messages,
// we need to make the global index cover all modules, so we do that here.
Expand Down Expand Up @@ -2152,9 +2151,9 @@ GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(
consumeError(std::move(Err));
return nullptr;
}
ModuleManager->resetForReload();
ModuleManager->loadGlobalIndex();
GlobalIndex = ModuleManager->getGlobalIndex();
TheASTReader->resetForReload();
TheASTReader->loadGlobalIndex();
GlobalIndex = TheASTReader->getGlobalIndex();
}
HaveFullGlobalModuleIndex = true;
}
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/Frontend/FrontendAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,9 +871,9 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
// extended by an external source.
if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
!CI.getASTContext().getExternalSource()) {
CI.createModuleManager();
CI.getModuleManager()->setDeserializationListener(DeserialListener,
DeleteDeserialListener);
CI.createASTReader();
CI.getASTReader()->setDeserializationListener(DeserialListener,
DeleteDeserialListener);
}
}

Expand All @@ -892,7 +892,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
} else {
// FIXME: If this is a problem, recover from it by creating a multiplex
// source.
assert((!CI.getLangOpts().Modules || CI.getModuleManager()) &&
assert((!CI.getLangOpts().Modules || CI.getASTReader()) &&
"modules enabled but created an external source that "
"doesn't support modules");
}
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Frontend/Rewrite/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class RewriteIncludesAction::RewriteImportsListener : public ASTReaderListener {
return;

serialization::ModuleFile *MF =
CI.getModuleManager()->getModuleManager().lookup(*File);
CI.getASTReader()->getModuleManager().lookup(*File);
assert(MF && "missing module file for loaded module?");

// Not interested in PCH / preambles.
Expand Down Expand Up @@ -293,8 +293,8 @@ bool RewriteIncludesAction::BeginSourceFileAction(CompilerInstance &CI) {
// If we're rewriting imports, set up a listener to track when we import
// module files.
if (CI.getPreprocessorOutputOpts().RewriteImports) {
CI.createModuleManager();
CI.getModuleManager()->addListener(
CI.createASTReader();
CI.getASTReader()->addListener(
std::make_unique<RewriteImportsListener>(CI, OutputStream));
}

Expand Down
5 changes: 2 additions & 3 deletions clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ void ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
MD.ModulePCMPath = M->getASTFile()->getName();
MD.ContextHash = MDC.ContextHash;
serialization::ModuleFile *MF =
MDC.Instance.getModuleManager()->getModuleManager().lookup(
M->getASTFile());
MDC.Instance.getModuleManager()->visitInputFiles(
MDC.Instance.getASTReader()->getModuleManager().lookup(M->getASTFile());
MDC.Instance.getASTReader()->visitInputFiles(
*MF, true, true, [&](const serialization::InputFile &IF, bool isSystem) {
MD.FileDeps.insert(IF.getFile()->getName());
});
Expand Down

0 comments on commit 20d51b2

Please sign in to comment.