[clang-tools-extra] [clangd] [Modules] Add VFS to ASTUnit::LoadFromASTFile (PR #113879)

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 28 01:55:26 PDT 2024


https://github.com/ChuanqiXu9 updated https://github.com/llvm/llvm-project/pull/113879

>From 3df5e9275a63ee9c51c4e9e9a77383a93be020a4 Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <yedeng.yd at linux.alibaba.com>
Date: Mon, 28 Oct 2024 15:54:37 +0800
Subject: [PATCH 1/2] [clangd] [Modules] Add VFS to ASTUnit::LoadFromASTFile

---
 clang-tools-extra/clangd/ModulesBuilder.cpp | 26 +++++++++++++--------
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/clang-tools-extra/clangd/ModulesBuilder.cpp b/clang-tools-extra/clangd/ModulesBuilder.cpp
index 1eeff468ef1236..25f9ab6631fca7 100644
--- a/clang-tools-extra/clangd/ModulesBuilder.cpp
+++ b/clang-tools-extra/clangd/ModulesBuilder.cpp
@@ -127,10 +127,10 @@ struct ModuleFile {
   std::string ModuleFilePath;
 };
 
-bool IsModuleFileUpToDate(
-    PathRef ModuleFilePath,
-    const PrerequisiteModules &RequisiteModules) {
-IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
+bool IsModuleFileUpToDate(PathRef ModuleFilePath,
+                          const PrerequisiteModules &RequisiteModules,
+                          llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
+  IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
       CompilerInstance::createDiagnostics(new DiagnosticOptions());
 
   auto HSOpts = std::make_shared<HeaderSearchOptions>();
@@ -141,7 +141,11 @@ IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
   PCHContainerOperations PCHOperations;
   std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
       ModuleFilePath.str(), PCHOperations.getRawReader(), ASTUnit::LoadASTOnly,
-      Diags, FileSystemOptions(), std::move(HSOpts));
+      Diags, FileSystemOptions(), std::move(HSOpts),
+      /*LangOpts=*/nullptr, /*OnlyLocalDecls=*/false,
+      /*CaptureDiagnostics=*/CaptureDiagsKind::None,
+      /*AllowASTWithCompilerErrors=*/false, /*UserFilesAreVolatile=*/false,
+      VFS);
 
   if (!Unit)
     return false;
@@ -167,10 +171,12 @@ IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
 
 bool IsModuleFilesUpToDate(
     llvm::SmallVector<PathRef> ModuleFilePaths,
-    const PrerequisiteModules &RequisiteModules) {
-  return llvm::all_of(ModuleFilePaths, [&RequisiteModules](auto ModuleFilePath) {
-    return IsModuleFileUpToDate(ModuleFilePath, RequisiteModules);
-  });
+    const PrerequisiteModules &RequisiteModules,
+    llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
+  return llvm::all_of(
+      ModuleFilePaths, [&RequisiteModules, VFS](auto ModuleFilePath) {
+        return IsModuleFileUpToDate(ModuleFilePath, RequisiteModules, VFS);
+      });
 }
 
 // StandalonePrerequisiteModules - stands for PrerequisiteModules for which all
@@ -347,7 +353,7 @@ bool StandalonePrerequisiteModules::canReuse(
   SmallVector<StringRef> BMIPaths;
   for (auto &MF : RequiredModules)
     BMIPaths.push_back(MF.ModuleFilePath);
-  return IsModuleFilesUpToDate(BMIPaths, *this);
+  return IsModuleFilesUpToDate(BMIPaths, *this, VFS);
 }
 
 } // namespace clangd

>From b6abb7365c439eaf631b04ee31dd8426df009ec5 Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <yedeng.yd at linux.alibaba.com>
Date: Mon, 28 Oct 2024 16:55:02 +0800
Subject: [PATCH 2/2] Use Compiler instance

---
 clang-tools-extra/clangd/ModulesBuilder.cpp | 52 ++++++++++++---------
 1 file changed, 29 insertions(+), 23 deletions(-)

