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/Support/Regex.h"
16 Symbol
symbol(llvm::StringRef QName
) {
18 Sym
.ID
= SymbolID(QName
.str());
19 size_t Pos
= QName
.rfind("::");
20 if (Pos
== llvm::StringRef::npos
) {
24 Sym
.Name
= QName
.substr(Pos
+ 2);
25 Sym
.Scope
= QName
.substr(0, Pos
+ 2);
30 static std::string
replace(llvm::StringRef Haystack
, llvm::StringRef Needle
,
31 llvm::StringRef Repl
) {
32 llvm::SmallVector
<llvm::StringRef
> Parts
;
33 Haystack
.split(Parts
, Needle
);
34 return llvm::join(Parts
, Repl
);
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
) {
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
) {
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
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
;
60 Symbol
func(llvm::StringRef Name
) { // Assumes the function has no args.
61 return sym(Name
, index::SymbolKind::Function
, "@F@\\0#"); // no args
64 Symbol
cls(llvm::StringRef Name
) {
65 return sym(Name
, index::SymbolKind::Class
, "@S@\\0");
68 Symbol
enm(llvm::StringRef Name
) {
69 return sym(Name
, index::SymbolKind::Enum
, "@E@\\0");
72 Symbol
var(llvm::StringRef Name
) {
73 return sym(Name
, index::SymbolKind::Variable
, "@\\0");
76 Symbol
ns(llvm::StringRef Name
) {
77 return sym(Name
, index::SymbolKind::Namespace
, "@N@\\0");
80 Symbol
conceptSym(llvm::StringRef Name
) {
81 return sym(Name
, index::SymbolKind::Concept
, "@CT@\\0");
84 Symbol
objcSym(llvm::StringRef Name
, index::SymbolKind Kind
,
85 llvm::StringRef USRPrefix
) {
87 std::string USR
= USRPrefix
.str() + Name
.str();
90 Sym
.ID
= SymbolID(USR
);
91 Sym
.SymInfo
.Kind
= Kind
;
92 Sym
.SymInfo
.Lang
= index::SymbolLanguage::ObjC
;
93 Sym
.Flags
|= Symbol::IndexedForCodeCompletion
;
94 Sym
.Origin
= SymbolOrigin::Static
;
98 Symbol
objcClass(llvm::StringRef Name
) {
99 return objcSym(Name
, index::SymbolKind::Class
, "objc(cs)");
102 Symbol
objcProtocol(llvm::StringRef Name
) {
103 return objcSym(Name
, index::SymbolKind::Protocol
, "objc(pl)");
106 SymbolSlab
generateSymbols(std::vector
<std::string
> QualifiedNames
) {
107 SymbolSlab::Builder Slab
;
108 for (llvm::StringRef QName
: QualifiedNames
)
109 Slab
.insert(symbol(QName
));
110 return std::move(Slab
).build();
113 SymbolSlab
generateNumSymbols(int Begin
, int End
) {
114 std::vector
<std::string
> Names
;
115 for (int I
= Begin
; I
<= End
; I
++)
116 Names
.push_back(std::to_string(I
));
117 return generateSymbols(Names
);
120 std::string
getQualifiedName(const Symbol
&Sym
) {
121 return (Sym
.Scope
+ Sym
.Name
+ Sym
.TemplateSpecializationArgs
).str();
124 std::vector
<std::string
> match(const SymbolIndex
&I
,
125 const FuzzyFindRequest
&Req
, bool *Incomplete
) {
126 std::vector
<std::string
> Matches
;
127 bool IsIncomplete
= I
.fuzzyFind(Req
, [&](const Symbol
&Sym
) {
128 Matches
.push_back(clang::clangd::getQualifiedName(Sym
));
131 *Incomplete
= IsIncomplete
;
135 // Returns qualified names of symbols with any of IDs in the index.
136 std::vector
<std::string
> lookup(const SymbolIndex
&I
,
137 llvm::ArrayRef
<SymbolID
> IDs
) {
139 Req
.IDs
.insert(IDs
.begin(), IDs
.end());
140 std::vector
<std::string
> Results
;
141 I
.lookup(Req
, [&](const Symbol
&Sym
) {
142 Results
.push_back(getQualifiedName(Sym
));
147 } // namespace clangd