clang-tools  14.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"
29 #include "support/Path.h"
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 "llvm/ADT/None.h"
37 #include "llvm/ADT/Optional.h"
38 #include "llvm/ADT/StringRef.h"
39 #include <memory>
40 #include <string>
41 #include <vector>
42 
43 namespace clang {
44 namespace clangd {
45 class HeuristicResolver;
46 class SymbolIndex;
47 
48 /// Stores and provides access to parsed AST.
49 class ParsedAST {
50 public:
51  /// Attempts to run Clang and store the parsed AST.
52  /// If \p Preamble is non-null it is reused during parsing.
53  /// This function does not check if preamble is valid to reuse.
54  static llvm::Optional<ParsedAST>
55  build(llvm::StringRef Filename, const ParseInputs &Inputs,
56  std::unique_ptr<clang::CompilerInvocation> CI,
57  llvm::ArrayRef<Diag> CompilerInvocationDiags,
58  std::shared_ptr<const PreambleData> Preamble);
59 
60  ParsedAST(ParsedAST &&Other);
61  ParsedAST &operator=(ParsedAST &&Other);
62 
63  ~ParsedAST();
64 
65  /// Note that the returned ast will not contain decls from the preamble that
66  /// were not deserialized during parsing. Clients should expect only decls
67  /// from the main file to be in the AST.
68  ASTContext &getASTContext();
69  const ASTContext &getASTContext() const;
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 
91  const llvm::Optional<std::vector<Diag>> &getDiagnostics() const {
92  return Diags;
93  }
94 
95  /// Returns the estimated size of the AST and the accessory structures, in
96  /// bytes. Does not include the size of the preamble.
97  std::size_t getUsedBytes() const;
100 
101  /// Gets all macro references (definition, expansions) present in the main
102  /// file, including those in the preamble region.
103  const MainFileMacros &getMacros() const;
104  /// Gets all pragma marks in the main file.
105  const std::vector<PragmaMark> &getMarks() const;
106  /// Tokens recorded while parsing the main file.
107  /// (!) does not have tokens from the preamble.
108  const syntax::TokenBuffer &getTokens() const { return Tokens; }
109 
110  /// Returns the version of the ParseInputs this AST was built from.
111  llvm::StringRef version() const { return Version; }
112 
113  /// Returns the version of the ParseInputs used to build Preamble part of this
114  /// AST. Might be None if no Preamble is used.
115  llvm::Optional<llvm::StringRef> preambleVersion() const;
116 
118  return Resolver.get();
119  }
120 
121 private:
122  ParsedAST(llvm::StringRef Version,
123  std::shared_ptr<const PreambleData> Preamble,
124  std::unique_ptr<CompilerInstance> Clang,
125  std::unique_ptr<FrontendAction> Action, syntax::TokenBuffer Tokens,
126  MainFileMacros Macros, std::vector<PragmaMark> Marks,
127  std::vector<Decl *> LocalTopLevelDecls,
128  llvm::Optional<std::vector<Diag>> Diags, IncludeStructure Includes,
129  CanonicalIncludes CanonIncludes);
130 
131  std::string Version;
132  // In-memory preambles must outlive the AST, it is important that this member
133  // goes before Clang and Action.
134  std::shared_ptr<const PreambleData> Preamble;
135  // We store an "incomplete" FrontendAction (i.e. no EndSourceFile was called
136  // on it) and CompilerInstance used to run it. That way we don't have to do
137  // complex memory management of all Clang structures on our own. (They are
138  // stored in CompilerInstance and cleaned up by
139  // FrontendAction.EndSourceFile).
140  std::unique_ptr<CompilerInstance> Clang;
141  std::unique_ptr<FrontendAction> Action;
142  /// Tokens recorded after the preamble finished.
143  /// - Includes all spelled tokens for the main file.
144  /// - Includes expanded tokens produced **after** preamble.
145  /// - Does not have spelled or expanded tokens for files from preamble.
146  syntax::TokenBuffer Tokens;
147 
148  /// All macro definitions and expansions in the main file.
149  MainFileMacros Macros;
150  // Pragma marks in the main file.
151  std::vector<PragmaMark> Marks;
152  // Data, stored after parsing. None if AST was built with a stale preamble.
153  llvm::Optional<std::vector<Diag>> Diags;
154  // Top-level decls inside the current file. Not that this does not include
155  // top-level decls from the preamble.
156  std::vector<Decl *> LocalTopLevelDecls;
157  IncludeStructure Includes;
158  CanonicalIncludes CanonIncludes;
159  std::unique_ptr<HeuristicResolver> Resolver;
160 };
161 
162 } // namespace clangd
163 } // namespace clang
164 
165 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
Headers.h
clang::clangd::ParsedAST::getPreprocessorPtr
std::shared_ptr< Preprocessor > getPreprocessorPtr()
Definition: ParsedAST.cpp:640
Path.h
clang::clangd::ParsedAST::getIncludeStructure
const IncludeStructure & getIncludeStructure() const
Definition: ParsedAST.cpp:687
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:632
CI
std::unique_ptr< CompilerInvocation > CI
Definition: TUScheduler.cpp:456
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:39
clang::clangd::ParsedAST::getCanonicalIncludes
const CanonicalIncludes & getCanonicalIncludes() const
Definition: ParsedAST.cpp:691
Preamble.h
clang::clangd::HeuristicResolver
Definition: HeuristicResolver.h:46
clang::clangd::ParsedAST::getLangOpts
const LangOptions & getLangOpts() const
Definition: ParsedAST.h:82
Preamble
const PreambleData & Preamble
Definition: CodeComplete.cpp:1193
clang::clangd::ParsedAST::ParsedAST
ParsedAST(ParsedAST &&Other)
clang::clangd::ParsedAST::build
static llvm::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.
Definition: ParsedAST.cpp:288
clang::clangd::IncludeStructure
Definition: Headers.h:192
Inputs
ParseInputs Inputs
Definition: TUScheduler.cpp:457
clang::clangd::ParseInputs
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:47
CanonicalIncludes.h
clang::clangd::ParsedAST::version
llvm::StringRef version() const
Returns the version of the ParseInputs this AST was built from.
Definition: ParsedAST.h:111
clang::clangd::CanonicalIncludes
Maps a definition location onto an #include file, based on a set of filename rules.
Definition: CanonicalIncludes.h:36
CollectMacros.h
clang::clangd::MainFileMacros
Definition: CollectMacros.h:31
clang::clangd::ParsedAST::operator=
ParsedAST & operator=(ParsedAST &&Other)
Diagnostics.h
clang::clangd::ParsedAST::~ParsedAST
~ParsedAST()
Definition: ParsedAST.cpp:621
clang::clangd::ParsedAST::getHeuristicResolver
const HeuristicResolver * getHeuristicResolver() const
Definition: ParsedAST.h:117
clang::clangd::ParsedAST::getPreprocessor
Preprocessor & getPreprocessor()
Definition: ParsedAST.cpp:638
clang::clangd::ParsedAST::getDiagnostics
const llvm::Optional< std::vector< Diag > > & getDiagnostics() const
Definition: ParsedAST.h:91
Compiler.h
clang::clangd::ParsedAST::getMarks
const std::vector< PragmaMark > & getMarks() const
Gets all pragma marks in the main file.
Definition: ParsedAST.cpp:653
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::ParsedAST::getTokens
const syntax::TokenBuffer & getTokens() const
Tokens recorded while parsing the main file.
Definition: ParsedAST.h:108
clang::clangd::ParsedAST::getLocalTopLevelDecls
ArrayRef< Decl * > getLocalTopLevelDecls()
This function returns top-level decls present in the main file of the AST.
Definition: ParsedAST.cpp:648
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:655
clang::clangd::ParsedAST
Stores and provides access to parsed AST.
Definition: ParsedAST.h:49
clang::clangd::ParsedAST::getSourceManager
SourceManager & getSourceManager()
Definition: ParsedAST.h:75
clang::clangd::ParsedAST::getSourceManager
const SourceManager & getSourceManager() const
Definition: ParsedAST.h:78
clang::clangd::ParsedAST::preambleVersion
llvm::Optional< llvm::StringRef > preambleVersion() const
Returns the version of the ParseInputs used to build Preamble part of this AST.
Definition: ParsedAST.cpp:715
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:652