150
|
1 //===--- ParsedAST.cpp -------------------------------------------*- C++-*-===//
|
|
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 "ParsedAST.h"
|
|
10 #include "../clang-tidy/ClangTidyDiagnosticConsumer.h"
|
|
11 #include "../clang-tidy/ClangTidyModuleRegistry.h"
|
|
12 #include "AST.h"
|
|
13 #include "Compiler.h"
|
|
14 #include "Diagnostics.h"
|
|
15 #include "Headers.h"
|
|
16 #include "IncludeFixer.h"
|
|
17 #include "Logger.h"
|
|
18 #include "SourceCode.h"
|
|
19 #include "Trace.h"
|
|
20 #include "index/CanonicalIncludes.h"
|
|
21 #include "index/Index.h"
|
|
22 #include "clang/AST/ASTContext.h"
|
|
23 #include "clang/AST/Decl.h"
|
|
24 #include "clang/Basic/LangOptions.h"
|
|
25 #include "clang/Basic/SourceLocation.h"
|
|
26 #include "clang/Basic/SourceManager.h"
|
|
27 #include "clang/Basic/TokenKinds.h"
|
|
28 #include "clang/Frontend/CompilerInstance.h"
|
|
29 #include "clang/Frontend/CompilerInvocation.h"
|
|
30 #include "clang/Frontend/FrontendActions.h"
|
|
31 #include "clang/Frontend/Utils.h"
|
|
32 #include "clang/Index/IndexDataConsumer.h"
|
|
33 #include "clang/Index/IndexingAction.h"
|
|
34 #include "clang/Lex/Lexer.h"
|
|
35 #include "clang/Lex/MacroInfo.h"
|
|
36 #include "clang/Lex/PPCallbacks.h"
|
|
37 #include "clang/Lex/Preprocessor.h"
|
|
38 #include "clang/Lex/PreprocessorOptions.h"
|
|
39 #include "clang/Sema/Sema.h"
|
|
40 #include "clang/Serialization/ASTWriter.h"
|
|
41 #include "clang/Serialization/PCHContainerOperations.h"
|
|
42 #include "clang/Tooling/CompilationDatabase.h"
|
|
43 #include "clang/Tooling/Syntax/Tokens.h"
|
|
44 #include "llvm/ADT/ArrayRef.h"
|
|
45 #include "llvm/ADT/STLExtras.h"
|
|
46 #include "llvm/ADT/SmallString.h"
|
|
47 #include "llvm/ADT/SmallVector.h"
|
|
48 #include "llvm/Support/raw_ostream.h"
|
|
49 #include <algorithm>
|
|
50 #include <memory>
|
|
51
|
|
52 // Force the linker to link in Clang-tidy modules.
|
|
53 // clangd doesn't support the static analyzer.
|
|
54 #define CLANG_TIDY_DISABLE_STATIC_ANALYZER_CHECKS
|
|
55 #include "../clang-tidy/ClangTidyForceLinker.h"
|
|
56
|
|
57 namespace clang {
|
|
58 namespace clangd {
|
|
59 namespace {
|
|
60
|
|
61 template <class T> std::size_t getUsedBytes(const std::vector<T> &Vec) {
|
|
62 return Vec.capacity() * sizeof(T);
|
|
63 }
|
|
64
|
|
65 class DeclTrackingASTConsumer : public ASTConsumer {
|
|
66 public:
|
|
67 DeclTrackingASTConsumer(std::vector<Decl *> &TopLevelDecls)
|
|
68 : TopLevelDecls(TopLevelDecls) {}
|
|
69
|
|
70 bool HandleTopLevelDecl(DeclGroupRef DG) override {
|
|
71 for (Decl *D : DG) {
|
|
72 auto &SM = D->getASTContext().getSourceManager();
|
|
73 if (!isInsideMainFile(D->getLocation(), SM))
|
|
74 continue;
|
|
75 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
|
|
76 if (isImplicitTemplateInstantiation(ND))
|
|
77 continue;
|
|
78
|
|
79 // ObjCMethodDecl are not actually top-level decls.
|
|
80 if (isa<ObjCMethodDecl>(D))
|
|
81 continue;
|
|
82
|
|
83 TopLevelDecls.push_back(D);
|
|
84 }
|
|
85 return true;
|
|
86 }
|
|
87
|
|
88 private:
|
|
89 std::vector<Decl *> &TopLevelDecls;
|
|
90 };
|
|
91
|
|
92 class ClangdFrontendAction : public SyntaxOnlyAction {
|
|
93 public:
|
|
94 std::vector<Decl *> takeTopLevelDecls() { return std::move(TopLevelDecls); }
|
|
95
|
|
96 protected:
|
|
97 std::unique_ptr<ASTConsumer>
|
|
98 CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) override {
|
|
99 return std::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls);
|
|
100 }
|
|
101
|
|
102 private:
|
|
103 std::vector<Decl *> TopLevelDecls;
|
|
104 };
|
|
105
|
|
106 // When using a preamble, only preprocessor events outside its bounds are seen.
|
|
107 // This is almost what we want: replaying transitive preprocessing wastes time.
|
|
108 // However this confuses clang-tidy checks: they don't see any #includes!
|
|
109 // So we replay the *non-transitive* #includes that appear in the main-file.
|
|
110 // It would be nice to replay other events (macro definitions, ifdefs etc) but
|
|
111 // this addresses the most common cases fairly cheaply.
|
|
112 class ReplayPreamble : private PPCallbacks {
|
|
113 public:
|
|
114 // Attach preprocessor hooks such that preamble events will be injected at
|
|
115 // the appropriate time.
|
|
116 // Events will be delivered to the *currently registered* PP callbacks.
|
|
117 static void attach(const IncludeStructure &Includes,
|
|
118 CompilerInstance &Clang) {
|
|
119 auto &PP = Clang.getPreprocessor();
|
|
120 auto *ExistingCallbacks = PP.getPPCallbacks();
|
|
121 // No need to replay events if nobody is listening.
|
|
122 if (!ExistingCallbacks)
|
|
123 return;
|
|
124 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(
|
|
125 new ReplayPreamble(Includes, ExistingCallbacks,
|
|
126 Clang.getSourceManager(), PP, Clang.getLangOpts())));
|
|
127 // We're relying on the fact that addPPCallbacks keeps the old PPCallbacks
|
|
128 // around, creating a chaining wrapper. Guard against other implementations.
|
|
129 assert(PP.getPPCallbacks() != ExistingCallbacks &&
|
|
130 "Expected chaining implementation");
|
|
131 }
|
|
132
|
|
133 private:
|
|
134 ReplayPreamble(const IncludeStructure &Includes, PPCallbacks *Delegate,
|
|
135 const SourceManager &SM, Preprocessor &PP,
|
|
136 const LangOptions &LangOpts)
|
|
137 : Includes(Includes), Delegate(Delegate), SM(SM), PP(PP),
|
|
138 LangOpts(LangOpts) {}
|
|
139
|
|
140 // In a normal compile, the preamble traverses the following structure:
|
|
141 //
|
|
142 // mainfile.cpp
|
|
143 // <built-in>
|
|
144 // ... macro definitions like __cplusplus ...
|
|
145 // <command-line>
|
|
146 // ... macro definitions for args like -Dfoo=bar ...
|
|
147 // "header1.h"
|
|
148 // ... header file contents ...
|
|
149 // "header2.h"
|
|
150 // ... header file contents ...
|
|
151 // ... main file contents ...
|
|
152 //
|
|
153 // When using a preamble, the "header1" and "header2" subtrees get skipped.
|
|
154 // We insert them right after the built-in header, which still appears.
|
|
155 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
|
156 SrcMgr::CharacteristicKind Kind, FileID PrevFID) override {
|
|
157 // It'd be nice if there was a better way to identify built-in headers...
|
|
158 if (Reason == FileChangeReason::ExitFile &&
|
|
159 SM.getBuffer(PrevFID)->getBufferIdentifier() == "<built-in>")
|
|
160 replay();
|
|
161 }
|
|
162
|
|
163 void replay() {
|
|
164 for (const auto &Inc : Includes.MainFileIncludes) {
|
|
165 const FileEntry *File = nullptr;
|
|
166 if (Inc.Resolved != "")
|
|
167 if (auto FE = SM.getFileManager().getFile(Inc.Resolved))
|
|
168 File = *FE;
|
|
169
|
|
170 llvm::StringRef WrittenFilename =
|
|
171 llvm::StringRef(Inc.Written).drop_front().drop_back();
|
|
172 bool Angled = llvm::StringRef(Inc.Written).startswith("<");
|
|
173
|
|
174 // Re-lex the #include directive to find its interesting parts.
|
|
175 llvm::StringRef Src = SM.getBufferData(SM.getMainFileID());
|
|
176 Lexer RawLexer(SM.getLocForStartOfFile(SM.getMainFileID()), LangOpts,
|
|
177 Src.begin(), Src.begin() + Inc.HashOffset, Src.end());
|
|
178 Token HashTok, IncludeTok, FilenameTok;
|
|
179 RawLexer.LexFromRawLexer(HashTok);
|
|
180 assert(HashTok.getKind() == tok::hash);
|
|
181 RawLexer.setParsingPreprocessorDirective(true);
|
|
182 RawLexer.LexFromRawLexer(IncludeTok);
|
|
183 IdentifierInfo *II = PP.getIdentifierInfo(IncludeTok.getRawIdentifier());
|
|
184 IncludeTok.setIdentifierInfo(II);
|
|
185 IncludeTok.setKind(II->getTokenID());
|
|
186 RawLexer.LexIncludeFilename(FilenameTok);
|
|
187
|
|
188 Delegate->InclusionDirective(
|
|
189 HashTok.getLocation(), IncludeTok, WrittenFilename, Angled,
|
|
190 CharSourceRange::getCharRange(FilenameTok.getLocation(),
|
|
191 FilenameTok.getEndLoc()),
|
|
192 File, "SearchPath", "RelPath", /*Imported=*/nullptr, Inc.FileKind);
|
|
193 if (File)
|
|
194 // FIXME: Use correctly named FileEntryRef.
|
|
195 Delegate->FileSkipped(FileEntryRef(File->getName(), *File), FilenameTok,
|
|
196 Inc.FileKind);
|
|
197 else {
|
|
198 llvm::SmallString<1> UnusedRecovery;
|
|
199 Delegate->FileNotFound(WrittenFilename, UnusedRecovery);
|
|
200 }
|
|
201 }
|
|
202 }
|
|
203
|
|
204 const IncludeStructure &Includes;
|
|
205 PPCallbacks *Delegate;
|
|
206 const SourceManager &SM;
|
|
207 Preprocessor &PP;
|
|
208 const LangOptions &LangOpts;
|
|
209 };
|
|
210
|
|
211 } // namespace
|
|
212
|
|
213 void dumpAST(ParsedAST &AST, llvm::raw_ostream &OS) {
|
|
214 AST.getASTContext().getTranslationUnitDecl()->dump(OS, true);
|
|
215 }
|
|
216
|
|
217 llvm::Optional<ParsedAST>
|
|
218 ParsedAST::build(std::unique_ptr<clang::CompilerInvocation> CI,
|
|
219 llvm::ArrayRef<Diag> CompilerInvocationDiags,
|
|
220 std::shared_ptr<const PreambleData> Preamble,
|
|
221 std::unique_ptr<llvm::MemoryBuffer> Buffer,
|
|
222 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
|
|
223 const SymbolIndex *Index, const ParseOptions &Opts) {
|
|
224 assert(CI);
|
|
225 // Command-line parsing sets DisableFree to true by default, but we don't want
|
|
226 // to leak memory in clangd.
|
|
227 CI->getFrontendOpts().DisableFree = false;
|
|
228 const PrecompiledPreamble *PreamblePCH =
|
|
229 Preamble ? &Preamble->Preamble : nullptr;
|
|
230
|
|
231 StoreDiags ASTDiags;
|
|
232 std::string Content = std::string(Buffer->getBuffer());
|
|
233 std::string Filename =
|
|
234 std::string(Buffer->getBufferIdentifier()); // Absolute.
|
|
235
|
|
236 auto Clang = prepareCompilerInstance(std::move(CI), PreamblePCH,
|
|
237 std::move(Buffer), VFS, ASTDiags);
|
|
238 if (!Clang)
|
|
239 return None;
|
|
240
|
|
241 auto Action = std::make_unique<ClangdFrontendAction>();
|
|
242 const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0];
|
|
243 if (!Action->BeginSourceFile(*Clang, MainInput)) {
|
|
244 log("BeginSourceFile() failed when building AST for {0}",
|
|
245 MainInput.getFile());
|
|
246 return None;
|
|
247 }
|
|
248
|
|
249 // Set up ClangTidy. Must happen after BeginSourceFile() so ASTContext exists.
|
|
250 // Clang-tidy has some limitiations to ensure reasonable performance:
|
|
251 // - checks don't see all preprocessor events in the preamble
|
|
252 // - matchers run only over the main-file top-level decls (and can't see
|
|
253 // ancestors outside this scope).
|
|
254 // In practice almost all checks work well without modifications.
|
|
255 std::vector<std::unique_ptr<tidy::ClangTidyCheck>> CTChecks;
|
|
256 ast_matchers::MatchFinder CTFinder;
|
|
257 llvm::Optional<tidy::ClangTidyContext> CTContext;
|
|
258 {
|
|
259 trace::Span Tracer("ClangTidyInit");
|
|
260 dlog("ClangTidy configuration for file {0}: {1}", Filename,
|
|
261 tidy::configurationAsText(Opts.ClangTidyOpts));
|
|
262 tidy::ClangTidyCheckFactories CTFactories;
|
|
263 for (const auto &E : tidy::ClangTidyModuleRegistry::entries())
|
|
264 E.instantiate()->addCheckFactories(CTFactories);
|
|
265 CTContext.emplace(std::make_unique<tidy::DefaultOptionsProvider>(
|
|
266 tidy::ClangTidyGlobalOptions(), Opts.ClangTidyOpts));
|
|
267 CTContext->setDiagnosticsEngine(&Clang->getDiagnostics());
|
|
268 CTContext->setASTContext(&Clang->getASTContext());
|
|
269 CTContext->setCurrentFile(Filename);
|
|
270 CTChecks = CTFactories.createChecks(CTContext.getPointer());
|
|
271 ASTDiags.setLevelAdjuster([&CTContext](DiagnosticsEngine::Level DiagLevel,
|
|
272 const clang::Diagnostic &Info) {
|
|
273 if (CTContext) {
|
|
274 std::string CheckName = CTContext->getCheckName(Info.getID());
|
|
275 bool IsClangTidyDiag = !CheckName.empty();
|
|
276 if (IsClangTidyDiag) {
|
|
277 // Check for warning-as-error.
|
|
278 // We deliberately let this take precedence over suppression comments
|
|
279 // to match clang-tidy's behaviour.
|
|
280 if (DiagLevel == DiagnosticsEngine::Warning &&
|
|
281 CTContext->treatAsError(CheckName)) {
|
|
282 return DiagnosticsEngine::Error;
|
|
283 }
|
|
284
|
|
285 // Check for suppression comment. Skip the check for diagnostics not
|
|
286 // in the main file, because we don't want that function to query the
|
|
287 // source buffer for preamble files. For the same reason, we ask
|
|
288 // shouldSuppressDiagnostic not to follow macro expansions, since
|
|
289 // those might take us into a preamble file as well.
|
|
290 bool IsInsideMainFile =
|
|
291 Info.hasSourceManager() &&
|
|
292 isInsideMainFile(Info.getLocation(), Info.getSourceManager());
|
|
293 if (IsInsideMainFile && tidy::shouldSuppressDiagnostic(
|
|
294 DiagLevel, Info, *CTContext,
|
|
295 /* CheckMacroExpansion = */ false)) {
|
|
296 return DiagnosticsEngine::Ignored;
|
|
297 }
|
|
298 }
|
|
299 }
|
|
300 return DiagLevel;
|
|
301 });
|
|
302 Preprocessor *PP = &Clang->getPreprocessor();
|
|
303 for (const auto &Check : CTChecks) {
|
|
304 // FIXME: the PP callbacks skip the entire preamble.
|
|
305 // Checks that want to see #includes in the main file do not see them.
|
|
306 Check->registerPPCallbacks(Clang->getSourceManager(), PP, PP);
|
|
307 Check->registerMatchers(&CTFinder);
|
|
308 }
|
|
309 }
|
|
310
|
|
311 // Add IncludeFixer which can recover diagnostics caused by missing includes
|
|
312 // (e.g. incomplete type) and attach include insertion fixes to diagnostics.
|
|
313 llvm::Optional<IncludeFixer> FixIncludes;
|
|
314 auto BuildDir = VFS->getCurrentWorkingDirectory();
|
|
315 if (Opts.SuggestMissingIncludes && Index && !BuildDir.getError()) {
|
|
316 auto Style = getFormatStyleForFile(Filename, Content, VFS.get());
|
|
317 auto Inserter = std::make_shared<IncludeInserter>(
|
|
318 Filename, Content, Style, BuildDir.get(),
|
|
319 &Clang->getPreprocessor().getHeaderSearchInfo());
|
|
320 if (Preamble) {
|
|
321 for (const auto &Inc : Preamble->Includes.MainFileIncludes)
|
|
322 Inserter->addExisting(Inc);
|
|
323 }
|
|
324 FixIncludes.emplace(Filename, Inserter, *Index,
|
|
325 /*IndexRequestLimit=*/5);
|
|
326 ASTDiags.contributeFixes([&FixIncludes](DiagnosticsEngine::Level DiagLevl,
|
|
327 const clang::Diagnostic &Info) {
|
|
328 return FixIncludes->fix(DiagLevl, Info);
|
|
329 });
|
|
330 Clang->setExternalSemaSource(FixIncludes->unresolvedNameRecorder());
|
|
331 }
|
|
332
|
|
333 // Copy over the includes from the preamble, then combine with the
|
|
334 // non-preamble includes below.
|
|
335 auto Includes = Preamble ? Preamble->Includes : IncludeStructure{};
|
|
336 // Replay the preamble includes so that clang-tidy checks can see them.
|
|
337 if (Preamble)
|
|
338 ReplayPreamble::attach(Includes, *Clang);
|
|
339 // Important: collectIncludeStructure is registered *after* ReplayPreamble!
|
|
340 // Otherwise we would collect the replayed includes again...
|
|
341 // (We can't *just* use the replayed includes, they don't have Resolved path).
|
|
342 Clang->getPreprocessor().addPPCallbacks(
|
|
343 collectIncludeStructureCallback(Clang->getSourceManager(), &Includes));
|
|
344 // Copy over the macros in the preamble region of the main file, and combine
|
|
345 // with non-preamble macros below.
|
|
346 MainFileMacros Macros;
|
|
347 if (Preamble)
|
|
348 Macros = Preamble->Macros;
|
|
349 Clang->getPreprocessor().addPPCallbacks(
|
|
350 std::make_unique<CollectMainFileMacros>(Clang->getSourceManager(),
|
|
351 Clang->getLangOpts(), Macros));
|
|
352
|
|
353 // Copy over the includes from the preamble, then combine with the
|
|
354 // non-preamble includes below.
|
|
355 CanonicalIncludes CanonIncludes;
|
|
356 if (Preamble)
|
|
357 CanonIncludes = Preamble->CanonIncludes;
|
|
358 else
|
|
359 CanonIncludes.addSystemHeadersMapping(Clang->getLangOpts());
|
|
360 std::unique_ptr<CommentHandler> IWYUHandler =
|
|
361 collectIWYUHeaderMaps(&CanonIncludes);
|
|
362 Clang->getPreprocessor().addCommentHandler(IWYUHandler.get());
|
|
363
|
|
364 // Collect tokens of the main file.
|
|
365 syntax::TokenCollector CollectTokens(Clang->getPreprocessor());
|
|
366
|
|
367 if (llvm::Error Err = Action->Execute())
|
|
368 log("Execute() failed when building AST for {0}: {1}", MainInput.getFile(),
|
|
369 toString(std::move(Err)));
|
|
370
|
|
371 // We have to consume the tokens before running clang-tidy to avoid collecting
|
|
372 // tokens from running the preprocessor inside the checks (only
|
|
373 // modernize-use-trailing-return-type does that today).
|
|
374 syntax::TokenBuffer Tokens = std::move(CollectTokens).consume();
|
|
375 std::vector<Decl *> ParsedDecls = Action->takeTopLevelDecls();
|
|
376 // AST traversals should exclude the preamble, to avoid performance cliffs.
|
|
377 Clang->getASTContext().setTraversalScope(ParsedDecls);
|
|
378 {
|
|
379 // Run the AST-dependent part of the clang-tidy checks.
|
|
380 // (The preprocessor part ran already, via PPCallbacks).
|
|
381 trace::Span Tracer("ClangTidyMatch");
|
|
382 CTFinder.matchAST(Clang->getASTContext());
|
|
383 }
|
|
384
|
|
385 // UnitDiagsConsumer is local, we can not store it in CompilerInstance that
|
|
386 // has a longer lifetime.
|
|
387 Clang->getDiagnostics().setClient(new IgnoreDiagnostics);
|
|
388 // CompilerInstance won't run this callback, do it directly.
|
|
389 ASTDiags.EndSourceFile();
|
|
390 // XXX: This is messy: clang-tidy checks flush some diagnostics at EOF.
|
|
391 // However Action->EndSourceFile() would destroy the ASTContext!
|
|
392 // So just inform the preprocessor of EOF, while keeping everything alive.
|
|
393 Clang->getPreprocessor().EndSourceFile();
|
|
394
|
|
395 std::vector<Diag> Diags = CompilerInvocationDiags;
|
|
396 // Add diagnostics from the preamble, if any.
|
|
397 if (Preamble)
|
|
398 Diags.insert(Diags.end(), Preamble->Diags.begin(), Preamble->Diags.end());
|
|
399 // Finally, add diagnostics coming from the AST.
|
|
400 {
|
|
401 std::vector<Diag> D = ASTDiags.take(CTContext.getPointer());
|
|
402 Diags.insert(Diags.end(), D.begin(), D.end());
|
|
403 }
|
|
404 return ParsedAST(std::move(Preamble), std::move(Clang), std::move(Action),
|
|
405 std::move(Tokens), std::move(Macros), std::move(ParsedDecls),
|
|
406 std::move(Diags), std::move(Includes),
|
|
407 std::move(CanonIncludes));
|
|
408 }
|
|
409
|
|
410 ParsedAST::ParsedAST(ParsedAST &&Other) = default;
|
|
411
|
|
412 ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default;
|
|
413
|
|
414 ParsedAST::~ParsedAST() {
|
|
415 if (Action) {
|
|
416 // We already notified the PP of end-of-file earlier, so detach it first.
|
|
417 // We must keep it alive until after EndSourceFile(), Sema relies on this.
|
|
418 auto PP = Clang->getPreprocessorPtr(); // Keep PP alive for now.
|
|
419 Clang->setPreprocessor(nullptr); // Detach so we don't send EOF again.
|
|
420 Action->EndSourceFile(); // Destroy ASTContext and Sema.
|
|
421 // Now Sema is gone, it's safe for PP to go out of scope.
|
|
422 }
|
|
423 }
|
|
424
|
|
425 ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); }
|
|
426
|
|
427 const ASTContext &ParsedAST::getASTContext() const {
|
|
428 return Clang->getASTContext();
|
|
429 }
|
|
430
|
|
431 Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); }
|
|
432
|
|
433 std::shared_ptr<Preprocessor> ParsedAST::getPreprocessorPtr() {
|
|
434 return Clang->getPreprocessorPtr();
|
|
435 }
|
|
436
|
|
437 const Preprocessor &ParsedAST::getPreprocessor() const {
|
|
438 return Clang->getPreprocessor();
|
|
439 }
|
|
440
|
|
441 llvm::ArrayRef<Decl *> ParsedAST::getLocalTopLevelDecls() {
|
|
442 return LocalTopLevelDecls;
|
|
443 }
|
|
444
|
|
445 const MainFileMacros &ParsedAST::getMacros() const { return Macros; }
|
|
446
|
|
447 const std::vector<Diag> &ParsedAST::getDiagnostics() const { return Diags; }
|
|
448
|
|
449 std::size_t ParsedAST::getUsedBytes() const {
|
|
450 auto &AST = getASTContext();
|
|
451 // FIXME(ibiryukov): we do not account for the dynamically allocated part of
|
|
452 // Message and Fixes inside each diagnostic.
|
|
453 std::size_t Total =
|
|
454 clangd::getUsedBytes(LocalTopLevelDecls) + clangd::getUsedBytes(Diags);
|
|
455
|
|
456 // FIXME: the rest of the function is almost a direct copy-paste from
|
|
457 // libclang's clang_getCXTUResourceUsage. We could share the implementation.
|
|
458
|
|
459 // Sum up variaous allocators inside the ast context and the preprocessor.
|
|
460 Total += AST.getASTAllocatedMemory();
|
|
461 Total += AST.getSideTableAllocatedMemory();
|
|
462 Total += AST.Idents.getAllocator().getTotalMemory();
|
|
463 Total += AST.Selectors.getTotalMemory();
|
|
464
|
|
465 Total += AST.getSourceManager().getContentCacheSize();
|
|
466 Total += AST.getSourceManager().getDataStructureSizes();
|
|
467 Total += AST.getSourceManager().getMemoryBufferSizes().malloc_bytes;
|
|
468
|
|
469 if (ExternalASTSource *Ext = AST.getExternalSource())
|
|
470 Total += Ext->getMemoryBufferSizes().malloc_bytes;
|
|
471
|
|
472 const Preprocessor &PP = getPreprocessor();
|
|
473 Total += PP.getTotalMemory();
|
|
474 if (PreprocessingRecord *PRec = PP.getPreprocessingRecord())
|
|
475 Total += PRec->getTotalMemory();
|
|
476 Total += PP.getHeaderSearchInfo().getTotalMemory();
|
|
477
|
|
478 return Total;
|
|
479 }
|
|
480
|
|
481 const IncludeStructure &ParsedAST::getIncludeStructure() const {
|
|
482 return Includes;
|
|
483 }
|
|
484
|
|
485 const CanonicalIncludes &ParsedAST::getCanonicalIncludes() const {
|
|
486 return CanonIncludes;
|
|
487 }
|
|
488
|
|
489 ParsedAST::ParsedAST(std::shared_ptr<const PreambleData> Preamble,
|
|
490 std::unique_ptr<CompilerInstance> Clang,
|
|
491 std::unique_ptr<FrontendAction> Action,
|
|
492 syntax::TokenBuffer Tokens, MainFileMacros Macros,
|
|
493 std::vector<Decl *> LocalTopLevelDecls,
|
|
494 std::vector<Diag> Diags, IncludeStructure Includes,
|
|
495 CanonicalIncludes CanonIncludes)
|
|
496 : Preamble(std::move(Preamble)), Clang(std::move(Clang)),
|
|
497 Action(std::move(Action)), Tokens(std::move(Tokens)),
|
|
498 Macros(std::move(Macros)), Diags(std::move(Diags)),
|
|
499 LocalTopLevelDecls(std::move(LocalTopLevelDecls)),
|
|
500 Includes(std::move(Includes)), CanonIncludes(std::move(CanonIncludes)) {
|
|
501 assert(this->Clang);
|
|
502 assert(this->Action);
|
|
503 }
|
|
504
|
|
505 llvm::Optional<ParsedAST>
|
|
506 buildAST(PathRef FileName, std::unique_ptr<CompilerInvocation> Invocation,
|
|
507 llvm::ArrayRef<Diag> CompilerInvocationDiags,
|
|
508 const ParseInputs &Inputs,
|
|
509 std::shared_ptr<const PreambleData> Preamble) {
|
|
510 trace::Span Tracer("BuildAST");
|
|
511 SPAN_ATTACH(Tracer, "File", FileName);
|
|
512
|
|
513 auto VFS = Inputs.FS;
|
|
514 if (Preamble && Preamble->StatCache)
|
|
515 VFS = Preamble->StatCache->getConsumingFS(std::move(VFS));
|
|
516 if (VFS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) {
|
|
517 log("Couldn't set working directory when building the preamble.");
|
|
518 // We proceed anyway, our lit-tests rely on results for non-existing working
|
|
519 // dirs.
|
|
520 }
|
|
521
|
|
522 return ParsedAST::build(
|
|
523 std::make_unique<CompilerInvocation>(*Invocation),
|
|
524 CompilerInvocationDiags, Preamble,
|
|
525 llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, FileName),
|
|
526 std::move(VFS), Inputs.Index, Inputs.Opts);
|
|
527 }
|
|
528
|
|
529 } // namespace clangd
|
|
530 } // namespace clang
|