1 //===-- TestIndex.cpp -------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 #include "clang/Index/IndexSymbol.h"
11 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/Support/Regex.h"
17 Symbol
symbol(llvm::StringRef QName
) {
19 Sym
.ID
= SymbolID(QName
.str());
20 size_t Pos
= QName
.rfind("::");
21 if (Pos
== llvm::StringRef::npos
) {
25 Sym
.Name
= QName
.substr(Pos
+ 2);
26 Sym
.Scope
= QName
.substr(0, Pos
+ 2);
31 static 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
);
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.
40 Symbol
sym(llvm::StringRef QName
, index::SymbolKind Kind
,
41 llvm::StringRef USRFormat
) {
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
) {
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
53 USR
+= llvm::Regex("^.*$").sub(USRFormat
, Sym
.Name
); // e.g. func -> @F@func#
54 Sym
.ID
= SymbolID(USR
);
55 Sym
.SymInfo
.Kind
= Kind
;
56 Sym
.Flags
|= Symbol::IndexedForCodeCompletion
;
57 Sym
.Origin
= SymbolOrigin::Static
;
61 Symbol
func(llvm::StringRef Name
) { // Assumes the function has no args.
62 return sym(Name
, index::SymbolKind::Function
, "@F@\\0#"); // no args
65 Symbol
cls(llvm::StringRef Name
) {
66 return sym(Name
, index::SymbolKind::Class
, "@S@\\0");
69 Symbol
enm(llvm::StringRef Name
) {
70 return sym(Name
, index::SymbolKind::Enum
, "@E@\\0");
73 Symbol
enmConstant(llvm::StringRef Name
) {
74 return sym(Name
, index::SymbolKind::EnumConstant
, "@\\0");
77 Symbol
var(llvm::StringRef Name
) {
78 return sym(Name
, index::SymbolKind::Variable
, "@\\0");
81 Symbol
ns(llvm::StringRef Name
) {
82 return sym(Name
, index::SymbolKind::Namespace
, "@N@\\0");
85 Symbol
conceptSym(llvm::StringRef Name
) {
86 return sym(Name
, index::SymbolKind::Concept
, "@CT@\\0");
89 Symbol
objcSym(llvm::StringRef Name
, index::SymbolKind Kind
,
90 llvm::StringRef USRPrefix
) {
92 std::string USR
= USRPrefix
.str() + Name
.str();
95 Sym
.ID
= SymbolID(USR
);
96 Sym
.SymInfo
.Kind
= Kind
;
97 Sym
.SymInfo
.Lang
= index::SymbolLanguage::ObjC
;
98 Sym
.Flags
|= Symbol::IndexedForCodeCompletion
;
99 Sym
.Origin
= SymbolOrigin::Static
;
103 Symbol
objcClass(llvm::StringRef Name
) {
104 return objcSym(Name
, index::SymbolKind::Class
, "objc(cs)");
107 Symbol
objcCategory(llvm::StringRef Name
, llvm::StringRef CategoryName
) {
108 std::string USRPrefix
= ("objc(cy)" + Name
+ "@").str();
109 return objcSym(CategoryName
, index::SymbolKind::Extension
, USRPrefix
);
112 Symbol
objcProtocol(llvm::StringRef Name
) {
113 return objcSym(Name
, index::SymbolKind::Protocol
, "objc(pl)");
116 SymbolSlab
generateSymbols(std::vector
<std::string
> QualifiedNames
) {
117 SymbolSlab::Builder Slab
;
118 for (llvm::StringRef QName
: QualifiedNames
)
119 Slab
.insert(symbol(QName
));
120 return std::move(Slab
).build();
123 SymbolSlab
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
);
130 std::string
getQualifiedName(const Symbol
&Sym
) {
131 return (Sym
.Scope
+ Sym
.Name
+ Sym
.TemplateSpecializationArgs
).str();
134 std::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
));
141 *Incomplete
= IsIncomplete
;
145 // Returns qualified names of symbols with any of IDs in the index.
146 std::vector
<std::string
> lookup(const SymbolIndex
&I
,
147 llvm::ArrayRef
<SymbolID
> IDs
) {
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
));
157 } // namespace clangd