20#include "llvm/ADT/SmallVector.h"
32 auto GetNextNamedNamespace = [](
const DeclContext *Context) {
34 while (Context && (!isa<NamespaceDecl>(Context) ||
35 cast<NamespaceDecl>(Context)->isAnonymousNamespace()))
36 Context = Context->getParent();
39 for (Context = GetNextNamedNamespace(Context); Context !=
nullptr;
40 Context = GetNextNamedNamespace(Context->getParent()))
41 Namespaces.push_back(cast<NamespaceDecl>(Context));
62 if (UseNamespaces.size() < FromNamespaces.size())
64 unsigned Diff = UseNamespaces.size() - FromNamespaces.size();
65 auto FromIter = FromNamespaces.begin();
69 auto UseIter = UseNamespaces.begin() + Diff;
70 for (; FromIter != FromNamespaces.end() && UseIter != UseNamespaces.end();
71 ++FromIter, ++UseIter) {
73 if (*FromIter == *UseIter)
77 if (cast<NamespaceDecl>(*FromIter)->getDeclName() ==
78 cast<NamespaceDecl>(*UseIter)->getDeclName())
81 assert(FromIter == FromNamespaces.end() && UseIter == UseNamespaces.end());
87 bool HadLeadingColonColon) {
89 while (DeclA && !isa<NamespaceDecl>(DeclA))
94 return HadLeadingColonColon ? NewName : NewName.substr(2);
100 "::" + cast<NamespaceDecl>(DeclA)->getQualifiedNameAsString() +
"::";
101 if (NewName.consume_front(NS))
131 assert(QName.starts_with(
"::"));
132 assert(QName.ends_with(Spelling));
133 if (Spelling.starts_with(
"::"))
134 return std::string(Spelling);
136 auto UnspelledSpecifier = QName.drop_back(Spelling.size());
138 UnspelledSpecifier.split(UnspelledScopes,
"::", -1,
144 StringRef TrimmedQName = QName.substr(2);
146 UseLoc =
SM.getSpellingLoc(UseLoc);
148 auto IsAmbiguousSpelling = [&](
const llvm::StringRef CurSpelling) {
149 if (CurSpelling.starts_with(
"::"))
154 StringRef Head = CurSpelling.split(
"::").first;
155 for (
const auto *NS : EnclosingNamespaces) {
157 if (!LookupRes.empty()) {
163 if (!TrimmedQName.starts_with(Res->getQualifiedNameAsString()) &&
164 SM.isBeforeInTranslationUnit(
165 SM.getSpellingLoc(Res->getLocation()), UseLoc))
173 std::string Disambiguated = std::string(Spelling);
174 while (IsAmbiguousSpelling(Disambiguated)) {
175 if (UnspelledScopes.empty()) {
176 Disambiguated =
"::" + Disambiguated;
178 Disambiguated = (UnspelledScopes.back() +
"::" + Disambiguated).str();
179 UnspelledScopes.pop_back();
182 return Disambiguated;
189 StringRef ReplacementString) {
190 assert(ReplacementString.starts_with(
"::") &&
191 "Expected fully-qualified name!");
201 const bool class_name_only = !Use;
202 const bool in_global_namespace =
204 const bool is_class_forward_decl =
206 !cast<CXXRecordDecl>(
FromDecl)->isCompleteDefinition();
207 if (class_name_only && !in_global_namespace && !is_class_forward_decl &&
210 auto Pos = ReplacementString.rfind(
"::");
211 return std::string(Pos != StringRef::npos
212 ? ReplacementString.substr(Pos + 2)
213 : ReplacementString);
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)
static bool usingFromDifferentCanonicalNamespace(const DeclContext *FromContext, const DeclContext *UseContext)
static StringRef getBestNamespaceSubstr(const DeclContext *DeclA, StringRef NewName, bool HadLeadingColonColon)
static std::string disambiguateSpellingInScope(StringRef Spelling, StringRef QName, const DeclContext &UseContext, SourceLocation UseLoc)
static bool isFullyQualified(const NestedNameSpecifier *NNS)
Check if the name specifier begins with a written "::".
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
const NamedDecl * FromDecl
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<int>::".
SpecifierKind getKind() const
Determine what kind of nested name specifier is stored.
NestedNameSpecifier * getPrefix() const
Return the prefix of this nested name specifier.
@ Global
The global specifier '::'. There is no stored value.
Encodes a location in the source.
The JSON file list parser is used to communicate input to InstallAPI.