2017-05-16 17:38:59 +08:00
|
|
|
//===--- ClangdUnit.cpp -----------------------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ClangdUnit.h"
|
2017-06-29 00:12:10 +08:00
|
|
|
|
2017-09-20 18:46:58 +08:00
|
|
|
#include "Logger.h"
|
2017-11-02 17:21:51 +08:00
|
|
|
#include "Trace.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
2017-07-21 21:29:29 +08:00
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
2017-05-26 20:26:51 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2017-06-29 00:12:10 +08:00
|
|
|
#include "clang/Index/IndexDataConsumer.h"
|
2017-07-21 21:29:29 +08:00
|
|
|
#include "clang/Index/IndexingAction.h"
|
2017-06-29 00:12:10 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
#include "clang/Lex/MacroInfo.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2017-07-21 21:29:29 +08:00
|
|
|
#include "clang/Lex/PreprocessorOptions.h"
|
|
|
|
#include "clang/Sema/Sema.h"
|
|
|
|
#include "clang/Serialization/ASTWriter.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
2017-07-21 21:29:29 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/Support/CrashRecoveryContext.h"
|
2017-06-15 17:11:57 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-06-29 00:12:10 +08:00
|
|
|
#include <algorithm>
|
2017-08-01 23:51:38 +08:00
|
|
|
#include <chrono>
|
2017-06-29 00:12:10 +08:00
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
using namespace clang::clangd;
|
|
|
|
using namespace clang;
|
|
|
|
|
2017-07-21 21:29:29 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class DeclTrackingASTConsumer : public ASTConsumer {
|
|
|
|
public:
|
|
|
|
DeclTrackingASTConsumer(std::vector<const Decl *> &TopLevelDecls)
|
|
|
|
: TopLevelDecls(TopLevelDecls) {}
|
|
|
|
|
|
|
|
bool HandleTopLevelDecl(DeclGroupRef DG) override {
|
|
|
|
for (const Decl *D : DG) {
|
|
|
|
// ObjCMethodDecl are not actually top-level decls.
|
|
|
|
if (isa<ObjCMethodDecl>(D))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
TopLevelDecls.push_back(D);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<const Decl *> &TopLevelDecls;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ClangdFrontendAction : public SyntaxOnlyAction {
|
|
|
|
public:
|
|
|
|
std::vector<const Decl *> takeTopLevelDecls() {
|
|
|
|
return std::move(TopLevelDecls);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
StringRef InFile) override {
|
|
|
|
return llvm::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<const Decl *> TopLevelDecls;
|
|
|
|
};
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
class CppFilePreambleCallbacks : public PreambleCallbacks {
|
2017-07-21 21:29:29 +08:00
|
|
|
public:
|
|
|
|
std::vector<serialization::DeclID> takeTopLevelDeclIDs() {
|
|
|
|
return std::move(TopLevelDeclIDs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AfterPCHEmitted(ASTWriter &Writer) override {
|
|
|
|
TopLevelDeclIDs.reserve(TopLevelDecls.size());
|
|
|
|
for (Decl *D : TopLevelDecls) {
|
|
|
|
// Invalid top-level decls may not have been serialized.
|
|
|
|
if (D->isInvalidDecl())
|
|
|
|
continue;
|
|
|
|
TopLevelDeclIDs.push_back(Writer.getDeclID(D));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandleTopLevelDecl(DeclGroupRef DG) override {
|
|
|
|
for (Decl *D : DG) {
|
|
|
|
if (isa<ObjCMethodDecl>(D))
|
|
|
|
continue;
|
|
|
|
TopLevelDecls.push_back(D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<Decl *> TopLevelDecls;
|
|
|
|
std::vector<serialization::DeclID> TopLevelDeclIDs;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Convert from clang diagnostic level to LSP severity.
|
|
|
|
static int getSeverity(DiagnosticsEngine::Level L) {
|
|
|
|
switch (L) {
|
|
|
|
case DiagnosticsEngine::Remark:
|
|
|
|
return 4;
|
|
|
|
case DiagnosticsEngine::Note:
|
|
|
|
return 3;
|
|
|
|
case DiagnosticsEngine::Warning:
|
|
|
|
return 2;
|
|
|
|
case DiagnosticsEngine::Fatal:
|
|
|
|
case DiagnosticsEngine::Error:
|
|
|
|
return 1;
|
|
|
|
case DiagnosticsEngine::Ignored:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown diagnostic level!");
|
|
|
|
}
|
|
|
|
|
2017-10-06 19:54:17 +08:00
|
|
|
/// Get the optional chunk as a string. This function is possibly recursive.
|
|
|
|
///
|
|
|
|
/// The parameter info for each parameter is appended to the Parameters.
|
|
|
|
std::string
|
|
|
|
getOptionalParameters(const CodeCompletionString &CCS,
|
|
|
|
std::vector<ParameterInformation> &Parameters) {
|
|
|
|
std::string Result;
|
|
|
|
for (const auto &Chunk : CCS) {
|
|
|
|
switch (Chunk.Kind) {
|
|
|
|
case CodeCompletionString::CK_Optional:
|
|
|
|
assert(Chunk.Optional &&
|
|
|
|
"Expected the optional code completion string to be non-null.");
|
|
|
|
Result += getOptionalParameters(*Chunk.Optional, Parameters);
|
|
|
|
break;
|
|
|
|
case CodeCompletionString::CK_VerticalSpace:
|
|
|
|
break;
|
|
|
|
case CodeCompletionString::CK_Placeholder:
|
|
|
|
// A string that acts as a placeholder for, e.g., a function call
|
|
|
|
// argument.
|
|
|
|
// Intentional fallthrough here.
|
|
|
|
case CodeCompletionString::CK_CurrentParameter: {
|
|
|
|
// A piece of text that describes the parameter that corresponds to
|
|
|
|
// the code-completion location within a function call, message send,
|
|
|
|
// macro invocation, etc.
|
|
|
|
Result += Chunk.Text;
|
|
|
|
ParameterInformation Info;
|
|
|
|
Info.label = Chunk.Text;
|
|
|
|
Parameters.push_back(std::move(Info));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
Result += Chunk.Text;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-10-29 01:32:56 +08:00
|
|
|
llvm::Optional<DiagWithFixIts> toClangdDiag(const StoredDiagnostic &D) {
|
2017-07-21 21:29:29 +08:00
|
|
|
auto Location = D.getLocation();
|
|
|
|
if (!Location.isValid() || !Location.getManager().isInMainFile(Location))
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
Position P;
|
|
|
|
P.line = Location.getSpellingLineNumber() - 1;
|
|
|
|
P.character = Location.getSpellingColumnNumber();
|
|
|
|
Range R = {P, P};
|
|
|
|
clangd::Diagnostic Diag = {R, getSeverity(D.getLevel()), D.getMessage()};
|
|
|
|
|
|
|
|
llvm::SmallVector<tooling::Replacement, 1> FixItsForDiagnostic;
|
|
|
|
for (const FixItHint &Fix : D.getFixIts()) {
|
|
|
|
FixItsForDiagnostic.push_back(clang::tooling::Replacement(
|
|
|
|
Location.getManager(), Fix.RemoveRange, Fix.CodeToInsert));
|
|
|
|
}
|
|
|
|
return DiagWithFixIts{Diag, std::move(FixItsForDiagnostic)};
|
|
|
|
}
|
|
|
|
|
|
|
|
class StoreDiagsConsumer : public DiagnosticConsumer {
|
|
|
|
public:
|
|
|
|
StoreDiagsConsumer(std::vector<DiagWithFixIts> &Output) : Output(Output) {}
|
|
|
|
|
|
|
|
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
const clang::Diagnostic &Info) override {
|
|
|
|
DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
|
|
|
|
|
|
|
|
if (auto convertedDiag = toClangdDiag(StoredDiagnostic(DiagLevel, Info)))
|
|
|
|
Output.push_back(std::move(*convertedDiag));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<DiagWithFixIts> &Output;
|
|
|
|
};
|
|
|
|
|
|
|
|
class EmptyDiagsConsumer : public DiagnosticConsumer {
|
|
|
|
public:
|
|
|
|
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
const clang::Diagnostic &Info) override {}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<CompilerInvocation>
|
|
|
|
createCompilerInvocation(ArrayRef<const char *> ArgList,
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
|
|
|
|
auto CI = createInvocationFromCommandLine(ArgList, std::move(Diags),
|
|
|
|
std::move(VFS));
|
|
|
|
// We rely on CompilerInstance to manage the resource (i.e. free them on
|
|
|
|
// EndSourceFile), but that won't happen if DisableFree is set to true.
|
|
|
|
// Since createInvocationFromCommandLine sets it to true, we have to override
|
|
|
|
// it.
|
|
|
|
CI->getFrontendOpts().DisableFree = false;
|
|
|
|
return CI;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a CompilerInstance from \p CI, with main buffer overriden to \p
|
|
|
|
/// Buffer and arguments to read the PCH from \p Preamble, if \p Preamble is not
|
|
|
|
/// null. Note that vfs::FileSystem inside returned instance may differ from \p
|
|
|
|
/// VFS if additional file remapping were set in command-line arguments.
|
|
|
|
/// On some errors, returns null. When non-null value is returned, it's expected
|
|
|
|
/// to be consumed by the FrontendAction as it will have a pointer to the \p
|
|
|
|
/// Buffer that will only be deleted if BeginSourceFile is called.
|
|
|
|
std::unique_ptr<CompilerInstance>
|
|
|
|
prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
|
|
|
|
const PrecompiledPreamble *Preamble,
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buffer,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs,
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> VFS,
|
|
|
|
DiagnosticConsumer &DiagsClient) {
|
|
|
|
assert(VFS && "VFS is null");
|
|
|
|
assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
|
|
|
|
"Setting RetainRemappedFileBuffers to true will cause a memory leak "
|
|
|
|
"of ContentsBuffer");
|
|
|
|
|
|
|
|
// NOTE: we use Buffer.get() when adding remapped files, so we have to make
|
|
|
|
// sure it will be released if no error is emitted.
|
|
|
|
if (Preamble) {
|
2017-11-17 00:25:18 +08:00
|
|
|
Preamble->AddImplicitPreamble(*CI, VFS, Buffer.get());
|
2017-07-21 21:29:29 +08:00
|
|
|
} else {
|
|
|
|
CI->getPreprocessorOpts().addRemappedFile(
|
|
|
|
CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Clang = llvm::make_unique<CompilerInstance>(PCHs);
|
|
|
|
Clang->setInvocation(std::move(CI));
|
|
|
|
Clang->createDiagnostics(&DiagsClient, false);
|
|
|
|
|
|
|
|
if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
|
|
|
|
Clang->getInvocation(), Clang->getDiagnostics(), VFS))
|
|
|
|
VFS = VFSWithRemapping;
|
|
|
|
Clang->setVirtualFileSystem(VFS);
|
|
|
|
|
|
|
|
Clang->setTarget(TargetInfo::CreateTargetInfo(
|
|
|
|
Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
|
|
|
|
if (!Clang->hasTarget())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// RemappedFileBuffers will handle the lifetime of the Buffer pointer,
|
|
|
|
// release it.
|
|
|
|
Buffer.release();
|
|
|
|
return Clang;
|
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
template <class T> bool futureIsReady(std::shared_future<T> const &Future) {
|
|
|
|
return Future.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
} // namespace
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-10-23 14:06:21 +08:00
|
|
|
CompletionItemKind getKindOfDecl(CXCursorKind CursorKind) {
|
|
|
|
switch (CursorKind) {
|
2017-05-16 17:38:59 +08:00
|
|
|
case CXCursor_MacroInstantiation:
|
|
|
|
case CXCursor_MacroDefinition:
|
|
|
|
return CompletionItemKind::Text;
|
|
|
|
case CXCursor_CXXMethod:
|
|
|
|
return CompletionItemKind::Method;
|
|
|
|
case CXCursor_FunctionDecl:
|
|
|
|
case CXCursor_FunctionTemplate:
|
|
|
|
return CompletionItemKind::Function;
|
|
|
|
case CXCursor_Constructor:
|
|
|
|
case CXCursor_Destructor:
|
|
|
|
return CompletionItemKind::Constructor;
|
|
|
|
case CXCursor_FieldDecl:
|
|
|
|
return CompletionItemKind::Field;
|
|
|
|
case CXCursor_VarDecl:
|
|
|
|
case CXCursor_ParmDecl:
|
|
|
|
return CompletionItemKind::Variable;
|
|
|
|
case CXCursor_ClassDecl:
|
|
|
|
case CXCursor_StructDecl:
|
|
|
|
case CXCursor_UnionDecl:
|
|
|
|
case CXCursor_ClassTemplate:
|
|
|
|
case CXCursor_ClassTemplatePartialSpecialization:
|
|
|
|
return CompletionItemKind::Class;
|
|
|
|
case CXCursor_Namespace:
|
|
|
|
case CXCursor_NamespaceAlias:
|
|
|
|
case CXCursor_NamespaceRef:
|
|
|
|
return CompletionItemKind::Module;
|
|
|
|
case CXCursor_EnumConstantDecl:
|
|
|
|
return CompletionItemKind::Value;
|
|
|
|
case CXCursor_EnumDecl:
|
|
|
|
return CompletionItemKind::Enum;
|
|
|
|
case CXCursor_TypeAliasDecl:
|
|
|
|
case CXCursor_TypeAliasTemplateDecl:
|
|
|
|
case CXCursor_TypedefDecl:
|
|
|
|
case CXCursor_MemberRef:
|
|
|
|
case CXCursor_TypeRef:
|
|
|
|
return CompletionItemKind::Reference;
|
|
|
|
default:
|
|
|
|
return CompletionItemKind::Missing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 14:06:21 +08:00
|
|
|
CompletionItemKind getKind(CodeCompletionResult::ResultKind ResKind,
|
|
|
|
CXCursorKind CursorKind) {
|
|
|
|
switch (ResKind) {
|
|
|
|
case CodeCompletionResult::RK_Declaration:
|
|
|
|
return getKindOfDecl(CursorKind);
|
|
|
|
case CodeCompletionResult::RK_Keyword:
|
|
|
|
return CompletionItemKind::Keyword;
|
|
|
|
case CodeCompletionResult::RK_Macro:
|
|
|
|
return CompletionItemKind::Text; // unfortunately, there's no 'Macro'
|
|
|
|
// completion items in LSP.
|
|
|
|
case CodeCompletionResult::RK_Pattern:
|
|
|
|
return CompletionItemKind::Snippet;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unhandled CodeCompletionResult::ResultKind.");
|
|
|
|
}
|
|
|
|
|
2017-09-12 21:57:14 +08:00
|
|
|
std::string escapeSnippet(const llvm::StringRef Text) {
|
|
|
|
std::string Result;
|
|
|
|
Result.reserve(Text.size()); // Assume '$', '}' and '\\' are rare.
|
|
|
|
for (const auto Character : Text) {
|
|
|
|
if (Character == '$' || Character == '}' || Character == '\\')
|
|
|
|
Result.push_back('\\');
|
|
|
|
Result.push_back(Character);
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-10-06 19:54:17 +08:00
|
|
|
std::string getDocumentation(const CodeCompletionString &CCS) {
|
|
|
|
// Things like __attribute__((nonnull(1,3))) and [[noreturn]]. Present this
|
|
|
|
// information in the documentation field.
|
|
|
|
std::string Result;
|
|
|
|
const unsigned AnnotationCount = CCS.getAnnotationCount();
|
|
|
|
if (AnnotationCount > 0) {
|
|
|
|
Result += "Annotation";
|
|
|
|
if (AnnotationCount == 1) {
|
|
|
|
Result += ": ";
|
|
|
|
} else /* AnnotationCount > 1 */ {
|
|
|
|
Result += "s: ";
|
|
|
|
}
|
|
|
|
for (unsigned I = 0; I < AnnotationCount; ++I) {
|
|
|
|
Result += CCS.getAnnotation(I);
|
|
|
|
Result.push_back(I == AnnotationCount - 1 ? '\n' : ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add brief documentation (if there is any).
|
|
|
|
if (CCS.getBriefComment() != nullptr) {
|
|
|
|
if (!Result.empty()) {
|
|
|
|
// This means we previously added annotations. Add an extra newline
|
|
|
|
// character to make the annotations stand out.
|
|
|
|
Result.push_back('\n');
|
|
|
|
}
|
|
|
|
Result += CCS.getBriefComment();
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-11-15 17:16:29 +08:00
|
|
|
/// A scored code completion result.
|
|
|
|
/// It may be promoted to a CompletionItem if it's among the top-ranked results.
|
|
|
|
struct CompletionCandidate {
|
|
|
|
CompletionCandidate(CodeCompletionResult &Result)
|
|
|
|
: Result(&Result), Score(score(Result)) {}
|
|
|
|
|
|
|
|
CodeCompletionResult *Result;
|
|
|
|
// Higher score is worse. FIXME: use a more natural scale!
|
|
|
|
int Score;
|
|
|
|
|
|
|
|
// Comparison reflects rank: better candidates are smaller.
|
|
|
|
bool operator<(const CompletionCandidate &C) const {
|
|
|
|
if (Score != C.Score)
|
|
|
|
return Score < C.Score;
|
|
|
|
return *Result < *C.Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string sortText() const {
|
|
|
|
// Fill in the sortText of the CompletionItem.
|
|
|
|
assert(Score <= 999999 && "Expecting score to have at most 6-digits");
|
|
|
|
std::string S, NameStorage;
|
|
|
|
StringRef Name = Result->getOrderedName(NameStorage);
|
|
|
|
llvm::raw_string_ostream(S)
|
|
|
|
<< llvm::format("%06d%.*s", Score, Name.size(), Name.data());
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static int score(const CodeCompletionResult &Result) {
|
|
|
|
int Score = Result.Priority;
|
|
|
|
// Fill in the sortText of the CompletionItem.
|
|
|
|
assert(Score <= 99999 && "Expecting code completion result "
|
|
|
|
"priority to have at most 5-digits");
|
|
|
|
|
|
|
|
const int Penalty = 100000;
|
|
|
|
switch (static_cast<CXAvailabilityKind>(Result.Availability)) {
|
|
|
|
case CXAvailability_Available:
|
|
|
|
// No penalty.
|
|
|
|
break;
|
|
|
|
case CXAvailability_Deprecated:
|
|
|
|
Score += Penalty;
|
|
|
|
break;
|
|
|
|
case CXAvailability_NotAccessible:
|
|
|
|
Score += 2 * Penalty;
|
|
|
|
break;
|
|
|
|
case CXAvailability_NotAvailable:
|
|
|
|
Score += 3 * Penalty;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Score;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-06 19:54:17 +08:00
|
|
|
class CompletionItemsCollector : public CodeCompleteConsumer {
|
2017-05-16 17:38:59 +08:00
|
|
|
public:
|
2017-11-15 17:16:29 +08:00
|
|
|
CompletionItemsCollector(const clangd::CodeCompleteOptions &CodeCompleteOpts,
|
|
|
|
CompletionList &Items)
|
|
|
|
: CodeCompleteConsumer(CodeCompleteOpts.getClangCompleteOpts(),
|
|
|
|
/*OutputIsBinary=*/false),
|
|
|
|
ClangdOpts(CodeCompleteOpts), Items(Items),
|
2017-05-16 17:38:59 +08:00
|
|
|
Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
|
|
|
|
CCTUInfo(Allocator) {}
|
|
|
|
|
|
|
|
void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
|
|
|
|
CodeCompletionResult *Results,
|
2017-09-12 21:57:14 +08:00
|
|
|
unsigned NumResults) override final {
|
2017-11-15 17:16:29 +08:00
|
|
|
std::priority_queue<CompletionCandidate> Candidates;
|
2017-09-12 21:57:14 +08:00
|
|
|
for (unsigned I = 0; I < NumResults; ++I) {
|
2017-11-15 17:16:29 +08:00
|
|
|
Candidates.emplace(Results[I]);
|
|
|
|
if (ClangdOpts.Limit && Candidates.size() > ClangdOpts.Limit) {
|
|
|
|
Candidates.pop();
|
|
|
|
Items.isIncomplete = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (!Candidates.empty()) {
|
|
|
|
auto &Candidate = Candidates.top();
|
|
|
|
const auto *CCS = Candidate.Result->CreateCodeCompletionString(
|
2017-05-16 17:38:59 +08:00
|
|
|
S, Context, *Allocator, CCTUInfo,
|
|
|
|
CodeCompleteOpts.IncludeBriefComments);
|
2017-09-12 21:57:14 +08:00
|
|
|
assert(CCS && "Expected the CodeCompletionString to be non-null");
|
2017-11-15 17:16:29 +08:00
|
|
|
Items.items.push_back(ProcessCodeCompleteResult(Candidate, *CCS));
|
|
|
|
Candidates.pop();
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
2017-11-15 17:16:29 +08:00
|
|
|
std::reverse(Items.items.begin(), Items.items.end());
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
|
|
|
|
|
|
|
|
CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
|
2017-09-12 21:57:14 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
CompletionItem
|
2017-11-15 17:16:29 +08:00
|
|
|
ProcessCodeCompleteResult(const CompletionCandidate &Candidate,
|
2017-09-12 21:57:14 +08:00
|
|
|
const CodeCompletionString &CCS) const {
|
|
|
|
|
|
|
|
// Adjust this to InsertTextFormat::Snippet iff we encounter a
|
|
|
|
// CK_Placeholder chunk in SnippetCompletionItemsCollector.
|
|
|
|
CompletionItem Item;
|
|
|
|
Item.insertTextFormat = InsertTextFormat::PlainText;
|
|
|
|
|
2017-10-06 19:54:17 +08:00
|
|
|
Item.documentation = getDocumentation(CCS);
|
2017-11-15 17:16:29 +08:00
|
|
|
Item.sortText = Candidate.sortText();
|
2017-09-12 21:57:14 +08:00
|
|
|
|
|
|
|
// Fill in the label, detail, insertText and filterText fields of the
|
|
|
|
// CompletionItem.
|
|
|
|
ProcessChunks(CCS, Item);
|
|
|
|
|
|
|
|
// Fill in the kind field of the CompletionItem.
|
2017-11-15 17:16:29 +08:00
|
|
|
Item.kind = getKind(Candidate.Result->Kind, Candidate.Result->CursorKind);
|
2017-09-12 21:57:14 +08:00
|
|
|
|
|
|
|
return Item;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void ProcessChunks(const CodeCompletionString &CCS,
|
|
|
|
CompletionItem &Item) const = 0;
|
|
|
|
|
2017-11-15 17:16:29 +08:00
|
|
|
clangd::CodeCompleteOptions ClangdOpts;
|
|
|
|
CompletionList &Items;
|
2017-09-12 21:57:14 +08:00
|
|
|
std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
|
|
|
|
CodeCompletionTUInfo CCTUInfo;
|
|
|
|
|
|
|
|
}; // CompletionItemsCollector
|
|
|
|
|
2017-09-29 02:39:59 +08:00
|
|
|
bool isInformativeQualifierChunk(CodeCompletionString::Chunk const &Chunk) {
|
|
|
|
return Chunk.Kind == CodeCompletionString::CK_Informative &&
|
|
|
|
StringRef(Chunk.Text).endswith("::");
|
|
|
|
}
|
|
|
|
|
2017-09-12 21:57:14 +08:00
|
|
|
class PlainTextCompletionItemsCollector final
|
|
|
|
: public CompletionItemsCollector {
|
|
|
|
|
|
|
|
public:
|
2017-10-23 22:46:48 +08:00
|
|
|
PlainTextCompletionItemsCollector(
|
2017-11-15 17:16:29 +08:00
|
|
|
const clangd::CodeCompleteOptions &CodeCompleteOpts,
|
|
|
|
CompletionList &Items)
|
2017-09-12 21:57:14 +08:00
|
|
|
: CompletionItemsCollector(CodeCompleteOpts, Items) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void ProcessChunks(const CodeCompletionString &CCS,
|
|
|
|
CompletionItem &Item) const override {
|
|
|
|
for (const auto &Chunk : CCS) {
|
2017-09-29 02:39:59 +08:00
|
|
|
// Informative qualifier chunks only clutter completion results, skip
|
|
|
|
// them.
|
|
|
|
if (isInformativeQualifierChunk(Chunk))
|
|
|
|
continue;
|
|
|
|
|
2017-09-12 21:57:14 +08:00
|
|
|
switch (Chunk.Kind) {
|
|
|
|
case CodeCompletionString::CK_TypedText:
|
|
|
|
// There's always exactly one CK_TypedText chunk.
|
|
|
|
Item.insertText = Item.filterText = Chunk.Text;
|
|
|
|
Item.label += Chunk.Text;
|
|
|
|
break;
|
|
|
|
case CodeCompletionString::CK_ResultType:
|
|
|
|
assert(Item.detail.empty() && "Unexpected extraneous CK_ResultType");
|
|
|
|
Item.detail = Chunk.Text;
|
|
|
|
break;
|
|
|
|
case CodeCompletionString::CK_Optional:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Item.label += Chunk.Text;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}; // PlainTextCompletionItemsCollector
|
|
|
|
|
|
|
|
class SnippetCompletionItemsCollector final : public CompletionItemsCollector {
|
|
|
|
|
|
|
|
public:
|
2017-10-23 22:46:48 +08:00
|
|
|
SnippetCompletionItemsCollector(
|
2017-11-15 17:16:29 +08:00
|
|
|
const clangd::CodeCompleteOptions &CodeCompleteOpts,
|
|
|
|
CompletionList &Items)
|
2017-09-12 21:57:14 +08:00
|
|
|
: CompletionItemsCollector(CodeCompleteOpts, Items) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void ProcessChunks(const CodeCompletionString &CCS,
|
|
|
|
CompletionItem &Item) const override {
|
|
|
|
unsigned ArgCount = 0;
|
|
|
|
for (const auto &Chunk : CCS) {
|
2017-09-29 02:39:59 +08:00
|
|
|
// Informative qualifier chunks only clutter completion results, skip
|
|
|
|
// them.
|
|
|
|
if (isInformativeQualifierChunk(Chunk))
|
|
|
|
continue;
|
|
|
|
|
2017-09-12 21:57:14 +08:00
|
|
|
switch (Chunk.Kind) {
|
|
|
|
case CodeCompletionString::CK_TypedText:
|
|
|
|
// The piece of text that the user is expected to type to match
|
|
|
|
// the code-completion string, typically a keyword or the name of
|
|
|
|
// a declarator or macro.
|
|
|
|
Item.filterText = Chunk.Text;
|
2017-11-01 17:22:03 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2017-09-12 21:57:14 +08:00
|
|
|
case CodeCompletionString::CK_Text:
|
|
|
|
// A piece of text that should be placed in the buffer,
|
|
|
|
// e.g., parentheses or a comma in a function call.
|
|
|
|
Item.label += Chunk.Text;
|
|
|
|
Item.insertText += Chunk.Text;
|
|
|
|
break;
|
|
|
|
case CodeCompletionString::CK_Optional:
|
|
|
|
// A code completion string that is entirely optional.
|
|
|
|
// For example, an optional code completion string that
|
|
|
|
// describes the default arguments in a function call.
|
|
|
|
|
|
|
|
// FIXME: Maybe add an option to allow presenting the optional chunks?
|
|
|
|
break;
|
|
|
|
case CodeCompletionString::CK_Placeholder:
|
|
|
|
// A string that acts as a placeholder for, e.g., a function call
|
|
|
|
// argument.
|
|
|
|
++ArgCount;
|
|
|
|
Item.insertText += "${" + std::to_string(ArgCount) + ':' +
|
|
|
|
escapeSnippet(Chunk.Text) + '}';
|
|
|
|
Item.label += Chunk.Text;
|
|
|
|
Item.insertTextFormat = InsertTextFormat::Snippet;
|
|
|
|
break;
|
|
|
|
case CodeCompletionString::CK_Informative:
|
|
|
|
// A piece of text that describes something about the result
|
|
|
|
// but should not be inserted into the buffer.
|
|
|
|
// For example, the word "const" for a const method, or the name of
|
|
|
|
// the base class for methods that are part of the base class.
|
|
|
|
Item.label += Chunk.Text;
|
|
|
|
// Don't put the informative chunks in the insertText.
|
|
|
|
break;
|
|
|
|
case CodeCompletionString::CK_ResultType:
|
|
|
|
// A piece of text that describes the type of an entity or,
|
|
|
|
// for functions and methods, the return type.
|
|
|
|
assert(Item.detail.empty() && "Unexpected extraneous CK_ResultType");
|
|
|
|
Item.detail = Chunk.Text;
|
|
|
|
break;
|
|
|
|
case CodeCompletionString::CK_CurrentParameter:
|
|
|
|
// A piece of text that describes the parameter that corresponds to
|
|
|
|
// the code-completion location within a function call, message send,
|
|
|
|
// macro invocation, etc.
|
|
|
|
//
|
|
|
|
// This should never be present while collecting completion items,
|
|
|
|
// only while collecting overload candidates.
|
|
|
|
llvm_unreachable("Unexpected CK_CurrentParameter while collecting "
|
|
|
|
"CompletionItems");
|
|
|
|
break;
|
|
|
|
case CodeCompletionString::CK_LeftParen:
|
|
|
|
// A left parenthesis ('(').
|
|
|
|
case CodeCompletionString::CK_RightParen:
|
|
|
|
// A right parenthesis (')').
|
|
|
|
case CodeCompletionString::CK_LeftBracket:
|
|
|
|
// A left bracket ('[').
|
|
|
|
case CodeCompletionString::CK_RightBracket:
|
|
|
|
// A right bracket (']').
|
|
|
|
case CodeCompletionString::CK_LeftBrace:
|
|
|
|
// A left brace ('{').
|
|
|
|
case CodeCompletionString::CK_RightBrace:
|
|
|
|
// A right brace ('}').
|
|
|
|
case CodeCompletionString::CK_LeftAngle:
|
|
|
|
// A left angle bracket ('<').
|
|
|
|
case CodeCompletionString::CK_RightAngle:
|
|
|
|
// A right angle bracket ('>').
|
|
|
|
case CodeCompletionString::CK_Comma:
|
|
|
|
// A comma separator (',').
|
|
|
|
case CodeCompletionString::CK_Colon:
|
|
|
|
// A colon (':').
|
|
|
|
case CodeCompletionString::CK_SemiColon:
|
|
|
|
// A semicolon (';').
|
|
|
|
case CodeCompletionString::CK_Equal:
|
|
|
|
// An '=' sign.
|
|
|
|
case CodeCompletionString::CK_HorizontalSpace:
|
|
|
|
// Horizontal whitespace (' ').
|
|
|
|
Item.insertText += Chunk.Text;
|
|
|
|
Item.label += Chunk.Text;
|
|
|
|
break;
|
|
|
|
case CodeCompletionString::CK_VerticalSpace:
|
|
|
|
// Vertical whitespace ('\n' or '\r\n', depending on the
|
|
|
|
// platform).
|
|
|
|
Item.insertText += Chunk.Text;
|
|
|
|
// Don't even add a space to the label.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}; // SnippetCompletionItemsCollector
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-10-06 19:54:17 +08:00
|
|
|
class SignatureHelpCollector final : public CodeCompleteConsumer {
|
|
|
|
|
|
|
|
public:
|
2017-10-23 22:46:48 +08:00
|
|
|
SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
|
2017-10-06 19:54:17 +08:00
|
|
|
SignatureHelp &SigHelp)
|
|
|
|
: CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false),
|
|
|
|
SigHelp(SigHelp),
|
|
|
|
Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
|
|
|
|
CCTUInfo(Allocator) {}
|
|
|
|
|
|
|
|
void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
|
|
|
|
OverloadCandidate *Candidates,
|
|
|
|
unsigned NumCandidates) override {
|
|
|
|
SigHelp.signatures.reserve(NumCandidates);
|
|
|
|
// FIXME(rwols): How can we determine the "active overload candidate"?
|
|
|
|
// Right now the overloaded candidates seem to be provided in a "best fit"
|
|
|
|
// order, so I'm not too worried about this.
|
|
|
|
SigHelp.activeSignature = 0;
|
2017-10-07 20:24:10 +08:00
|
|
|
assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
|
2017-10-06 19:54:17 +08:00
|
|
|
"too many arguments");
|
|
|
|
SigHelp.activeParameter = static_cast<int>(CurrentArg);
|
|
|
|
for (unsigned I = 0; I < NumCandidates; ++I) {
|
|
|
|
const auto &Candidate = Candidates[I];
|
|
|
|
const auto *CCS = Candidate.CreateSignatureString(
|
|
|
|
CurrentArg, S, *Allocator, CCTUInfo, true);
|
|
|
|
assert(CCS && "Expected the CodeCompletionString to be non-null");
|
|
|
|
SigHelp.signatures.push_back(ProcessOverloadCandidate(Candidate, *CCS));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
|
|
|
|
|
|
|
|
CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
SignatureInformation
|
|
|
|
ProcessOverloadCandidate(const OverloadCandidate &Candidate,
|
|
|
|
const CodeCompletionString &CCS) const {
|
|
|
|
SignatureInformation Result;
|
|
|
|
const char *ReturnType = nullptr;
|
|
|
|
|
|
|
|
Result.documentation = getDocumentation(CCS);
|
|
|
|
|
|
|
|
for (const auto &Chunk : CCS) {
|
|
|
|
switch (Chunk.Kind) {
|
|
|
|
case CodeCompletionString::CK_ResultType:
|
|
|
|
// A piece of text that describes the type of an entity or,
|
|
|
|
// for functions and methods, the return type.
|
|
|
|
assert(!ReturnType && "Unexpected CK_ResultType");
|
|
|
|
ReturnType = Chunk.Text;
|
|
|
|
break;
|
|
|
|
case CodeCompletionString::CK_Placeholder:
|
|
|
|
// A string that acts as a placeholder for, e.g., a function call
|
|
|
|
// argument.
|
|
|
|
// Intentional fallthrough here.
|
|
|
|
case CodeCompletionString::CK_CurrentParameter: {
|
|
|
|
// A piece of text that describes the parameter that corresponds to
|
|
|
|
// the code-completion location within a function call, message send,
|
|
|
|
// macro invocation, etc.
|
|
|
|
Result.label += Chunk.Text;
|
|
|
|
ParameterInformation Info;
|
|
|
|
Info.label = Chunk.Text;
|
|
|
|
Result.parameters.push_back(std::move(Info));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CodeCompletionString::CK_Optional: {
|
|
|
|
// The rest of the parameters are defaulted/optional.
|
|
|
|
assert(Chunk.Optional &&
|
|
|
|
"Expected the optional code completion string to be non-null.");
|
|
|
|
Result.label +=
|
|
|
|
getOptionalParameters(*Chunk.Optional, Result.parameters);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CodeCompletionString::CK_VerticalSpace:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Result.label += Chunk.Text;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ReturnType) {
|
|
|
|
Result.label += " -> ";
|
|
|
|
Result.label += ReturnType;
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
SignatureHelp &SigHelp;
|
|
|
|
std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
|
|
|
|
CodeCompletionTUInfo CCTUInfo;
|
|
|
|
|
|
|
|
}; // SignatureHelpCollector
|
|
|
|
|
|
|
|
bool invokeCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
|
2017-10-23 22:46:48 +08:00
|
|
|
const clang::CodeCompleteOptions &Options,
|
|
|
|
PathRef FileName,
|
2017-10-06 19:54:17 +08:00
|
|
|
const tooling::CompileCommand &Command,
|
|
|
|
PrecompiledPreamble const *Preamble, StringRef Contents,
|
|
|
|
Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs,
|
|
|
|
clangd::Logger &Logger) {
|
2017-07-21 21:29:29 +08:00
|
|
|
std::vector<const char *> ArgStrs;
|
|
|
|
for (const auto &S : Command.CommandLine)
|
|
|
|
ArgStrs.push_back(S.c_str());
|
|
|
|
|
2017-07-25 19:37:43 +08:00
|
|
|
VFS->setCurrentWorkingDirectory(Command.Directory);
|
|
|
|
|
2017-07-21 21:29:29 +08:00
|
|
|
std::unique_ptr<CompilerInvocation> CI;
|
|
|
|
EmptyDiagsConsumer DummyDiagsConsumer;
|
|
|
|
{
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
|
|
|
|
CompilerInstance::createDiagnostics(new DiagnosticOptions,
|
|
|
|
&DummyDiagsConsumer, false);
|
|
|
|
CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS);
|
|
|
|
}
|
|
|
|
assert(CI && "Couldn't create CompilerInvocation");
|
|
|
|
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
|
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(Contents, FileName);
|
|
|
|
|
|
|
|
// Attempt to reuse the PCH from precompiled preamble, if it was built.
|
|
|
|
if (Preamble) {
|
|
|
|
auto Bounds =
|
|
|
|
ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0);
|
2017-08-01 23:51:38 +08:00
|
|
|
if (!Preamble->CanReuse(*CI, ContentsBuffer.get(), Bounds, VFS.get()))
|
|
|
|
Preamble = nullptr;
|
2017-07-21 21:29:29 +08:00
|
|
|
}
|
|
|
|
|
2017-10-29 01:32:56 +08:00
|
|
|
auto Clang = prepareCompilerInstance(
|
|
|
|
std::move(CI), Preamble, std::move(ContentsBuffer), std::move(PCHs),
|
|
|
|
std::move(VFS), DummyDiagsConsumer);
|
2017-07-21 21:29:29 +08:00
|
|
|
auto &DiagOpts = Clang->getDiagnosticOpts();
|
|
|
|
DiagOpts.IgnoreWarnings = true;
|
|
|
|
|
|
|
|
auto &FrontendOpts = Clang->getFrontendOpts();
|
|
|
|
FrontendOpts.SkipFunctionBodies = true;
|
2017-10-06 19:54:17 +08:00
|
|
|
FrontendOpts.CodeCompleteOpts = Options;
|
2017-07-21 21:29:29 +08:00
|
|
|
FrontendOpts.CodeCompletionAt.FileName = FileName;
|
|
|
|
FrontendOpts.CodeCompletionAt.Line = Pos.line + 1;
|
|
|
|
FrontendOpts.CodeCompletionAt.Column = Pos.character + 1;
|
|
|
|
|
2017-10-06 19:54:17 +08:00
|
|
|
Clang->setCodeCompletionConsumer(Consumer.release());
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-07-21 21:29:29 +08:00
|
|
|
SyntaxOnlyAction Action;
|
|
|
|
if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
|
2017-09-20 15:24:15 +08:00
|
|
|
Logger.log("BeginSourceFile() failed when running codeComplete for " +
|
|
|
|
FileName);
|
2017-10-06 19:54:17 +08:00
|
|
|
return false;
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
2017-10-06 19:54:17 +08:00
|
|
|
if (!Action.Execute()) {
|
2017-09-20 15:24:15 +08:00
|
|
|
Logger.log("Execute() failed when running codeComplete for " + FileName);
|
2017-10-06 19:54:17 +08:00
|
|
|
return false;
|
|
|
|
}
|
2017-09-20 15:24:15 +08:00
|
|
|
|
2017-07-21 21:29:29 +08:00
|
|
|
Action.EndSourceFile();
|
|
|
|
|
2017-10-06 19:54:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-10-23 22:46:48 +08:00
|
|
|
clangd::CodeCompleteOptions::CodeCompleteOptions(
|
|
|
|
bool EnableSnippetsAndCodePatterns)
|
|
|
|
: CodeCompleteOptions() {
|
|
|
|
EnableSnippets = EnableSnippetsAndCodePatterns;
|
|
|
|
IncludeCodePatterns = EnableSnippetsAndCodePatterns;
|
|
|
|
}
|
|
|
|
|
|
|
|
clangd::CodeCompleteOptions::CodeCompleteOptions(bool EnableSnippets,
|
|
|
|
bool IncludeCodePatterns,
|
|
|
|
bool IncludeMacros,
|
|
|
|
bool IncludeGlobals,
|
|
|
|
bool IncludeBriefComments)
|
|
|
|
: EnableSnippets(EnableSnippets), IncludeCodePatterns(IncludeCodePatterns),
|
|
|
|
IncludeMacros(IncludeMacros), IncludeGlobals(IncludeGlobals),
|
|
|
|
IncludeBriefComments(IncludeBriefComments) {}
|
|
|
|
|
2017-11-15 17:16:29 +08:00
|
|
|
clang::CodeCompleteOptions
|
|
|
|
clangd::CodeCompleteOptions::getClangCompleteOpts() const {
|
2017-10-23 22:46:48 +08:00
|
|
|
clang::CodeCompleteOptions Result;
|
|
|
|
Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns;
|
|
|
|
Result.IncludeMacros = IncludeMacros;
|
|
|
|
Result.IncludeGlobals = IncludeGlobals;
|
|
|
|
Result.IncludeBriefComments = IncludeBriefComments;
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-11-15 17:16:29 +08:00
|
|
|
CompletionList
|
2017-10-29 01:32:56 +08:00
|
|
|
clangd::codeComplete(PathRef FileName, const tooling::CompileCommand &Command,
|
2017-10-06 19:54:17 +08:00
|
|
|
PrecompiledPreamble const *Preamble, StringRef Contents,
|
|
|
|
Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs,
|
2017-10-23 22:46:48 +08:00
|
|
|
clangd::CodeCompleteOptions Opts, clangd::Logger &Logger) {
|
2017-11-15 17:16:29 +08:00
|
|
|
CompletionList Results;
|
2017-10-06 19:54:17 +08:00
|
|
|
std::unique_ptr<CodeCompleteConsumer> Consumer;
|
2017-10-23 22:46:48 +08:00
|
|
|
if (Opts.EnableSnippets) {
|
2017-11-15 17:16:29 +08:00
|
|
|
Consumer =
|
|
|
|
llvm::make_unique<SnippetCompletionItemsCollector>(Opts, Results);
|
2017-10-06 19:54:17 +08:00
|
|
|
} else {
|
2017-11-15 17:16:29 +08:00
|
|
|
Consumer =
|
|
|
|
llvm::make_unique<PlainTextCompletionItemsCollector>(Opts, Results);
|
2017-10-06 19:54:17 +08:00
|
|
|
}
|
2017-11-15 17:16:29 +08:00
|
|
|
invokeCodeComplete(std::move(Consumer), Opts.getClangCompleteOpts(), FileName,
|
|
|
|
Command, Preamble, Contents, Pos, std::move(VFS),
|
|
|
|
std::move(PCHs), Logger);
|
2017-10-06 19:54:17 +08:00
|
|
|
return Results;
|
|
|
|
}
|
|
|
|
|
|
|
|
SignatureHelp
|
2017-10-29 01:32:56 +08:00
|
|
|
clangd::signatureHelp(PathRef FileName, const tooling::CompileCommand &Command,
|
2017-10-06 19:54:17 +08:00
|
|
|
PrecompiledPreamble const *Preamble, StringRef Contents,
|
|
|
|
Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs,
|
|
|
|
clangd::Logger &Logger) {
|
|
|
|
SignatureHelp Result;
|
2017-10-23 22:46:48 +08:00
|
|
|
clang::CodeCompleteOptions Options;
|
2017-10-06 19:54:17 +08:00
|
|
|
Options.IncludeGlobals = false;
|
|
|
|
Options.IncludeMacros = false;
|
|
|
|
Options.IncludeCodePatterns = false;
|
|
|
|
Options.IncludeBriefComments = true;
|
|
|
|
invokeCodeComplete(llvm::make_unique<SignatureHelpCollector>(Options, Result),
|
|
|
|
Options, FileName, Command, Preamble, Contents, Pos,
|
|
|
|
std::move(VFS), std::move(PCHs), Logger);
|
|
|
|
return Result;
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
void clangd::dumpAST(ParsedAST &AST, llvm::raw_ostream &OS) {
|
|
|
|
AST.getASTContext().getTranslationUnitDecl()->dump(OS, true);
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
2017-05-23 21:42:59 +08:00
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
llvm::Optional<ParsedAST>
|
|
|
|
ParsedAST::Build(std::unique_ptr<clang::CompilerInvocation> CI,
|
|
|
|
const PrecompiledPreamble *Preamble,
|
|
|
|
ArrayRef<serialization::DeclID> PreambleDeclIDs,
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buffer,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs,
|
2017-09-20 15:24:15 +08:00
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> VFS,
|
|
|
|
clangd::Logger &Logger) {
|
2017-07-21 21:29:29 +08:00
|
|
|
|
|
|
|
std::vector<DiagWithFixIts> ASTDiags;
|
|
|
|
StoreDiagsConsumer UnitDiagsConsumer(/*ref*/ ASTDiags);
|
|
|
|
|
2017-10-29 01:32:56 +08:00
|
|
|
auto Clang = prepareCompilerInstance(
|
|
|
|
std::move(CI), Preamble, std::move(Buffer), std::move(PCHs),
|
|
|
|
std::move(VFS), /*ref*/ UnitDiagsConsumer);
|
2017-07-21 21:29:29 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
|
|
|
|
Clang.get());
|
|
|
|
|
|
|
|
auto Action = llvm::make_unique<ClangdFrontendAction>();
|
2017-09-20 15:24:15 +08:00
|
|
|
const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0];
|
|
|
|
if (!Action->BeginSourceFile(*Clang, MainInput)) {
|
|
|
|
Logger.log("BeginSourceFile() failed when building AST for " +
|
|
|
|
MainInput.getFile());
|
2017-07-21 21:29:29 +08:00
|
|
|
return llvm::None;
|
|
|
|
}
|
2017-09-20 15:24:15 +08:00
|
|
|
if (!Action->Execute())
|
|
|
|
Logger.log("Execute() failed when building AST for " + MainInput.getFile());
|
2017-07-21 21:29:29 +08:00
|
|
|
|
|
|
|
// UnitDiagsConsumer is local, we can not store it in CompilerInstance that
|
|
|
|
// has a longer lifetime.
|
|
|
|
Clang->getDiagnostics().setClient(new EmptyDiagsConsumer);
|
|
|
|
|
|
|
|
std::vector<const Decl *> ParsedDecls = Action->takeTopLevelDecls();
|
|
|
|
std::vector<serialization::DeclID> PendingDecls;
|
|
|
|
if (Preamble) {
|
|
|
|
PendingDecls.reserve(PreambleDeclIDs.size());
|
|
|
|
PendingDecls.insert(PendingDecls.begin(), PreambleDeclIDs.begin(),
|
|
|
|
PreambleDeclIDs.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParsedAST(std::move(Clang), std::move(Action), std::move(ParsedDecls),
|
|
|
|
std::move(PendingDecls), std::move(ASTDiags));
|
|
|
|
}
|
|
|
|
|
2017-06-29 00:12:10 +08:00
|
|
|
namespace {
|
2017-07-21 21:29:29 +08:00
|
|
|
|
|
|
|
SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr,
|
|
|
|
const FileEntry *FE,
|
|
|
|
unsigned Offset) {
|
|
|
|
SourceLocation FileLoc = Mgr.translateFileLineCol(FE, 1, 1);
|
|
|
|
return Mgr.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr,
|
|
|
|
const FileEntry *FE, Position Pos) {
|
|
|
|
SourceLocation InputLoc =
|
|
|
|
Mgr.translateFileLineCol(FE, Pos.line + 1, Pos.character + 1);
|
|
|
|
return Mgr.getMacroArgExpandedLocation(InputLoc);
|
|
|
|
}
|
|
|
|
|
2017-06-29 00:12:10 +08:00
|
|
|
/// Finds declarations locations that a given source location refers to.
|
|
|
|
class DeclarationLocationsFinder : public index::IndexDataConsumer {
|
|
|
|
std::vector<Location> DeclarationLocations;
|
|
|
|
const SourceLocation &SearchedLocation;
|
2017-07-21 21:29:29 +08:00
|
|
|
const ASTContext &AST;
|
|
|
|
Preprocessor &PP;
|
|
|
|
|
2017-06-29 00:12:10 +08:00
|
|
|
public:
|
|
|
|
DeclarationLocationsFinder(raw_ostream &OS,
|
2017-07-21 21:29:29 +08:00
|
|
|
const SourceLocation &SearchedLocation,
|
|
|
|
ASTContext &AST, Preprocessor &PP)
|
|
|
|
: SearchedLocation(SearchedLocation), AST(AST), PP(PP) {}
|
2017-06-29 00:12:10 +08:00
|
|
|
|
|
|
|
std::vector<Location> takeLocations() {
|
|
|
|
// Don't keep the same location multiple times.
|
|
|
|
// This can happen when nodes in the AST are visited twice.
|
|
|
|
std::sort(DeclarationLocations.begin(), DeclarationLocations.end());
|
2017-06-29 04:57:28 +08:00
|
|
|
auto last =
|
|
|
|
std::unique(DeclarationLocations.begin(), DeclarationLocations.end());
|
2017-06-29 00:12:10 +08:00
|
|
|
DeclarationLocations.erase(last, DeclarationLocations.end());
|
|
|
|
return std::move(DeclarationLocations);
|
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
bool
|
|
|
|
handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
|
|
|
|
ArrayRef<index::SymbolRelation> Relations, FileID FID,
|
|
|
|
unsigned Offset,
|
|
|
|
index::IndexDataConsumer::ASTNodeInfo ASTNode) override {
|
2017-06-29 00:12:10 +08:00
|
|
|
if (isSearchedLocation(FID, Offset)) {
|
|
|
|
addDeclarationLocation(D->getSourceRange());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool isSearchedLocation(FileID FID, unsigned Offset) const {
|
2017-07-21 21:29:29 +08:00
|
|
|
const SourceManager &SourceMgr = AST.getSourceManager();
|
|
|
|
return SourceMgr.getFileOffset(SearchedLocation) == Offset &&
|
|
|
|
SourceMgr.getFileID(SearchedLocation) == FID;
|
2017-06-29 00:12:10 +08:00
|
|
|
}
|
|
|
|
|
2017-07-21 21:29:29 +08:00
|
|
|
void addDeclarationLocation(const SourceRange &ValSourceRange) {
|
|
|
|
const SourceManager &SourceMgr = AST.getSourceManager();
|
|
|
|
const LangOptions &LangOpts = AST.getLangOpts();
|
2017-06-29 00:12:10 +08:00
|
|
|
SourceLocation LocStart = ValSourceRange.getBegin();
|
|
|
|
SourceLocation LocEnd = Lexer::getLocForEndOfToken(ValSourceRange.getEnd(),
|
2017-07-21 21:29:29 +08:00
|
|
|
0, SourceMgr, LangOpts);
|
2017-06-29 04:57:28 +08:00
|
|
|
Position Begin;
|
|
|
|
Begin.line = SourceMgr.getSpellingLineNumber(LocStart) - 1;
|
|
|
|
Begin.character = SourceMgr.getSpellingColumnNumber(LocStart) - 1;
|
|
|
|
Position End;
|
|
|
|
End.line = SourceMgr.getSpellingLineNumber(LocEnd) - 1;
|
|
|
|
End.character = SourceMgr.getSpellingColumnNumber(LocEnd) - 1;
|
|
|
|
Range R = {Begin, End};
|
2017-06-29 00:12:10 +08:00
|
|
|
Location L;
|
2017-11-08 00:16:45 +08:00
|
|
|
if (const FileEntry *F =
|
|
|
|
SourceMgr.getFileEntryForID(SourceMgr.getFileID(LocStart))) {
|
|
|
|
StringRef FilePath = F->tryGetRealPathName();
|
|
|
|
if (FilePath.empty())
|
|
|
|
FilePath = F->getName();
|
|
|
|
L.uri = URI::fromFile(FilePath);
|
|
|
|
L.range = R;
|
|
|
|
DeclarationLocations.push_back(L);
|
|
|
|
}
|
2017-06-29 00:12:10 +08:00
|
|
|
}
|
|
|
|
|
2017-06-29 04:57:28 +08:00
|
|
|
void finish() override {
|
2017-06-29 00:12:10 +08:00
|
|
|
// Also handle possible macro at the searched location.
|
|
|
|
Token Result;
|
2017-07-21 21:29:29 +08:00
|
|
|
if (!Lexer::getRawToken(SearchedLocation, Result, AST.getSourceManager(),
|
|
|
|
AST.getLangOpts(), false)) {
|
2017-06-29 00:12:10 +08:00
|
|
|
if (Result.is(tok::raw_identifier)) {
|
2017-07-21 21:29:29 +08:00
|
|
|
PP.LookUpIdentifierInfo(Result);
|
2017-06-29 00:12:10 +08:00
|
|
|
}
|
2017-07-21 21:29:29 +08:00
|
|
|
IdentifierInfo *IdentifierInfo = Result.getIdentifierInfo();
|
2017-06-29 00:12:10 +08:00
|
|
|
if (IdentifierInfo && IdentifierInfo->hadMacroDefinition()) {
|
|
|
|
std::pair<FileID, unsigned int> DecLoc =
|
2017-07-21 21:29:29 +08:00
|
|
|
AST.getSourceManager().getDecomposedExpansionLoc(SearchedLocation);
|
2017-06-29 00:12:10 +08:00
|
|
|
// Get the definition just before the searched location so that a macro
|
|
|
|
// referenced in a '#undef MACRO' can still be found.
|
2017-07-21 21:29:29 +08:00
|
|
|
SourceLocation BeforeSearchedLocation = getMacroArgExpandedLocation(
|
|
|
|
AST.getSourceManager(),
|
|
|
|
AST.getSourceManager().getFileEntryForID(DecLoc.first),
|
2017-06-29 00:12:10 +08:00
|
|
|
DecLoc.second - 1);
|
|
|
|
MacroDefinition MacroDef =
|
2017-07-21 21:29:29 +08:00
|
|
|
PP.getMacroDefinitionAtLoc(IdentifierInfo, BeforeSearchedLocation);
|
|
|
|
MacroInfo *MacroInf = MacroDef.getMacroInfo();
|
2017-06-29 00:12:10 +08:00
|
|
|
if (MacroInf) {
|
2017-08-01 23:51:38 +08:00
|
|
|
addDeclarationLocation(SourceRange(MacroInf->getDefinitionLoc(),
|
|
|
|
MacroInf->getDefinitionEndLoc()));
|
2017-06-29 00:12:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
} // namespace
|
|
|
|
|
2017-09-20 15:24:15 +08:00
|
|
|
std::vector<Location> clangd::findDefinitions(ParsedAST &AST, Position Pos,
|
|
|
|
clangd::Logger &Logger) {
|
2017-08-01 23:51:38 +08:00
|
|
|
const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
|
|
|
|
const FileEntry *FE = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
|
|
|
|
if (!FE)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
SourceLocation SourceLocationBeg = getBeginningOfIdentifier(AST, Pos, FE);
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
auto DeclLocationsFinder = std::make_shared<DeclarationLocationsFinder>(
|
|
|
|
llvm::errs(), SourceLocationBeg, AST.getASTContext(),
|
|
|
|
AST.getPreprocessor());
|
|
|
|
index::IndexingOptions IndexOpts;
|
|
|
|
IndexOpts.SystemSymbolFilter =
|
|
|
|
index::IndexingOptions::SystemSymbolFilterKind::All;
|
|
|
|
IndexOpts.IndexFunctionLocals = true;
|
|
|
|
|
|
|
|
indexTopLevelDecls(AST.getASTContext(), AST.getTopLevelDecls(),
|
|
|
|
DeclLocationsFinder, IndexOpts);
|
|
|
|
|
|
|
|
return DeclLocationsFinder->takeLocations();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParsedAST::ensurePreambleDeclsDeserialized() {
|
2017-07-21 21:29:29 +08:00
|
|
|
if (PendingTopLevelDecls.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::vector<const Decl *> Resolved;
|
|
|
|
Resolved.reserve(PendingTopLevelDecls.size());
|
|
|
|
|
|
|
|
ExternalASTSource &Source = *getASTContext().getExternalSource();
|
|
|
|
for (serialization::DeclID TopLevelDecl : PendingTopLevelDecls) {
|
|
|
|
// Resolve the declaration ID to an actual declaration, possibly
|
|
|
|
// deserializing the declaration in the process.
|
|
|
|
if (Decl *D = Source.GetExternalDecl(TopLevelDecl))
|
|
|
|
Resolved.push_back(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
TopLevelDecls.reserve(TopLevelDecls.size() + PendingTopLevelDecls.size());
|
|
|
|
TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
|
|
|
|
|
|
|
|
PendingTopLevelDecls.clear();
|
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
ParsedAST::ParsedAST(ParsedAST &&Other) = default;
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default;
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
ParsedAST::~ParsedAST() {
|
2017-07-21 21:29:29 +08:00
|
|
|
if (Action) {
|
|
|
|
Action->EndSourceFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); }
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
const ASTContext &ParsedAST::getASTContext() const {
|
2017-07-21 21:29:29 +08:00
|
|
|
return Clang->getASTContext();
|
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); }
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
const Preprocessor &ParsedAST::getPreprocessor() const {
|
2017-07-21 21:29:29 +08:00
|
|
|
return Clang->getPreprocessor();
|
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
ArrayRef<const Decl *> ParsedAST::getTopLevelDecls() {
|
2017-07-21 21:29:29 +08:00
|
|
|
ensurePreambleDeclsDeserialized();
|
|
|
|
return TopLevelDecls;
|
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
const std::vector<DiagWithFixIts> &ParsedAST::getDiagnostics() const {
|
2017-07-21 21:29:29 +08:00
|
|
|
return Diags;
|
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
ParsedAST::ParsedAST(std::unique_ptr<CompilerInstance> Clang,
|
|
|
|
std::unique_ptr<FrontendAction> Action,
|
|
|
|
std::vector<const Decl *> TopLevelDecls,
|
|
|
|
std::vector<serialization::DeclID> PendingTopLevelDecls,
|
|
|
|
std::vector<DiagWithFixIts> Diags)
|
2017-07-21 21:29:29 +08:00
|
|
|
: Clang(std::move(Clang)), Action(std::move(Action)),
|
|
|
|
Diags(std::move(Diags)), TopLevelDecls(std::move(TopLevelDecls)),
|
|
|
|
PendingTopLevelDecls(std::move(PendingTopLevelDecls)) {
|
|
|
|
assert(this->Clang);
|
|
|
|
assert(this->Action);
|
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
ParsedASTWrapper::ParsedASTWrapper(ParsedASTWrapper &&Wrapper)
|
|
|
|
: AST(std::move(Wrapper.AST)) {}
|
|
|
|
|
|
|
|
ParsedASTWrapper::ParsedASTWrapper(llvm::Optional<ParsedAST> AST)
|
|
|
|
: AST(std::move(AST)) {}
|
|
|
|
|
|
|
|
PreambleData::PreambleData(PrecompiledPreamble Preamble,
|
|
|
|
std::vector<serialization::DeclID> TopLevelDeclIDs,
|
|
|
|
std::vector<DiagWithFixIts> Diags)
|
2017-07-21 21:29:29 +08:00
|
|
|
: Preamble(std::move(Preamble)),
|
|
|
|
TopLevelDeclIDs(std::move(TopLevelDeclIDs)), Diags(std::move(Diags)) {}
|
2017-08-01 23:51:38 +08:00
|
|
|
|
|
|
|
std::shared_ptr<CppFile>
|
|
|
|
CppFile::Create(PathRef FileName, tooling::CompileCommand Command,
|
2017-11-17 00:25:18 +08:00
|
|
|
bool StorePreamblesInMemory,
|
2017-09-20 18:46:58 +08:00
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs,
|
|
|
|
clangd::Logger &Logger) {
|
2017-11-17 00:25:18 +08:00
|
|
|
return std::shared_ptr<CppFile>(new CppFile(FileName, std::move(Command),
|
|
|
|
StorePreamblesInMemory,
|
|
|
|
std::move(PCHs), Logger));
|
2017-08-01 23:51:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CppFile::CppFile(PathRef FileName, tooling::CompileCommand Command,
|
2017-11-17 00:25:18 +08:00
|
|
|
bool StorePreamblesInMemory,
|
2017-09-20 15:24:15 +08:00
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs,
|
|
|
|
clangd::Logger &Logger)
|
2017-11-17 00:25:18 +08:00
|
|
|
: FileName(FileName), Command(std::move(Command)),
|
|
|
|
StorePreamblesInMemory(StorePreamblesInMemory), RebuildCounter(0),
|
2017-09-20 15:24:15 +08:00
|
|
|
RebuildInProgress(false), PCHs(std::move(PCHs)), Logger(Logger) {
|
2017-08-01 23:51:38 +08:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
LatestAvailablePreamble = nullptr;
|
|
|
|
PreamblePromise.set_value(nullptr);
|
|
|
|
PreambleFuture = PreamblePromise.get_future();
|
|
|
|
|
2017-08-02 02:27:58 +08:00
|
|
|
ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None));
|
2017-08-01 23:51:38 +08:00
|
|
|
ASTFuture = ASTPromise.get_future();
|
|
|
|
}
|
|
|
|
|
2017-10-11 00:12:54 +08:00
|
|
|
void CppFile::cancelRebuild() { deferCancelRebuild()(); }
|
2017-08-14 16:17:24 +08:00
|
|
|
|
2017-10-11 00:12:54 +08:00
|
|
|
UniqueFunction<void()> CppFile::deferCancelRebuild() {
|
2017-08-01 23:51:38 +08:00
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
// Cancel an ongoing rebuild, if any, and wait for it to finish.
|
2017-08-14 16:17:24 +08:00
|
|
|
unsigned RequestRebuildCounter = ++this->RebuildCounter;
|
2017-08-01 23:51:38 +08:00
|
|
|
// Rebuild asserts that futures aren't ready if rebuild is cancelled.
|
|
|
|
// We want to keep this invariant.
|
|
|
|
if (futureIsReady(PreambleFuture)) {
|
|
|
|
PreamblePromise = std::promise<std::shared_ptr<const PreambleData>>();
|
|
|
|
PreambleFuture = PreamblePromise.get_future();
|
|
|
|
}
|
|
|
|
if (futureIsReady(ASTFuture)) {
|
2017-08-02 02:27:58 +08:00
|
|
|
ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>();
|
2017-08-01 23:51:38 +08:00
|
|
|
ASTFuture = ASTPromise.get_future();
|
|
|
|
}
|
|
|
|
|
2017-08-14 16:17:24 +08:00
|
|
|
Lock.unlock();
|
|
|
|
// Notify about changes to RebuildCounter.
|
|
|
|
RebuildCond.notify_all();
|
|
|
|
|
|
|
|
std::shared_ptr<CppFile> That = shared_from_this();
|
2017-10-11 00:12:54 +08:00
|
|
|
return [That, RequestRebuildCounter]() {
|
2017-08-14 16:17:24 +08:00
|
|
|
std::unique_lock<std::mutex> Lock(That->Mutex);
|
|
|
|
CppFile *This = &*That;
|
|
|
|
This->RebuildCond.wait(Lock, [This, RequestRebuildCounter]() {
|
|
|
|
return !This->RebuildInProgress ||
|
|
|
|
This->RebuildCounter != RequestRebuildCounter;
|
|
|
|
});
|
|
|
|
|
|
|
|
// This computation got cancelled itself, do nothing.
|
|
|
|
if (This->RebuildCounter != RequestRebuildCounter)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Set empty results for Promises.
|
|
|
|
That->PreamblePromise.set_value(nullptr);
|
|
|
|
That->ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None));
|
2017-10-11 00:12:54 +08:00
|
|
|
};
|
2017-08-01 23:51:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<std::vector<DiagWithFixIts>>
|
|
|
|
CppFile::rebuild(StringRef NewContents,
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
|
2017-10-11 00:12:54 +08:00
|
|
|
return deferRebuild(NewContents, std::move(VFS))();
|
2017-08-01 23:51:38 +08:00
|
|
|
}
|
|
|
|
|
2017-10-11 00:12:54 +08:00
|
|
|
UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>()>
|
2017-08-01 23:51:38 +08:00
|
|
|
CppFile::deferRebuild(StringRef NewContents,
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
|
|
|
|
std::shared_ptr<const PreambleData> OldPreamble;
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs;
|
|
|
|
unsigned RequestRebuildCounter;
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
// Increase RebuildCounter to cancel all ongoing FinishRebuild operations.
|
|
|
|
// They will try to exit as early as possible and won't call set_value on
|
|
|
|
// our promises.
|
|
|
|
RequestRebuildCounter = ++this->RebuildCounter;
|
|
|
|
PCHs = this->PCHs;
|
|
|
|
|
|
|
|
// Remember the preamble to be used during rebuild.
|
|
|
|
OldPreamble = this->LatestAvailablePreamble;
|
|
|
|
// Setup std::promises and std::futures for Preamble and AST. Corresponding
|
|
|
|
// futures will wait until the rebuild process is finished.
|
|
|
|
if (futureIsReady(this->PreambleFuture)) {
|
|
|
|
this->PreamblePromise =
|
|
|
|
std::promise<std::shared_ptr<const PreambleData>>();
|
|
|
|
this->PreambleFuture = this->PreamblePromise.get_future();
|
|
|
|
}
|
|
|
|
if (futureIsReady(this->ASTFuture)) {
|
2017-08-02 02:27:58 +08:00
|
|
|
this->ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>();
|
2017-08-01 23:51:38 +08:00
|
|
|
this->ASTFuture = this->ASTPromise.get_future();
|
|
|
|
}
|
|
|
|
} // unlock Mutex.
|
2017-08-14 16:17:24 +08:00
|
|
|
// Notify about changes to RebuildCounter.
|
|
|
|
RebuildCond.notify_all();
|
2017-08-01 23:51:38 +08:00
|
|
|
|
|
|
|
// A helper to function to finish the rebuild. May be run on a different
|
|
|
|
// thread.
|
|
|
|
|
|
|
|
// Don't let this CppFile die before rebuild is finished.
|
|
|
|
std::shared_ptr<CppFile> That = shared_from_this();
|
|
|
|
auto FinishRebuild = [OldPreamble, VFS, RequestRebuildCounter, PCHs,
|
2017-11-18 03:05:56 +08:00
|
|
|
That](std::string NewContents) mutable // 'mutable' to
|
|
|
|
// allow changing
|
|
|
|
// OldPreamble.
|
2017-08-01 23:51:38 +08:00
|
|
|
-> llvm::Optional<std::vector<DiagWithFixIts>> {
|
|
|
|
// Only one execution of this method is possible at a time.
|
|
|
|
// RebuildGuard will wait for any ongoing rebuilds to finish and will put us
|
|
|
|
// into a state for doing a rebuild.
|
|
|
|
RebuildGuard Rebuild(*That, RequestRebuildCounter);
|
|
|
|
if (Rebuild.wasCancelledBeforeConstruction())
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
std::vector<const char *> ArgStrs;
|
|
|
|
for (const auto &S : That->Command.CommandLine)
|
|
|
|
ArgStrs.push_back(S.c_str());
|
|
|
|
|
|
|
|
VFS->setCurrentWorkingDirectory(That->Command.Directory);
|
|
|
|
|
|
|
|
std::unique_ptr<CompilerInvocation> CI;
|
|
|
|
{
|
|
|
|
// FIXME(ibiryukov): store diagnostics from CommandLine when we start
|
|
|
|
// reporting them.
|
|
|
|
EmptyDiagsConsumer CommandLineDiagsConsumer;
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
|
|
|
|
CompilerInstance::createDiagnostics(new DiagnosticOptions,
|
|
|
|
&CommandLineDiagsConsumer, false);
|
|
|
|
CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS);
|
|
|
|
}
|
|
|
|
assert(CI && "Couldn't create CompilerInvocation");
|
|
|
|
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
|
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(NewContents, That->FileName);
|
|
|
|
|
|
|
|
// A helper function to rebuild the preamble or reuse the existing one. Does
|
2017-11-18 03:05:56 +08:00
|
|
|
// not mutate any fields of CppFile, only does the actual computation.
|
|
|
|
// Lamdba is marked mutable to call reset() on OldPreamble.
|
|
|
|
auto DoRebuildPreamble =
|
|
|
|
[&]() mutable -> std::shared_ptr<const PreambleData> {
|
2017-08-01 23:51:38 +08:00
|
|
|
auto Bounds =
|
|
|
|
ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0);
|
|
|
|
if (OldPreamble && OldPreamble->Preamble.CanReuse(
|
|
|
|
*CI, ContentsBuffer.get(), Bounds, VFS.get())) {
|
|
|
|
return OldPreamble;
|
|
|
|
}
|
2017-11-18 03:05:56 +08:00
|
|
|
// We won't need the OldPreamble anymore, release it so it can be deleted
|
|
|
|
// (if there are no other references to it).
|
|
|
|
OldPreamble.reset();
|
2017-08-01 23:51:38 +08:00
|
|
|
|
2017-11-02 17:21:51 +08:00
|
|
|
trace::Span Tracer(llvm::Twine("Preamble: ") + That->FileName);
|
2017-08-01 23:51:38 +08:00
|
|
|
std::vector<DiagWithFixIts> PreambleDiags;
|
|
|
|
StoreDiagsConsumer PreambleDiagnosticsConsumer(/*ref*/ PreambleDiags);
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine =
|
|
|
|
CompilerInstance::createDiagnostics(
|
|
|
|
&CI->getDiagnosticOpts(), &PreambleDiagnosticsConsumer, false);
|
|
|
|
CppFilePreambleCallbacks SerializedDeclsCollector;
|
|
|
|
auto BuiltPreamble = PrecompiledPreamble::Build(
|
|
|
|
*CI, ContentsBuffer.get(), Bounds, *PreambleDiagsEngine, VFS, PCHs,
|
2017-11-17 00:25:18 +08:00
|
|
|
/*StoreInMemory=*/That->StorePreamblesInMemory,
|
2017-08-01 23:51:38 +08:00
|
|
|
SerializedDeclsCollector);
|
|
|
|
|
|
|
|
if (BuiltPreamble) {
|
|
|
|
return std::make_shared<PreambleData>(
|
|
|
|
std::move(*BuiltPreamble),
|
|
|
|
SerializedDeclsCollector.takeTopLevelDeclIDs(),
|
|
|
|
std::move(PreambleDiags));
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Compute updated Preamble.
|
|
|
|
std::shared_ptr<const PreambleData> NewPreamble = DoRebuildPreamble();
|
|
|
|
// Publish the new Preamble.
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(That->Mutex);
|
|
|
|
// We always set LatestAvailablePreamble to the new value, hoping that it
|
|
|
|
// will still be usable in the further requests.
|
|
|
|
That->LatestAvailablePreamble = NewPreamble;
|
|
|
|
if (RequestRebuildCounter != That->RebuildCounter)
|
|
|
|
return llvm::None; // Our rebuild request was cancelled, do nothing.
|
|
|
|
That->PreamblePromise.set_value(NewPreamble);
|
|
|
|
} // unlock Mutex
|
|
|
|
|
|
|
|
// Prepare the Preamble and supplementary data for rebuilding AST.
|
|
|
|
const PrecompiledPreamble *PreambleForAST = nullptr;
|
|
|
|
ArrayRef<serialization::DeclID> SerializedPreambleDecls = llvm::None;
|
|
|
|
std::vector<DiagWithFixIts> Diagnostics;
|
|
|
|
if (NewPreamble) {
|
|
|
|
PreambleForAST = &NewPreamble->Preamble;
|
|
|
|
SerializedPreambleDecls = NewPreamble->TopLevelDeclIDs;
|
|
|
|
Diagnostics.insert(Diagnostics.begin(), NewPreamble->Diags.begin(),
|
|
|
|
NewPreamble->Diags.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute updated AST.
|
2017-11-02 17:21:51 +08:00
|
|
|
llvm::Optional<ParsedAST> NewAST;
|
|
|
|
{
|
|
|
|
trace::Span Tracer(llvm::Twine("Build: ") + That->FileName);
|
|
|
|
NewAST = ParsedAST::Build(
|
|
|
|
std::move(CI), PreambleForAST, SerializedPreambleDecls,
|
|
|
|
std::move(ContentsBuffer), PCHs, VFS, That->Logger);
|
|
|
|
}
|
2017-08-01 23:51:38 +08:00
|
|
|
|
|
|
|
if (NewAST) {
|
|
|
|
Diagnostics.insert(Diagnostics.end(), NewAST->getDiagnostics().begin(),
|
|
|
|
NewAST->getDiagnostics().end());
|
|
|
|
} else {
|
|
|
|
// Don't report even Preamble diagnostics if we coulnd't build AST.
|
|
|
|
Diagnostics.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish the new AST.
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(That->Mutex);
|
|
|
|
if (RequestRebuildCounter != That->RebuildCounter)
|
|
|
|
return Diagnostics; // Our rebuild request was cancelled, don't set
|
|
|
|
// ASTPromise.
|
|
|
|
|
2017-08-02 17:08:39 +08:00
|
|
|
That->ASTPromise.set_value(
|
|
|
|
std::make_shared<ParsedASTWrapper>(std::move(NewAST)));
|
2017-08-01 23:51:38 +08:00
|
|
|
} // unlock Mutex
|
|
|
|
|
|
|
|
return Diagnostics;
|
|
|
|
};
|
|
|
|
|
2017-10-11 00:12:54 +08:00
|
|
|
return BindWithForward(FinishRebuild, NewContents.str());
|
2017-08-01 23:51:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_future<std::shared_ptr<const PreambleData>>
|
|
|
|
CppFile::getPreamble() const {
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
return PreambleFuture;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<const PreambleData> CppFile::getPossiblyStalePreamble() const {
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
return LatestAvailablePreamble;
|
|
|
|
}
|
|
|
|
|
2017-08-02 02:27:58 +08:00
|
|
|
std::shared_future<std::shared_ptr<ParsedASTWrapper>> CppFile::getAST() const {
|
2017-08-01 23:51:38 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
return ASTFuture;
|
|
|
|
}
|
|
|
|
|
|
|
|
tooling::CompileCommand const &CppFile::getCompileCommand() const {
|
|
|
|
return Command;
|
|
|
|
}
|
|
|
|
|
|
|
|
CppFile::RebuildGuard::RebuildGuard(CppFile &File,
|
|
|
|
unsigned RequestRebuildCounter)
|
|
|
|
: File(File), RequestRebuildCounter(RequestRebuildCounter) {
|
|
|
|
std::unique_lock<std::mutex> Lock(File.Mutex);
|
|
|
|
WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter;
|
|
|
|
if (WasCancelledBeforeConstruction)
|
|
|
|
return;
|
|
|
|
|
2017-08-14 16:17:24 +08:00
|
|
|
File.RebuildCond.wait(Lock, [&File, RequestRebuildCounter]() {
|
|
|
|
return !File.RebuildInProgress ||
|
|
|
|
File.RebuildCounter != RequestRebuildCounter;
|
|
|
|
});
|
2017-08-01 23:51:38 +08:00
|
|
|
|
|
|
|
WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter;
|
|
|
|
if (WasCancelledBeforeConstruction)
|
|
|
|
return;
|
|
|
|
|
|
|
|
File.RebuildInProgress = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CppFile::RebuildGuard::wasCancelledBeforeConstruction() const {
|
|
|
|
return WasCancelledBeforeConstruction;
|
|
|
|
}
|
|
|
|
|
|
|
|
CppFile::RebuildGuard::~RebuildGuard() {
|
|
|
|
if (WasCancelledBeforeConstruction)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> Lock(File.Mutex);
|
|
|
|
assert(File.RebuildInProgress);
|
|
|
|
File.RebuildInProgress = false;
|
|
|
|
|
|
|
|
if (File.RebuildCounter == RequestRebuildCounter) {
|
|
|
|
// Our rebuild request was successful.
|
|
|
|
assert(futureIsReady(File.ASTFuture));
|
|
|
|
assert(futureIsReady(File.PreambleFuture));
|
|
|
|
} else {
|
|
|
|
// Our rebuild request was cancelled, because further reparse was requested.
|
|
|
|
assert(!futureIsReady(File.ASTFuture));
|
|
|
|
assert(!futureIsReady(File.PreambleFuture));
|
|
|
|
}
|
|
|
|
|
|
|
|
Lock.unlock();
|
|
|
|
File.RebuildCond.notify_all();
|
|
|
|
}
|
2017-11-09 19:30:04 +08:00
|
|
|
|
|
|
|
SourceLocation clangd::getBeginningOfIdentifier(ParsedAST &Unit,
|
|
|
|
const Position &Pos,
|
|
|
|
const FileEntry *FE) {
|
|
|
|
// The language server protocol uses zero-based line and column numbers.
|
|
|
|
// Clang uses one-based numbers.
|
|
|
|
|
|
|
|
const ASTContext &AST = Unit.getASTContext();
|
|
|
|
const SourceManager &SourceMgr = AST.getSourceManager();
|
|
|
|
|
|
|
|
SourceLocation InputLocation =
|
|
|
|
getMacroArgExpandedLocation(SourceMgr, FE, Pos);
|
|
|
|
if (Pos.character == 0) {
|
|
|
|
return InputLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This handle cases where the position is in the middle of a token or right
|
|
|
|
// after the end of a token. In theory we could just use GetBeginningOfToken
|
|
|
|
// to find the start of the token at the input position, but this doesn't
|
|
|
|
// work when right after the end, i.e. foo|.
|
|
|
|
// So try to go back by one and see if we're still inside the an identifier
|
|
|
|
// token. If so, Take the beginning of this token.
|
|
|
|
// (It should be the same identifier because you can't have two adjacent
|
|
|
|
// identifiers without another token in between.)
|
|
|
|
SourceLocation PeekBeforeLocation = getMacroArgExpandedLocation(
|
|
|
|
SourceMgr, FE, Position{Pos.line, Pos.character - 1});
|
|
|
|
Token Result;
|
|
|
|
if (Lexer::getRawToken(PeekBeforeLocation, Result, SourceMgr,
|
|
|
|
AST.getLangOpts(), false)) {
|
|
|
|
// getRawToken failed, just use InputLocation.
|
|
|
|
return InputLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Result.is(tok::raw_identifier)) {
|
|
|
|
return Lexer::GetBeginningOfToken(PeekBeforeLocation, SourceMgr,
|
|
|
|
AST.getLangOpts());
|
|
|
|
}
|
|
|
|
|
|
|
|
return InputLocation;
|
|
|
|
}
|