Debian Codegraph @

llvm-project/clang-tools-extra/clangd/ParsedAST.h

raw
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
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 "clang-include-cleaner/Record.h"
29 #include "index/CanonicalIncludes.h"
30 #include "support/Path.h"
31 #include "clang/Frontend/FrontendAction.h"
32 #include "clang/Lex/Preprocessor.h"
33 #include "clang/Tooling/Syntax/Tokens.h"
34 #include "llvm/ADT/ArrayRef.h"
35 #include "llvm/ADT/StringRef.h"
36 #include <memory>
37 #include <optional>
38 #include <string>
39 #include <vector>
40
41 namespace clang {
42 class Sema;
43 namespace clangd {
44 class HeuristicResolver;
45
46 /// Stores and provides access to parsed AST.
47 class ParsedAST {
48 public:
49 /// Attempts to run Clang and store the parsed AST.
50 /// If \p Preamble is non-null it is reused during parsing.
51 /// This function does not check if preamble is valid to reuse.
52 static std::optional<ParsedAST>
53 build(llvm::StringRef Filename, const ParseInputs &Inputs,
54 std::unique_ptr<clang::CompilerInvocation> CI,
55 llvm::ArrayRef<Diag> CompilerInvocationDiags,
56 std::shared_ptr<const PreambleData> Preamble);
57
58 ParsedAST(ParsedAST &&Other);
59 ParsedAST &operator=(ParsedAST &&Other);
60
61 ~ParsedAST();
62
63 /// Note that the returned ast will not contain decls from the preamble that
64 /// were not deserialized during parsing. Clients should expect only decls
65 /// from the main file to be in the AST.
66 ASTContext &getASTContext();
67 const ASTContext &getASTContext() const;
68
69 Sema &getSema();
70
71 Preprocessor &getPreprocessor();
72 std::shared_ptr<Preprocessor> getPreprocessorPtr();
73 const Preprocessor &getPreprocessor() const;
74
75 SourceManager &getSourceManager() {
76 return getASTContext().getSourceManager();
77 }
78 const SourceManager &getSourceManager() const {
79 return getASTContext().getSourceManager();
80 }
81
82 const LangOptions &getLangOpts() const {
83 return getASTContext().getLangOpts();
84 }
85
86 /// This function returns top-level decls present in the main file of the AST.
87 /// The result does not include the decls that come from the preamble.
88 /// (These should be const, but RecursiveASTVisitor requires Decl*).
89 ArrayRef<Decl *> getLocalTopLevelDecls();
90 ArrayRef<const Decl *> getLocalTopLevelDecls() const;
91
92 const std::optional<std::vector<Diag>> &getDiagnostics() const {
93 return Diags;
94 }
95
96 /// Returns the estimated size of the AST and the accessory structures, in
97 /// bytes. Does not include the size of the preamble.
98 std::size_t getUsedBytes() const;
99 const IncludeStructure &getIncludeStructure() const;
100 const CanonicalIncludes &getCanonicalIncludes() const;
101
102 /// Gets all macro references (definition, expansions) present in the main
103 /// file, including those in the preamble region.
104 const MainFileMacros &getMacros() const;
105 /// Gets all pragma marks in the main file.
106 const std::vector<PragmaMark> &getMarks() const;
107 /// Tokens recorded while parsing the main file.
108 /// (!) does not have tokens from the preamble.
109 const syntax::TokenBuffer &getTokens() const { return Tokens; }
110 /// Returns the PramaIncludes from the preamble.
111 /// Might be null if AST is built without a preamble.
112 const include_cleaner::PragmaIncludes *getPragmaIncludes() const;
113
114 /// Returns the version of the ParseInputs this AST was built from.
115 llvm::StringRef version() const { return Version; }
116
117 /// Returns the path passed by the caller when building this AST.
118 PathRef tuPath() const { return TUPath; }
119
120 /// Returns the version of the ParseInputs used to build Preamble part of this
121 /// AST. Might be std::nullopt if no Preamble is used.
122 std::optional<llvm::StringRef> preambleVersion() const;
123
124 const HeuristicResolver *getHeuristicResolver() const {
125 return Resolver.get();
126 }
127
128 private:
129 ParsedAST(PathRef TUPath, llvm::StringRef Version,
130 std::shared_ptr<const PreambleData> Preamble,
131 std::unique_ptr<CompilerInstance> Clang,
132 std::unique_ptr<FrontendAction> Action, syntax::TokenBuffer Tokens,
133 MainFileMacros Macros, std::vector<PragmaMark> Marks,
134 std::vector<Decl *> LocalTopLevelDecls,
135 std::optional<std::vector<Diag>> Diags, IncludeStructure Includes,
136 CanonicalIncludes CanonIncludes);
137
138 Path TUPath;
139 std::string Version;
140 // In-memory preambles must outlive the AST, it is important that this member
141 // goes before Clang and Action.
142 std::shared_ptr<const PreambleData> Preamble;
143 // We store an "incomplete" FrontendAction (i.e. no EndSourceFile was called
144 // on it) and CompilerInstance used to run it. That way we don't have to do
145 // complex memory management of all Clang structures on our own. (They are
146 // stored in CompilerInstance and cleaned up by
147 // FrontendAction.EndSourceFile).
148 std::unique_ptr<CompilerInstance> Clang;
149 std::unique_ptr<FrontendAction> Action;
150 /// Tokens recorded after the preamble finished.
151 /// - Includes all spelled tokens for the main file.
152 /// - Includes expanded tokens produced **after** preamble.
153 /// - Does not have spelled or expanded tokens for files from preamble.
154 syntax::TokenBuffer Tokens;
155
156 /// All macro definitions and expansions in the main file.
157 MainFileMacros Macros;
158 // Pragma marks in the main file.
159 std::vector<PragmaMark> Marks;
160 // Data, stored after parsing. std::nullopt if AST was built with a stale
161 // preamble.
162 std::optional<std::vector<Diag>> Diags;
163 // Top-level decls inside the current file. Not that this does not include
164 // top-level decls from the preamble.
165 std::vector<Decl *> LocalTopLevelDecls;
166 IncludeStructure Includes;
167 CanonicalIncludes CanonIncludes;
168 std::unique_ptr<HeuristicResolver> Resolver;
169 };
170
171 } // namespace clangd
172 } // namespace clang
173
174 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H