150
|
1 //===--- ParsedAST.h - Building translation units ----------------*- 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 // This file exposes building a file as if it were open in clangd, and defines
|
|
10 // the ParsedAST structure that holds the results.
|
|
11 //
|
|
12 // This is similar to a clang -fsyntax-only run that produces a clang AST, but
|
|
13 // we have several customizations:
|
|
14 // - preamble handling
|
|
15 // - capturing diagnostics for later access
|
|
16 // - running clang-tidy checks checks
|
|
17 //
|
|
18 //===----------------------------------------------------------------------===//
|
|
19
|
|
20 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
|
|
21 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
|
|
22
|
|
23 #include "CollectMacros.h"
|
|
24 #include "Compiler.h"
|
|
25 #include "Diagnostics.h"
|
|
26 #include "Headers.h"
|
|
27 #include "Preamble.h"
|
|
28 #include "index/CanonicalIncludes.h"
|
173
|
29 #include "support/Path.h"
|
150
|
30 #include "clang/Frontend/FrontendAction.h"
|
|
31 #include "clang/Frontend/PrecompiledPreamble.h"
|
|
32 #include "clang/Lex/Preprocessor.h"
|
|
33 #include "clang/Tooling/CompilationDatabase.h"
|
|
34 #include "clang/Tooling/Syntax/Tokens.h"
|
|
35 #include "llvm/ADT/ArrayRef.h"
|
|
36 #include <memory>
|
|
37 #include <string>
|
|
38 #include <vector>
|
|
39
|
|
40 namespace clang {
|
|
41 namespace clangd {
|
|
42 class SymbolIndex;
|
|
43
|
|
44 /// Stores and provides access to parsed AST.
|
|
45 class ParsedAST {
|
|
46 public:
|
173
|
47 /// Attempts to run Clang and store the parsed AST.
|
|
48 /// If \p Preamble is non-null it is reused during parsing.
|
|
49 /// This function does not check if preamble is valid to reuse.
|
150
|
50 static llvm::Optional<ParsedAST>
|
173
|
51 build(llvm::StringRef Filename, const ParseInputs &Inputs,
|
|
52 std::unique_ptr<clang::CompilerInvocation> CI,
|
150
|
53 llvm::ArrayRef<Diag> CompilerInvocationDiags,
|
173
|
54 std::shared_ptr<const PreambleData> Preamble);
|
150
|
55
|
|
56 ParsedAST(ParsedAST &&Other);
|
|
57 ParsedAST &operator=(ParsedAST &&Other);
|
|
58
|
|
59 ~ParsedAST();
|
|
60
|
|
61 /// Note that the returned ast will not contain decls from the preamble that
|
|
62 /// were not deserialized during parsing. Clients should expect only decls
|
|
63 /// from the main file to be in the AST.
|
|
64 ASTContext &getASTContext();
|
|
65 const ASTContext &getASTContext() const;
|
|
66
|
|
67 Preprocessor &getPreprocessor();
|
|
68 std::shared_ptr<Preprocessor> getPreprocessorPtr();
|
|
69 const Preprocessor &getPreprocessor() const;
|
|
70
|
|
71 SourceManager &getSourceManager() {
|
|
72 return getASTContext().getSourceManager();
|
|
73 }
|
|
74 const SourceManager &getSourceManager() const {
|
|
75 return getASTContext().getSourceManager();
|
|
76 }
|
|
77
|
|
78 const LangOptions &getLangOpts() const {
|
|
79 return getASTContext().getLangOpts();
|
|
80 }
|
|
81
|
|
82 /// This function returns top-level decls present in the main file of the AST.
|
|
83 /// The result does not include the decls that come from the preamble.
|
|
84 /// (These should be const, but RecursiveASTVisitor requires Decl*).
|
|
85 ArrayRef<Decl *> getLocalTopLevelDecls();
|
|
86
|
|
87 const std::vector<Diag> &getDiagnostics() const;
|
|
88
|
|
89 /// Returns the estimated size of the AST and the accessory structures, in
|
|
90 /// bytes. Does not include the size of the preamble.
|
|
91 std::size_t getUsedBytes() const;
|
|
92 const IncludeStructure &getIncludeStructure() const;
|
|
93 const CanonicalIncludes &getCanonicalIncludes() const;
|
|
94
|
|
95 /// Gets all macro references (definition, expansions) present in the main
|
|
96 /// file, including those in the preamble region.
|
|
97 const MainFileMacros &getMacros() const;
|
|
98 /// Tokens recorded while parsing the main file.
|
|
99 /// (!) does not have tokens from the preamble.
|
|
100 const syntax::TokenBuffer &getTokens() const { return Tokens; }
|
|
101
|
173
|
102 /// Returns the version of the ParseInputs this AST was built from.
|
|
103 llvm::StringRef version() const { return Version; }
|
|
104
|
150
|
105 private:
|
173
|
106 ParsedAST(llvm::StringRef Version,
|
|
107 std::shared_ptr<const PreambleData> Preamble,
|
150
|
108 std::unique_ptr<CompilerInstance> Clang,
|
|
109 std::unique_ptr<FrontendAction> Action, syntax::TokenBuffer Tokens,
|
|
110 MainFileMacros Macros, std::vector<Decl *> LocalTopLevelDecls,
|
|
111 std::vector<Diag> Diags, IncludeStructure Includes,
|
|
112 CanonicalIncludes CanonIncludes);
|
|
113
|
173
|
114 std::string Version;
|
150
|
115 // In-memory preambles must outlive the AST, it is important that this member
|
|
116 // goes before Clang and Action.
|
|
117 std::shared_ptr<const PreambleData> Preamble;
|
|
118 // We store an "incomplete" FrontendAction (i.e. no EndSourceFile was called
|
|
119 // on it) and CompilerInstance used to run it. That way we don't have to do
|
|
120 // complex memory management of all Clang structures on our own. (They are
|
|
121 // stored in CompilerInstance and cleaned up by
|
|
122 // FrontendAction.EndSourceFile).
|
|
123 std::unique_ptr<CompilerInstance> Clang;
|
|
124 std::unique_ptr<FrontendAction> Action;
|
|
125 /// Tokens recorded after the preamble finished.
|
|
126 /// - Includes all spelled tokens for the main file.
|
|
127 /// - Includes expanded tokens produced **after** preabmle.
|
|
128 /// - Does not have spelled or expanded tokens for files from preamble.
|
|
129 syntax::TokenBuffer Tokens;
|
|
130
|
|
131 /// All macro definitions and expansions in the main file.
|
|
132 MainFileMacros Macros;
|
|
133 // Data, stored after parsing.
|
|
134 std::vector<Diag> Diags;
|
|
135 // Top-level decls inside the current file. Not that this does not include
|
|
136 // top-level decls from the preamble.
|
|
137 std::vector<Decl *> LocalTopLevelDecls;
|
|
138 IncludeStructure Includes;
|
|
139 CanonicalIncludes CanonIncludes;
|
|
140 };
|
|
141
|
|
142 /// For testing/debugging purposes. Note that this method deserializes all
|
|
143 /// unserialized Decls, so use with care.
|
|
144 void dumpAST(ParsedAST &AST, llvm::raw_ostream &OS);
|
|
145
|
|
146 } // namespace clangd
|
|
147 } // namespace clang
|
|
148
|
|
149 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
|