Skip to content

Commit aa99026

Browse files
committed
[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.
1 parent 34b55e1 commit aa99026

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

clang-tools-extra/clangd/ModulesBuilder.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ class ReusablePrerequisiteModules : public PrerequisiteModules {
160160
RequiredModule->getModuleFilePath().str());
161161
}
162162

163+
std::string getAsString() const {
164+
std::string Result;
165+
llvm::raw_string_ostream OS(Result);
166+
for (const auto &ModuleFile : RequiredModules) {
167+
OS << "-fmodule-file=" << ModuleFile->getModuleName() << "="
168+
<< ModuleFile->getModuleFilePath() << " ";
169+
}
170+
return Result;
171+
}
172+
163173
bool canReuse(const CompilerInvocation &CI,
164174
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) const override;
165175

@@ -296,8 +306,27 @@ buildModuleFile(llvm::StringRef ModuleName, PathRef ModuleUnitFileName,
296306
GenerateReducedModuleInterfaceAction Action;
297307
Clang->ExecuteAction(Action);
298308

299-
if (Clang->getDiagnostics().hasErrorOccurred())
300-
return llvm::createStringError("Compilation failed");
309+
if (Clang->getDiagnostics().hasErrorOccurred()) {
310+
std::string Cmds;
311+
for (const auto &Arg : Inputs.CompileCommand.CommandLine) {
312+
if (!Cmds.empty())
313+
Cmds += " ";
314+
Cmds += Arg;
315+
}
316+
317+
clangd::vlog("Failed to compile {0} with command: {1}.", ModuleUnitFileName,
318+
Cmds);
319+
320+
std::string BuiltModuleFilesStr = BuiltModuleFiles.getAsString();
321+
if (!BuiltModuleFilesStr.empty())
322+
clangd::vlog("The actual used module files built by clangd is {0}",
323+
BuiltModuleFilesStr);
324+
325+
return llvm::createStringError(
326+
llvm::formatv("Failed to compile {0}. Use '--log=verbose' to view "
327+
"detailed failure reasons.",
328+
ModuleUnitFileName));
329+
}
301330

302331
return ModuleFile{ModuleName, Inputs.CompileCommand.Output};
303332
}

0 commit comments

Comments
 (0)