[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / clang-tools-extra / clangd / ParsedAST.h
blob307100ae0fc4942004255ab8a1161301784eca84
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.
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
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
21 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
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 "index/CanonicalIncludes.h"
30 #include "support/Path.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>
41 namespace clang {
42 class Sema;
43 namespace clangd {
44 class HeuristicResolver;
46 /// Stores and provides access to parsed AST.
47 class ParsedAST {
48 public:
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);
58 ParsedAST(ParsedAST &&Other);
59 ParsedAST &operator=(ParsedAST &&Other);
61 ~ParsedAST();
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;
69 Sema &getSema();
71 Preprocessor &getPreprocessor();
72 std::shared_ptr<Preprocessor> getPreprocessorPtr();
73 const Preprocessor &getPreprocessor() const;
75 SourceManager &getSourceManager() {
76 return getASTContext().getSourceManager();
78 const SourceManager &getSourceManager() const {
79 return getASTContext().getSourceManager();
82 const LangOptions &getLangOpts() const {
83 return getASTContext().getLangOpts();
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 ArrayRef<const Decl *> getLocalTopLevelDecls() const;
92 llvm::ArrayRef<Diag> getDiagnostics() const;
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;
97 const IncludeStructure &getIncludeStructure() const;
98 const CanonicalIncludes &getCanonicalIncludes() const;
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 /// Returns the PramaIncludes from the preamble.
109 /// Might be null if AST is built without a preamble.
110 const include_cleaner::PragmaIncludes *getPragmaIncludes() const;
112 /// Returns the version of the ParseInputs this AST was built from.
113 llvm::StringRef version() const { return Version; }
115 /// Returns the path passed by the caller when building this AST.
116 PathRef tuPath() const { return TUPath; }
118 /// Returns the version of the ParseInputs used to build Preamble part of this
119 /// AST. Might be std::nullopt if no Preamble is used.
120 std::optional<llvm::StringRef> preambleVersion() const;
122 const HeuristicResolver *getHeuristicResolver() const {
123 return Resolver.get();
126 private:
127 ParsedAST(PathRef TUPath, llvm::StringRef Version,
128 std::shared_ptr<const PreambleData> Preamble,
129 std::unique_ptr<CompilerInstance> Clang,
130 std::unique_ptr<FrontendAction> Action, syntax::TokenBuffer Tokens,
131 MainFileMacros Macros, std::vector<PragmaMark> Marks,
132 std::vector<Decl *> LocalTopLevelDecls, std::vector<Diag> Diags,
133 IncludeStructure Includes, CanonicalIncludes CanonIncludes);
135 Path TUPath;
136 std::string Version;
137 // In-memory preambles must outlive the AST, it is important that this member
138 // goes before Clang and Action.
139 std::shared_ptr<const PreambleData> Preamble;
140 // We store an "incomplete" FrontendAction (i.e. no EndSourceFile was called
141 // on it) and CompilerInstance used to run it. That way we don't have to do
142 // complex memory management of all Clang structures on our own. (They are
143 // stored in CompilerInstance and cleaned up by
144 // FrontendAction.EndSourceFile).
145 std::unique_ptr<CompilerInstance> Clang;
146 std::unique_ptr<FrontendAction> Action;
147 /// Tokens recorded after the preamble finished.
148 /// - Includes all spelled tokens for the main file.
149 /// - Includes expanded tokens produced **after** preamble.
150 /// - Does not have spelled or expanded tokens for files from preamble.
151 syntax::TokenBuffer Tokens;
153 /// All macro definitions and expansions in the main file.
154 MainFileMacros Macros;
155 // Pragma marks in the main file.
156 std::vector<PragmaMark> Marks;
157 // Diags emitted while parsing this AST (including preamble and compiler
158 // invocation).
159 std::vector<Diag> Diags;
160 // Top-level decls inside the current file. Not that this does not include
161 // top-level decls from the preamble.
162 std::vector<Decl *> LocalTopLevelDecls;
163 IncludeStructure Includes;
164 CanonicalIncludes CanonIncludes;
165 std::unique_ptr<HeuristicResolver> Resolver;
168 } // namespace clangd
169 } // namespace clang
171 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H