clang: lib/Tooling/Refactoring/Lookup.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
20#include "llvm/ADT/SmallVector.h"
21using namespace clang;
23
24
25
26
27
28
32 auto GetNextNamedNamespace = [](const DeclContext *Context) {
33
36 Context = Context->getParent();
37 return Context;
38 };
39 for (Context = GetNextNamedNamespace(Context); Context != nullptr;
40 Context = GetNextNamedNamespace(Context->getParent()))
42 return Namespaces;
43}
44
45
46
47
48static bool
51
52
53
54
55
60
61
62 if (UseNamespaces.size() < FromNamespaces.size())
63 return false;
64 unsigned Diff = UseNamespaces.size() - FromNamespaces.size();
65 auto FromIter = FromNamespaces.begin();
66
67
68
69 auto UseIter = UseNamespaces.begin() + Diff;
70 for (; FromIter != FromNamespaces.end() && UseIter != UseNamespaces.end();
71 ++FromIter, ++UseIter) {
72
73 if (*FromIter == *UseIter)
74 return false;
75
76
79 return true;
80 }
81 assert(FromIter == FromNamespaces.end() && UseIter == UseNamespaces.end());
82 return false;
83}
84
86 StringRef NewName,
87 bool HadLeadingColonColon) {
88 while (true) {
91
92
93 if (!DeclA)
94 return HadLeadingColonColon ? NewName : NewName.substr(2);
95
96
97
98
99 std::string NS =
101 if (NewName.consume_front(NS))
102 return NewName;
103
104
105
106
108 }
109}
110
111
112
113
114
115
116
118 StringRef QName,
121 assert(QName.starts_with("::"));
122 assert(QName.ends_with(Spelling));
123 if (Spelling.starts_with("::"))
124 return std::string(Spelling);
125
126 auto UnspelledSpecifier = QName.drop_back(Spelling.size());
128 UnspelledSpecifier.split(UnspelledScopes, "::", -1,
129 false);
130
134 StringRef TrimmedQName = QName.substr(2);
136 UseLoc = SM.getSpellingLoc(UseLoc);
137
138 auto IsAmbiguousSpelling = [&](const llvm::StringRef CurSpelling) {
139 if (CurSpelling.starts_with("::"))
140 return false;
141
142
143
144 StringRef Head = CurSpelling.split("::").first;
145 for (const auto *NS : EnclosingNamespaces) {
146 auto LookupRes = NS->lookup(DeclarationName(&AST.Idents.get(Head)));
147 if (!LookupRes.empty()) {
148 for (const NamedDecl *Res : LookupRes)
149
150
151
152
153 if (!TrimmedQName.starts_with(Res->getQualifiedNameAsString()) &&
154 SM.isBeforeInTranslationUnit(
155 SM.getSpellingLoc(Res->getLocation()), UseLoc))
156 return true;
157 }
158 }
159 return false;
160 };
161
162
163 std::string Disambiguated = std::string(Spelling);
164 while (IsAmbiguousSpelling(Disambiguated)) {
165 if (UnspelledScopes.empty()) {
166 Disambiguated = "::" + Disambiguated;
167 } else {
168 Disambiguated = (UnspelledScopes.back() + "::" + Disambiguated).str();
169 UnspelledScopes.pop_back();
170 }
171 }
172 return Disambiguated;
173}
174
179 StringRef ReplacementString) {
180 assert(ReplacementString.starts_with("::") &&
181 "Expected fully-qualified name!");
182
183
184
185
186
187
188
189
190
191 const bool class_name_only = !Use;
192 const bool in_global_namespace =
194 const bool is_class_forward_decl =
197 if (class_name_only && !in_global_namespace && !is_class_forward_decl &&
199 UseContext)) {
200 auto Pos = ReplacementString.rfind("::");
201 return std::string(Pos != StringRef::npos
202 ? ReplacementString.substr(Pos + 2)
203 : ReplacementString);
204 }
205
206
207
208
211
213 UseLoc);
214}
Defines the clang::ASTContext interface.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
static llvm::SmallVector< const NamespaceDecl *, 4 > getAllNamedNamespaces(const DeclContext *Context)
Definition Lookup.cpp:30
static bool usingFromDifferentCanonicalNamespace(const DeclContext *FromContext, const DeclContext *UseContext)
Definition Lookup.cpp:49
static StringRef getBestNamespaceSubstr(const DeclContext *DeclA, StringRef NewName, bool HadLeadingColonColon)
Definition Lookup.cpp:85
static std::string disambiguateSpellingInScope(StringRef Spelling, StringRef QName, const DeclContext &UseContext, SourceLocation UseLoc)
Definition Lookup.cpp:117
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
SourceManager & getSourceManager()
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
DeclContext * getParent()
getParent - Returns the containing DeclContext.
ASTContext & getParentASTContext() const
DeclContext * getDeclContext()
The name of a declaration.
This represents a decl that may have a name.
Represents a C++ nested name specifier, such as "\::std::vector::".
bool isFullyQualified() const
Whether this nested name specifier starts with a '::'.
Encodes a location in the source.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
U cast(CodeGen::Address addr)