Skip to content

Commit e9b7fe8

Browse files
authored
[clangd] [Modules] Use ASTReader directly in IsModuleFileUpToDate (#113879)
@kadircet mentioned in 448d8fa#diff-fb3ba8a781117ff04736f951a274812cb7ad1678f9d71d4d91870b711ab45da0L285 that: > this is definitely a functional change, clangd is used in environments that solely relies on VFS, and doesn't depend on ASTUnit at all. > right now this is both introducing a dependency on ASTUnit, and making all the logical IO physical instead. can you instead use the regular compiler utilities in clangd, and get the astreader from CompilerInstance directly, which is VFS-aware, and doesn't depend on ASTUnit ? This tries to resolve the problem by creating ASTReader directly and use VFS to create the FileManager.
1 parent 3243e3d commit e9b7fe8

File tree

1 file changed

+41
-22
lines changed

1 file changed

+41
-22
lines changed

clang-tools-extra/clangd/ModulesBuilder.cpp

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "clang/Frontend/FrontendAction.h"
1313
#include "clang/Frontend/FrontendActions.h"
1414
#include "clang/Serialization/ASTReader.h"
15+
#include "clang/Serialization/InMemoryModuleCache.h"
1516

1617
namespace clang {
1718
namespace clangd {
@@ -127,50 +128,68 @@ struct ModuleFile {
127128
std::string ModuleFilePath;
128129
};
129130

130-
bool IsModuleFileUpToDate(
131-
PathRef ModuleFilePath,
132-
const PrerequisiteModules &RequisiteModules) {
133-
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
134-
CompilerInstance::createDiagnostics(new DiagnosticOptions());
135-
131+
bool IsModuleFileUpToDate(PathRef ModuleFilePath,
132+
const PrerequisiteModules &RequisiteModules,
133+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
136134
auto HSOpts = std::make_shared<HeaderSearchOptions>();
137135
RequisiteModules.adjustHeaderSearchOptions(*HSOpts);
138136
HSOpts->ForceCheckCXX20ModulesInputFiles = true;
139137
HSOpts->ValidateASTInputFilesContent = true;
140138

139+
clang::clangd::IgnoreDiagnostics IgnoreDiags;
140+
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
141+
CompilerInstance::createDiagnostics(new DiagnosticOptions, &IgnoreDiags,
142+
/*ShouldOwnClient=*/false);
143+
144+
LangOptions LangOpts;
145+
LangOpts.SkipODRCheckInGMF = true;
146+
147+
FileManager FileMgr(FileSystemOptions(), VFS);
148+
149+
SourceManager SourceMgr(*Diags, FileMgr);
150+
151+
HeaderSearch HeaderInfo(HSOpts, SourceMgr, *Diags, LangOpts,
152+
/*Target=*/nullptr);
153+
154+
TrivialModuleLoader ModuleLoader;
155+
Preprocessor PP(std::make_shared<PreprocessorOptions>(), *Diags, LangOpts,
156+
SourceMgr, HeaderInfo, ModuleLoader);
157+
158+
IntrusiveRefCntPtr<InMemoryModuleCache> ModuleCache = new InMemoryModuleCache;
141159
PCHContainerOperations PCHOperations;
142-
std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
143-
ModuleFilePath.str(), PCHOperations.getRawReader(), ASTUnit::LoadASTOnly,
144-
Diags, FileSystemOptions(), std::move(HSOpts));
160+
ASTReader Reader(PP, *ModuleCache, /*ASTContext=*/nullptr,
161+
PCHOperations.getRawReader(), {});
145162

146-
if (!Unit)
147-
return false;
163+
// We don't need any listener here. By default it will use a validator
164+
// listener.
165+
Reader.setListener(nullptr);
148166

149-
auto Reader = Unit->getASTReader();
150-
if (!Reader)
167+
if (Reader.ReadAST(ModuleFilePath, serialization::MK_MainFile,
168+
SourceLocation(),
169+
ASTReader::ARR_None) != ASTReader::Success)
151170
return false;
152171

153172
bool UpToDate = true;
154-
Reader->getModuleManager().visit([&](serialization::ModuleFile &MF) -> bool {
155-
Reader->visitInputFiles(
173+
Reader.getModuleManager().visit([&](serialization::ModuleFile &MF) -> bool {
174+
Reader.visitInputFiles(
156175
MF, /*IncludeSystem=*/false, /*Complain=*/false,
157176
[&](const serialization::InputFile &IF, bool isSystem) {
158177
if (!IF.getFile() || IF.isOutOfDate())
159178
UpToDate = false;
160179
});
161-
162180
return !UpToDate;
163181
});
164-
165182
return UpToDate;
166183
}
167184

168185
bool IsModuleFilesUpToDate(
169186
llvm::SmallVector<PathRef> ModuleFilePaths,
170-
const PrerequisiteModules &RequisiteModules) {
171-
return llvm::all_of(ModuleFilePaths, [&RequisiteModules](auto ModuleFilePath) {
172-
return IsModuleFileUpToDate(ModuleFilePath, RequisiteModules);
173-
});
187+
const PrerequisiteModules &RequisiteModules,
188+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
189+
return llvm::all_of(
190+
ModuleFilePaths, [&RequisiteModules, VFS](auto ModuleFilePath) {
191+
return IsModuleFileUpToDate(ModuleFilePath, RequisiteModules, VFS);
192+
});
174193
}
175194

176195
// StandalonePrerequisiteModules - stands for PrerequisiteModules for which all
@@ -347,7 +366,7 @@ bool StandalonePrerequisiteModules::canReuse(
347366
SmallVector<StringRef> BMIPaths;
348367
for (auto &MF : RequiredModules)
349368
BMIPaths.push_back(MF.ModuleFilePath);
350-
return IsModuleFilesUpToDate(BMIPaths, *this);
369+
return IsModuleFilesUpToDate(BMIPaths, *this, VFS);
351370
}
352371

353372
} // namespace clangd

0 commit comments

Comments
 (0)