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
symbol(llvm::StringRef QName)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
replace(llvm::StringRef Haystack,llvm::StringRef Needle,llvm::StringRef Repl)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.
sym(llvm::StringRef QName,index::SymbolKind Kind,llvm::StringRef USRFormat)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
func(llvm::StringRef Name)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
cls(llvm::StringRef Name)64 Symbol cls(llvm::StringRef Name) {
65 return sym(Name, index::SymbolKind::Class, "@S@\\0");
66 }
67
var(llvm::StringRef Name)68 Symbol var(llvm::StringRef Name) {
69 return sym(Name, index::SymbolKind::Variable, "@\\0");
70 }
71
ns(llvm::StringRef Name)72 Symbol ns(llvm::StringRef Name) {
73 return sym(Name, index::SymbolKind::Namespace, "@N@\\0");
74 }
75
generateSymbols(std::vector<std::string> QualifiedNames)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
generateNumSymbols(int Begin,int End)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
getQualifiedName(const Symbol & Sym)90 std::string getQualifiedName(const Symbol &Sym) {
91 return (Sym.Scope + Sym.Name + Sym.TemplateSpecializationArgs).str();
92 }
93
match(const SymbolIndex & I,const FuzzyFindRequest & Req,bool * Incomplete)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.
lookup(const SymbolIndex & I,llvm::ArrayRef<SymbolID> IDs)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
119