clang-tools 24.0.0git
ParsedAST.h
Go to the documentation of this file.
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 "support/Path.h"
30#include "clang/AST/DeclCXX.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
41namespace clang {
42class HeuristicResolver;
43class Sema;
44namespace clangd {
45
46/// Stores and provides access to parsed AST.
47class ParsedAST {
48public:
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
60
61 ~ParsedAST();
62
63 ParsedAST(const ParsedAST &Other) = delete;
64 ParsedAST &operator=(const ParsedAST &Other) = delete;
65
66 /// Note that the returned ast will not contain decls from the preamble that
67 /// were not deserialized during parsing. Clients should expect only decls
68 /// from the main file to be in the AST.
69 ASTContext &getASTContext();
70 const ASTContext &getASTContext() const;
71
72 Sema &getSema();
73
74 Preprocessor &getPreprocessor();
75 std::shared_ptr<Preprocessor> getPreprocessorPtr();
76 const Preprocessor &getPreprocessor() const;
77
78 SourceManager &getSourceManager() {
79 return getASTContext().getSourceManager();
80 }
81 const SourceManager &getSourceManager() const {
82 return getASTContext().getSourceManager();
83 }
84
85 const LangOptions &getLangOpts() const {
86 return getASTContext().getLangOpts();
87 }
88
89 /// This function returns top-level decls present in the main file of the AST.
90 /// The result does not include the decls that come from the preamble.
91 /// (These should be const, but RecursiveASTVisitor requires Decl*).
92 ArrayRef<Decl *> getLocalTopLevelDecls();
93 ArrayRef<const Decl *> getLocalTopLevelDecls() const;
94
95 llvm::ArrayRef<Diag> getDiagnostics() const;
96
97 /// Returns the estimated size of the AST and the accessory structures, in
98 /// bytes. Does not include the size of the preamble.
99 std::size_t getUsedBytes() 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 for preamble + main file includes.
111 const include_cleaner::PragmaIncludes &getPragmaIncludes() const;
112
113 /// Returns the version of the ParseInputs this AST was built from.
114 llvm::StringRef version() const { return Version; }
115
116 /// Returns the path passed by the caller when building this AST.
117 PathRef tuPath() const { return TUPath; }
118
119 /// Returns the version of the ParseInputs used to build Preamble part of this
120 /// AST. Might be std::nullopt if no Preamble is used.
121 std::optional<llvm::StringRef> preambleVersion() const;
122
123 const HeuristicResolver *getHeuristicResolver() const {
124 return Resolver.get();
125 }
126
127 /// Cache for constructors called through forwarding, e.g. make_unique
128 llvm::DenseMap<const FunctionDecl *,
131
132private:
133 ParsedAST(PathRef TUPath, llvm::StringRef Version,
134 std::shared_ptr<const PreambleData> Preamble,
135 std::unique_ptr<CompilerInstance> Clang,
136 std::unique_ptr<FrontendAction> Action, syntax::TokenBuffer Tokens,
137 MainFileMacros Macros, std::vector<PragmaMark> Marks,
138 std::vector<Decl *> LocalTopLevelDecls, std::vector<Diag> Diags,
139 IncludeStructure Includes, include_cleaner::PragmaIncludes PI);
140 Path TUPath;
141 std::string Version;
142 // In-memory preambles must outlive the AST, it is important that this member
143 // goes before Clang and Action.
144 std::shared_ptr<const PreambleData> Preamble;
145 // We store an "incomplete" FrontendAction (i.e. no EndSourceFile was called
146 // on it) and CompilerInstance used to run it. That way we don't have to do
147 // complex memory management of all Clang structures on our own. (They are
148 // stored in CompilerInstance and cleaned up by
149 // FrontendAction.EndSourceFile).
150 std::unique_ptr<CompilerInstance> Clang;
151 std::unique_ptr<FrontendAction> Action;
152 /// Tokens recorded after the preamble finished.
153 /// - Includes all spelled tokens for the main file.
154 /// - Includes expanded tokens produced **after** preamble.
155 /// - Does not have spelled or expanded tokens for files from preamble.
156 syntax::TokenBuffer Tokens;
157
158 /// All macro definitions and expansions in the main file.
159 MainFileMacros Macros;
160 // Pragma marks in the main file.
161 std::vector<PragmaMark> Marks;
162 // Diags emitted while parsing this AST (including preamble and compiler
163 // invocation).
164 std::vector<Diag> Diags;
165 // Top-level decls inside the current file. Not that this does not include
166 // top-level decls from the preamble.
167 std::vector<Decl *> LocalTopLevelDecls;
168 IncludeStructure Includes;
169 include_cleaner::PragmaIncludes PI;
170 std::unique_ptr<HeuristicResolver> Resolver;
171};
172
173} // namespace clangd
174} // namespace clang
175
176#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
ParsedAST & operator=(const ParsedAST &Other)=delete
std::size_t getUsedBytes() const
Returns the estimated size of the AST and the accessory structures, in bytes.
PathRef tuPath() const
Returns the path passed by the caller when building this AST.
Definition ParsedAST.h:117
std::optional< llvm::StringRef > preambleVersion() const
Returns the version of the ParseInputs used to build Preamble part of this AST.
llvm::DenseMap< const FunctionDecl *, SmallVector< const CXXConstructorDecl *, 1 > > ForwardingToConstructorCache
Cache for constructors called through forwarding, e.g. make_unique.
Definition ParsedAST.h:130
llvm::StringRef version() const
Returns the version of the ParseInputs this AST was built from.
Definition ParsedAST.h:114
const include_cleaner::PragmaIncludes & getPragmaIncludes() const
Returns the PramaIncludes for preamble + main file includes.
const std::vector< PragmaMark > & getMarks() const
Gets all pragma marks in the main file.
SourceManager & getSourceManager()
Definition ParsedAST.h:78
ASTContext & getASTContext()
Note that the returned ast will not contain decls from the preamble that were not deserialized during...
const HeuristicResolver * getHeuristicResolver() const
Definition ParsedAST.h:123
const LangOptions & getLangOpts() const
Definition ParsedAST.h:85
static std::optional< ParsedAST > build(llvm::StringRef Filename, const ParseInputs &Inputs, std::unique_ptr< clang::CompilerInvocation > CI, llvm::ArrayRef< Diag > CompilerInvocationDiags, std::shared_ptr< const PreambleData > Preamble)
Attempts to run Clang and store the parsed AST.
llvm::ArrayRef< Diag > getDiagnostics() const
std::shared_ptr< Preprocessor > getPreprocessorPtr()
const syntax::TokenBuffer & getTokens() const
Tokens recorded while parsing the main file.
Definition ParsedAST.h:109
ParsedAST(const ParsedAST &Other)=delete
Preprocessor & getPreprocessor()
ArrayRef< Decl * > getLocalTopLevelDecls()
This function returns top-level decls present in the main file of the AST.
ParsedAST & operator=(ParsedAST &&Other)
const IncludeStructure & getIncludeStructure() const
const SourceManager & getSourceManager() const
Definition ParsedAST.h:81
ParsedAST(ParsedAST &&Other)
const MainFileMacros & getMacros() const
Gets all macro references (definition, expansions) present in the main file, including those in the p...
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:44
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition Path.h:29
std::string Path
A typedef to represent a file path.
Definition Path.h:26
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Information required to run clang, e.g. to parse AST or do code completion.
Definition Compiler.h:51