[PATCH] D104056: [clangd][nfc] Show more information in logs when compiler instance prepare fails

Aleksandr Platonov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 10 13:44:35 PDT 2021


ArcsinX created this revision.
ArcsinX added reviewers: sammccall, kadircet.
Herald added subscribers: usaxena95, arphaman.
ArcsinX requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

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 https://github.com/clangd/clangd/issues/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 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'


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D104056

Files:
  clang-tools-extra/clangd/ParsedAST.cpp
  clang-tools-extra/clangd/Preamble.cpp


Index: clang-tools-extra/clangd/Preamble.cpp
===================================================================
--- clang-tools-extra/clangd/Preamble.cpp
+++ clang-tools-extra/clangd/Preamble.cpp
@@ -390,8 +390,8 @@
         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;
   }
 }
Index: clang-tools-extra/clangd/ParsedAST.cpp
===================================================================
--- clang-tools-extra/clangd/ParsedAST.cpp
+++ clang-tools-extra/clangd/ParsedAST.cpp
@@ -289,8 +289,15 @@
       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.
+    elog("Failed to prepare a compiler instance: {0}",
+         ASTDiags.getNumErrors()
+             ? static_cast<DiagBase &>(ASTDiags.take().back()).Message
+             : "unknown error");
     return None;
+  }
 
   auto Action = std::make_unique<ClangdFrontendAction>();
   const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0];


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104056.351251.patch
Type: text/x-patch
Size: 1463 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210610/ea8b02b9/attachment.bin>


More information about the cfe-commits mailing list