clang-tools 19.0.0git
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/ADT/StringExtras.h"
12#include "llvm/Support/Regex.h"
13
14namespace clang {
15namespace clangd {
16
17Symbol symbol(llvm::StringRef QName) {
18 Symbol Sym;
19 Sym.ID = SymbolID(QName.str());
20 size_t Pos = QName.rfind("::");
21 if (Pos == llvm::StringRef::npos) {
22 Sym.Name = QName;
23 Sym.Scope = "";
24 } else {
25 Sym.Name = QName.substr(Pos + 2);
26 Sym.Scope = QName.substr(0, Pos + 2);
27 }
28 return Sym;
29}
30
31static std::string replace(llvm::StringRef Haystack, llvm::StringRef Needle,
32 llvm::StringRef Repl) {
33 llvm::SmallVector<llvm::StringRef> Parts;
34 Haystack.split(Parts, Needle);
35 return llvm::join(Parts, Repl);
36}
37
38// Helpers to produce fake index symbols for memIndex() or completions().
39// USRFormat is a regex replacement string for the unqualified part of the USR.
40Symbol sym(llvm::StringRef QName, index::SymbolKind Kind,
41 llvm::StringRef USRFormat) {
42 Symbol Sym;
43 std::string USR = "c:"; // We synthesize a few simple cases of USRs by hand!
44 size_t Pos = QName.rfind("::");
45 if (Pos == llvm::StringRef::npos) {
46 Sym.Name = QName;
47 Sym.Scope = "";
48 } else {
49 Sym.Name = QName.substr(Pos + 2);
50 Sym.Scope = QName.substr(0, Pos + 2);
51 USR += "@N@" + replace(QName.substr(0, Pos), "::", "@N@"); // ns:: -> @N@ns
52 }
53 USR += llvm::Regex("^.*$").sub(USRFormat, Sym.Name); // e.g. func -> @F@func#
54 Sym.ID = SymbolID(USR);
55 Sym.SymInfo.Kind = Kind;
58 return Sym;
59}
60
61Symbol func(llvm::StringRef Name) { // Assumes the function has no args.
62 return sym(Name, index::SymbolKind::Function, "@F@\\0#"); // no args
63}
64
65Symbol cls(llvm::StringRef Name) {
66 return sym(Name, index::SymbolKind::Class, "@S@\\0");
67}
68
69Symbol enm(llvm::StringRef Name) {
70 return sym(Name, index::SymbolKind::Enum, "@E@\\0");
71}
72
73Symbol enmConstant(llvm::StringRef Name) {
74 return sym(Name, index::SymbolKind::EnumConstant, "@\\0");
75}
76
77Symbol var(llvm::StringRef Name) {
78 return sym(Name, index::SymbolKind::Variable, "@\\0");
79}
80
81Symbol ns(llvm::StringRef Name) {
82 return sym(Name, index::SymbolKind::Namespace, "@N@\\0");
83}
84
85Symbol conceptSym(llvm::StringRef Name) {
86 return sym(Name, index::SymbolKind::Concept, "@CT@\\0");
87}
88
89Symbol objcSym(llvm::StringRef Name, index::SymbolKind Kind,
90 llvm::StringRef USRPrefix) {
91 Symbol Sym;
92 std::string USR = USRPrefix.str() + Name.str();
93 Sym.Name = Name;
94 Sym.Scope = "";
95 Sym.ID = SymbolID(USR);
96 Sym.SymInfo.Kind = Kind;
97 Sym.SymInfo.Lang = index::SymbolLanguage::ObjC;
100 return Sym;
101}
102
103Symbol objcClass(llvm::StringRef Name) {
104 return objcSym(Name, index::SymbolKind::Class, "objc(cs)");
105}
106
107Symbol objcCategory(llvm::StringRef Name, llvm::StringRef CategoryName) {
108 std::string USRPrefix = ("objc(cy)" + Name + "@").str();
109 return objcSym(CategoryName, index::SymbolKind::Extension, USRPrefix);
110}
111
112Symbol objcProtocol(llvm::StringRef Name) {
113 return objcSym(Name, index::SymbolKind::Protocol, "objc(pl)");
114}
115
116SymbolSlab generateSymbols(std::vector<std::string> QualifiedNames) {
118 for (llvm::StringRef QName : QualifiedNames)
119 Slab.insert(symbol(QName));
120 return std::move(Slab).build();
121}
122
123SymbolSlab generateNumSymbols(int Begin, int End) {
124 std::vector<std::string> Names;
125 for (int I = Begin; I <= End; I++)
126 Names.push_back(std::to_string(I));
127 return generateSymbols(Names);
128}
129
130std::string getQualifiedName(const Symbol &Sym) {
131 return (Sym.Scope + Sym.Name + Sym.TemplateSpecializationArgs).str();
132}
133
134std::vector<std::string> match(const SymbolIndex &I,
135 const FuzzyFindRequest &Req, bool *Incomplete) {
136 std::vector<std::string> Matches;
137 bool IsIncomplete = I.fuzzyFind(Req, [&](const Symbol &Sym) {
138 Matches.push_back(clang::clangd::getQualifiedName(Sym));
139 });
140 if (Incomplete)
141 *Incomplete = IsIncomplete;
142 return Matches;
143}
144
145// Returns qualified names of symbols with any of IDs in the index.
146std::vector<std::string> lookup(const SymbolIndex &I,
147 llvm::ArrayRef<SymbolID> IDs) {
148 LookupRequest Req;
149 Req.IDs.insert(IDs.begin(), IDs.end());
150 std::vector<std::string> Results;
151 I.lookup(Req, [&](const Symbol &Sym) {
152 Results.push_back(getQualifiedName(Sym));
153 });
154 return Results;
155}
156
157} // namespace clangd
158} // namespace clang
BindArgumentKind Kind
llvm::SmallString< 256U > Name
std::vector< CodeCompletionResult > Results
size_t Pos
std::string USR
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:113
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.
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.
SymbolSlab::Builder is a mutable container that can 'freeze' to SymbolSlab.
Definition: Symbol.h:222
void insert(const Symbol &S)
Adds a symbol, overwriting any existing one with the same ID.
Definition: Symbol.cpp:52
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:199
static std::string replace(llvm::StringRef Haystack, llvm::StringRef Needle, llvm::StringRef Repl)
Definition: TestIndex.cpp:31
Symbol objcProtocol(llvm::StringRef Name)
Definition: TestIndex.cpp:112
Symbol objcClass(llvm::StringRef Name)
Definition: TestIndex.cpp:103
Symbol func(llvm::StringRef Name)
Definition: TestIndex.cpp:61
Symbol cls(llvm::StringRef Name)
Definition: TestIndex.cpp:65
Symbol objcCategory(llvm::StringRef Name, llvm::StringRef CategoryName)
Definition: TestIndex.cpp:107
Symbol conceptSym(llvm::StringRef Name)
Definition: TestIndex.cpp:85
Symbol ns(llvm::StringRef Name)
Definition: TestIndex.cpp:81
Symbol sym(llvm::StringRef QName, index::SymbolKind Kind, llvm::StringRef USRFormat)
Definition: TestIndex.cpp:40
Symbol objcSym(llvm::StringRef Name, index::SymbolKind Kind, llvm::StringRef USRPrefix)
Definition: TestIndex.cpp:89
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
Definition: TestIndex.cpp:134
Symbol symbol(llvm::StringRef QName)
Definition: TestIndex.cpp:17
Symbol enm(llvm::StringRef Name)
Definition: TestIndex.cpp:69
SymbolSlab generateSymbols(std::vector< std::string > QualifiedNames)
Definition: TestIndex.cpp:116
std::string getQualifiedName(const Symbol &Sym)
Definition: TestIndex.cpp:130
std::vector< std::string > lookup(const SymbolIndex &I, llvm::ArrayRef< SymbolID > IDs)
Definition: TestIndex.cpp:146
Symbol enmConstant(llvm::StringRef Name)
Definition: TestIndex.cpp:73
SymbolSlab generateNumSymbols(int Begin, int End)
Definition: TestIndex.cpp:123
Symbol var(llvm::StringRef Name)
Definition: TestIndex.cpp:77
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:65
The class presents a C++ symbol, e.g.
Definition: Symbol.h:39
SymbolFlag Flags
Definition: Symbol.h:150
@ IndexedForCodeCompletion
Whether or not this symbol is meant to be used for the code completion.
Definition: Symbol.h:141
index::SymbolInfo SymInfo
The symbol information, like symbol kind.
Definition: Symbol.h:43
llvm::StringRef Name
The unqualified name of the symbol, e.g. "bar" (for ns::bar).
Definition: Symbol.h:45
llvm::StringRef Scope
The containing namespace. e.g. "" (global), "ns::" (top-level namespace).
Definition: Symbol.h:47
llvm::StringRef TemplateSpecializationArgs
Argument list in human-readable format, will be displayed to help disambiguate between different spec...
Definition: Symbol.h:72
SymbolID ID
The ID of the symbol.
Definition: Symbol.h:41
SymbolOrigin Origin
Where this symbol came from. Usually an index provides a constant value.
Definition: Symbol.h:64