clang-tools 10.0.0

TestIndex.cpp
Go to the documentation of this file.
1 //===-- TestIndex.cpp -------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "TestIndex.h"
10 #include "clang/Index/IndexSymbol.h"
11 #include "llvm/Support/Regex.h"
12
13 namespace clang {
14 namespace clangd {
15
16 Symbol symbol(llvm::StringRef QName) {
17 Symbol Sym;
18 Sym.ID = SymbolID(QName.str());
19 size_t Pos = QName.rfind("::");
20 if (Pos == llvm::StringRef::npos) {
21 Sym.Name = QName;
22 Sym.Scope = "";
23 } else {
24 Sym.Name = QName.substr(Pos + 2);
25 Sym.Scope = QName.substr(0, Pos + 2);
26 }
27 return Sym;
28 }
29
30 static std::string replace(llvm::StringRef Haystack, llvm::StringRef Needle,
31 llvm::StringRef Repl) {
32 llvm::SmallVector<llvm::StringRef, 8> Parts;
33 Haystack.split(Parts, Needle);
34 return llvm::join(Parts, Repl);
35 }
36
37 // Helpers to produce fake index symbols for memIndex() or completions().
38 // USRFormat is a regex replacement string for the unqualified part of the USR.
39 Symbol sym(llvm::StringRef QName, index::SymbolKind Kind,
40 llvm::StringRef USRFormat) {
41 Symbol Sym;
42 std::string USR = "c:"; // We synthesize a few simple cases of USRs by hand!
43 size_t Pos = QName.rfind("::");
44 if (Pos == llvm::StringRef::npos) {
45 Sym.Name = QName;
46 Sym.Scope = "";
47 } else {
48 Sym.Name = QName.substr(Pos + 2);
49 Sym.Scope = QName.substr(0, Pos + 2);
50 USR += "@N@" + replace(QName.substr(0, Pos), "::", "@N@"); // ns:: -> @N@ns
51 }
52 USR += llvm::Regex("^.*$").sub(USRFormat, Sym.Name); // e.g. func -> @F@func#
53 Sym.ID = SymbolID(USR);
54 Sym.SymInfo.Kind = Kind;
55 Sym.Flags |= Symbol::IndexedForCodeCompletion;
56 Sym.Origin = SymbolOrigin::Static;
57 return Sym;
58 }
59
60 Symbol func(llvm::StringRef Name) { // Assumes the function has no args.
61 return sym(Name, index::SymbolKind::Function, "@F@\\0#"); // no args
62 }
63
64 Symbol cls(llvm::StringRef Name) {
65 return sym(Name, index::SymbolKind::Class, "@S@\\0");
66 }
67
68 Symbol var(llvm::StringRef Name) {
69 return sym(Name, index::SymbolKind::Variable, "@\\0");
70 }
71
72 Symbol ns(llvm::StringRef Name) {
73 return sym(Name, index::SymbolKind::Namespace, "@N@\\0");
74 }
75
76 SymbolSlab generateSymbols(std::vector<std::string> QualifiedNames) {
77 SymbolSlab::Builder Slab;
78 for (llvm::StringRef QName : QualifiedNames)
79 Slab.insert(symbol(QName));
80 return std::move(Slab).build();
81 }
82
83 SymbolSlab generateNumSymbols(int Begin, int End) {
84 std::vector<std::string> Names;
85 for (int i = Begin; i <= End; i++)
86 Names.push_back(std::to_string(i));
87 return generateSymbols(Names);
88 }
89
90 std::string getQualifiedName(const Symbol &Sym) {
91 return (Sym.Scope + Sym.Name + Sym.TemplateSpecializationArgs).str();
92 }
93
94 std::vector<std::string> match(const SymbolIndex &I,
95 const FuzzyFindRequest &Req, bool *Incomplete) {
96 std::vector<std::string> Matches;
97 bool IsIncomplete = I.fuzzyFind(Req, [&](const Symbol &Sym) {
98 Matches.push_back(clang::clangd::getQualifiedName(Sym));
99 });
100 if (Incomplete)
101 *Incomplete = IsIncomplete;
102 return Matches;
103 }
104
105 // Returns qualified names of symbols with any of IDs in the index.
106 std::vector<std::string> lookup(const SymbolIndex &I,
107 llvm::ArrayRef<SymbolID> IDs) {
108 LookupRequest Req;
109 Req.IDs.insert(IDs.begin(), IDs.end());
110 std::vector<std::string> Results;
111 I.lookup(Req, [&](const Symbol &Sym) {
112 Results.push_back(getQualifiedName(Sym));
113 });
114 return Results;
115 }
116
117 } // namespace clangd
118 } // namespace clang
clang::clangd::SymbolIndex::lookup
virtual void lookup(const LookupRequest &Req, llvm::function_ref< void(const Symbol &)> Callback) const =0
Looks up symbols with any of the given symbol IDs and applies Callback on each matched symbol...
clang::clangd::symbol
Symbol symbol(llvm::StringRef QName)
Definition: TestIndex.cpp:16
clang::clangd::SymbolSlab
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
clang::clangd::cls
Symbol cls(llvm::StringRef Name)
Definition: TestIndex.cpp:64
SymbolKind
clang::find_all_symbols::SymbolInfo::SymbolKind SymbolKind
Definition: SymbolInfo.cpp:21
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:85
clang::clangd::LookupRequest::IDs
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:64
clang::clangd::ns
Symbol ns(llvm::StringRef Name)
Definition: TestIndex.cpp:72
clang::clangd::SymbolIndex::fuzzyFind
virtual bool fuzzyFind(const FuzzyFindRequest &Req, llvm::function_ref< void(const Symbol &)> Callback) const =0
Matches symbols in the index fuzzily and applies Callback on each matched symbol before returning...
clang::clangd::SymbolSlab::Builder::insert
void insert(const Symbol &S)
Adds a symbol, overwriting any existing one with the same ID.
Definition: Symbol.cpp:50
Results
std::vector< CodeCompletionResult > Results
Definition: CodeComplete.cpp:689
clang::clangd::Symbol::Scope
llvm::StringRef Scope
The containing namespace. e.g. "" (global), "ns::" (top-level namespace).
Definition: Symbol.h:44
clang::clangd::FuzzyFindRequest
Definition: Index.h:26
clang::clangd::sym
Symbol sym(llvm::StringRef QName, index::SymbolKind Kind, llvm::StringRef USRFormat)
Definition: TestIndex.cpp:39
clang::clangd::replace
static std::string replace(llvm::StringRef Haystack, llvm::StringRef Needle, llvm::StringRef Repl)
Definition: TestIndex.cpp:30
clang::clangd::var
Symbol var(llvm::StringRef Name)
Definition: TestIndex.cpp:68
clang::clangd::SymbolSlab::Builder
SymbolSlab::Builder is a mutable container that can &#39;freeze&#39; to SymbolSlab.
Definition: Symbol.h:199
clang::clangd::Symbol::ID
SymbolID ID
The ID of the symbol.
Definition: Symbol.h:38
clang::clangd::Symbol::SymInfo
index::SymbolInfo SymInfo
The symbol information, like symbol kind.
Definition: Symbol.h:40
Kind
BindArgumentKind Kind
Definition: AvoidBindCheck.cpp:57
QName
std::string QName
Definition: DiagnosticsTests.cpp:523
clang::clangd::Symbol::IndexedForCodeCompletion
Whether or not this symbol is meant to be used for the code completion.
Definition: Symbol.h:119
clang::clangd::lookup
std::vector< std::string > lookup(const SymbolIndex &I, llvm::ArrayRef< SymbolID > IDs)
Definition: TestIndex.cpp:106
clang::clangd::match
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
Definition: TestIndex.cpp:94
clang::clangd::Symbol::Flags
SymbolFlag Flags
Definition: Symbol.h:128
clang::clangd::generateSymbols
SymbolSlab generateSymbols(std::vector< std::string > QualifiedNames)
Definition: TestIndex.cpp:76
clang::clangd::SymbolOrigin::Static
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
Pos
Position Pos
Definition: SourceCode.cpp:772
clang::clangd::generateNumSymbols
SymbolSlab generateNumSymbols(int Begin, int End)
Definition: TestIndex.cpp:83
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
clang::clangd::getQualifiedName
std::string getQualifiedName(const Symbol &Sym)
Definition: TestIndex.cpp:90
clang::clangd::LookupRequest
Definition: Index.h:63
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::Symbol::Name
llvm::StringRef Name
The unqualified name of the symbol, e.g. "bar" (for ns::bar).
Definition: Symbol.h:42
TestIndex.h
clang::tidy::cppcoreguidelines::join
static std::string join(ArrayRef< SpecialMemberFunctionsCheck::SpecialMemberFunctionKind > SMFS, llvm::StringRef AndOr)
Definition: SpecialMemberFunctionsCheck.cpp:81
clang::clangd::Symbol::Origin
SymbolOrigin Origin
Where this symbol came from. Usually an index provides a constant value.
Definition: Symbol.h:61
clang::clangd::Symbol::TemplateSpecializationArgs
llvm::StringRef TemplateSpecializationArgs
Argument list in human-readable format, will be displayed to help disambiguate between different spec...
Definition: Symbol.h:69
clang::doc::SymbolID
std::array< uint8_t, 20 > SymbolID
Definition: Representation.h:30
clang::clangd::func
Symbol func(llvm::StringRef Name)
Definition: TestIndex.cpp:60