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  /// Tokens recorded while parsing the main file.
105  /// (!) does not have tokens from the preamble.
106  const syntax::TokenBuffer &getTokens() const { return Tokens; }
107 
108  /// Returns the version of the ParseInputs this AST was built from.
109  llvm::StringRef version() const { return Version; }
110 
111  /// Returns the version of the ParseInputs used to build Preamble part of this
112  /// AST. Might be None if no Preamble is used.
113  llvm::Optional<llvm::StringRef> preambleVersion() const;
114 
116  return Resolver.get();
117  }
118 
119 private:
120  ParsedAST(llvm::StringRef Version,
121  std::shared_ptr<const PreambleData> Preamble,
122  std::unique_ptr<CompilerInstance> Clang,
123  std::unique_ptr<FrontendAction> Action, syntax::TokenBuffer Tokens,
124  MainFileMacros Macros, std::vector<Decl *> LocalTopLevelDecls,
125  llvm::Optional<std::vector<Diag>> Diags, IncludeStructure Includes,
126  CanonicalIncludes CanonIncludes);
127 
128  std::string Version;
129  // In-memory preambles must outlive the AST, it is important that this member
130  // goes before Clang and Action.
131  std::shared_ptr<const PreambleData> Preamble;
132  // We store an "incomplete" FrontendAction (i.e. no EndSourceFile was called
133  // on it) and CompilerInstance used to run it. That way we don't have to do
134  // complex memory management of all Clang structures on our own. (They are
135  // stored in CompilerInstance and cleaned up by
136  // FrontendAction.EndSourceFile).
137  std::unique_ptr<CompilerInstance> Clang;
138  std::unique_ptr<FrontendAction> Action;
139  /// Tokens recorded after the preamble finished.
140  /// - Includes all spelled tokens for the main file.
141  /// - Includes expanded tokens produced **after** preamble.
142  /// - Does not have spelled or expanded tokens for files from preamble.
143  syntax::TokenBuffer Tokens;
144 
145  /// All macro definitions and expansions in the main file.
146  MainFileMacros Macros;
147  // Data, stored after parsing. None if AST was built with a stale preamble.
148  llvm::Optional<std::vector<Diag>> Diags;
149  // Top-level decls inside the current file. Not that this does not include
150  // top-level decls from the preamble.
151  std::vector<Decl *> LocalTopLevelDecls;
152  IncludeStructure Includes;
153  CanonicalIncludes CanonIncludes;
154  std::unique_ptr<HeuristicResolver> Resolver;
155 };
156 
157 } // namespace clangd
158 } // namespace clang
159 
160 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
Headers.h
clang::clangd::ParsedAST::getPreprocessorPtr
std::shared_ptr< Preprocessor > getPreprocessorPtr()
Definition: ParsedAST.cpp:540
Path.h
clang::clangd::ParsedAST::getIncludeStructure
const IncludeStructure & getIncludeStructure() const
Definition: ParsedAST.cpp:586
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:532
CI
std::unique_ptr< CompilerInvocation > CI
Definition: TUScheduler.cpp:450
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:39
clang::clangd::ParsedAST::getCanonicalIncludes
const CanonicalIncludes & getCanonicalIncludes() const
Definition: ParsedAST.cpp:590
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:1104
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:248
clang::clangd::IncludeStructure
Definition: Headers.h:113
Inputs
ParseInputs Inputs
Definition: TUScheduler.cpp:451
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:109
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:521
clang::clangd::ParsedAST::getHeuristicResolver
const HeuristicResolver * getHeuristicResolver() const
Definition: ParsedAST.h:115
clang::clangd::ParsedAST::getPreprocessor
Preprocessor & getPreprocessor()
Definition: ParsedAST.cpp:538
clang::clangd::ParsedAST::getDiagnostics
const llvm::Optional< std::vector< Diag > > & getDiagnostics() const
Definition: ParsedAST.h:91
Compiler.h
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:106
clang::clangd::ParsedAST::getLocalTopLevelDecls
ArrayRef< Decl * > getLocalTopLevelDecls()
This function returns top-level decls present in the main file of the AST.
Definition: ParsedAST.cpp:548
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:554
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:612
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:552