1 //===--- Check.cpp - clangd self-diagnostics ------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Many basic problems can occur processing a file in clangd, e.g.:
10 //  - system includes are not found
11 //  - crash when indexing its AST
12 // clangd --check provides a simplified, isolated way to reproduce these,
13 // with no editor, LSP, threads, background indexing etc to contend with.
14 //
15 // One important use case is gathering information for bug reports.
16 // Another is reproducing crashes, and checking which setting prevent them.
17 //
18 // It simulates opening a file (determining compile command, parsing, indexing)
19 // and then running features at many locations.
20 //
21 // Currently it adds some basic logging of progress and results.
22 // We should consider extending it to also recognize common symptoms and
23 // recommend solutions (e.g. standard library installation issues).
24 //
25 //===----------------------------------------------------------------------===//
26 
27 #include "ClangdLSPServer.h"
28 #include "CodeComplete.h"
29 #include "CompileCommands.h"
30 #include "Config.h"
31 #include "GlobalCompilationDatabase.h"
32 #include "Hover.h"
33 #include "ParsedAST.h"
34 #include "Preamble.h"
35 #include "SourceCode.h"
36 #include "XRefs.h"
37 #include "index/CanonicalIncludes.h"
38 #include "index/FileIndex.h"
39 #include "refactor/Tweak.h"
40 #include "support/ThreadsafeFS.h"
41 #include "support/Trace.h"
42 #include "clang/AST/ASTContext.h"
43 #include "clang/Format/Format.h"
44 #include "clang/Frontend/CompilerInvocation.h"
45 #include "clang/Tooling/CompilationDatabase.h"
46 #include "llvm/ADT/ArrayRef.h"
47 #include "llvm/ADT/Optional.h"
48 #include "llvm/Support/Path.h"
49 
50 namespace clang {
51 namespace clangd {
52 namespace {
53 
54 // Print (and count) the error-level diagnostics (warnings are ignored).
55 unsigned showErrors(llvm::ArrayRef<Diag> Diags) {
56   unsigned ErrCount = 0;
57   for (const auto &D : Diags) {
58     if (D.Severity >= DiagnosticsEngine::Error) {
59       elog("[{0}] Line {1}: {2}", D.Name, D.Range.start.line + 1, D.Message);
60       ++ErrCount;
61     }
62   }
63   return ErrCount;
64 }
65 
66 // This class is just a linear pipeline whose functions get called in sequence.
67 // Each exercises part of clangd's logic on our test file and logs results.
68 // Later steps depend on state built in earlier ones (such as the AST).
69 // Many steps can fatally fail (return false), then subsequent ones cannot run.
70 // Nonfatal failures are logged and tracked in ErrCount.
71 class Checker {
72   // from constructor
73   std::string File;
74   ClangdLSPServer::Options Opts;
75   // from buildCommand
76   tooling::CompileCommand Cmd;
77   // from buildInvocation
78   ParseInputs Inputs;
79   std::unique_ptr<CompilerInvocation> Invocation;
80   format::FormatStyle Style;
81   // from buildAST
82   std::shared_ptr<const PreambleData> Preamble;
83   llvm::Optional<ParsedAST> AST;
84   FileIndex Index;
85 
86 public:
87   // Number of non-fatal errors seen.
88   unsigned ErrCount = 0;
89 
90   Checker(llvm::StringRef File, const ClangdLSPServer::Options &Opts)
91       : File(File), Opts(Opts) {}
92 
93   // Read compilation database and choose a compile command for the file.
94   bool buildCommand(const ThreadsafeFS &TFS) {
95     log("Loading compilation database...");
96     DirectoryBasedGlobalCompilationDatabase::Options CDBOpts(TFS);
97     CDBOpts.CompileCommandsDir =
98         Config::current().CompileFlags.CDBSearch.FixedCDBPath;
99     std::unique_ptr<GlobalCompilationDatabase> BaseCDB =
100         std::make_unique<DirectoryBasedGlobalCompilationDatabase>(CDBOpts);
101     BaseCDB = getQueryDriverDatabase(llvm::makeArrayRef(Opts.QueryDriverGlobs),
102                                      std::move(BaseCDB));
103     auto Mangler = CommandMangler::detect();
104     if (Opts.ResourceDir)
105       Mangler.ResourceDir = *Opts.ResourceDir;
106     auto CDB = std::make_unique<OverlayCDB>(
107         BaseCDB.get(), std::vector<std::string>{},
108         tooling::ArgumentsAdjuster(std::move(Mangler)));
109 
110     if (auto TrueCmd = CDB->getCompileCommand(File)) {
111       Cmd = std::move(*TrueCmd);
112       log("Compile command from CDB is: {0}", printArgv(Cmd.CommandLine));
113     } else {
114       Cmd = CDB->getFallbackCommand(File);
115       log("Generic fallback command is: {0}", printArgv(Cmd.CommandLine));
116     }
117 
118     return true;
119   }
120 
121   // Prepare inputs and build CompilerInvocation (parsed compile command).
122   bool buildInvocation(const ThreadsafeFS &TFS,
123                        llvm::Optional<std::string> Contents) {
124     StoreDiags CaptureInvocationDiags;
125     std::vector<std::string> CC1Args;
126     Inputs.CompileCommand = Cmd;
127     Inputs.TFS = &TFS;
128     Inputs.ClangTidyProvider = Opts.ClangTidyProvider;
129     if (Contents.hasValue()) {
130       Inputs.Contents = *Contents;
131       log("Imaginary source file contents:\n{0}", Inputs.Contents);
132     } else {
133       if (auto Contents = TFS.view(llvm::None)->getBufferForFile(File)) {
134         Inputs.Contents = Contents->get()->getBuffer().str();
135       } else {
136         elog("Couldn't read {0}: {1}", File, Contents.getError().message());
137         return false;
138       }
139     }
140     log("Parsing command...");
141     Invocation =
142         buildCompilerInvocation(Inputs, CaptureInvocationDiags, &CC1Args);
143     auto InvocationDiags = CaptureInvocationDiags.take();
144     ErrCount += showErrors(InvocationDiags);
145     log("internal (cc1) args are: {0}", printArgv(CC1Args));
146     if (!Invocation) {
147       elog("Failed to parse command line");
148       return false;
149     }
150 
151     // FIXME: Check that resource-dir/built-in-headers exist?
152 
153     Style = getFormatStyleForFile(File, Inputs.Contents, TFS);
154 
155     return true;
156   }
157 
158   // Build preamble and AST, and index them.
159   bool buildAST() {
160     log("Building preamble...");
161     Preamble = buildPreamble(File, *Invocation, Inputs, /*StoreInMemory=*/true,
162                              [&](ASTContext &Ctx, Preprocessor &PP,
163                                  const CanonicalIncludes &Includes) {
164                                if (!Opts.BuildDynamicSymbolIndex)
165                                  return;
166                                log("Indexing headers...");
167                                Index.updatePreamble(File, /*Version=*/"null",
168                                                     Ctx, PP, Includes);
169                              });
170     if (!Preamble) {
171       elog("Failed to build preamble");
172       return false;
173     }
174     ErrCount += showErrors(Preamble->Diags);
175 
176     log("Building AST...");
177     AST = ParsedAST::build(File, Inputs, std::move(Invocation),
178                            /*InvocationDiags=*/std::vector<Diag>{}, Preamble);
179     if (!AST) {
180       elog("Failed to build AST");
181       return false;
182     }
183     ErrCount += showErrors(llvm::makeArrayRef(*AST->getDiagnostics())
184                                .drop_front(Preamble->Diags.size()));
185 
186     if (Opts.BuildDynamicSymbolIndex) {
187       log("Indexing AST...");
188       Index.updateMain(File, *AST);
189     }
190     return true;
191   }
192 
193   // Run AST-based features at each token in the file.
194   void testLocationFeatures(
195       llvm::function_ref<bool(const Position &)> ShouldCheckLine,
196       const bool EnableCodeCompletion) {
197     trace::Span Trace("testLocationFeatures");
198     log("Testing features at each token (may be slow in large files)");
199     auto &SM = AST->getSourceManager();
200     auto SpelledTokens = AST->getTokens().spelledTokens(SM.getMainFileID());
201 
202     CodeCompleteOptions CCOpts = Opts.CodeComplete;
203     CCOpts.Index = &Index;
204 
205     for (const auto &Tok : SpelledTokens) {
206       unsigned Start = AST->getSourceManager().getFileOffset(Tok.location());
207       unsigned End = Start + Tok.length();
208       Position Pos = offsetToPosition(Inputs.Contents, Start);
209 
210       if (!ShouldCheckLine(Pos))
211         continue;
212 
213       trace::Span Trace("Token");
214       SPAN_ATTACH(Trace, "pos", Pos);
215       SPAN_ATTACH(Trace, "text", Tok.text(AST->getSourceManager()));
216 
217       // FIXME: dumping the tokens may leak sensitive code into bug reports.
218       // Add an option to turn this off, once we decide how options work.
219       vlog("  {0} {1}", Pos, Tok.text(AST->getSourceManager()));
220       auto Tree = SelectionTree::createRight(AST->getASTContext(),
221                                              AST->getTokens(), Start, End);
222       Tweak::Selection Selection(&Index, *AST, Start, End, std::move(Tree),
223                                  nullptr);
224       // FS is only populated when applying a tweak, not during prepare as
225       // prepare should not do any I/O to be fast.
226       auto Tweaks =
227           prepareTweaks(Selection, Opts.TweakFilter, Opts.FeatureModules);
228       Selection.FS =
229           &AST->getSourceManager().getFileManager().getVirtualFileSystem();
230       for (const auto &T : Tweaks) {
231         auto Result = T->apply(Selection);
232         if (!Result) {
233           elog("    tweak: {0} ==> FAIL: {1}", T->id(), Result.takeError());
234           ++ErrCount;
235         } else {
236           vlog("    tweak: {0}", T->id());
237         }
238       }
239       unsigned Definitions = locateSymbolAt(*AST, Pos, &Index).size();
240       vlog("    definition: {0}", Definitions);
241 
242       auto Hover = getHover(*AST, Pos, Style, &Index);
243       vlog("    hover: {0}", Hover.hasValue());
244 
245       if (EnableCodeCompletion) {
246         Position EndPos = offsetToPosition(Inputs.Contents, End);
247         auto CC = codeComplete(File, EndPos, Preamble.get(), Inputs, CCOpts);
248         vlog("    code completion: {0}",
249              CC.Completions.empty() ? "<empty>" : CC.Completions[0].Name);
250       }
251     }
252   }
253 };
254 
255 } // namespace
256 
257 bool check(llvm::StringRef File,
258            llvm::function_ref<bool(const Position &)> ShouldCheckLine,
259            const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts,
260            bool EnableCodeCompletion) {
261   llvm::SmallString<0> FakeFile;
262   llvm::Optional<std::string> Contents;
263   if (File.empty()) {
264     llvm::sys::path::system_temp_directory(false, FakeFile);
265     llvm::sys::path::append(FakeFile, "test.cc");
266     File = FakeFile;
267     Contents = R"cpp(
268       #include <stddef.h>
269       #include <string>
270 
271       size_t N = 50;
272       auto xxx = std::string(N, 'x');
273     )cpp";
274   }
275   log("Testing on source file {0}", File);
276 
277   auto ContextProvider = ClangdServer::createConfiguredContextProvider(
278       Opts.ConfigProvider, nullptr);
279   WithContext Ctx(ContextProvider(
280       FakeFile.empty()
281           ? File
282           : /*Don't turn on local configs for an arbitrary temp path.*/ ""));
283   Checker C(File, Opts);
284   if (!C.buildCommand(TFS) || !C.buildInvocation(TFS, Contents) ||
285       !C.buildAST())
286     return false;
287   C.testLocationFeatures(ShouldCheckLine, EnableCodeCompletion);
288 
289   log("All checks completed, {0} errors", C.ErrCount);
290   return C.ErrCount == 0;
291 }
292 
293 } // namespace clangd
294 } // namespace clang
295