2018-05-28 20:23:17 +08:00
|
|
|
//===--- TestTU.cpp - Scratch source files for testing --------------------===//
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-16 01:43:27 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
#include "TestTU.h"
|
|
|
|
#include "TestFS.h"
|
|
|
|
#include "index/FileIndex.h"
|
|
|
|
#include "index/MemIndex.h"
|
2018-06-06 00:30:25 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-16 01:43:27 +08:00
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
|
|
|
#include "clang/Frontend/PCHContainerOperations.h"
|
|
|
|
#include "clang/Frontend/Utils.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
ParsedAST TestTU::build() const {
|
|
|
|
std::string FullFilename = testPath(Filename),
|
|
|
|
FullHeaderName = testPath(HeaderFilename);
|
|
|
|
std::vector<const char *> Cmd = {"clang", FullFilename.c_str()};
|
|
|
|
// FIXME: this shouldn't need to be conditional, but it breaks a
|
|
|
|
// GoToDefinition test for some reason (getMacroArgExpandedLocation fails).
|
|
|
|
if (!HeaderCode.empty()) {
|
|
|
|
Cmd.push_back("-include");
|
|
|
|
Cmd.push_back(FullHeaderName.c_str());
|
|
|
|
}
|
2018-07-03 00:28:34 +08:00
|
|
|
Cmd.insert(Cmd.end(), ExtraArgs.begin(), ExtraArgs.end());
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-16 01:43:27 +08:00
|
|
|
auto AST = ParsedAST::Build(
|
|
|
|
createInvocationFromCommandLine(Cmd), nullptr,
|
|
|
|
MemoryBuffer::getMemBufferCopy(Code),
|
|
|
|
std::make_shared<PCHContainerOperations>(),
|
|
|
|
buildTestFS({{FullFilename, Code}, {FullHeaderName, HeaderCode}}));
|
|
|
|
if (!AST.hasValue()) {
|
|
|
|
ADD_FAILURE() << "Failed to build code:\n" << Code;
|
|
|
|
llvm_unreachable("Failed to build TestTU!");
|
|
|
|
}
|
|
|
|
return std::move(*AST);
|
|
|
|
}
|
|
|
|
|
|
|
|
SymbolSlab TestTU::headerSymbols() const {
|
|
|
|
auto AST = build();
|
2018-05-24 23:50:15 +08:00
|
|
|
return indexAST(AST.getASTContext(), AST.getPreprocessorPtr());
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-16 01:43:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<SymbolIndex> TestTU::index() const {
|
|
|
|
return MemIndex::build(headerSymbols());
|
|
|
|
}
|
|
|
|
|
|
|
|
const Symbol &findSymbol(const SymbolSlab &Slab, llvm::StringRef QName) {
|
|
|
|
const Symbol *Result = nullptr;
|
|
|
|
for (const Symbol &S : Slab) {
|
|
|
|
if (QName != (S.Scope + S.Name).str())
|
|
|
|
continue;
|
|
|
|
if (Result) {
|
|
|
|
ADD_FAILURE() << "Multiple symbols named " << QName << ":\n"
|
|
|
|
<< *Result << "\n---\n"
|
|
|
|
<< S;
|
|
|
|
assert(false && "QName is not unique");
|
|
|
|
}
|
|
|
|
Result = &S;
|
|
|
|
}
|
|
|
|
if (!Result) {
|
|
|
|
ADD_FAILURE() << "No symbol named " << QName << " in "
|
|
|
|
<< ::testing::PrintToString(Slab);
|
|
|
|
assert(false && "No symbol with QName");
|
|
|
|
}
|
|
|
|
return *Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName) {
|
2018-05-28 20:23:17 +08:00
|
|
|
llvm::SmallVector<llvm::StringRef, 4> Components;
|
|
|
|
QName.split(Components, "::");
|
|
|
|
|
|
|
|
auto &Ctx = AST.getASTContext();
|
|
|
|
auto LookupDecl = [&Ctx](const DeclContext &Scope,
|
|
|
|
llvm::StringRef Name) -> const NamedDecl & {
|
|
|
|
auto LookupRes = Scope.lookup(DeclarationName(&Ctx.Idents.get(Name)));
|
|
|
|
assert(!LookupRes.empty() && "Lookup failed");
|
|
|
|
assert(LookupRes.size() == 1 && "Lookup returned multiple results");
|
|
|
|
return *LookupRes.front();
|
|
|
|
};
|
|
|
|
|
|
|
|
const DeclContext *Scope = Ctx.getTranslationUnitDecl();
|
|
|
|
for (auto NameIt = Components.begin(), End = Components.end() - 1;
|
|
|
|
NameIt != End; ++NameIt) {
|
|
|
|
Scope = &cast<DeclContext>(LookupDecl(*Scope, *NameIt));
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-16 01:43:27 +08:00
|
|
|
}
|
2018-05-28 20:23:17 +08:00
|
|
|
return LookupDecl(*Scope, Components.back());
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-16 01:43:27 +08:00
|
|
|
}
|
|
|
|
|
2018-07-05 16:14:04 +08:00
|
|
|
const NamedDecl &findAnyDecl(ParsedAST &AST,
|
|
|
|
std::function<bool(const NamedDecl &)> Callback) {
|
2018-06-06 00:30:25 +08:00
|
|
|
struct Visitor : RecursiveASTVisitor<Visitor> {
|
2018-07-05 16:14:04 +08:00
|
|
|
decltype(Callback) CB;
|
2018-06-06 00:30:25 +08:00
|
|
|
llvm::SmallVector<const NamedDecl *, 1> Decls;
|
|
|
|
bool VisitNamedDecl(const NamedDecl *ND) {
|
2018-07-05 16:14:04 +08:00
|
|
|
if (CB(*ND))
|
|
|
|
Decls.push_back(ND);
|
2018-06-06 00:30:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} Visitor;
|
2018-07-05 16:14:04 +08:00
|
|
|
Visitor.CB = Callback;
|
2018-06-06 00:30:25 +08:00
|
|
|
for (Decl *D : AST.getLocalTopLevelDecls())
|
|
|
|
Visitor.TraverseDecl(D);
|
|
|
|
if (Visitor.Decls.size() != 1) {
|
2018-07-05 16:14:04 +08:00
|
|
|
ADD_FAILURE() << Visitor.Decls.size() << " symbols matched.";
|
2018-06-06 00:30:25 +08:00
|
|
|
assert(Visitor.Decls.size() == 1);
|
|
|
|
}
|
|
|
|
return *Visitor.Decls.front();
|
|
|
|
}
|
|
|
|
|
2018-07-05 16:14:04 +08:00
|
|
|
const NamedDecl &findAnyDecl(ParsedAST &AST, llvm::StringRef Name) {
|
|
|
|
return findAnyDecl(AST, [Name](const NamedDecl &ND) {
|
|
|
|
if (auto *ID = ND.getIdentifier())
|
|
|
|
if (ID->getName() == Name)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-16 01:43:27 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|