diff --git a/clang-tools-extra/clangd/ModulesBuilder.cpp b/clang-tools-extra/clangd/ModulesBuilder.cpp
index 25f9ab6631fca7..83df35acbc8ca3 100644
--- a/clang-tools-extra/clangd/ModulesBuilder.cpp
+++ b/clang-tools-extra/clangd/ModulesBuilder.cpp
@@ -127,33 +127,39 @@ struct ModuleFile {
   std::string ModuleFilePath;
 };
 
-bool IsModuleFileUpToDate(PathRef ModuleFilePath,
-                          const PrerequisiteModules &RequisiteModules,
-                          llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
+bool IsModuleFileUpToDate(
+    PathRef ModuleFilePath,
+    const PrerequisiteModules &RequisiteModules,
+    const CompilerInvocation &CI,
+    llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
+  CompilerInstance Clang;
+  Clang.setInvocation(std::make_shared<CompilerInvocation>(CI));
+
   IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
       CompilerInstance::createDiagnostics(new DiagnosticOptions());
+  Clang.setDiagnostics(Diags.get());
+
+  HeaderSearchOptions &HSOpts = Clang.getHeaderSearchOpts();
+  RequisiteModules.adjustHeaderSearchOptions(HSOpts);
+  HSOpts.ForceCheckCXX20ModulesInputFiles = true;
+  HSOpts.ValidateASTInputFilesContent = true;
 
-  auto HSOpts = std::make_shared<HeaderSearchOptions>();
-  RequisiteModules.adjustHeaderSearchOptions(*HSOpts);
-  HSOpts->ForceCheckCXX20ModulesInputFiles = true;
-  HSOpts->ValidateASTInputFilesContent = true;
-
-  PCHContainerOperations PCHOperations;
-  std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
-      ModuleFilePath.str(), PCHOperations.getRawReader(), ASTUnit::LoadASTOnly,
-      Diags, FileSystemOptions(), std::move(HSOpts),
-      /*LangOpts=*/nullptr, /*OnlyLocalDecls=*/false,
-      /*CaptureDiagnostics=*/CaptureDiagsKind::None,
-      /*AllowASTWithCompilerErrors=*/false, /*UserFilesAreVolatile=*/false,
-      VFS);
-
-  if (!Unit)
+  Clang.getLangOpts().SkipODRCheckInGMF = true;
+  Clang.createPreprocessor(TU_Complete);
+
+  FileManager *FM = Clang.createFileManager(VFS);
+  Clang.createSourceManager(*FM);
+  if (!Clang.createTarget())
     return false;
 
-  auto Reader = Unit->getASTReader();
+  auto Reader = Clang.getASTReader();
   if (!Reader)
     return false;
 
+  if (Reader->ReadAST(ModuleFilePath, serialization::MK_MainFile,
+                      SourceLocation(), ASTReader::ARR_None) != ASTReader::Success)
+    return false;
+
   bool UpToDate = true;
   Reader->getModuleManager().visit([&](serialization::ModuleFile &MF) -> bool {
     Reader->visitInputFiles(
@@ -172,11 +178,11 @@ bool IsModuleFileUpToDate(PathRef ModuleFilePath,
 bool IsModuleFilesUpToDate(
     llvm::SmallVector<PathRef> ModuleFilePaths,
     const PrerequisiteModules &RequisiteModules,
+    const CompilerInvocation &CI,
     llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
-  return llvm::all_of(
-      ModuleFilePaths, [&RequisiteModules, VFS](auto ModuleFilePath) {
-        return IsModuleFileUpToDate(ModuleFilePath, RequisiteModules, VFS);
-      });
+  return llvm::all_of(ModuleFilePaths, [&RequisiteModules, &CI, VFS](auto ModuleFilePath) {
+    return IsModuleFileUpToDate(ModuleFilePath, RequisiteModules, CI, VFS);
+  });
 }
 
 // StandalonePrerequisiteModules - stands for PrerequisiteModules for which all



More information about the cfe-commits mailing list