2017-05-16 17:38:59 +08:00
|
|
|
//===--- 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
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
#include "Diagnostics.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"
|
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
|
|
|
namespace clang {
|
|
|
|
class PCHContainerOperations;
|
|
|
|
|
2017-05-26 20:26:51 +08:00
|
|
|
namespace vfs {
|
|
|
|
class FileSystem;
|
|
|
|
}
|
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
namespace tooling {
|
|
|
|
struct CompileCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
std::vector<Inclusion> Inclusions);
|
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.
|
|
|
|
std::vector<Inclusion> Inclusions;
|
2017-11-24 21:04:21 +08:00
|
|
|
};
|
|
|
|
|
2018-01-23 23:07:52 +08:00
|
|
|
/// Information required to run clang, e.g. to parse AST or do code completion.
|
|
|
|
struct ParseInputs {
|
|
|
|
tooling::CompileCommand CompileCommand;
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> FS;
|
|
|
|
std::string Contents;
|
|
|
|
};
|
|
|
|
|
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>
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +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,
|
2017-12-13 20:51:22 +08:00
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> VFS);
|
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.
|
|
|
|
ArrayRef<const 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-05-14 20:19:16 +08:00
|
|
|
const std::vector<Inclusion> &getInclusions() 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-05-28 20:23:17 +08:00
|
|
|
std::vector<const Decl *> LocalTopLevelDecls,
|
|
|
|
std::vector<Diag> Diags, std::vector<Inclusion> Inclusions);
|
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.
|
|
|
|
std::vector<const Decl *> LocalTopLevelDecls;
|
2018-05-14 20:19:16 +08:00
|
|
|
std::vector<Inclusion> Inclusions;
|
2017-08-01 23:51:38 +08:00
|
|
|
};
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2018-05-24 23:50:15 +08:00
|
|
|
using PreambleParsedCallback = std::function<void(
|
|
|
|
PathRef Path, ASTContext &, std::shared_ptr<clang::Preprocessor>)>;
|
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
|
|
|
/// Builds compiler invocation that could be used to build AST or preamble.
|
|
|
|
std::unique_ptr<CompilerInvocation>
|
|
|
|
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<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
|
|
|
|
#endif
|