Skip to content

Commit

Permalink
[clangd][nfc] Show more information in logs when compiler instance pr…
Browse files Browse the repository at this point in the history
…epare fails

Without this patch clangd silently process compiler instance prepare failure and only LSP errors "Invalid AST" could be found in logs.
E.g. the reason of the problem clangd/clangd#734 is impossible to understand without verbose logs or with disabled background index.
This patch adds more information into logs to help understand the reason of such failures.

Logs without this patch:
```
E[...] Could not build a preamble for file test.cpp version 1
```

Logs with this patch:
```
E[...] Could not build a preamble for file test.cpp version 1: CreateTargetInfo() return null
..
E[...] Failed to prepare a compiler instance: unknown target ABI 'lp64'
```

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D104056
  • Loading branch information
ArcsinX committed Jun 30, 2021
1 parent fae0569 commit a62579f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion clang-tools-extra/clangd/ParsedAST.cpp
Expand Up @@ -289,8 +289,15 @@ ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
std::move(CI), PreamblePCH,
llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, Filename), VFS,
ASTDiags);
if (!Clang)
if (!Clang) {
// The last diagnostic contains information about the reason of this
// failure.
std::vector<Diag> Diags(ASTDiags.take());
elog("Failed to prepare a compiler instance: {0}",
!Diags.empty() ? static_cast<DiagBase &>(Diags.back()).Message
: "unknown error");
return None;
}

auto Action = std::make_unique<ClangdFrontendAction>();
const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0];
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clangd/Preamble.cpp
Expand Up @@ -390,8 +390,8 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
SerializedDeclsCollector.takeMacros(), std::move(StatCache),
SerializedDeclsCollector.takeCanonicalIncludes());
} else {
elog("Could not build a preamble for file {0} version {1}", FileName,
Inputs.Version);
elog("Could not build a preamble for file {0} version {1}: {2}", FileName,
Inputs.Version, BuiltPreamble.getError().message());
return nullptr;
}
}
Expand Down

0 comments on commit a62579f

Please sign in to comment.