clang-tools  17.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/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> 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;
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 enm(llvm::StringRef Name) {
69  return sym(Name, index::SymbolKind::Enum, "@E@\\0");
70 }
71 
72 Symbol enmConstant(llvm::StringRef Name) {
73  return sym(Name, index::SymbolKind::EnumConstant, "@\\0");
74 }
75 
76 Symbol var(llvm::StringRef Name) {
77  return sym(Name, index::SymbolKind::Variable, "@\\0");
78 }
79 
80 Symbol ns(llvm::StringRef Name) {
81  return sym(Name, index::SymbolKind::Namespace, "@N@\\0");
82 }
83 
84 Symbol conceptSym(llvm::StringRef Name) {
85  return sym(Name, index::SymbolKind::Concept, "@CT@\\0");
86 }
87 
89  llvm::StringRef USRPrefix) {
90  Symbol Sym;
91  std::string USR = USRPrefix.str() + Name.str();
92  Sym.Name = Name;
93  Sym.Scope = "";
94  Sym.ID = SymbolID(USR);
95  Sym.SymInfo.Kind = Kind;
96  Sym.SymInfo.Lang = index::SymbolLanguage::ObjC;
99  return Sym;
100 }
101 
102 Symbol objcClass(llvm::StringRef Name) {
103  return objcSym(Name, index::SymbolKind::Class, "objc(cs)");
104 }
105 
106 Symbol objcCategory(llvm::StringRef Name, llvm::StringRef CategoryName) {
107  std::string USRPrefix = ("objc(cy)" + Name + "@").str();
108  return objcSym(CategoryName, index::SymbolKind::Extension, USRPrefix);
109 }
110 
111 Symbol objcProtocol(llvm::StringRef Name) {
112  return objcSym(Name, index::SymbolKind::Protocol, "objc(pl)");
113 }
114 
115 SymbolSlab generateSymbols(std::vector<std::string> QualifiedNames) {
116  SymbolSlab::Builder Slab;
117  for (llvm::StringRef QName : QualifiedNames)
118  Slab.insert(symbol(QName));
119  return std::move(Slab).build();
120 }
121 
122 SymbolSlab generateNumSymbols(int Begin, int End) {
123  std::vector<std::string> Names;
124  for (int I = Begin; I <= End; I++)
125  Names.push_back(std::to_string(I));
126  return generateSymbols(Names);
127 }
128 
129 std::string getQualifiedName(const Symbol &Sym) {
130  return (Sym.Scope + Sym.Name + Sym.TemplateSpecializationArgs).str();
131 }
132 
133 std::vector<std::string> match(const SymbolIndex &I,
134  const FuzzyFindRequest &Req, bool *Incomplete) {
135  std::vector<std::string> Matches;
136  bool IsIncomplete = I.fuzzyFind(Req, [&](const Symbol &Sym) {
137  Matches.push_back(clang::clangd::getQualifiedName(Sym));
138  });
139  if (Incomplete)
140  *Incomplete = IsIncomplete;
141  return Matches;
142 }
143 
144 // Returns qualified names of symbols with any of IDs in the index.
145 std::vector<std::string> lookup(const SymbolIndex &I,
146  llvm::ArrayRef<SymbolID> IDs) {
147  LookupRequest Req;
148  Req.IDs.insert(IDs.begin(), IDs.end());
149  std::vector<std::string> Results;
150  I.lookup(Req, [&](const Symbol &Sym) {
151  Results.push_back(getQualifiedName(Sym));
152  });
153  return Results;
154 }
155 
156 } // namespace clangd
157 } // namespace clang
clang::clangd::Symbol::IndexedForCodeCompletion
@ IndexedForCodeCompletion
Whether or not this symbol is meant to be used for the code completion.
Definition: Symbol.h:141
clang::clangd::Symbol::ID
SymbolID ID
The ID of the symbol.
Definition: Symbol.h:41
USR
std::string USR
Definition: SymbolInfoTests.cpp:26
clang::clangd::var
Symbol var(llvm::StringRef Name)
Definition: TestIndex.cpp:76
Kind
BindArgumentKind Kind
Definition: AvoidBindCheck.cpp:57
clang::clangd::cls
Symbol cls(llvm::StringRef Name)
Definition: TestIndex.cpp:64
clang::tidy::cppcoreguidelines::join
static std::string join(ArrayRef< SpecialMemberFunctionsCheck::SpecialMemberFunctionKind > SMFS, llvm::StringRef AndOr)
Definition: SpecialMemberFunctionsCheck.cpp:76
clang::clangd::func
Symbol func(llvm::StringRef Name)
Definition: TestIndex.cpp:60
clang::clangd::ns
Symbol ns(llvm::StringRef Name)
Definition: TestIndex.cpp:80
clang::clangd::FuzzyFindRequest
Definition: Index.h:26
clang::clangd::generateNumSymbols
SymbolSlab generateNumSymbols(int Begin, int End)
Definition: TestIndex.cpp:122
Pos
size_t Pos
Definition: NoLintDirectiveHandler.cpp:96
clang::clangd::match
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
Definition: TestIndex.cpp:133
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::generateSymbols
SymbolSlab generateSymbols(std::vector< std::string > QualifiedNames)
Definition: TestIndex.cpp:115
clang::clangd::Symbol::Origin
SymbolOrigin Origin
Where this symbol came from. Usually an index provides a constant value.
Definition: Symbol.h:64
clang::clangd::objcCategory
Symbol objcCategory(llvm::StringRef Name, llvm::StringRef CategoryName)
Definition: TestIndex.cpp:106
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:39
clang::clangd::Symbol::Flags
SymbolFlag Flags
Definition: Symbol.h:150
clang::clangd::Symbol::SymInfo
index::SymbolInfo SymInfo
The symbol information, like symbol kind.
Definition: Symbol.h:43
clang::clangd::replace
static std::string replace(llvm::StringRef Haystack, llvm::StringRef Needle, llvm::StringRef Repl)
Definition: TestIndex.cpp:30
Results
std::vector< CodeCompletionResult > Results
Definition: CodeComplete.cpp:819
clang::clangd::Symbol::Name
llvm::StringRef Name
The unqualified name of the symbol, e.g. "bar" (for ns::bar).
Definition: Symbol.h:45
clang::doc::SymbolID
std::array< uint8_t, 20 > SymbolID
Definition: Representation.h:31
Name
Token Name
Definition: MacroToEnumCheck.cpp:87
clang::clangd::enm
Symbol enm(llvm::StringRef Name)
Definition: TestIndex.cpp:68
clang::clangd::conceptSym
Symbol conceptSym(llvm::StringRef Name)
Definition: TestIndex.cpp:84
clang::clangd::getQualifiedName
std::string getQualifiedName(const Symbol &Sym)
Definition: TestIndex.cpp:129
clang::clangd::lookup
std::vector< std::string > lookup(const SymbolIndex &I, llvm::ArrayRef< SymbolID > IDs)
Definition: TestIndex.cpp:145
SymbolKind
clang::find_all_symbols::SymbolInfo::SymbolKind SymbolKind
Definition: SymbolInfo.cpp:19
clang::clangd::sym
Symbol sym(llvm::StringRef QName, index::SymbolKind Kind, llvm::StringRef USRFormat)
Definition: TestIndex.cpp:39
clang::clangd::LookupRequest::IDs
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:65
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:113
clang::clangd::LookupRequest
Definition: Index.h:64
clang::clangd::SymbolOrigin::Static
@ Static
clang::clangd::objcProtocol
Symbol objcProtocol(llvm::StringRef Name)
Definition: TestIndex.cpp:111
TestIndex.h
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:72
clang::clangd::Symbol::Scope
llvm::StringRef Scope
The containing namespace. e.g. "" (global), "ns::" (top-level namespace).
Definition: Symbol.h:47
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::objcClass
Symbol objcClass(llvm::StringRef Name)
Definition: TestIndex.cpp:102
clang::clangd::symbol
Symbol symbol(llvm::StringRef QName)
Definition: TestIndex.cpp:16
clang::clangd::SymbolSlab::Builder
SymbolSlab::Builder is a mutable container that can 'freeze' to SymbolSlab.
Definition: Symbol.h:222
clang::clangd::enmConstant
Symbol enmConstant(llvm::StringRef Name)
Definition: TestIndex.cpp:72
clang::clangd::SymbolSlab
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:199
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:52
clang::clangd::objcSym
Symbol objcSym(llvm::StringRef Name, index::SymbolKind Kind, llvm::StringRef USRPrefix)
Definition: TestIndex.cpp:88