2018-08-15 00:03:32 +08:00
|
|
|
//===--- ClangdUnit.h --------------------------------------------*- C++-*-===//
|
2017-05-16 17:38:59 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-05-16 17:38:59 +08:00
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H
|
|
|
|
|
2019-01-22 17:58:53 +08:00
|
|
|
#include "Compiler.h"
|
2018-03-12 23:28:22 +08:00
|
|
|
#include "Diagnostics.h"
|
2018-10-02 18:43:55 +08:00
|
|
|
#include "FS.h"
|
2017-10-11 00:12:54 +08:00
|
|
|
#include "Function.h"
|
2018-05-14 20:19:16 +08:00
|
|
|
#include "Headers.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "Path.h"
|
2017-05-26 20:26:51 +08:00
|
|
|
#include "Protocol.h"
|
2019-02-05 00:19:57 +08:00
|
|
|
#include "index/CanonicalIncludes.h"
|
2019-01-28 22:01:55 +08:00
|
|
|
#include "index/Index.h"
|
2017-09-30 00:41:23 +08:00
|
|
|
#include "clang/Frontend/FrontendAction.h"
|
2017-07-21 21:29:29 +08:00
|
|
|
#include "clang/Frontend/PrecompiledPreamble.h"
|
2018-05-24 23:50:15 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2017-07-21 21:29:29 +08:00
|
|
|
#include "clang/Serialization/ASTBitCodes.h"
|
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "clang/Tooling/Core/Replacement.h"
|
|
|
|
#include <memory>
|
2018-02-09 18:17:23 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-23 21:42:59 +08:00
|
|
|
namespace llvm {
|
|
|
|
class raw_ostream;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-26 20:26:51 +08:00
|
|
|
namespace vfs {
|
|
|
|
class FileSystem;
|
2019-01-25 23:14:03 +08:00
|
|
|
} // namespace vfs
|
2018-10-10 21:27:25 +08:00
|
|
|
} // namespace llvm
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
class PCHContainerOperations;
|
2017-05-26 20:26:51 +08:00
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
namespace tooling {
|
|
|
|
struct CompileCommand;
|
2019-01-25 23:14:03 +08:00
|
|
|
} // namespace tooling
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
namespace clangd {
|
|
|
|
|
2017-11-24 21:04:21 +08:00
|
|
|
// Stores Preamble and associated data.
|
|
|
|
struct PreambleData {
|
2018-05-28 20:23:17 +08:00
|
|
|
PreambleData(PrecompiledPreamble Preamble, std::vector<Diag> Diags,
|
2018-10-02 18:43:55 +08:00
|
|
|
IncludeStructure Includes,
|
2019-02-05 00:19:57 +08:00
|
|
|
std::unique_ptr<PreambleFileStatusCache> StatCache,
|
|
|
|
CanonicalIncludes CanonIncludes);
|
2017-11-24 21:04:21 +08:00
|
|
|
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
tooling::CompileCommand CompileCommand;
|
2017-11-24 21:04:21 +08:00
|
|
|
PrecompiledPreamble Preamble;
|
2018-03-12 23:28:22 +08:00
|
|
|
std::vector<Diag> Diags;
|
2018-05-14 20:19:16 +08:00
|
|
|
// Processes like code completions and go-to-definitions will need #include
|
|
|
|
// information, and their compile action skips preamble range.
|
2018-07-03 16:09:29 +08:00
|
|
|
IncludeStructure Includes;
|
2018-10-02 18:43:55 +08:00
|
|
|
// Cache of FS operations performed when building the preamble.
|
|
|
|
// When reusing a preamble, this cache can be consumed to save IO.
|
|
|
|
std::unique_ptr<PreambleFileStatusCache> StatCache;
|
2019-02-05 00:19:57 +08:00
|
|
|
CanonicalIncludes CanonIncludes;
|
2017-11-24 21:04:21 +08:00
|
|
|
};
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
/// Stores and provides access to parsed AST.
|
|
|
|
class ParsedAST {
|
2017-05-16 17:38:59 +08:00
|
|
|
public:
|
2017-08-01 23:51:38 +08:00
|
|
|
/// Attempts to run Clang and store parsed AST. If \p Preamble is non-null
|
|
|
|
/// it is reused during parsing.
|
|
|
|
static llvm::Optional<ParsedAST>
|
2018-07-26 20:05:31 +08:00
|
|
|
build(std::unique_ptr<clang::CompilerInvocation> CI,
|
2017-11-24 21:04:21 +08:00
|
|
|
std::shared_ptr<const PreambleData> Preamble,
|
2017-08-01 23:51:38 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buffer,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs,
|
2019-01-28 22:01:55 +08:00
|
|
|
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, const SymbolIndex *Index,
|
|
|
|
const ParseOptions &Opts);
|
2017-08-01 23:51:38 +08:00
|
|
|
|
|
|
|
ParsedAST(ParsedAST &&Other);
|
|
|
|
ParsedAST &operator=(ParsedAST &&Other);
|
|
|
|
|
|
|
|
~ParsedAST();
|
|
|
|
|
2018-05-28 20:23:17 +08:00
|
|
|
/// 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.
|
2017-08-01 23:51:38 +08:00
|
|
|
ASTContext &getASTContext();
|
|
|
|
const ASTContext &getASTContext() const;
|
|
|
|
|
|
|
|
Preprocessor &getPreprocessor();
|
2018-01-10 01:32:00 +08:00
|
|
|
std::shared_ptr<Preprocessor> getPreprocessorPtr();
|
2017-08-01 23:51:38 +08:00
|
|
|
const Preprocessor &getPreprocessor() const;
|
|
|
|
|
2018-05-28 20:23:17 +08:00
|
|
|
/// 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.
|
2018-06-06 00:30:25 +08:00
|
|
|
/// (These should be const, but RecursiveASTVisitor requires Decl*).
|
|
|
|
ArrayRef<Decl *> getLocalTopLevelDecls();
|
2017-08-01 23:51:38 +08:00
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
const std::vector<Diag> &getDiagnostics() const;
|
2017-05-23 21:42:59 +08:00
|
|
|
|
2018-01-25 22:32:21 +08:00
|
|
|
/// 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;
|
2018-07-03 16:09:29 +08:00
|
|
|
const IncludeStructure &getIncludeStructure() const;
|
2019-02-05 00:19:57 +08:00
|
|
|
const CanonicalIncludes &getCanonicalIncludes() const;
|
2018-01-25 22:32:21 +08:00
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
private:
|
2017-11-24 21:04:21 +08:00
|
|
|
ParsedAST(std::shared_ptr<const PreambleData> Preamble,
|
|
|
|
std::unique_ptr<CompilerInstance> Clang,
|
2017-08-01 23:51:38 +08:00
|
|
|
std::unique_ptr<FrontendAction> Action,
|
2018-06-06 00:30:25 +08:00
|
|
|
std::vector<Decl *> LocalTopLevelDecls, std::vector<Diag> Diags,
|
2019-02-05 00:19:57 +08:00
|
|
|
IncludeStructure Includes, CanonicalIncludes CanonIncludes);
|
2017-08-01 23:51:38 +08:00
|
|
|
|
2017-11-24 21:04:21 +08:00
|
|
|
// In-memory preambles must outlive the AST, it is important that this member
|
|
|
|
// goes before Clang and Action.
|
|
|
|
std::shared_ptr<const PreambleData> Preamble;
|
2017-08-01 23:51:38 +08:00
|
|
|
// 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<CompilerInstance> Clang;
|
|
|
|
std::unique_ptr<FrontendAction> Action;
|
|
|
|
|
|
|
|
// Data, stored after parsing.
|
2018-03-12 23:28:22 +08:00
|
|
|
std::vector<Diag> Diags;
|
2018-05-28 20:23:17 +08:00
|
|
|
// Top-level decls inside the current file. Not that this does not include
|
|
|
|
// top-level decls from the preamble.
|
2018-06-06 00:30:25 +08:00
|
|
|
std::vector<Decl *> LocalTopLevelDecls;
|
2018-07-03 16:09:29 +08:00
|
|
|
IncludeStructure Includes;
|
2019-02-05 00:19:57 +08:00
|
|
|
CanonicalIncludes CanonIncludes;
|
2017-08-01 23:51:38 +08:00
|
|
|
};
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2018-09-04 00:37:59 +08:00
|
|
|
using PreambleParsedCallback =
|
2019-02-05 00:19:57 +08:00
|
|
|
std::function<void(ASTContext &, std::shared_ptr<clang::Preprocessor>,
|
|
|
|
const CanonicalIncludes &)>;
|
2017-12-20 02:00:37 +08:00
|
|
|
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
/// 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<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);
|
|
|
|
|
|
|
|
/// 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<ParsedAST>
|
|
|
|
buildAST(PathRef FileName, std::unique_ptr<CompilerInvocation> Invocation,
|
|
|
|
const ParseInputs &Inputs,
|
|
|
|
std::shared_ptr<const PreambleData> Preamble,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-11-09 19:30:04 +08:00
|
|
|
/// Get the beginning SourceLocation at a specified \p Pos.
|
[clangd] Fix unicode handling, using UTF-16 where LSP requires it.
Summary:
The Language Server Protocol unfortunately mandates that locations in files
be represented by line/column pairs, where the "column" is actually an index
into the UTF-16-encoded text of the line.
(This is because VSCode is written in JavaScript, which is UTF-16-native).
Internally clangd treats source files at UTF-8, the One True Encoding, and
generally deals with byte offsets (though there are exceptions).
Before this patch, conversions between offsets and LSP Position pretended
that Position.character was UTF-8 bytes, which is only true for ASCII lines.
Now we examine the text to convert correctly (but don't actually need to
transcode it, due to some nice details of the encodings).
The updated functions in SourceCode are the blessed way to interact with
the Position.character field, and anything else is likely to be wrong.
So I also updated the other accesses:
- CodeComplete needs a "clang-style" line/column, with column in utf-8 bytes.
This is now converted via Position -> offset -> clang line/column
(a new function is added to SourceCode.h for the second conversion).
- getBeginningOfIdentifier skipped backwards in UTF-16 space, which is will
behave badly when it splits a surrogate pair. Skipping backwards in UTF-8
coordinates gives the lexer a fighting chance of getting this right.
While here, I clarified(?) the logic comments, fixed a bug with identifiers
containing digits, simplified the signature slightly and added a test.
This seems likely to cause problems with editors that have the same bug, and
treat the protocol as if columns are UTF-8 bytes. But we can find and fix those.
Reviewers: hokein
Subscribers: klimek, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D46035
llvm-svn: 331029
2018-04-27 19:59:28 +08:00
|
|
|
/// May be invalid if Pos is, or if there's no identifier.
|
2017-11-09 19:30:04 +08:00
|
|
|
SourceLocation getBeginningOfIdentifier(ParsedAST &Unit, const Position &Pos,
|
[clangd] Fix unicode handling, using UTF-16 where LSP requires it.
Summary:
The Language Server Protocol unfortunately mandates that locations in files
be represented by line/column pairs, where the "column" is actually an index
into the UTF-16-encoded text of the line.
(This is because VSCode is written in JavaScript, which is UTF-16-native).
Internally clangd treats source files at UTF-8, the One True Encoding, and
generally deals with byte offsets (though there are exceptions).
Before this patch, conversions between offsets and LSP Position pretended
that Position.character was UTF-8 bytes, which is only true for ASCII lines.
Now we examine the text to convert correctly (but don't actually need to
transcode it, due to some nice details of the encodings).
The updated functions in SourceCode are the blessed way to interact with
the Position.character field, and anything else is likely to be wrong.
So I also updated the other accesses:
- CodeComplete needs a "clang-style" line/column, with column in utf-8 bytes.
This is now converted via Position -> offset -> clang line/column
(a new function is added to SourceCode.h for the second conversion).
- getBeginningOfIdentifier skipped backwards in UTF-16 space, which is will
behave badly when it splits a surrogate pair. Skipping backwards in UTF-8
coordinates gives the lexer a fighting chance of getting this right.
While here, I clarified(?) the logic comments, fixed a bug with identifiers
containing digits, simplified the signature slightly and added a test.
This seems likely to cause problems with editors that have the same bug, and
treat the protocol as if columns are UTF-8 bytes. But we can find and fix those.
Reviewers: hokein
Subscribers: klimek, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D46035
llvm-svn: 331029
2018-04-27 19:59:28 +08:00
|
|
|
const FileID FID);
|
2017-11-09 19:30:04 +08:00
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
/// For testing/debugging purposes. Note that this method deserializes all
|
|
|
|
/// unserialized Decls, so use with care.
|
|
|
|
void dumpAST(ParsedAST &AST, llvm::raw_ostream &OS);
|
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
2018-08-15 00:03:32 +08:00
|
|
|
|
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H
|