clang-tools 8.0.0

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