1 //===--- ParsedAST.h - Building translation units ----------------*- C++-*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file exposes building a file as if it were open in clangd, and defines
10 // the ParsedAST structure that holds the results.
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
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
21 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
23 #include "CollectMacros.h"
25 #include "Diagnostics.h"
28 #include "clang-include-cleaner/Record.h"
29 #include "support/Path.h"
30 #include "clang/Frontend/FrontendAction.h"
31 #include "clang/Lex/Preprocessor.h"
32 #include "clang/Tooling/Syntax/Tokens.h"
33 #include "llvm/ADT/ArrayRef.h"
34 #include "llvm/ADT/StringRef.h"
43 class HeuristicResolver
;
45 /// Stores and provides access to parsed AST.
48 /// Attempts to run Clang and store the parsed AST.
49 /// If \p Preamble is non-null it is reused during parsing.
50 /// This function does not check if preamble is valid to reuse.
51 static std::optional
<ParsedAST
>
52 build(llvm::StringRef Filename
, const ParseInputs
&Inputs
,
53 std::unique_ptr
<clang::CompilerInvocation
> CI
,
54 llvm::ArrayRef
<Diag
> CompilerInvocationDiags
,
55 std::shared_ptr
<const PreambleData
> Preamble
);
57 ParsedAST(ParsedAST
&&Other
);
58 ParsedAST
&operator=(ParsedAST
&&Other
);
62 /// Note that the returned ast will not contain decls from the preamble that
63 /// were not deserialized during parsing. Clients should expect only decls
64 /// from the main file to be in the AST.
65 ASTContext
&getASTContext();
66 const ASTContext
&getASTContext() const;
70 Preprocessor
&getPreprocessor();
71 std::shared_ptr
<Preprocessor
> getPreprocessorPtr();
72 const Preprocessor
&getPreprocessor() const;
74 SourceManager
&getSourceManager() {
75 return getASTContext().getSourceManager();
77 const SourceManager
&getSourceManager() const {
78 return getASTContext().getSourceManager();
81 const LangOptions
&getLangOpts() const {
82 return getASTContext().getLangOpts();
85 /// This function returns top-level decls present in the main file of the AST.
86 /// The result does not include the decls that come from the preamble.
87 /// (These should be const, but RecursiveASTVisitor requires Decl*).
88 ArrayRef
<Decl
*> getLocalTopLevelDecls();
89 ArrayRef
<const Decl
*> getLocalTopLevelDecls() const;
91 llvm::ArrayRef
<Diag
> getDiagnostics() const;
93 /// Returns the estimated size of the AST and the accessory structures, in
94 /// bytes. Does not include the size of the preamble.
95 std::size_t getUsedBytes() const;
96 const IncludeStructure
&getIncludeStructure() const;
98 /// Gets all macro references (definition, expansions) present in the main
99 /// file, including those in the preamble region.
100 const MainFileMacros
&getMacros() const;
101 /// Gets all pragma marks in the main file.
102 const std::vector
<PragmaMark
> &getMarks() const;
103 /// Tokens recorded while parsing the main file.
104 /// (!) does not have tokens from the preamble.
105 const syntax::TokenBuffer
&getTokens() const { return Tokens
; }
106 /// Returns the PramaIncludes from the preamble.
107 /// Might be null if AST is built without a preamble.
108 std::shared_ptr
<const include_cleaner::PragmaIncludes
>
109 getPragmaIncludes() const;
111 /// Returns the version of the ParseInputs this AST was built from.
112 llvm::StringRef
version() const { return Version
; }
114 /// Returns the path passed by the caller when building this AST.
115 PathRef
tuPath() const { return TUPath
; }
117 /// Returns the version of the ParseInputs used to build Preamble part of this
118 /// AST. Might be std::nullopt if no Preamble is used.
119 std::optional
<llvm::StringRef
> preambleVersion() const;
121 const HeuristicResolver
*getHeuristicResolver() const {
122 return Resolver
.get();
126 ParsedAST(PathRef TUPath
, llvm::StringRef Version
,
127 std::shared_ptr
<const PreambleData
> Preamble
,
128 std::unique_ptr
<CompilerInstance
> Clang
,
129 std::unique_ptr
<FrontendAction
> Action
, syntax::TokenBuffer Tokens
,
130 MainFileMacros Macros
, std::vector
<PragmaMark
> Marks
,
131 std::vector
<Decl
*> LocalTopLevelDecls
, std::vector
<Diag
> Diags
,
132 IncludeStructure Includes
);
135 // In-memory preambles must outlive the AST, it is important that this member
136 // goes before Clang and Action.
137 std::shared_ptr
<const PreambleData
> Preamble
;
138 // We store an "incomplete" FrontendAction (i.e. no EndSourceFile was called
139 // on it) and CompilerInstance used to run it. That way we don't have to do
140 // complex memory management of all Clang structures on our own. (They are
141 // stored in CompilerInstance and cleaned up by
142 // FrontendAction.EndSourceFile).
143 std::unique_ptr
<CompilerInstance
> Clang
;
144 std::unique_ptr
<FrontendAction
> Action
;
145 /// Tokens recorded after the preamble finished.
146 /// - Includes all spelled tokens for the main file.
147 /// - Includes expanded tokens produced **after** preamble.
148 /// - Does not have spelled or expanded tokens for files from preamble.
149 syntax::TokenBuffer Tokens
;
151 /// All macro definitions and expansions in the main file.
152 MainFileMacros Macros
;
153 // Pragma marks in the main file.
154 std::vector
<PragmaMark
> Marks
;
155 // Diags emitted while parsing this AST (including preamble and compiler
157 std::vector
<Diag
> Diags
;
158 // Top-level decls inside the current file. Not that this does not include
159 // top-level decls from the preamble.
160 std::vector
<Decl
*> LocalTopLevelDecls
;
161 IncludeStructure Includes
;
162 std::unique_ptr
<HeuristicResolver
> Resolver
;
165 } // namespace clangd
168 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H