clang-tools  16.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/Lex/Preprocessor.h"
32 #include "clang/Tooling/Syntax/Tokens.h"
33 #include "llvm/ADT/ArrayRef.h"
34 #include "llvm/ADT/Optional.h"
35 #include "llvm/ADT/StringRef.h"
36 #include <memory>
37 #include <string>
38 #include <vector>
39 
40 namespace clang {
41 class Sema;
42 namespace clangd {
43 class HeuristicResolver;
44 
45 /// Stores and provides access to parsed AST.
46 class ParsedAST {
47 public:
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 llvm::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);
56 
57  ParsedAST(ParsedAST &&Other);
58  ParsedAST &operator=(ParsedAST &&Other);
59 
60  ~ParsedAST();
61 
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;
67 
68  Sema &getSema();
69 
70  Preprocessor &getPreprocessor();
71  std::shared_ptr<Preprocessor> getPreprocessorPtr();
72  const Preprocessor &getPreprocessor() const;
73 
74  SourceManager &getSourceManager() {
75  return getASTContext().getSourceManager();
76  }
77  const SourceManager &getSourceManager() const {
78  return getASTContext().getSourceManager();
79  }
80 
81  const LangOptions &getLangOpts() const {
82  return getASTContext().getLangOpts();
83  }
84 
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 
90  const llvm::Optional<std::vector<Diag>> &getDiagnostics() const {
91  return Diags;
92  }
93 
94  /// Returns the estimated size of the AST and the accessory structures, in
95  /// bytes. Does not include the size of the preamble.
96  std::size_t getUsedBytes() const;
99 
100  /// Gets all macro references (definition, expansions) present in the main
101  /// file, including those in the preamble region.
102  const MainFileMacros &getMacros() const;
103  /// Gets all pragma marks in the main file.
104  const std::vector<PragmaMark> &getMarks() const;
105  /// Tokens recorded while parsing the main file.
106  /// (!) does not have tokens from the preamble.
107  const syntax::TokenBuffer &getTokens() const { return Tokens; }
108 
109  /// Returns the version of the ParseInputs this AST was built from.
110  llvm::StringRef version() const { return Version; }
111 
112  /// Returns the path passed by the caller when building this AST.
113  PathRef tuPath() const { return TUPath; }
114 
115  /// Returns the version of the ParseInputs used to build Preamble part of this
116  /// AST. Might be None if no Preamble is used.
117  llvm::Optional<llvm::StringRef> preambleVersion() const;
118 
120  return Resolver.get();
121  }
122 
123 private:
124  ParsedAST(PathRef TUPath, llvm::StringRef Version,
125  std::shared_ptr<const PreambleData> Preamble,
126  std::unique_ptr<CompilerInstance> Clang,
127  std::unique_ptr<FrontendAction> Action, syntax::TokenBuffer Tokens,
128  MainFileMacros Macros, std::vector<PragmaMark> Marks,
129  std::vector<Decl *> LocalTopLevelDecls,
130  llvm::Optional<std::vector<Diag>> Diags, IncludeStructure Includes,
131  CanonicalIncludes CanonIncludes);
132 
133  Path TUPath;
134  std::string Version;
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;
150 
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  // Data, stored after parsing. None if AST was built with a stale preamble.
156  llvm::Optional<std::vector<Diag>> Diags;
157  // Top-level decls inside the current file. Not that this does not include
158  // top-level decls from the preamble.
159  std::vector<Decl *> LocalTopLevelDecls;
160  IncludeStructure Includes;
161  CanonicalIncludes CanonIncludes;
162  std::unique_ptr<HeuristicResolver> Resolver;
163 };
164 
165 } // namespace clangd
166 } // namespace clang
167 
168 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
Headers.h
clang::clangd::Path
std::string Path
A typedef to represent a file path.
Definition: Path.h:26
clang::clangd::ParsedAST::getPreprocessorPtr
std::shared_ptr< Preprocessor > getPreprocessorPtr()
Definition: ParsedAST.cpp:710
Path.h
clang::clangd::ParsedAST::getIncludeStructure
const IncludeStructure & getIncludeStructure() const
Definition: ParsedAST.cpp:757
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:700
CI
std::unique_ptr< CompilerInvocation > CI
Definition: TUScheduler.cpp:549
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:40
clang::clangd::ParsedAST::getCanonicalIncludes
const CanonicalIncludes & getCanonicalIncludes() const
Definition: ParsedAST.cpp:761
Preamble.h
clang::clangd::ParsedAST::getSema
Sema & getSema()
Definition: ParsedAST.cpp:706
clang::clangd::HeuristicResolver
Definition: HeuristicResolver.h:45
clang::clangd::ParsedAST::getLangOpts
const LangOptions & getLangOpts() const
Definition: ParsedAST.h:81
Preamble
const PreambleData & Preamble
Definition: CodeComplete.cpp:1214
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:342
clang::clangd::IncludeStructure
Definition: Headers.h:119
Inputs
ParseInputs Inputs
Definition: TUScheduler.cpp:550
clang::clangd::ParseInputs
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:46
CanonicalIncludes.h
clang::clangd::ParsedAST::version
llvm::StringRef version() const
Returns the version of the ParseInputs this AST was built from.
Definition: ParsedAST.h:110
clang::clangd::CanonicalIncludes
Maps a definition location onto an #include file, based on a set of filename rules.
Definition: CanonicalIncludes.h:37
CollectMacros.h
clang::clangd::MainFileMacros
Definition: CollectMacros.h:29
clang::clangd::ParsedAST::operator=
ParsedAST & operator=(ParsedAST &&Other)
Diagnostics.h
clang::clangd::ParsedAST::~ParsedAST
~ParsedAST()
Definition: ParsedAST.cpp:689
clang::clangd::ParsedAST::getHeuristicResolver
const HeuristicResolver * getHeuristicResolver() const
Definition: ParsedAST.h:119
clang::clangd::ParsedAST::getPreprocessor
Preprocessor & getPreprocessor()
Definition: ParsedAST.cpp:708
clang::clangd::ParsedAST::tuPath
PathRef tuPath() const
Returns the path passed by the caller when building this AST.
Definition: ParsedAST.h:113
clang::clangd::ParsedAST::getDiagnostics
const llvm::Optional< std::vector< Diag > > & getDiagnostics() const
Definition: ParsedAST.h:90
Compiler.h
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:29
clang::clangd::ParsedAST::getMarks
const std::vector< PragmaMark > & getMarks() const
Gets all pragma marks in the main file.
Definition: ParsedAST.cpp:723
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:107
clang::clangd::ParsedAST::getLocalTopLevelDecls
ArrayRef< Decl * > getLocalTopLevelDecls()
This function returns top-level decls present in the main file of the AST.
Definition: ParsedAST.cpp:718
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:725
clang::clangd::ParsedAST
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
clang::clangd::ParsedAST::getSourceManager
SourceManager & getSourceManager()
Definition: ParsedAST.h:74
clang::clangd::ParsedAST::getSourceManager
const SourceManager & getSourceManager() const
Definition: ParsedAST.h:77
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:785
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:722