clang-tools 10.0.0

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 checks
17 //
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
22 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
23
24 #include "CollectMacros.h"
25 #include "Compiler.h"
26 #include "Diagnostics.h"
27 #include "Headers.h"
28 #include "Path.h"
29 #include "Preamble.h"
30 #include "index/CanonicalIncludes.h"
31 #include "clang/Frontend/FrontendAction.h"
32 #include "clang/Frontend/PrecompiledPreamble.h"
33 #include "clang/Lex/Preprocessor.h"
34 #include "clang/Tooling/CompilationDatabase.h"
35 #include "clang/Tooling/Syntax/Tokens.h"
36 #include "llvm/ADT/ArrayRef.h"
37 #include <memory>
38 #include <string>
39 #include <vector>
40
41 namespace clang {
42 namespace clangd {
43 class SymbolIndex;
44
45 /// Stores and provides access to parsed AST.
46 class ParsedAST {
47 public:
48 /// Attempts to run Clang and store parsed AST. If \p Preamble is non-null
49 /// it is reused during parsing.
50 static llvm::Optional<ParsedAST>
51 build(std::unique_ptr<clang::CompilerInvocation> CI,
52 llvm::ArrayRef<Diag> CompilerInvocationDiags,
53 std::shared_ptr<const PreambleData> Preamble,
54 std::unique_ptr<llvm::MemoryBuffer> Buffer,
55 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
56 const SymbolIndex *Index, const ParseOptions &Opts);
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 Preprocessor &getPreprocessor();
70 std::shared_ptr<Preprocessor> getPreprocessorPtr();
71 const Preprocessor &getPreprocessor() const;
72
73 SourceManager &getSourceManager() {
74 return getASTContext().getSourceManager();
75 }
76 const SourceManager &getSourceManager() const {
77 return getASTContext().getSourceManager();
78 }
79
80 const LangOptions &getLangOpts() const {
81 return getASTContext().getLangOpts();
82 }
83
84 /// This function returns top-level decls present in the main file of the AST.
85 /// The result does not include the decls that come from the preamble.
86 /// (These should be const, but RecursiveASTVisitor requires Decl*).
87 ArrayRef<Decl *> getLocalTopLevelDecls();
88
89 const std::vector<Diag> &getDiagnostics() const;
90
91 /// Returns the estimated size of the AST and the accessory structures, in
92 /// bytes. Does not include the size of the preamble.
93 std::size_t getUsedBytes() const;
94 const IncludeStructure &getIncludeStructure() const;
95 const CanonicalIncludes &getCanonicalIncludes() const;
96
97 /// Gets all macro references (definition, expansions) present in the main
98 /// file, including those in the preamble region.
99 const MainFileMacros &getMacros() const;
100 /// Tokens recorded while parsing the main file.
101 /// (!) does not have tokens from the preamble.
102 const syntax::TokenBuffer &getTokens() const { return Tokens; }
103
104 private:
105 ParsedAST(std::shared_ptr<const PreambleData> Preamble,
106 std::unique_ptr<CompilerInstance> Clang,
107 std::unique_ptr<FrontendAction> Action, syntax::TokenBuffer Tokens,
108 MainFileMacros Macros, std::vector<Decl *> LocalTopLevelDecls,
109 std::vector<Diag> Diags, IncludeStructure Includes,
110 CanonicalIncludes CanonIncludes);
111
112 // In-memory preambles must outlive the AST, it is important that this member
113 // goes before Clang and Action.
114 std::shared_ptr<const PreambleData> Preamble;
115 // We store an "incomplete" FrontendAction (i.e. no EndSourceFile was called
116 // on it) and CompilerInstance used to run it. That way we don't have to do
117 // complex memory management of all Clang structures on our own. (They are
118 // stored in CompilerInstance and cleaned up by
119 // FrontendAction.EndSourceFile).
120 std::unique_ptr<CompilerInstance> Clang;
121 std::unique_ptr<FrontendAction> Action;
122 /// Tokens recorded after the preamble finished.
123 /// - Includes all spelled tokens for the main file.
124 /// - Includes expanded tokens produced **after** preabmle.
125 /// - Does not have spelled or expanded tokens for files from preamble.
126 syntax::TokenBuffer Tokens;
127
128 /// All macro definitions and expansions in the main file.
129 MainFileMacros Macros;
130 // Data, stored after parsing.
131 std::vector<Diag> Diags;
132 // Top-level decls inside the current file. Not that this does not include
133 // top-level decls from the preamble.
134 std::vector<Decl *> LocalTopLevelDecls;
135 IncludeStructure Includes;
136 CanonicalIncludes CanonIncludes;
137 };
138
139 /// Build an AST from provided user inputs. This function does not check if
140 /// preamble can be reused, as this function expects that \p Preamble is the
141 /// result of calling buildPreamble.
142 llvm::Optional<ParsedAST>
143 buildAST(PathRef FileName, std::unique_ptr<CompilerInvocation> Invocation,
144 llvm::ArrayRef<Diag> CompilerInvocationDiags,
145 const ParseInputs &Inputs,
146 std::shared_ptr<const PreambleData> Preamble);
147
148 /// For testing/debugging purposes. Note that this method deserializes all
149 /// unserialized Decls, so use with care.
150 void dumpAST(ParsedAST &AST, llvm::raw_ostream &OS);
151
152 } // namespace clangd
153 } // namespace clang
154
155 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
clang::clangd::ParsedAST::~ParsedAST
~ParsedAST()
Definition: ParsedAST.cpp:413
clang::clangd::ParsedAST::operator=
ParsedAST & operator=(ParsedAST &&Other)
clang::clangd::ParsedAST::getLangOpts
const LangOptions & getLangOpts() const
Definition: ParsedAST.h:80
clang::clangd::buildAST
llvm::Optional< ParsedAST > buildAST(PathRef FileName, std::unique_ptr< CompilerInvocation > Invocation, llvm::ArrayRef< Diag > CompilerInvocationDiags, const ParseInputs &Inputs, std::shared_ptr< const PreambleData > Preamble)
Build an AST from provided user inputs.
Definition: ParsedAST.cpp:505
clang::clangd::ParsedAST::getPreprocessor
Preprocessor & getPreprocessor()
Definition: ParsedAST.cpp:430
VFS
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS
Definition: CodeComplete.cpp:1023
clang::clangd::dumpAST
void dumpAST(ParsedAST &AST, llvm::raw_ostream &OS)
For testing/debugging purposes.
Definition: ParsedAST.cpp:213
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:85
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:23
clang::clangd::ParsedAST::getLocalTopLevelDecls
ArrayRef< Decl * > getLocalTopLevelDecls()
This function returns top-level decls present in the main file of the AST.
Definition: ParsedAST.cpp:440
clang::clangd::IncludeStructure
Definition: Headers.h:109
clang::clangd::MainFileMacros
Definition: CollectMacros.h:24
Headers.h
clang::clangd::ParsedAST::getTokens
const syntax::TokenBuffer & getTokens() const
Tokens recorded while parsing the main file.
Definition: ParsedAST.h:102
CanonicalIncludes.h
clang::clangd::ParsedAST::getASTContext
ASTContext & getASTContext()
Note that the returned ast will not contain decls from the preamble that were not deserialized during...
Definition: ParsedAST.cpp:424
clang::clangd::ParsedAST::getUsedBytes
std::size_t getUsedBytes() const
Returns the estimated size of the AST and the accessory structures, in bytes.
Definition: ParsedAST.cpp:448
clang::clangd::CanonicalIncludes
Maps a definition location onto an #include file, based on a set of filename rules.
Definition: CanonicalIncludes.h:36
clang::clangd::ParsedAST::getCanonicalIncludes
const CanonicalIncludes & getCanonicalIncludes() const
Definition: ParsedAST.cpp:484
clang::clangd::ParsedAST::getIncludeStructure
const IncludeStructure & getIncludeStructure() const
Definition: ParsedAST.cpp:480
clang::clangd::SymbolOrigin::AST
Preamble.h
clang::clangd::ParsedAST::getSourceManager
const SourceManager & getSourceManager() const
Definition: ParsedAST.h:76
clang::clangd::ParseOptions
Definition: Compiler.h:38
Compiler.h
Diagnostics.h
clang::clangd::ParsedAST::getPreprocessorPtr
std::shared_ptr< Preprocessor > getPreprocessorPtr()
Definition: ParsedAST.cpp:432
FileName
PathRef FileName
Definition: CodeComplete.cpp:1018
clang::clangd::ParsedAST
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
clang::clangd::ParsedAST::getSourceManager
SourceManager & getSourceManager()
Definition: ParsedAST.h:73
clang::clangd::ParsedAST::getMacros
const MainFileMacros & getMacros() const
Gets all macro references (definition, expansions) present in the main file, including those in the p...
Definition: ParsedAST.cpp:444
clang::clangd::ParseInputs
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:44
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Path.h
CollectMacros.h
clang::clangd::ParsedAST::build
static llvm::Optional< ParsedAST > build(std::unique_ptr< clang::CompilerInvocation > CI, llvm::ArrayRef< Diag > CompilerInvocationDiags, std::shared_ptr< const PreambleData > Preamble, std::unique_ptr< llvm::MemoryBuffer > Buffer, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, const SymbolIndex *Index, const ParseOptions &Opts)
Attempts to run Clang and store parsed AST.
Definition: ParsedAST.cpp:218
clang::clangd::ParsedAST::ParsedAST
ParsedAST(ParsedAST &&Other)
clang::clangd::ParsedAST::getDiagnostics
const std::vector< Diag > & getDiagnostics() const
Definition: ParsedAST.cpp:446
Index
const SymbolIndex * Index
Definition: Dexp.cpp:84