//===--- ClangdUnit.h --------------------------------------------*- C++-*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H #include "Diagnostics.h" #include "FS.h" #include "Function.h" #include "Headers.h" #include "Path.h" #include "Protocol.h" #include "clang/Frontend/FrontendAction.h" #include "clang/Frontend/PrecompiledPreamble.h" #include "clang/Lex/Preprocessor.h" #include "clang/Serialization/ASTBitCodes.h" #include "clang/Tooling/CompilationDatabase.h" #include "clang/Tooling/Core/Replacement.h" #include #include #include namespace llvm { class raw_ostream; namespace vfs { class FileSystem; } } // namespace llvm namespace clang { class PCHContainerOperations; namespace tooling { struct CompileCommand; } namespace clangd { // Stores Preamble and associated data. struct PreambleData { PreambleData(PrecompiledPreamble Preamble, std::vector Diags, IncludeStructure Includes, std::unique_ptr StatCache); tooling::CompileCommand CompileCommand; PrecompiledPreamble Preamble; std::vector Diags; // Processes like code completions and go-to-definitions will need #include // information, and their compile action skips preamble range. IncludeStructure Includes; // Cache of FS operations performed when building the preamble. // When reusing a preamble, this cache can be consumed to save IO. std::unique_ptr StatCache; }; /// Information required to run clang, e.g. to parse AST or do code completion. struct ParseInputs { tooling::CompileCommand CompileCommand; IntrusiveRefCntPtr FS; std::string Contents; }; /// Stores and provides access to parsed AST. class ParsedAST { public: /// Attempts to run Clang and store parsed AST. If \p Preamble is non-null /// it is reused during parsing. static llvm::Optional build(std::unique_ptr CI, std::shared_ptr Preamble, std::unique_ptr Buffer, std::shared_ptr PCHs, IntrusiveRefCntPtr VFS); ParsedAST(ParsedAST &&Other); ParsedAST &operator=(ParsedAST &&Other); ~ParsedAST(); /// Note that the returned ast will not contain decls from the preamble that /// were not deserialized during parsing. Clients should expect only decls /// from the main file to be in the AST. ASTContext &getASTContext(); const ASTContext &getASTContext() const; Preprocessor &getPreprocessor(); std::shared_ptr getPreprocessorPtr(); const Preprocessor &getPreprocessor() const; /// This function returns top-level decls present in the main file of the AST. /// The result does not include the decls that come from the preamble. /// (These should be const, but RecursiveASTVisitor requires Decl*). ArrayRef getLocalTopLevelDecls(); const std::vector &getDiagnostics() const; /// Returns the esitmated size of the AST and the accessory structures, in /// bytes. Does not include the size of the preamble. std::size_t getUsedBytes() const; const IncludeStructure &getIncludeStructure() const; private: ParsedAST(std::shared_ptr Preamble, std::unique_ptr Clang, std::unique_ptr Action, std::vector LocalTopLevelDecls, std::vector Diags, IncludeStructure Includes); // In-memory preambles must outlive the AST, it is important that this member // goes before Clang and Action. std::shared_ptr Preamble; // We store an "incomplete" FrontendAction (i.e. no EndSourceFile was called // on it) and CompilerInstance used to run it. That way we don't have to do // complex memory management of all Clang structures on our own. (They are // stored in CompilerInstance and cleaned up by // FrontendAction.EndSourceFile). std::unique_ptr Clang; std::unique_ptr Action; // Data, stored after parsing. std::vector Diags; // Top-level decls inside the current file. Not that this does not include // top-level decls from the preamble. std::vector LocalTopLevelDecls; IncludeStructure Includes; }; using PreambleParsedCallback = std::function)>; /// Builds compiler invocation that could be used to build AST or preamble. std::unique_ptr buildCompilerInvocation(const ParseInputs &Inputs); /// Rebuild the preamble for the new inputs unless the old one can be reused. /// If \p OldPreamble can be reused, it is returned unchanged. /// If \p OldPreamble is null, always builds the preamble. /// If \p PreambleCallback is set, it will be run on top of the AST while /// building the preamble. Note that if the old preamble was reused, no AST is /// built and, therefore, the callback will not be executed. std::shared_ptr buildPreamble(PathRef FileName, CompilerInvocation &CI, std::shared_ptr OldPreamble, const tooling::CompileCommand &OldCompileCommand, const ParseInputs &Inputs, std::shared_ptr PCHs, bool StoreInMemory, PreambleParsedCallback PreambleCallback); /// Build an AST from provided user inputs. This function does not check if /// preamble can be reused, as this function expects that \p Preamble is the /// result of calling buildPreamble. llvm::Optional buildAST(PathRef FileName, std::unique_ptr Invocation, const ParseInputs &Inputs, std::shared_ptr Preamble, std::shared_ptr PCHs); /// Get the beginning SourceLocation at a specified \p Pos. /// May be invalid if Pos is, or if there's no identifier. SourceLocation getBeginningOfIdentifier(ParsedAST &Unit, const Position &Pos, const FileID FID); /// For testing/debugging purposes. Note that this method deserializes all /// unserialized Decls, so use with care. void dumpAST(ParsedAST &AST, llvm::raw_ostream &OS); } // namespace clangd } // namespace clang #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H