150
|
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"
|
173
|
10 #include "Compiler.h"
|
|
11 #include "Headers.h"
|
|
12 #include "SourceCode.h"
|
|
13 #include "support/Logger.h"
|
221
|
14 #include "support/ThreadsafeFS.h"
|
173
|
15 #include "support/Trace.h"
|
221
|
16 #include "clang/AST/DeclTemplate.h"
|
173
|
17 #include "clang/Basic/Diagnostic.h"
|
221
|
18 #include "clang/Basic/DiagnosticLex.h"
|
173
|
19 #include "clang/Basic/LangOptions.h"
|
150
|
20 #include "clang/Basic/SourceLocation.h"
|
221
|
21 #include "clang/Basic/SourceManager.h"
|
173
|
22 #include "clang/Basic/TokenKinds.h"
|
|
23 #include "clang/Frontend/CompilerInvocation.h"
|
|
24 #include "clang/Frontend/FrontendActions.h"
|
|
25 #include "clang/Lex/Lexer.h"
|
150
|
26 #include "clang/Lex/PPCallbacks.h"
|
173
|
27 #include "clang/Lex/Preprocessor.h"
|
150
|
28 #include "clang/Lex/PreprocessorOptions.h"
|
173
|
29 #include "clang/Tooling/CompilationDatabase.h"
|
|
30 #include "llvm/ADT/ArrayRef.h"
|
221
|
31 #include "llvm/ADT/DenseMap.h"
|
|
32 #include "llvm/ADT/DenseSet.h"
|
173
|
33 #include "llvm/ADT/IntrusiveRefCntPtr.h"
|
221
|
34 #include "llvm/ADT/None.h"
|
|
35 #include "llvm/ADT/Optional.h"
|
173
|
36 #include "llvm/ADT/STLExtras.h"
|
|
37 #include "llvm/ADT/SmallString.h"
|
|
38 #include "llvm/ADT/StringExtras.h"
|
|
39 #include "llvm/ADT/StringRef.h"
|
|
40 #include "llvm/ADT/StringSet.h"
|
|
41 #include "llvm/Support/Error.h"
|
|
42 #include "llvm/Support/ErrorHandling.h"
|
|
43 #include "llvm/Support/FormatVariadic.h"
|
|
44 #include "llvm/Support/MemoryBuffer.h"
|
|
45 #include "llvm/Support/Path.h"
|
|
46 #include "llvm/Support/VirtualFileSystem.h"
|
|
47 #include "llvm/Support/raw_ostream.h"
|
|
48 #include <iterator>
|
|
49 #include <memory>
|
|
50 #include <string>
|
|
51 #include <system_error>
|
|
52 #include <utility>
|
|
53 #include <vector>
|
150
|
54
|
|
55 namespace clang {
|
|
56 namespace clangd {
|
|
57 namespace {
|
221
|
58 constexpr llvm::StringLiteral PreamblePatchHeaderName = "__preamble_patch__.h";
|
150
|
59
|
|
60 bool compileCommandsAreEqual(const tooling::CompileCommand &LHS,
|
|
61 const tooling::CompileCommand &RHS) {
|
|
62 // We don't check for Output, it should not matter to clangd.
|
|
63 return LHS.Directory == RHS.Directory && LHS.Filename == RHS.Filename &&
|
|
64 llvm::makeArrayRef(LHS.CommandLine).equals(RHS.CommandLine);
|
|
65 }
|
|
66
|
|
67 class CppFilePreambleCallbacks : public PreambleCallbacks {
|
|
68 public:
|
|
69 CppFilePreambleCallbacks(PathRef File, PreambleParsedCallback ParsedCallback)
|
|
70 : File(File), ParsedCallback(ParsedCallback) {}
|
|
71
|
|
72 IncludeStructure takeIncludes() { return std::move(Includes); }
|
|
73
|
|
74 MainFileMacros takeMacros() { return std::move(Macros); }
|
|
75
|
|
76 CanonicalIncludes takeCanonicalIncludes() { return std::move(CanonIncludes); }
|
|
77
|
|
78 void AfterExecute(CompilerInstance &CI) override {
|
|
79 if (!ParsedCallback)
|
|
80 return;
|
|
81 trace::Span Tracer("Running PreambleCallback");
|
|
82 ParsedCallback(CI.getASTContext(), CI.getPreprocessorPtr(), CanonIncludes);
|
|
83 }
|
|
84
|
|
85 void BeforeExecute(CompilerInstance &CI) override {
|
|
86 CanonIncludes.addSystemHeadersMapping(CI.getLangOpts());
|
|
87 LangOpts = &CI.getLangOpts();
|
|
88 SourceMgr = &CI.getSourceManager();
|
|
89 }
|
|
90
|
|
91 std::unique_ptr<PPCallbacks> createPPCallbacks() override {
|
|
92 assert(SourceMgr && LangOpts &&
|
|
93 "SourceMgr and LangOpts must be set at this point");
|
|
94
|
|
95 return std::make_unique<PPChainedCallbacks>(
|
|
96 collectIncludeStructureCallback(*SourceMgr, &Includes),
|
173
|
97 std::make_unique<CollectMainFileMacros>(*SourceMgr, Macros));
|
150
|
98 }
|
|
99
|
|
100 CommentHandler *getCommentHandler() override {
|
|
101 IWYUHandler = collectIWYUHeaderMaps(&CanonIncludes);
|
|
102 return IWYUHandler.get();
|
|
103 }
|
|
104
|
221
|
105 bool shouldSkipFunctionBody(Decl *D) override {
|
|
106 // Generally we skip function bodies in preambles for speed.
|
|
107 // We can make exceptions for functions that are cheap to parse and
|
|
108 // instantiate, widely used, and valuable (e.g. commonly produce errors).
|
|
109 if (const auto *FT = llvm::dyn_cast<clang::FunctionTemplateDecl>(D)) {
|
|
110 if (const auto *II = FT->getDeclName().getAsIdentifierInfo())
|
|
111 // std::make_unique is trivial, and we diagnose bad constructor calls.
|
|
112 if (II->isStr("make_unique") && FT->isInStdNamespace())
|
|
113 return false;
|
|
114 }
|
|
115 return true;
|
|
116 }
|
|
117
|
150
|
118 private:
|
|
119 PathRef File;
|
|
120 PreambleParsedCallback ParsedCallback;
|
|
121 IncludeStructure Includes;
|
|
122 CanonicalIncludes CanonIncludes;
|
|
123 MainFileMacros Macros;
|
|
124 std::unique_ptr<CommentHandler> IWYUHandler = nullptr;
|
|
125 const clang::LangOptions *LangOpts = nullptr;
|
|
126 const SourceManager *SourceMgr = nullptr;
|
|
127 };
|
|
128
|
221
|
129 // Represents directives other than includes, where basic textual information is
|
|
130 // enough.
|
|
131 struct TextualPPDirective {
|
|
132 unsigned DirectiveLine;
|
|
133 // Full text that's representing the directive, including the `#`.
|
|
134 std::string Text;
|
|
135
|
|
136 bool operator==(const TextualPPDirective &RHS) const {
|
|
137 return std::tie(DirectiveLine, Text) ==
|
|
138 std::tie(RHS.DirectiveLine, RHS.Text);
|
173
|
139 }
|
|
140 };
|
|
141
|
221
|
142 // Formats a PP directive consisting of Prefix (e.g. "#define ") and Body ("X
|
|
143 // 10"). The formatting is copied so that the tokens in Body have PresumedLocs
|
|
144 // with correct columns and lines.
|
|
145 std::string spellDirective(llvm::StringRef Prefix,
|
|
146 CharSourceRange DirectiveRange,
|
|
147 const LangOptions &LangOpts, const SourceManager &SM,
|
|
148 unsigned &DirectiveLine) {
|
|
149 std::string SpelledDirective;
|
|
150 llvm::raw_string_ostream OS(SpelledDirective);
|
|
151 OS << Prefix;
|
|
152
|
|
153 // Make sure DirectiveRange is a char range and doesn't contain macro ids.
|
|
154 DirectiveRange = SM.getExpansionRange(DirectiveRange);
|
|
155 if (DirectiveRange.isTokenRange()) {
|
|
156 DirectiveRange.setEnd(
|
|
157 Lexer::getLocForEndOfToken(DirectiveRange.getEnd(), 0, SM, LangOpts));
|
|
158 }
|
|
159
|
|
160 auto DecompLoc = SM.getDecomposedLoc(DirectiveRange.getBegin());
|
|
161 DirectiveLine = SM.getLineNumber(DecompLoc.first, DecompLoc.second);
|
|
162 auto TargetColumn = SM.getColumnNumber(DecompLoc.first, DecompLoc.second) - 1;
|
|
163
|
|
164 // Pad with spaces before DirectiveRange to make sure it will be on right
|
|
165 // column when patched.
|
|
166 if (Prefix.size() <= TargetColumn) {
|
|
167 // There is enough space for Prefix and space before directive, use it.
|
|
168 // We try to squeeze the Prefix into the same line whenever we can, as
|
|
169 // putting onto a separate line won't work at the beginning of the file.
|
|
170 OS << std::string(TargetColumn - Prefix.size(), ' ');
|
|
171 } else {
|
|
172 // Prefix was longer than the space we had. We produce e.g.:
|
|
173 // #line N-1
|
|
174 // #define \
|
|
175 // X 10
|
|
176 OS << "\\\n" << std::string(TargetColumn, ' ');
|
|
177 // Decrement because we put an additional line break before
|
|
178 // DirectiveRange.begin().
|
|
179 --DirectiveLine;
|
|
180 }
|
|
181 OS << toSourceCode(SM, DirectiveRange.getAsRange());
|
|
182 return OS.str();
|
|
183 }
|
|
184
|
|
185 // Collects #define directives inside the main file.
|
|
186 struct DirectiveCollector : public PPCallbacks {
|
|
187 DirectiveCollector(const Preprocessor &PP,
|
|
188 std::vector<TextualPPDirective> &TextualDirectives)
|
|
189 : LangOpts(PP.getLangOpts()), SM(PP.getSourceManager()),
|
|
190 TextualDirectives(TextualDirectives) {}
|
|
191
|
|
192 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
|
193 SrcMgr::CharacteristicKind FileType,
|
|
194 FileID PrevFID) override {
|
|
195 InMainFile = SM.isWrittenInMainFile(Loc);
|
|
196 }
|
|
197
|
|
198 void MacroDefined(const Token &MacroNameTok,
|
|
199 const MacroDirective *MD) override {
|
|
200 if (!InMainFile)
|
|
201 return;
|
|
202 TextualDirectives.emplace_back();
|
|
203 TextualPPDirective &TD = TextualDirectives.back();
|
|
204
|
|
205 const auto *MI = MD->getMacroInfo();
|
|
206 TD.Text =
|
|
207 spellDirective("#define ",
|
|
208 CharSourceRange::getTokenRange(
|
|
209 MI->getDefinitionLoc(), MI->getDefinitionEndLoc()),
|
|
210 LangOpts, SM, TD.DirectiveLine);
|
|
211 }
|
|
212
|
|
213 private:
|
|
214 bool InMainFile = true;
|
|
215 const LangOptions &LangOpts;
|
|
216 const SourceManager &SM;
|
|
217 std::vector<TextualPPDirective> &TextualDirectives;
|
|
218 };
|
|
219
|
|
220 struct ScannedPreamble {
|
|
221 std::vector<Inclusion> Includes;
|
|
222 std::vector<TextualPPDirective> TextualDirectives;
|
|
223 PreambleBounds Bounds = {0, false};
|
|
224 };
|
|
225
|
|
226 /// Scans the preprocessor directives in the preamble section of the file by
|
|
227 /// running preprocessor over \p Contents. Returned includes do not contain
|
|
228 /// resolved paths. \p Cmd is used to build the compiler invocation, which might
|
|
229 /// stat/read files.
|
|
230 llvm::Expected<ScannedPreamble>
|
|
231 scanPreamble(llvm::StringRef Contents, const tooling::CompileCommand &Cmd) {
|
|
232 class EmptyFS : public ThreadsafeFS {
|
|
233 private:
|
|
234 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> viewImpl() const override {
|
|
235 return new llvm::vfs::InMemoryFileSystem;
|
|
236 }
|
|
237 };
|
|
238 EmptyFS FS;
|
173
|
239 // Build and run Preprocessor over the preamble.
|
|
240 ParseInputs PI;
|
|
241 PI.Contents = Contents.str();
|
221
|
242 PI.TFS = &FS;
|
173
|
243 PI.CompileCommand = Cmd;
|
|
244 IgnoringDiagConsumer IgnoreDiags;
|
|
245 auto CI = buildCompilerInvocation(PI, IgnoreDiags);
|
|
246 if (!CI)
|
221
|
247 return error("failed to create compiler invocation");
|
173
|
248 CI->getDiagnosticOpts().IgnoreWarnings = true;
|
|
249 auto ContentsBuffer = llvm::MemoryBuffer::getMemBuffer(Contents);
|
221
|
250 // This means we're scanning (though not preprocessing) the preamble section
|
|
251 // twice. However, it's important to precisely follow the preamble bounds used
|
|
252 // elsewhere.
|
|
253 auto Bounds = ComputePreambleBounds(*CI->getLangOpts(), *ContentsBuffer, 0);
|
|
254 auto PreambleContents =
|
|
255 llvm::MemoryBuffer::getMemBufferCopy(Contents.substr(0, Bounds.Size));
|
173
|
256 auto Clang = prepareCompilerInstance(
|
221
|
257 std::move(CI), nullptr, std::move(PreambleContents),
|
173
|
258 // Provide an empty FS to prevent preprocessor from performing IO. This
|
|
259 // also implies missing resolved paths for includes.
|
221
|
260 FS.view(llvm::None), IgnoreDiags);
|
173
|
261 if (Clang->getFrontendOpts().Inputs.empty())
|
221
|
262 return error("compiler instance had no inputs");
|
173
|
263 // We are only interested in main file includes.
|
|
264 Clang->getPreprocessorOpts().SingleFileParseMode = true;
|
221
|
265 PreprocessOnlyAction Action;
|
173
|
266 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0]))
|
221
|
267 return error("failed BeginSourceFile");
|
|
268 const auto &SM = Clang->getSourceManager();
|
173
|
269 Preprocessor &PP = Clang->getPreprocessor();
|
|
270 IncludeStructure Includes;
|
221
|
271 PP.addPPCallbacks(collectIncludeStructureCallback(SM, &Includes));
|
|
272 ScannedPreamble SP;
|
|
273 SP.Bounds = Bounds;
|
173
|
274 PP.addPPCallbacks(
|
221
|
275 std::make_unique<DirectiveCollector>(PP, SP.TextualDirectives));
|
173
|
276 if (llvm::Error Err = Action.Execute())
|
|
277 return std::move(Err);
|
|
278 Action.EndSourceFile();
|
221
|
279 SP.Includes = std::move(Includes.MainFileIncludes);
|
|
280 return SP;
|
173
|
281 }
|
|
282
|
|
283 const char *spellingForIncDirective(tok::PPKeywordKind IncludeDirective) {
|
|
284 switch (IncludeDirective) {
|
|
285 case tok::pp_include:
|
|
286 return "include";
|
|
287 case tok::pp_import:
|
|
288 return "import";
|
|
289 case tok::pp_include_next:
|
|
290 return "include_next";
|
|
291 default:
|
|
292 break;
|
|
293 }
|
|
294 llvm_unreachable("not an include directive");
|
|
295 }
|
221
|
296
|
|
297 // Checks whether \p FileName is a valid spelling of main file.
|
|
298 bool isMainFile(llvm::StringRef FileName, const SourceManager &SM) {
|
|
299 auto FE = SM.getFileManager().getFile(FileName);
|
|
300 return FE && *FE == SM.getFileEntryForID(SM.getMainFileID());
|
|
301 }
|
|
302
|
150
|
303 } // namespace
|
|
304
|
173
|
305 PreambleData::PreambleData(const ParseInputs &Inputs,
|
|
306 PrecompiledPreamble Preamble,
|
150
|
307 std::vector<Diag> Diags, IncludeStructure Includes,
|
|
308 MainFileMacros Macros,
|
|
309 std::unique_ptr<PreambleFileStatusCache> StatCache,
|
|
310 CanonicalIncludes CanonIncludes)
|
173
|
311 : Version(Inputs.Version), CompileCommand(Inputs.CompileCommand),
|
|
312 Preamble(std::move(Preamble)), Diags(std::move(Diags)),
|
150
|
313 Includes(std::move(Includes)), Macros(std::move(Macros)),
|
|
314 StatCache(std::move(StatCache)), CanonIncludes(std::move(CanonIncludes)) {
|
|
315 }
|
|
316
|
|
317 std::shared_ptr<const PreambleData>
|
173
|
318 buildPreamble(PathRef FileName, CompilerInvocation CI,
|
150
|
319 const ParseInputs &Inputs, bool StoreInMemory,
|
|
320 PreambleParsedCallback PreambleCallback) {
|
|
321 // Note that we don't need to copy the input contents, preamble can live
|
|
322 // without those.
|
|
323 auto ContentsBuffer =
|
|
324 llvm::MemoryBuffer::getMemBuffer(Inputs.Contents, FileName);
|
221
|
325 auto Bounds = ComputePreambleBounds(*CI.getLangOpts(), *ContentsBuffer, 0);
|
150
|
326
|
|
327 trace::Span Tracer("BuildPreamble");
|
|
328 SPAN_ATTACH(Tracer, "File", FileName);
|
221
|
329 std::vector<std::unique_ptr<FeatureModule::ASTListener>> ASTListeners;
|
|
330 if (Inputs.FeatureModules) {
|
|
331 for (auto &M : *Inputs.FeatureModules) {
|
|
332 if (auto Listener = M.astListeners())
|
|
333 ASTListeners.emplace_back(std::move(Listener));
|
|
334 }
|
|
335 }
|
150
|
336 StoreDiags PreambleDiagnostics;
|
221
|
337 PreambleDiagnostics.setDiagCallback(
|
|
338 [&ASTListeners](const clang::Diagnostic &D, clangd::Diag &Diag) {
|
|
339 llvm::for_each(ASTListeners,
|
|
340 [&](const auto &L) { L->sawDiagnostic(D, Diag); });
|
|
341 });
|
150
|
342 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine =
|
|
343 CompilerInstance::createDiagnostics(&CI.getDiagnosticOpts(),
|
|
344 &PreambleDiagnostics, false);
|
221
|
345 PreambleDiagnostics.setLevelAdjuster(
|
|
346 [&](DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) {
|
|
347 switch (Info.getID()) {
|
|
348 case diag::warn_no_newline_eof:
|
|
349 case diag::warn_cxx98_compat_no_newline_eof:
|
|
350 case diag::ext_no_newline_eof:
|
|
351 // If the preamble doesn't span the whole file, drop the no newline at
|
|
352 // eof warnings.
|
|
353 return Bounds.Size != ContentsBuffer->getBufferSize()
|
|
354 ? DiagnosticsEngine::Level::Ignored
|
|
355 : DiagLevel;
|
|
356 }
|
|
357 return DiagLevel;
|
|
358 });
|
150
|
359
|
|
360 // Skip function bodies when building the preamble to speed up building
|
|
361 // the preamble and make it smaller.
|
|
362 assert(!CI.getFrontendOpts().SkipFunctionBodies);
|
|
363 CI.getFrontendOpts().SkipFunctionBodies = true;
|
|
364 // We don't want to write comment locations into PCH. They are racy and slow
|
|
365 // to read back. We rely on dynamic index for the comments instead.
|
|
366 CI.getPreprocessorOpts().WriteCommentListToPCH = false;
|
|
367
|
|
368 CppFilePreambleCallbacks SerializedDeclsCollector(FileName, PreambleCallback);
|
221
|
369 auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
|
150
|
370 llvm::SmallString<32> AbsFileName(FileName);
|
221
|
371 VFS->makeAbsolute(AbsFileName);
|
150
|
372 auto StatCache = std::make_unique<PreambleFileStatusCache>(AbsFileName);
|
|
373 auto BuiltPreamble = PrecompiledPreamble::Build(
|
|
374 CI, ContentsBuffer.get(), Bounds, *PreambleDiagsEngine,
|
221
|
375 StatCache->getProducingFS(VFS),
|
150
|
376 std::make_shared<PCHContainerOperations>(), StoreInMemory,
|
|
377 SerializedDeclsCollector);
|
|
378
|
|
379 // When building the AST for the main file, we do want the function
|
|
380 // bodies.
|
|
381 CI.getFrontendOpts().SkipFunctionBodies = false;
|
|
382
|
|
383 if (BuiltPreamble) {
|
173
|
384 vlog("Built preamble of size {0} for file {1} version {2}",
|
|
385 BuiltPreamble->getSize(), FileName, Inputs.Version);
|
150
|
386 std::vector<Diag> Diags = PreambleDiagnostics.take();
|
|
387 return std::make_shared<PreambleData>(
|
173
|
388 Inputs, std::move(*BuiltPreamble), std::move(Diags),
|
150
|
389 SerializedDeclsCollector.takeIncludes(),
|
|
390 SerializedDeclsCollector.takeMacros(), std::move(StatCache),
|
|
391 SerializedDeclsCollector.takeCanonicalIncludes());
|
|
392 } else {
|
173
|
393 elog("Could not build a preamble for file {0} version {1}", FileName,
|
|
394 Inputs.Version);
|
150
|
395 return nullptr;
|
|
396 }
|
|
397 }
|
|
398
|
173
|
399 bool isPreambleCompatible(const PreambleData &Preamble,
|
|
400 const ParseInputs &Inputs, PathRef FileName,
|
|
401 const CompilerInvocation &CI) {
|
|
402 auto ContentsBuffer =
|
|
403 llvm::MemoryBuffer::getMemBuffer(Inputs.Contents, FileName);
|
221
|
404 auto Bounds = ComputePreambleBounds(*CI.getLangOpts(), *ContentsBuffer, 0);
|
|
405 auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
|
173
|
406 return compileCommandsAreEqual(Inputs.CompileCommand,
|
|
407 Preamble.CompileCommand) &&
|
221
|
408 Preamble.Preamble.CanReuse(CI, *ContentsBuffer, Bounds, *VFS);
|
173
|
409 }
|
|
410
|
|
411 void escapeBackslashAndQuotes(llvm::StringRef Text, llvm::raw_ostream &OS) {
|
|
412 for (char C : Text) {
|
|
413 switch (C) {
|
|
414 case '\\':
|
|
415 case '"':
|
|
416 OS << '\\';
|
|
417 break;
|
|
418 default:
|
|
419 break;
|
|
420 }
|
|
421 OS << C;
|
|
422 }
|
|
423 }
|
|
424
|
|
425 PreamblePatch PreamblePatch::create(llvm::StringRef FileName,
|
|
426 const ParseInputs &Modified,
|
|
427 const PreambleData &Baseline) {
|
221
|
428 trace::Span Tracer("CreatePreamblePatch");
|
|
429 SPAN_ATTACH(Tracer, "File", FileName);
|
|
430 assert(llvm::sys::path::is_absolute(FileName) && "relative FileName!");
|
|
431 // First scan preprocessor directives in Baseline and Modified. These will be
|
173
|
432 // used to figure out newly added directives in Modified. Scanning can fail,
|
|
433 // the code just bails out and creates an empty patch in such cases, as:
|
|
434 // - If scanning for Baseline fails, no knowledge of existing includes hence
|
|
435 // patch will contain all the includes in Modified. Leading to rebuild of
|
|
436 // whole preamble, which is terribly slow.
|
|
437 // - If scanning for Modified fails, cannot figure out newly added ones so
|
|
438 // there's nothing to do but generate an empty patch.
|
221
|
439 auto BaselineScan = scanPreamble(
|
173
|
440 // Contents needs to be null-terminated.
|
221
|
441 Baseline.Preamble.getContents().str(), Modified.CompileCommand);
|
|
442 if (!BaselineScan) {
|
|
443 elog("Failed to scan baseline of {0}: {1}", FileName,
|
|
444 BaselineScan.takeError());
|
|
445 return PreamblePatch::unmodified(Baseline);
|
173
|
446 }
|
221
|
447 auto ModifiedScan = scanPreamble(Modified.Contents, Modified.CompileCommand);
|
|
448 if (!ModifiedScan) {
|
|
449 elog("Failed to scan modified contents of {0}: {1}", FileName,
|
|
450 ModifiedScan.takeError());
|
|
451 return PreamblePatch::unmodified(Baseline);
|
173
|
452 }
|
221
|
453
|
|
454 bool IncludesChanged = BaselineScan->Includes != ModifiedScan->Includes;
|
|
455 bool DirectivesChanged =
|
|
456 BaselineScan->TextualDirectives != ModifiedScan->TextualDirectives;
|
|
457 if (!IncludesChanged && !DirectivesChanged)
|
|
458 return PreamblePatch::unmodified(Baseline);
|
173
|
459
|
|
460 PreamblePatch PP;
|
|
461 // This shouldn't coincide with any real file name.
|
|
462 llvm::SmallString<128> PatchName;
|
|
463 llvm::sys::path::append(PatchName, llvm::sys::path::parent_path(FileName),
|
221
|
464 PreamblePatchHeaderName);
|
173
|
465 PP.PatchFileName = PatchName.str().str();
|
221
|
466 PP.ModifiedBounds = ModifiedScan->Bounds;
|
173
|
467
|
|
468 llvm::raw_string_ostream Patch(PP.PatchContents);
|
|
469 // Set default filename for subsequent #line directives
|
|
470 Patch << "#line 0 \"";
|
|
471 // FileName part of a line directive is subject to backslash escaping, which
|
|
472 // might lead to problems on windows especially.
|
|
473 escapeBackslashAndQuotes(FileName, Patch);
|
|
474 Patch << "\"\n";
|
221
|
475
|
|
476 if (IncludesChanged) {
|
|
477 // We are only interested in newly added includes, record the ones in
|
|
478 // Baseline for exclusion.
|
|
479 llvm::DenseMap<std::pair<tok::PPKeywordKind, llvm::StringRef>,
|
|
480 /*Resolved=*/llvm::StringRef>
|
|
481 ExistingIncludes;
|
|
482 for (const auto &Inc : Baseline.Includes.MainFileIncludes)
|
|
483 ExistingIncludes[{Inc.Directive, Inc.Written}] = Inc.Resolved;
|
|
484 // There might be includes coming from disabled regions, record these for
|
|
485 // exclusion too. note that we don't have resolved paths for those.
|
|
486 for (const auto &Inc : BaselineScan->Includes)
|
|
487 ExistingIncludes.try_emplace({Inc.Directive, Inc.Written});
|
|
488 // Calculate extra includes that needs to be inserted.
|
|
489 for (auto &Inc : ModifiedScan->Includes) {
|
|
490 auto It = ExistingIncludes.find({Inc.Directive, Inc.Written});
|
|
491 // Include already present in the baseline preamble. Set resolved path and
|
|
492 // put into preamble includes.
|
|
493 if (It != ExistingIncludes.end()) {
|
|
494 Inc.Resolved = It->second.str();
|
|
495 PP.PreambleIncludes.push_back(Inc);
|
|
496 continue;
|
|
497 }
|
|
498 // Include is new in the modified preamble. Inject it into the patch and
|
|
499 // use #line to set the presumed location to where it is spelled.
|
|
500 auto LineCol = offsetToClangLineColumn(Modified.Contents, Inc.HashOffset);
|
|
501 Patch << llvm::formatv("#line {0}\n", LineCol.first);
|
|
502 Patch << llvm::formatv(
|
|
503 "#{0} {1}\n", spellingForIncDirective(Inc.Directive), Inc.Written);
|
|
504 }
|
173
|
505 }
|
221
|
506
|
|
507 if (DirectivesChanged) {
|
|
508 // We need to patch all the directives, since they are order dependent. e.g:
|
|
509 // #define BAR(X) NEW(X) // Newly introduced in Modified
|
|
510 // #define BAR(X) OLD(X) // Exists in the Baseline
|
|
511 //
|
|
512 // If we've patched only the first directive, the macro definition would've
|
|
513 // been wrong for the rest of the file, since patch is applied after the
|
|
514 // baseline preamble.
|
|
515 //
|
|
516 // Note that we deliberately ignore conditional directives and undefs to
|
|
517 // reduce complexity. The former might cause problems because scanning is
|
|
518 // imprecise and might pick directives from disabled regions.
|
|
519 for (const auto &TD : ModifiedScan->TextualDirectives) {
|
|
520 Patch << "#line " << TD.DirectiveLine << '\n';
|
|
521 Patch << TD.Text << '\n';
|
|
522 }
|
|
523 }
|
|
524 dlog("Created preamble patch: {0}", Patch.str());
|
173
|
525 Patch.flush();
|
|
526 return PP;
|
|
527 }
|
|
528
|
|
529 void PreamblePatch::apply(CompilerInvocation &CI) const {
|
|
530 // No need to map an empty file.
|
|
531 if (PatchContents.empty())
|
|
532 return;
|
|
533 auto &PPOpts = CI.getPreprocessorOpts();
|
|
534 auto PatchBuffer =
|
|
535 // we copy here to ensure contents are still valid if CI outlives the
|
|
536 // PreamblePatch.
|
|
537 llvm::MemoryBuffer::getMemBufferCopy(PatchContents, PatchFileName);
|
|
538 // CI will take care of the lifetime of the buffer.
|
|
539 PPOpts.addRemappedFile(PatchFileName, PatchBuffer.release());
|
|
540 // The patch will be parsed after loading the preamble ast and before parsing
|
|
541 // the main file.
|
|
542 PPOpts.Includes.push_back(PatchFileName);
|
|
543 }
|
|
544
|
221
|
545 std::vector<Inclusion> PreamblePatch::preambleIncludes() const {
|
|
546 return PreambleIncludes;
|
|
547 }
|
|
548
|
|
549 PreamblePatch PreamblePatch::unmodified(const PreambleData &Preamble) {
|
|
550 PreamblePatch PP;
|
|
551 PP.PreambleIncludes = Preamble.Includes.MainFileIncludes;
|
|
552 PP.ModifiedBounds = Preamble.Preamble.getBounds();
|
|
553 return PP;
|
|
554 }
|
|
555
|
|
556 SourceLocation translatePreamblePatchLocation(SourceLocation Loc,
|
|
557 const SourceManager &SM) {
|
|
558 auto DefFile = SM.getFileID(Loc);
|
|
559 if (auto *FE = SM.getFileEntryForID(DefFile)) {
|
|
560 auto IncludeLoc = SM.getIncludeLoc(DefFile);
|
|
561 // Preamble patch is included inside the builtin file.
|
|
562 if (IncludeLoc.isValid() && SM.isWrittenInBuiltinFile(IncludeLoc) &&
|
|
563 FE->getName().endswith(PreamblePatchHeaderName)) {
|
|
564 auto Presumed = SM.getPresumedLoc(Loc);
|
|
565 // Check that line directive is pointing at main file.
|
|
566 if (Presumed.isValid() && Presumed.getFileID().isInvalid() &&
|
|
567 isMainFile(Presumed.getFilename(), SM)) {
|
|
568 Loc = SM.translateLineCol(SM.getMainFileID(), Presumed.getLine(),
|
|
569 Presumed.getColumn());
|
|
570 }
|
|
571 }
|
|
572 }
|
|
573 return Loc;
|
|
574 }
|
150
|
575 } // namespace clangd
|
|
576 } // namespace clang
|