Author: Chuanqi Xu
Date: 2025-07-11T11:18:21+08:00
New Revision: aa9902691db5cbb9db77270902f615f3c0877deb

URL: 
https://github.com/llvm/llvm-project/commit/aa9902691db5cbb9db77270902f615f3c0877deb
DIFF: 
https://github.com/llvm/llvm-project/commit/aa9902691db5cbb9db77270902f615f3c0877deb.diff

LOG: [NFC] [clangd] [Modules] Logging more if compilation failed

Coming from internal using experience.

The original log is just confusing... Users can't understand it unless
he reads the code.

This patch tries to make this more understandable.

Note that the mentioned module files in the logs might be removed
intentionally. We need another option to allow users to remain these
module files. Let's done this in another patch.

Added: 
    

Modified: 
    clang-tools-extra/clangd/ModulesBuilder.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/ModulesBuilder.cpp 
b/clang-tools-extra/clangd/ModulesBuilder.cpp
index d88aa01aad05d..6658111d6c7b4 100644
--- a/clang-tools-extra/clangd/ModulesBuilder.cpp
+++ b/clang-tools-extra/clangd/ModulesBuilder.cpp
@@ -160,6 +160,16 @@ class ReusablePrerequisiteModules : public 
PrerequisiteModules {
           RequiredModule->getModuleFilePath().str());
   }
 
+  std::string getAsString() const {
+    std::string Result;
+    llvm::raw_string_ostream OS(Result);
+    for (const auto &ModuleFile : RequiredModules) {
+      OS << "-fmodule-file=" << ModuleFile->getModuleName() << "="
+         << ModuleFile->getModuleFilePath() << " ";
+    }
+    return Result;
+  }
+
   bool canReuse(const CompilerInvocation &CI,
                 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) const 
override;
 
@@ -296,8 +306,27 @@ buildModuleFile(llvm::StringRef ModuleName, PathRef 
ModuleUnitFileName,
   GenerateReducedModuleInterfaceAction Action;
   Clang->ExecuteAction(Action);
 
-  if (Clang->getDiagnostics().hasErrorOccurred())
-    return llvm::createStringError("Compilation failed");
+  if (Clang->getDiagnostics().hasErrorOccurred()) {
+    std::string Cmds;
+    for (const auto &Arg : Inputs.CompileCommand.CommandLine) {
+      if (!Cmds.empty())
+        Cmds += " ";
+      Cmds += Arg;
+    }
+
+    clangd::vlog("Failed to compile {0} with command: {1}.", 
ModuleUnitFileName,
+                 Cmds);
+
+    std::string BuiltModuleFilesStr = BuiltModuleFiles.getAsString();
+    if (!BuiltModuleFilesStr.empty())
+      clangd::vlog("The actual used module files built by clangd is {0}",
+                   BuiltModuleFilesStr);
+
+    return llvm::createStringError(
+        llvm::formatv("Failed to compile {0}. Use '--log=verbose' to view "
+                      "detailed failure reasons.",
+                      ModuleUnitFileName));
+  }
 
   return ModuleFile{ModuleName, Inputs.CompileCommand.Output};
 }


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to