Line data Source code
1 : //===--- ClangdUnit.h -------------------------------------------*- C++-*-===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===---------------------------------------------------------------------===//
9 :
10 : #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H
11 : #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H
12 :
13 : #include "Diagnostics.h"
14 : #include "Function.h"
15 : #include "Headers.h"
16 : #include "Path.h"
17 : #include "Protocol.h"
18 : #include "clang/Frontend/FrontendAction.h"
19 : #include "clang/Frontend/PrecompiledPreamble.h"
20 : #include "clang/Lex/Preprocessor.h"
21 : #include "clang/Serialization/ASTBitCodes.h"
22 : #include "clang/Tooling/CompilationDatabase.h"
23 : #include "clang/Tooling/Core/Replacement.h"
24 : #include <memory>
25 : #include <string>
26 : #include <vector>
27 :
28 : namespace llvm {
29 : class raw_ostream;
30 : }
31 :
32 : namespace clang {
33 : class PCHContainerOperations;
34 :
35 : namespace vfs {
36 : class FileSystem;
37 : }
38 :
39 : namespace tooling {
40 : struct CompileCommand;
41 : }
42 :
43 : namespace clangd {
44 :
45 : // Stores Preamble and associated data.
46 : struct PreambleData {
47 : PreambleData(PrecompiledPreamble Preamble, std::vector<Diag> Diags,
48 : IncludeStructure Includes);
49 :
50 : tooling::CompileCommand CompileCommand;
51 : PrecompiledPreamble Preamble;
52 : std::vector<Diag> Diags;
53 : // Processes like code completions and go-to-definitions will need #include
54 : // information, and their compile action skips preamble range.
55 : IncludeStructure Includes;
56 : };
57 :
58 : /// Information required to run clang, e.g. to parse AST or do code completion.
59 0 : struct ParseInputs {
60 : tooling::CompileCommand CompileCommand;
61 : IntrusiveRefCntPtr<vfs::FileSystem> FS;
62 : std::string Contents;
63 : };
64 :
65 : /// Stores and provides access to parsed AST.
66 : class ParsedAST {
67 : public:
68 : /// Attempts to run Clang and store parsed AST. If \p Preamble is non-null
69 : /// it is reused during parsing.
70 : static llvm::Optional<ParsedAST>
71 : Build(std::unique_ptr<clang::CompilerInvocation> CI,
72 : std::shared_ptr<const PreambleData> Preamble,
73 : std::unique_ptr<llvm::MemoryBuffer> Buffer,
74 : std::shared_ptr<PCHContainerOperations> PCHs,
75 : IntrusiveRefCntPtr<vfs::FileSystem> VFS);
76 :
77 : ParsedAST(ParsedAST &&Other);
78 : ParsedAST &operator=(ParsedAST &&Other);
79 :
80 : ~ParsedAST();
81 :
82 : /// Note that the returned ast will not contain decls from the preamble that
83 : /// were not deserialized during parsing. Clients should expect only decls
84 : /// from the main file to be in the AST.
85 : ASTContext &getASTContext();
86 : const ASTContext &getASTContext() const;
87 :
88 : Preprocessor &getPreprocessor();
89 : std::shared_ptr<Preprocessor> getPreprocessorPtr();
90 : const Preprocessor &getPreprocessor() const;
91 :
92 : /// This function returns top-level decls present in the main file of the AST.
93 : /// The result does not include the decls that come from the preamble.
94 : /// (These should be const, but RecursiveASTVisitor requires Decl*).
95 : ArrayRef<Decl *> getLocalTopLevelDecls();
96 :
97 : const std::vector<Diag> &getDiagnostics() const;
98 :
99 : /// Returns the esitmated size of the AST and the accessory structures, in
100 : /// bytes. Does not include the size of the preamble.
101 : std::size_t getUsedBytes() const;
102 : const IncludeStructure &getIncludeStructure() const;
103 :
104 : private:
105 : ParsedAST(std::shared_ptr<const PreambleData> Preamble,
106 : std::unique_ptr<CompilerInstance> Clang,
107 : std::unique_ptr<FrontendAction> Action,
108 : std::vector<Decl *> LocalTopLevelDecls, std::vector<Diag> Diags,
109 : IncludeStructure Includes);
110 :
111 : // In-memory preambles must outlive the AST, it is important that this member
112 : // goes before Clang and Action.
113 : std::shared_ptr<const PreambleData> Preamble;
114 : // We store an "incomplete" FrontendAction (i.e. no EndSourceFile was called
115 : // on it) and CompilerInstance used to run it. That way we don't have to do
116 : // complex memory management of all Clang structures on our own. (They are
117 : // stored in CompilerInstance and cleaned up by
118 : // FrontendAction.EndSourceFile).
119 : std::unique_ptr<CompilerInstance> Clang;
120 : std::unique_ptr<FrontendAction> Action;
121 :
122 : // Data, stored after parsing.
123 : std::vector<Diag> Diags;
124 : // Top-level decls inside the current file. Not that this does not include
125 : // top-level decls from the preamble.
126 : std::vector<Decl *> LocalTopLevelDecls;
127 : IncludeStructure Includes;
128 : };
129 :
130 : using PreambleParsedCallback = std::function<void(
131 : PathRef Path, ASTContext &, std::shared_ptr<clang::Preprocessor>)>;
132 :
133 : /// Builds compiler invocation that could be used to build AST or preamble.
134 : std::unique_ptr<CompilerInvocation>
135 : buildCompilerInvocation(const ParseInputs &Inputs);
136 :
137 : /// Rebuild the preamble for the new inputs unless the old one can be reused.
138 : /// If \p OldPreamble can be reused, it is returned unchanged.
139 : /// If \p OldPreamble is null, always builds the preamble.
140 : /// If \p PreambleCallback is set, it will be run on top of the AST while
141 : /// building the preamble. Note that if the old preamble was reused, no AST is
142 : /// built and, therefore, the callback will not be executed.
143 : std::shared_ptr<const PreambleData>
144 : buildPreamble(PathRef FileName, CompilerInvocation &CI,
145 : std::shared_ptr<const PreambleData> OldPreamble,
146 : const tooling::CompileCommand &OldCompileCommand,
147 : const ParseInputs &Inputs,
148 : std::shared_ptr<PCHContainerOperations> PCHs, bool StoreInMemory,
149 : PreambleParsedCallback PreambleCallback);
150 :
151 : /// Build an AST from provided user inputs. This function does not check if
152 : /// preamble can be reused, as this function expects that \p Preamble is the
153 : /// result of calling buildPreamble.
154 : llvm::Optional<ParsedAST>
155 : buildAST(PathRef FileName, std::unique_ptr<CompilerInvocation> Invocation,
156 : const ParseInputs &Inputs,
157 : std::shared_ptr<const PreambleData> Preamble,
158 : std::shared_ptr<PCHContainerOperations> PCHs);
159 :
160 : /// Get the beginning SourceLocation at a specified \p Pos.
161 : /// May be invalid if Pos is, or if there's no identifier.
162 : SourceLocation getBeginningOfIdentifier(ParsedAST &Unit, const Position &Pos,
163 : const FileID FID);
164 :
165 : /// For testing/debugging purposes. Note that this method deserializes all
166 : /// unserialized Decls, so use with care.
167 : void dumpAST(ParsedAST &AST, llvm::raw_ostream &OS);
168 :
169 : } // namespace clangd
170 : } // namespace clang
171 : #endif
|