1 //===--- Preamble.cpp - Reusing expensive parts of the AST ----------------===//
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 #include "Preamble.h"
10 #include "Compiler.h"
11 #include "Headers.h"
12 #include "support/Logger.h"
13 #include "support/Trace.h"
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Basic/LangOptions.h"
16 #include "clang/Basic/SourceLocation.h"
17 #include "clang/Basic/TokenKinds.h"
18 #include "clang/Frontend/CompilerInvocation.h"
19 #include "clang/Frontend/FrontendActions.h"
20 #include "clang/Lex/Lexer.h"
21 #include "clang/Lex/PPCallbacks.h"
22 #include "clang/Lex/Preprocessor.h"
23 #include "clang/Lex/PreprocessorOptions.h"
24 #include "clang/Tooling/CompilationDatabase.h"
25 #include "llvm/ADT/ArrayRef.h"
26 #include "llvm/ADT/IntrusiveRefCntPtr.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/ADT/StringSet.h"
31 #include "llvm/Support/Error.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/FormatVariadic.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/Path.h"
36 #include "llvm/Support/VirtualFileSystem.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include <iterator>
39 #include <memory>
40 #include <string>
41 #include <system_error>
42 #include <utility>
43 #include <vector>
44 
45 namespace clang {
46 namespace clangd {
47 namespace {
48 
49 bool compileCommandsAreEqual(const tooling::CompileCommand &LHS,
50                              const tooling::CompileCommand &RHS) {
51   // We don't check for Output, it should not matter to clangd.
52   return LHS.Directory == RHS.Directory && LHS.Filename == RHS.Filename &&
53          llvm::makeArrayRef(LHS.CommandLine).equals(RHS.CommandLine);
54 }
55 
56 class CppFilePreambleCallbacks : public PreambleCallbacks {
57 public:
58   CppFilePreambleCallbacks(PathRef File, PreambleParsedCallback ParsedCallback)
59       : File(File), ParsedCallback(ParsedCallback) {}
60 
61   IncludeStructure takeIncludes() { return std::move(Includes); }
62 
63   MainFileMacros takeMacros() { return std::move(Macros); }
64 
65   CanonicalIncludes takeCanonicalIncludes() { return std::move(CanonIncludes); }
66 
67   void AfterExecute(CompilerInstance &CI) override {
68     if (!ParsedCallback)
69       return;
70     trace::Span Tracer("Running PreambleCallback");
71     ParsedCallback(CI.getASTContext(), CI.getPreprocessorPtr(), CanonIncludes);
72   }
73 
74   void BeforeExecute(CompilerInstance &CI) override {
75     CanonIncludes.addSystemHeadersMapping(CI.getLangOpts());
76     LangOpts = &CI.getLangOpts();
77     SourceMgr = &CI.getSourceManager();
78   }
79 
80   std::unique_ptr<PPCallbacks> createPPCallbacks() override {
81     assert(SourceMgr && LangOpts &&
82            "SourceMgr and LangOpts must be set at this point");
83 
84     return std::make_unique<PPChainedCallbacks>(
85         collectIncludeStructureCallback(*SourceMgr, &Includes),
86         std::make_unique<CollectMainFileMacros>(*SourceMgr, Macros));
87   }
88 
89   CommentHandler *getCommentHandler() override {
90     IWYUHandler = collectIWYUHeaderMaps(&CanonIncludes);
91     return IWYUHandler.get();
92   }
93 
94 private:
95   PathRef File;
96   PreambleParsedCallback ParsedCallback;
97   IncludeStructure Includes;
98   CanonicalIncludes CanonIncludes;
99   MainFileMacros Macros;
100   std::unique_ptr<CommentHandler> IWYUHandler = nullptr;
101   const clang::LangOptions *LangOpts = nullptr;
102   const SourceManager *SourceMgr = nullptr;
103 };
104 
105 // Runs preprocessor over preamble section.
106 class PreambleOnlyAction : public PreprocessorFrontendAction {
107 protected:
108   void ExecuteAction() override {
109     Preprocessor &PP = getCompilerInstance().getPreprocessor();
110     auto &SM = PP.getSourceManager();
111     PP.EnterMainSourceFile();
112     auto Bounds = ComputePreambleBounds(getCompilerInstance().getLangOpts(),
113                                         SM.getBuffer(SM.getMainFileID()), 0);
114     Token Tok;
115     do {
116       PP.Lex(Tok);
117       assert(SM.isInMainFile(Tok.getLocation()));
118     } while (Tok.isNot(tok::eof) &&
119              SM.getDecomposedLoc(Tok.getLocation()).second < Bounds.Size);
120   }
121 };
122 
123 /// Gets the includes in the preamble section of the file by running
124 /// preprocessor over \p Contents. Returned includes do not contain resolved
125 /// paths. \p VFS and \p Cmd is used to build the compiler invocation, which
126 /// might stat/read files.
127 llvm::Expected<std::vector<Inclusion>>
128 scanPreambleIncludes(llvm::StringRef Contents,
129                      llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
130                      const tooling::CompileCommand &Cmd) {
131   // Build and run Preprocessor over the preamble.
132   ParseInputs PI;
133   PI.Contents = Contents.str();
134   PI.FS = std::move(VFS);
135   PI.CompileCommand = Cmd;
136   IgnoringDiagConsumer IgnoreDiags;
137   auto CI = buildCompilerInvocation(PI, IgnoreDiags);
138   if (!CI)
139     return llvm::createStringError(llvm::inconvertibleErrorCode(),
140                                    "failed to create compiler invocation");
141   CI->getDiagnosticOpts().IgnoreWarnings = true;
142   auto ContentsBuffer = llvm::MemoryBuffer::getMemBuffer(Contents);
143   auto Clang = prepareCompilerInstance(
144       std::move(CI), nullptr, std::move(ContentsBuffer),
145       // Provide an empty FS to prevent preprocessor from performing IO. This
146       // also implies missing resolved paths for includes.
147       new llvm::vfs::InMemoryFileSystem, IgnoreDiags);
148   if (Clang->getFrontendOpts().Inputs.empty())
149     return llvm::createStringError(llvm::inconvertibleErrorCode(),
150                                    "compiler instance had no inputs");
151   // We are only interested in main file includes.
152   Clang->getPreprocessorOpts().SingleFileParseMode = true;
153   PreambleOnlyAction Action;
154   if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0]))
155     return llvm::createStringError(llvm::inconvertibleErrorCode(),
156                                    "failed BeginSourceFile");
157   Preprocessor &PP = Clang->getPreprocessor();
158   IncludeStructure Includes;
159   PP.addPPCallbacks(
160       collectIncludeStructureCallback(Clang->getSourceManager(), &Includes));
161   if (llvm::Error Err = Action.Execute())
162     return std::move(Err);
163   Action.EndSourceFile();
164   return Includes.MainFileIncludes;
165 }
166 
167 const char *spellingForIncDirective(tok::PPKeywordKind IncludeDirective) {
168   switch (IncludeDirective) {
169   case tok::pp_include:
170     return "include";
171   case tok::pp_import:
172     return "import";
173   case tok::pp_include_next:
174     return "include_next";
175   default:
176     break;
177   }
178   llvm_unreachable("not an include directive");
179 }
180 } // namespace
181 
182 PreambleData::PreambleData(const ParseInputs &Inputs,
183                            PrecompiledPreamble Preamble,
184                            std::vector<Diag> Diags, IncludeStructure Includes,
185                            MainFileMacros Macros,
186                            std::unique_ptr<PreambleFileStatusCache> StatCache,
187                            CanonicalIncludes CanonIncludes)
188     : Version(Inputs.Version), CompileCommand(Inputs.CompileCommand),
189       Preamble(std::move(Preamble)), Diags(std::move(Diags)),
190       Includes(std::move(Includes)), Macros(std::move(Macros)),
191       StatCache(std::move(StatCache)), CanonIncludes(std::move(CanonIncludes)) {
192 }
193 
194 std::shared_ptr<const PreambleData>
195 buildPreamble(PathRef FileName, CompilerInvocation CI,
196               const ParseInputs &Inputs, bool StoreInMemory,
197               PreambleParsedCallback PreambleCallback) {
198   // Note that we don't need to copy the input contents, preamble can live
199   // without those.
200   auto ContentsBuffer =
201       llvm::MemoryBuffer::getMemBuffer(Inputs.Contents, FileName);
202   auto Bounds =
203       ComputePreambleBounds(*CI.getLangOpts(), ContentsBuffer.get(), 0);
204 
205   trace::Span Tracer("BuildPreamble");
206   SPAN_ATTACH(Tracer, "File", FileName);
207   StoreDiags PreambleDiagnostics;
208   llvm::IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine =
209       CompilerInstance::createDiagnostics(&CI.getDiagnosticOpts(),
210                                           &PreambleDiagnostics, false);
211 
212   // Skip function bodies when building the preamble to speed up building
213   // the preamble and make it smaller.
214   assert(!CI.getFrontendOpts().SkipFunctionBodies);
215   CI.getFrontendOpts().SkipFunctionBodies = true;
216   // We don't want to write comment locations into PCH. They are racy and slow
217   // to read back. We rely on dynamic index for the comments instead.
218   CI.getPreprocessorOpts().WriteCommentListToPCH = false;
219 
220   // Recovery expression currently only works for C++.
221   if (CI.getLangOpts()->CPlusPlus)
222     CI.getLangOpts()->RecoveryAST = Inputs.Opts.BuildRecoveryAST;
223 
224   CppFilePreambleCallbacks SerializedDeclsCollector(FileName, PreambleCallback);
225   if (Inputs.FS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) {
226     log("Couldn't set working directory when building the preamble.");
227     // We proceed anyway, our lit-tests rely on results for non-existing working
228     // dirs.
229   }
230 
231   llvm::SmallString<32> AbsFileName(FileName);
232   Inputs.FS->makeAbsolute(AbsFileName);
233   auto StatCache = std::make_unique<PreambleFileStatusCache>(AbsFileName);
234   auto BuiltPreamble = PrecompiledPreamble::Build(
235       CI, ContentsBuffer.get(), Bounds, *PreambleDiagsEngine,
236       StatCache->getProducingFS(Inputs.FS),
237       std::make_shared<PCHContainerOperations>(), StoreInMemory,
238       SerializedDeclsCollector);
239 
240   // When building the AST for the main file, we do want the function
241   // bodies.
242   CI.getFrontendOpts().SkipFunctionBodies = false;
243 
244   if (BuiltPreamble) {
245     vlog("Built preamble of size {0} for file {1} version {2}",
246          BuiltPreamble->getSize(), FileName, Inputs.Version);
247     std::vector<Diag> Diags = PreambleDiagnostics.take();
248     return std::make_shared<PreambleData>(
249         Inputs, std::move(*BuiltPreamble), std::move(Diags),
250         SerializedDeclsCollector.takeIncludes(),
251         SerializedDeclsCollector.takeMacros(), std::move(StatCache),
252         SerializedDeclsCollector.takeCanonicalIncludes());
253   } else {
254     elog("Could not build a preamble for file {0} version {1}", FileName,
255          Inputs.Version);
256     return nullptr;
257   }
258 }
259 
260 bool isPreambleCompatible(const PreambleData &Preamble,
261                           const ParseInputs &Inputs, PathRef FileName,
262                           const CompilerInvocation &CI) {
263   auto ContentsBuffer =
264       llvm::MemoryBuffer::getMemBuffer(Inputs.Contents, FileName);
265   auto Bounds =
266       ComputePreambleBounds(*CI.getLangOpts(), ContentsBuffer.get(), 0);
267   return compileCommandsAreEqual(Inputs.CompileCommand,
268                                  Preamble.CompileCommand) &&
269          Preamble.Preamble.CanReuse(CI, ContentsBuffer.get(), Bounds,
270                                     Inputs.FS.get());
271 }
272 
273 PreamblePatch PreamblePatch::create(llvm::StringRef FileName,
274                                     const ParseInputs &Modified,
275                                     const PreambleData &Baseline) {
276   // First scan the include directives in Baseline and Modified. These will be
277   // used to figure out newly added directives in Modified. Scanning can fail,
278   // the code just bails out and creates an empty patch in such cases, as:
279   // - If scanning for Baseline fails, no knowledge of existing includes hence
280   //   patch will contain all the includes in Modified. Leading to rebuild of
281   //   whole preamble, which is terribly slow.
282   // - If scanning for Modified fails, cannot figure out newly added ones so
283   //   there's nothing to do but generate an empty patch.
284   auto BaselineIncludes = scanPreambleIncludes(
285       // Contents needs to be null-terminated.
286       Baseline.Preamble.getContents().str(),
287       Baseline.StatCache->getConsumingFS(Modified.FS), Modified.CompileCommand);
288   if (!BaselineIncludes) {
289     elog("Failed to scan includes for baseline of {0}: {1}", FileName,
290          BaselineIncludes.takeError());
291     return {};
292   }
293   auto ModifiedIncludes = scanPreambleIncludes(
294       Modified.Contents, Baseline.StatCache->getConsumingFS(Modified.FS),
295       Modified.CompileCommand);
296   if (!ModifiedIncludes) {
297     elog("Failed to scan includes for modified contents of {0}: {1}", FileName,
298          ModifiedIncludes.takeError());
299     return {};
300   }
301 
302   PreamblePatch PP;
303   // This shouldn't coincide with any real file name.
304   llvm::SmallString<128> PatchName;
305   llvm::sys::path::append(PatchName, llvm::sys::path::parent_path(FileName),
306                           "__preamble_patch__.h");
307   PP.PatchFileName = PatchName.str().str();
308 
309   // We are only interested in newly added includes, record the ones in Baseline
310   // for exclusion.
311   llvm::DenseSet<std::pair<tok::PPKeywordKind, llvm::StringRef>>
312       ExistingIncludes;
313   for (const auto &Inc : *BaselineIncludes)
314     ExistingIncludes.insert({Inc.Directive, Inc.Written});
315   // Calculate extra includes that needs to be inserted.
316   llvm::raw_string_ostream Patch(PP.PatchContents);
317   for (const auto &Inc : *ModifiedIncludes) {
318     if (ExistingIncludes.count({Inc.Directive, Inc.Written}))
319       continue;
320     Patch << llvm::formatv("#{0} {1}\n", spellingForIncDirective(Inc.Directive),
321                            Inc.Written);
322   }
323   Patch.flush();
324 
325   // FIXME: Handle more directives, e.g. define/undef.
326   return PP;
327 }
328 
329 void PreamblePatch::apply(CompilerInvocation &CI) const {
330   // No need to map an empty file.
331   if (PatchContents.empty())
332     return;
333   auto &PPOpts = CI.getPreprocessorOpts();
334   auto PatchBuffer =
335       // we copy here to ensure contents are still valid if CI outlives the
336       // PreamblePatch.
337       llvm::MemoryBuffer::getMemBufferCopy(PatchContents, PatchFileName);
338   // CI will take care of the lifetime of the buffer.
339   PPOpts.addRemappedFile(PatchFileName, PatchBuffer.release());
340   // The patch will be parsed after loading the preamble ast and before parsing
341   // the main file.
342   PPOpts.Includes.push_back(PatchFileName);
343 }
344 
345 } // namespace clangd
346 } // namespace clang
347