clang-tools  10.0.0svn
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, 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.
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 var(llvm::StringRef Name) {
69  return sym(Name, index::SymbolKind::Variable, "@\\0");
70 }
71 
72 Symbol ns(llvm::StringRef Name) {
73  return sym(Name, index::SymbolKind::Namespace, "@N@\\0");
74 }
75 
76 SymbolSlab generateSymbols(std::vector<std::string> QualifiedNames) {
78  for (llvm::StringRef QName : QualifiedNames)
79  Slab.insert(symbol(QName));
80  return std::move(Slab).build();
81 }
82 
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 
90 std::string getQualifiedName(const Symbol &Sym) {
91  return (Sym.Scope + Sym.Name + Sym.TemplateSpecializationArgs).str();
92 }
93 
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.
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
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...
Symbol symbol(llvm::StringRef QName)
Definition: TestIndex.cpp:16
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
Symbol cls(llvm::StringRef Name)
Definition: TestIndex.cpp:64
clang::find_all_symbols::SymbolInfo::SymbolKind SymbolKind
Definition: SymbolInfo.cpp:21
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:85
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:64
Symbol ns(llvm::StringRef Name)
Definition: TestIndex.cpp:72
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...
void insert(const Symbol &S)
Adds a symbol, overwriting any existing one with the same ID.
Definition: Symbol.cpp:50
std::vector< CodeCompletionResult > Results
llvm::StringRef Scope
The containing namespace. e.g. "" (global), "ns::" (top-level namespace).
Definition: Symbol.h:44
Symbol sym(llvm::StringRef QName, index::SymbolKind Kind, llvm::StringRef USRFormat)
Definition: TestIndex.cpp:39
static std::string replace(llvm::StringRef Haystack, llvm::StringRef Needle, llvm::StringRef Repl)
Definition: TestIndex.cpp:30
Symbol var(llvm::StringRef Name)
Definition: TestIndex.cpp:68
SymbolSlab::Builder is a mutable container that can &#39;freeze&#39; to SymbolSlab.
Definition: Symbol.h:199
SymbolID ID
The ID of the symbol.
Definition: Symbol.h:38
index::SymbolInfo SymInfo
The symbol information, like symbol kind.
Definition: Symbol.h:40
BindArgumentKind Kind
std::string QName
Whether or not this symbol is meant to be used for the code completion.
Definition: Symbol.h:119
std::vector< std::string > lookup(const SymbolIndex &I, llvm::ArrayRef< SymbolID > IDs)
Definition: TestIndex.cpp:106
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
Definition: TestIndex.cpp:94
SymbolFlag Flags
Definition: Symbol.h:128
SymbolSlab generateSymbols(std::vector< std::string > QualifiedNames)
Definition: TestIndex.cpp:76
static constexpr llvm::StringLiteral Name
SymbolSlab generateNumSymbols(int Begin, int End)
Definition: TestIndex.cpp:83
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
std::string getQualifiedName(const Symbol &Sym)
Definition: TestIndex.cpp:90
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::StringRef Name
The unqualified name of the symbol, e.g. "bar" (for ns::bar).
Definition: Symbol.h:42
static std::string join(ArrayRef< SpecialMemberFunctionsCheck::SpecialMemberFunctionKind > SMFS, llvm::StringRef AndOr)
SymbolOrigin Origin
Where this symbol came from. Usually an index provides a constant value.
Definition: Symbol.h:61
llvm::StringRef TemplateSpecializationArgs
Argument list in human-readable format, will be displayed to help disambiguate between different spec...
Definition: Symbol.h:69
std::array< uint8_t, 20 > SymbolID
Symbol func(llvm::StringRef Name)
Definition: TestIndex.cpp:60