#include "ChangeNamespace.h"
#include "clang/AST/ASTContext.h"
#include "clang/Format/Format.h"
#include "clang/Lex/Lexer.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
using namespace clang::ast_matchers;
namespace clang {
namespace change_namespace {
namespace {
inline std::string joinNamespaces(ArrayRef<StringRef> Namespaces) {
return llvm::join(Namespaces, "::");
}
llvm::SmallVector<llvm::StringRef, 4> splitSymbolName(llvm::StringRef Name) {
llvm::SmallVector<llvm::StringRef, 4> Splitted;
Name.split(Splitted, "::", -1,
false);
return Splitted;
}
SourceLocation startLocationForType(TypeLoc TLoc) {
if (TLoc.getTypeLocClass() == TypeLoc::Elaborated) {
NestedNameSpecifierLoc NestedNameSpecifier =
TLoc.castAs<ElaboratedTypeLoc>().getQualifierLoc();
if (NestedNameSpecifier.getNestedNameSpecifier())
return NestedNameSpecifier.getBeginLoc();
TLoc = TLoc.getNextTypeLoc();
}
return TLoc.getBeginLoc();
}
SourceLocation endLocationForType(TypeLoc TLoc) {
while (TLoc.getTypeLocClass() == TypeLoc::Elaborated ||
TLoc.getTypeLocClass() == TypeLoc::Qualified)
TLoc = TLoc.getNextTypeLoc();
if (TLoc.getTypeLocClass() == TypeLoc::TemplateSpecialization)
return TLoc.castAs<TemplateSpecializationTypeLoc>()
.getLAngleLoc()
.getLocWithOffset(-1);
return TLoc.getEndLoc();
}
const NamespaceDecl *getOuterNamespace(const NamespaceDecl *InnerNs,
llvm::StringRef PartialNsName) {
if (!InnerNs || PartialNsName.empty())
return nullptr;
const auto *CurrentContext = llvm::cast<DeclContext>(InnerNs);
const auto *CurrentNs = InnerNs;
auto PartialNsNameSplitted = splitSymbolName(PartialNsName);
while (!PartialNsNameSplitted.empty()) {
while (CurrentContext && !llvm::isa<NamespaceDecl>(CurrentContext))
CurrentContext = CurrentContext->getParent();
if (!CurrentContext)
return nullptr;
CurrentNs = llvm::cast<NamespaceDecl>(CurrentContext);
if (PartialNsNameSplitted.back() != CurrentNs->getNameAsString())
return nullptr;
PartialNsNameSplitted.pop_back();
CurrentContext = CurrentContext->getParent();
}
return CurrentNs;
}
static std::unique_ptr<Lexer>
getLexerStartingFromLoc(SourceLocation Loc, const SourceManager &SM,
const LangOptions &LangOpts) {
if (Loc.isMacroID() &&
!Lexer::isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc))
return nullptr;
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
bool InvalidTemp = false;
llvm::StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp);
if (InvalidTemp)
return nullptr;
const char *TokBegin = File.data() + LocInfo.second;
return std::make_unique<Lexer>(SM.getLocForStartOfFile(LocInfo.first),
LangOpts, File.begin(), TokBegin, File.end());
}
static SourceLocation getStartOfNextLine(SourceLocation Loc,
const SourceManager &SM,
const LangOptions &LangOpts) {
std::unique_ptr<Lexer> Lex = getLexerStartingFromLoc(Loc, SM, LangOpts);
if (!Lex.get())
return SourceLocation();
llvm::SmallVector<char, 16> Line;
Lex->setParsingPreprocessorDirective(true);
Lex->ReadToEndOfLine(&Line);
auto End = Loc.getLocWithOffset(Line.size());
return SM.getLocForEndOfFile(SM.getDecomposedLoc(Loc).first) == End
? End
: End.getLocWithOffset(1);
}
tooling::Replacement
getReplacementInChangedCode(const tooling::Replacements &Replaces,
const tooling::Replacement &R) {
unsigned NewStart = Replaces.getShiftedCodePosition(R.getOffset());
unsigned NewEnd =
Replaces.getShiftedCodePosition(R.getOffset() + R.getLength());
return tooling::Replacement(R.getFilePath(), NewStart, NewEnd - NewStart,
R.getReplacementText());
}
void addOrMergeReplacement(const tooling::Replacement &R,
tooling::Replacements *Replaces) {
auto Err = Replaces->add(R);
if (Err) {
llvm::consumeError(std::move(Err));
auto Replace = getReplacementInChangedCode(*Replaces, R);
*Replaces = Replaces->merge(tooling::Replacements(Replace));
}
}
tooling::Replacement createReplacement(SourceLocation Start, SourceLocation End,
llvm::StringRef ReplacementText,
const SourceManager &SM) {
if (!Start.isValid() || !End.isValid()) {
llvm::errs() << "start or end location were invalid\n";
return tooling::Replacement();
}
if (SM.getDecomposedLoc(Start).first != SM.getDecomposedLoc(End).first) {
llvm::errs()
<< "start or end location were in different macro expansions\n";
return tooling::Replacement();
}
Start = SM.getSpellingLoc(Start);
End = SM.getSpellingLoc(End);
if (SM.getFileID(Start) != SM.getFileID(End)) {
llvm::errs() << "start or end location were in different files\n";
return tooling::Replacement();
}
return tooling::Replacement(
SM, CharSourceRange::getTokenRange(SM.getSpellingLoc(Start),
SM.getSpellingLoc(End)),
ReplacementText);
}
void addReplacementOrDie(
SourceLocation Start, SourceLocation End, llvm::StringRef ReplacementText,
const SourceManager &SM,
std::map<std::string, tooling::Replacements> *FileToReplacements) {
const auto R = createReplacement(Start, End, ReplacementText, SM);
auto Err = (*FileToReplacements)[std::string(R.getFilePath())].add(R);
if (Err)
llvm_unreachable(llvm::toString(std::move(Err)).c_str());
}
tooling::Replacement createInsertion(SourceLocation Loc,
llvm::StringRef InsertText,
const SourceManager &SM) {
if (Loc.isInvalid()) {
llvm::errs() << "insert Location is invalid.\n";
return tooling::Replacement();
}
Loc = SM.getSpellingLoc(Loc);
return tooling::Replacement(SM, Loc, 0, InsertText);
}
std::string getShortestQualifiedNameInNamespace(llvm::StringRef DeclName,
llvm::StringRef NsName) {
DeclName = DeclName.ltrim(':');
NsName = NsName.ltrim(':');
if (!DeclName.contains(':'))
return std::string(DeclName);
auto NsNameSplitted = splitSymbolName(NsName);
auto DeclNsSplitted = splitSymbolName(DeclName);
llvm::StringRef UnqualifiedDeclName = DeclNsSplitted.pop_back_val();
if (DeclNsSplitted.empty())
return std::string(UnqualifiedDeclName);
if (NsNameSplitted.empty())
return std::string(DeclName);
if (NsNameSplitted.front() != DeclNsSplitted.front()) {
if (llvm::is_contained(NsNameSplitted, DeclNsSplitted.front()))
return ("::" + DeclName).str();
return std::string(DeclName);
}
auto DeclI = DeclNsSplitted.begin();
auto DeclE = DeclNsSplitted.end();
auto NsI = NsNameSplitted.begin();
auto NsE = NsNameSplitted.end();
for (; DeclI != DeclE && NsI != NsE && *DeclI == *NsI; ++DeclI, ++NsI) {
}
return (DeclI == DeclE)
? UnqualifiedDeclName.str()
: (llvm::join(DeclI, DeclE, "::") + "::" + UnqualifiedDeclName)
.str();
}
std::string wrapCodeInNamespace(StringRef NestedNs, std::string Code) {
if (Code.back() != '\n')
Code += "\n";
auto NsSplitted = splitSymbolName(NestedNs);
while (!NsSplitted.empty()) {
Code = ("namespace " + NsSplitted.back() + " {\n" + Code +
"} // namespace " + NsSplitted.back() + "\n")
.str();
NsSplitted.pop_back();
}
return Code;
}
bool isNestedDeclContext(const DeclContext *D, const DeclContext *Context) {
while (D) {
if (D == Context)
return true;
D = D->getParent();
}
return false;
}
bool isDeclVisibleAtLocation(const SourceManager &SM, const Decl *D,
const DeclContext *DeclCtx, SourceLocation Loc) {
SourceLocation DeclLoc = SM.getSpellingLoc(D->getBeginLoc());
Loc = SM.getSpellingLoc(Loc);
return SM.isBeforeInTranslationUnit(DeclLoc, Loc) &&
(SM.getFileID(DeclLoc) == SM.getFileID(Loc) &&
isNestedDeclContext(DeclCtx, D->getDeclContext()));
}
bool conflictInNamespace(const ASTContext &AST, llvm::StringRef QualifiedSymbol,
llvm::StringRef Namespace) {
auto SymbolSplitted = splitSymbolName(QualifiedSymbol.trim(":"));
assert(!SymbolSplitted.empty());
SymbolSplitted.pop_back();
if (SymbolSplitted.size() >= 1 && !Namespace.empty()) {
auto SymbolTopNs = SymbolSplitted.front();
auto NsSplitted = splitSymbolName(Namespace.trim(":"));
assert(!NsSplitted.empty());
auto LookupDecl = [&AST](const Decl &Scope,
llvm::StringRef Name) -> const NamedDecl * {
const auto *DC = llvm::dyn_cast<DeclContext>(&Scope);
if (!DC)
return nullptr;
auto LookupRes = DC->lookup(DeclarationName(&AST.Idents.get(Name)));
if (LookupRes.empty())
return nullptr;
return LookupRes.front();
};
const NamedDecl *Scope =
LookupDecl(*AST.getTranslationUnitDecl(), NsSplitted.front());
for (const auto &I : llvm::drop_begin(NsSplitted)) {
if (I == SymbolTopNs)
return true;
if (Scope) {
if (LookupDecl(*Scope, SymbolTopNs))
return true;
Scope = LookupDecl(*Scope, I);
}
}
if (Scope && LookupDecl(*Scope, SymbolTopNs))
return true;
}
return false;
}
bool isTemplateParameter(TypeLoc Type) {
while (!Type.isNull()) {
if (Type.getTypeLocClass() == TypeLoc::SubstTemplateTypeParm)
return true;
Type = Type.getNextTypeLoc();
}
return false;
}
}
ChangeNamespaceTool::ChangeNamespaceTool(
llvm::StringRef OldNs, llvm::StringRef NewNs, llvm::StringRef FilePattern,
llvm::ArrayRef<std::string> AllowedSymbolPatterns,
std::map<std::string, tooling::Replacements> *FileToReplacements,
llvm::StringRef FallbackStyle)
: FallbackStyle(FallbackStyle), FileToReplacements(*FileToReplacements),
OldNamespace(OldNs.ltrim(':')), NewNamespace(NewNs.ltrim(':')),
FilePattern(FilePattern), FilePatternRE(FilePattern) {
FileToReplacements->clear();
auto OldNsSplitted = splitSymbolName(OldNamespace);
auto NewNsSplitted = splitSymbolName(NewNamespace);
while (!OldNsSplitted.empty() && !NewNsSplitted.empty() &&
OldNsSplitted.front() == NewNsSplitted.front()) {
OldNsSplitted.erase(OldNsSplitted.begin());
NewNsSplitted.erase(NewNsSplitted.begin());
}
DiffOldNamespace = joinNamespaces(OldNsSplitted);
DiffNewNamespace = joinNamespaces(NewNsSplitted);
for (const auto &Pattern : AllowedSymbolPatterns)
AllowedSymbolRegexes.emplace_back(Pattern);
}
void ChangeNamespaceTool::registerMatchers(ast_matchers::MatchFinder *Finder) {
std::string FullOldNs = "::" + OldNamespace;
llvm::SmallVector<llvm::StringRef, 4> DiffOldNsSplitted;
llvm::StringRef(DiffOldNamespace)
.split(DiffOldNsSplitted, "::", -1,
false);
std::string Prefix = "-";
if (!DiffOldNsSplitted.empty())
Prefix = (StringRef(FullOldNs).drop_back(DiffOldNamespace.size()) +
DiffOldNsSplitted.front())
.str();
auto IsInMovedNs =
allOf(hasAncestor(namespaceDecl(hasName(FullOldNs)).bind("ns_decl")),
isExpansionInFileMatching(FilePattern));
auto IsVisibleInNewNs = anyOf(
IsInMovedNs, unless(hasAncestor(namespaceDecl(hasName(Prefix)))));
Finder->addMatcher(
usingDecl(isExpansionInFileMatching(FilePattern), IsVisibleInNewNs)
.bind("using"),
this);
Finder->addMatcher(usingDirectiveDecl(isExpansionInFileMatching(FilePattern),
IsVisibleInNewNs)
.bind("using_namespace"),
this);
Finder->addMatcher(namespaceAliasDecl(isExpansionInFileMatching(FilePattern),
IsVisibleInNewNs)
.bind("namespace_alias"),
this);
Finder->addMatcher(
namespaceDecl(hasName(FullOldNs), isExpansionInFileMatching(FilePattern))
.bind("old_ns"),
this);
Finder->addMatcher(cxxRecordDecl(unless(anyOf(isImplicit(), isDefinition())),
IsInMovedNs, hasParent(namespaceDecl()))
.bind("class_fwd_decl"),
this);
Finder->addMatcher(
classTemplateDecl(unless(hasDescendant(cxxRecordDecl(isDefinition()))),
IsInMovedNs, hasParent(namespaceDecl()))
.bind("template_class_fwd_decl"),
this);
auto DeclMatcher = namedDecl(
hasAncestor(namespaceDecl()),
unless(anyOf(
isImplicit(), hasAncestor(namespaceDecl(isAnonymous())),
hasAncestor(cxxRecordDecl()),
allOf(IsInMovedNs, unless(cxxRecordDecl(unless(isDefinition())))))));
auto UsingShadowDeclInClass =
usingDecl(hasAnyUsingShadowDecl(decl()), hasParent(cxxRecordDecl()));
Finder->addMatcher(
typeLoc(IsInMovedNs,
loc(qualType(hasDeclaration(DeclMatcher.bind("from_decl")))),
unless(anyOf(hasParent(typeLoc(loc(qualType(
hasDeclaration(DeclMatcher),
unless(templateSpecializationType()))))),
hasParent(nestedNameSpecifierLoc()),
hasAncestor(decl(isImplicit())),
hasAncestor(UsingShadowDeclInClass),
hasAncestor(functionDecl(isDefaulted())))),
hasAncestor(decl().bind("dc")))
.bind("type"),
this);
Finder->addMatcher(usingDecl(IsInMovedNs, hasAnyUsingShadowDecl(decl()),
unless(UsingShadowDeclInClass))
.bind("using_with_shadow"),
this);
Finder->addMatcher(
nestedNameSpecifierLoc(
hasAncestor(decl(IsInMovedNs).bind("dc")),
loc(nestedNameSpecifier(
specifiesType(hasDeclaration(DeclMatcher.bind("from_decl"))))),
unless(anyOf(hasAncestor(decl(isImplicit())),
hasAncestor(UsingShadowDeclInClass),
hasAncestor(functionDecl(isDefaulted())),
hasAncestor(typeLoc(loc(qualType(hasDeclaration(
decl(equalsBoundNode("from_decl"))))))))))
.bind("nested_specifier_loc"),
this);
Finder->addMatcher(
cxxCtorInitializer(isBaseInitializer()).bind("base_initializer"), this);
auto FuncMatcher =
functionDecl(unless(anyOf(cxxMethodDecl(), IsInMovedNs,
hasAncestor(namespaceDecl(isAnonymous())),
hasAncestor(cxxRecordDecl()))),
hasParent(namespaceDecl()));
Finder->addMatcher(expr(hasAncestor(decl().bind("dc")), IsInMovedNs,
unless(hasAncestor(decl(isImplicit()))),
anyOf(callExpr(callee(FuncMatcher)).bind("call"),
declRefExpr(to(FuncMatcher.bind("func_decl")))
.bind("func_ref"))),
this);
auto GlobalVarMatcher = varDecl(
hasGlobalStorage(), hasParent(namespaceDecl()),
unless(anyOf(IsInMovedNs, hasAncestor(namespaceDecl(isAnonymous())))));
Finder->addMatcher(declRefExpr(IsInMovedNs, hasAncestor(decl().bind("dc")),
to(GlobalVarMatcher.bind("var_decl")))
.bind("var_ref"),
this);
auto UnscopedEnumMatcher = enumConstantDecl(hasParent(enumDecl(
hasParent(namespaceDecl()),
unless(anyOf(isScoped(), IsInMovedNs, hasAncestor(cxxRecordDecl()),
hasAncestor(namespaceDecl(isAnonymous())))))));
Finder->addMatcher(
declRefExpr(IsInMovedNs, hasAncestor(decl().bind("dc")),
to(UnscopedEnumMatcher.bind("enum_const_decl")))
.bind("enum_const_ref"),
this);
}
void ChangeNamespaceTool::run(
const ast_matchers::MatchFinder::MatchResult &Result) {
if (const auto *Using = Result.Nodes.getNodeAs<UsingDecl>("using")) {
UsingDecls.insert(Using);
} else if (const auto *UsingNamespace =
Result.Nodes.getNodeAs<UsingDirectiveDecl>(
"using_namespace")) {
UsingNamespaceDecls.insert(UsingNamespace);
} else if (const auto *NamespaceAlias =
Result.Nodes.getNodeAs<NamespaceAliasDecl>(
"namespace_alias")) {
NamespaceAliasDecls.insert(NamespaceAlias);
} else if (const auto *NsDecl =
Result.Nodes.getNodeAs<NamespaceDecl>("old_ns")) {
moveOldNamespace(Result, NsDecl);
} else if (const auto *FwdDecl =
Result.Nodes.getNodeAs<CXXRecordDecl>("class_fwd_decl")) {
moveClassForwardDeclaration(Result, cast<NamedDecl>(FwdDecl));
} else if (const auto *TemplateFwdDecl =
Result.Nodes.getNodeAs<ClassTemplateDecl>(
"template_class_fwd_decl")) {
moveClassForwardDeclaration(Result, cast<NamedDecl>(TemplateFwdDecl));
} else if (const auto *UsingWithShadow =
Result.Nodes.getNodeAs<UsingDecl>("using_with_shadow")) {
fixUsingShadowDecl(Result, UsingWithShadow);
} else if (const auto *Specifier =
Result.Nodes.getNodeAs<NestedNameSpecifierLoc>(
"nested_specifier_loc")) {
SourceLocation Start = Specifier->getBeginLoc();
SourceLocation End = endLocationForType(Specifier->getTypeLoc());
fixTypeLoc(Result, Start, End, Specifier->getTypeLoc());
} else if (const auto *BaseInitializer =
Result.Nodes.getNodeAs<CXXCtorInitializer>(
"base_initializer")) {
BaseCtorInitializerTypeLocs.push_back(
BaseInitializer->getTypeSourceInfo()->getTypeLoc());
} else if (const auto *TLoc = Result.Nodes.getNodeAs<TypeLoc>("type")) {
TypeLoc Loc = *TLoc;
while (Loc.getTypeLocClass() == TypeLoc::Qualified)
Loc = Loc.getNextTypeLoc();
if (Loc.getTypeLocClass() == TypeLoc::Elaborated) {
NestedNameSpecifierLoc NestedNameSpecifier =
Loc.castAs<ElaboratedTypeLoc>().getQualifierLoc();
if (auto *NNS = NestedNameSpecifier.getNestedNameSpecifier()) {
const Type *SpecifierType = NNS->getAsType();
if (SpecifierType && SpecifierType->isRecordType())
return;
}
}
fixTypeLoc(Result, startLocationForType(Loc), endLocationForType(Loc), Loc);
} else if (const auto *VarRef =
Result.Nodes.getNodeAs<DeclRefExpr>("var_ref")) {
const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var_decl");
assert(Var);
if (Var->getCanonicalDecl()->isStaticDataMember())
return;
const auto *Context = Result.Nodes.getNodeAs<Decl>("dc");
assert(Context && "Empty decl context.");
fixDeclRefExpr(Result, Context->getDeclContext(),
llvm::cast<NamedDecl>(Var), VarRef);
} else if (const auto *EnumConstRef =
Result.Nodes.getNodeAs<DeclRefExpr>("enum_const_ref")) {
if (EnumConstRef->hasQualifier() &&
EnumConstRef->getQualifier()->getKind() ==
NestedNameSpecifier::SpecifierKind::TypeSpec &&
EnumConstRef->getQualifier()->getAsType()->isEnumeralType())
return;
const auto *EnumConstDecl =
Result.Nodes.getNodeAs<EnumConstantDecl>("enum_const_decl");
assert(EnumConstDecl);
const auto *Context = Result.Nodes.getNodeAs<Decl>("dc");
assert(Context && "Empty decl context.");
fixDeclRefExpr(Result, Context->getDeclContext(),
llvm::cast<NamedDecl>(EnumConstDecl), EnumConstRef);
} else if (const auto *FuncRef =
Result.Nodes.getNodeAs<DeclRefExpr>("func_ref")) {
if (ProcessedFuncRefs.count(FuncRef))
return;
ProcessedFuncRefs.insert(FuncRef);
const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func_decl");
assert(Func);
const auto *Context = Result.Nodes.getNodeAs<Decl>("dc");
assert(Context && "Empty decl context.");
fixDeclRefExpr(Result, Context->getDeclContext(),
llvm::cast<NamedDecl>(Func), FuncRef);
} else {
const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
assert(Call != nullptr && "Expecting callback for CallExpr.");
const auto *CalleeFuncRef =
llvm::cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit());
ProcessedFuncRefs.insert(CalleeFuncRef);
const FunctionDecl *Func = Call->getDirectCallee();
assert(Func != nullptr);
if (Func->isOverloadedOperator())
return;
if (Func->getCanonicalDecl()->getStorageClass() ==
StorageClass::SC_Static &&
Func->isOutOfLine())
return;
const auto *Context = Result.Nodes.getNodeAs<Decl>("dc");
assert(Context && "Empty decl context.");
SourceRange CalleeRange = Call->getCallee()->getSourceRange();
replaceQualifiedSymbolInDeclContext(
Result, Context->getDeclContext(), CalleeRange.getBegin(),
CalleeRange.getEnd(), llvm::cast<NamedDecl>(Func));
}
}
static SourceLocation getLocAfterNamespaceLBrace(const NamespaceDecl *NsDecl,
const SourceManager &SM,
const LangOptions &LangOpts) {
std::unique_ptr<Lexer> Lex =
getLexerStartingFromLoc(NsDecl->getBeginLoc(), SM, LangOpts);
assert(Lex.get() &&
"Failed to create lexer from the beginning of namespace.");
if (!Lex.get())
return SourceLocation();
Token Tok;
while (!Lex->LexFromRawLexer(Tok) && Tok.isNot(tok::TokenKind::l_brace)) {
}
return Tok.isNot(tok::TokenKind::l_brace)
? SourceLocation()
: Tok.getEndLoc().getLocWithOffset(1);
}
void ChangeNamespaceTool::moveOldNamespace(
const ast_matchers::MatchFinder::MatchResult &Result,
const NamespaceDecl *NsDecl) {
if (Decl::castToDeclContext(NsDecl)->decls_empty())
return;
const SourceManager &SM = *Result.SourceManager;
SourceLocation Start =
getLocAfterNamespaceLBrace(NsDecl, SM, Result.Context->getLangOpts());
assert(Start.isValid() && "Can't find l_brace for namespace.");
MoveNamespace MoveNs;
MoveNs.Offset = SM.getFileOffset(Start);
MoveNs.Length = SM.getFileOffset(NsDecl->getRBraceLoc()) - MoveNs.Offset;
const NamespaceDecl *OuterNs = getOuterNamespace(NsDecl, DiffOldNamespace);
SourceLocation InsertionLoc = Start;
if (OuterNs) {
SourceLocation LocAfterNs = getStartOfNextLine(
OuterNs->getRBraceLoc(), SM, Result.Context->getLangOpts());
assert(LocAfterNs.isValid() &&
"Failed to get location after DiffOldNamespace");
InsertionLoc = LocAfterNs;
}
MoveNs.InsertionOffset = SM.getFileOffset(SM.getSpellingLoc(InsertionLoc));
MoveNs.FID = SM.getFileID(Start);
MoveNs.SourceMgr = Result.SourceManager;
MoveNamespaces[std::string(SM.getFilename(Start))].push_back(MoveNs);
}
void ChangeNamespaceTool::moveClassForwardDeclaration(
const ast_matchers::MatchFinder::MatchResult &Result,
const NamedDecl *FwdDecl) {
SourceLocation Start = FwdDecl->getBeginLoc();
SourceLocation End = FwdDecl->getEndLoc();
const SourceManager &SM = *Result.SourceManager;
SourceLocation AfterSemi = Lexer::findLocationAfterToken(
End, tok::semi, SM, Result.Context->getLangOpts(),
true);
if (AfterSemi.isValid())
End = AfterSemi.getLocWithOffset(-1);
addReplacementOrDie(Start, End, "", SM, &FileToReplacements);
llvm::StringRef Code = Lexer::getSourceText(
CharSourceRange::getTokenRange(SM.getSpellingLoc(Start),
SM.getSpellingLoc(End)),
SM, Result.Context->getLangOpts());
const auto *NsDecl = Result.Nodes.getNodeAs<NamespaceDecl>("ns_decl");
assert(!NsDecl->decls_empty());
const auto Insertion = createInsertion(
getLocAfterNamespaceLBrace(NsDecl, SM, Result.Context->getLangOpts()),
Code, SM);
InsertForwardDeclaration InsertFwd;
InsertFwd.InsertionOffset = Insertion.getOffset();
InsertFwd.ForwardDeclText = Insertion.getReplacementText().str();
InsertFwdDecls[std::string(Insertion.getFilePath())].push_back(InsertFwd);
}
void ChangeNamespaceTool::replaceQualifiedSymbolInDeclContext(
const ast_matchers::MatchFinder::MatchResult &Result,
const DeclContext *DeclCtx, SourceLocation Start, SourceLocation End,
const NamedDecl *FromDecl) {
const auto *NsDeclContext = DeclCtx->getEnclosingNamespaceContext();
if (llvm::isa<TranslationUnitDecl>(NsDeclContext)) {
addReplacementOrDie(Start, End, FromDecl->getQualifiedNameAsString(),
*Result.SourceManager, &FileToReplacements);
return;
}
const auto *NsDecl = llvm::cast<NamespaceDecl>(NsDeclContext);
std::string OldNs = NsDecl->getQualifiedNameAsString();
llvm::StringRef Postfix = OldNs;
bool Consumed = Postfix.consume_front(OldNamespace);
assert(Consumed && "Expect OldNS to start with OldNamespace.");
(void)Consumed;
const std::string NewNs = (NewNamespace + Postfix).str();
llvm::StringRef NestedName = Lexer::getSourceText(
CharSourceRange::getTokenRange(
Result.SourceManager->getSpellingLoc(Start),
Result.SourceManager->getSpellingLoc(End)),
*Result.SourceManager, Result.Context->getLangOpts());
std::string FromDeclName = FromDecl->getQualifiedNameAsString();
for (llvm::Regex &RE : AllowedSymbolRegexes)
if (RE.match(FromDeclName))
return;
std::string ReplaceName =
getShortestQualifiedNameInNamespace(FromDeclName, NewNs);
for (const auto *UsingNamespace : UsingNamespaceDecls) {
if (!isDeclVisibleAtLocation(*Result.SourceManager, UsingNamespace, DeclCtx,
Start))
continue;
StringRef FromDeclNameRef = FromDeclName;
if (FromDeclNameRef.consume_front(UsingNamespace->getNominatedNamespace()
->getQualifiedNameAsString())) {
FromDeclNameRef = FromDeclNameRef.drop_front(2);
if (FromDeclNameRef.size() < ReplaceName.size())
ReplaceName = std::string(FromDeclNameRef);
}
}
for (const auto *NamespaceAlias : NamespaceAliasDecls) {
if (!isDeclVisibleAtLocation(*Result.SourceManager, NamespaceAlias, DeclCtx,
Start))
continue;
StringRef FromDeclNameRef = FromDeclName;
if (FromDeclNameRef.consume_front(
NamespaceAlias->getNamespace()->getQualifiedNameAsString() +
"::")) {
std::string AliasName = NamespaceAlias->getNameAsString();
std::string AliasQualifiedName =
NamespaceAlias->getQualifiedNameAsString();
if (AliasQualifiedName != AliasName) {
assert(StringRef(AliasQualifiedName).ends_with("::" + AliasName));
llvm::StringRef AliasNs =
StringRef(AliasQualifiedName).drop_back(AliasName.size() + 2);
if (!llvm::StringRef(OldNs).starts_with(AliasNs))
continue;
}
std::string NameWithAliasNamespace =
(AliasName + "::" + FromDeclNameRef).str();
if (NameWithAliasNamespace.size() < ReplaceName.size())
ReplaceName = NameWithAliasNamespace;
}
}
bool Matched = false;
for (const UsingDecl *Using : UsingDecls) {
if (Matched)
break;
if (isDeclVisibleAtLocation(*Result.SourceManager, Using, DeclCtx, Start)) {
for (const auto *UsingShadow : Using->shadows()) {
const auto *TargetDecl = UsingShadow->getTargetDecl();
if (TargetDecl->getQualifiedNameAsString() ==
FromDecl->getQualifiedNameAsString()) {
ReplaceName = FromDecl->getNameAsString();
Matched = true;
break;
}
}
}
}
bool Conflict = conflictInNamespace(DeclCtx->getParentASTContext(),
ReplaceName, NewNamespace);
if ((NestedName == ReplaceName && !Conflict) ||
(NestedName.starts_with("::") && NestedName.drop_front(2) == ReplaceName))
return;
if (ReplaceName == FromDeclName && !NewNamespace.empty() && Conflict)
ReplaceName = "::" + ReplaceName;
addReplacementOrDie(Start, End, ReplaceName, *Result.SourceManager,
&FileToReplacements);
}
void ChangeNamespaceTool::fixTypeLoc(
const ast_matchers::MatchFinder::MatchResult &Result, SourceLocation Start,
SourceLocation End, TypeLoc Type) {
if (Start.isInvalid() || End.isInvalid())
return;
if (llvm::is_contained(BaseCtorInitializerTypeLocs, Type))
return;
if (isTemplateParameter(Type))
return;
const auto *FromDecl = Result.Nodes.getNodeAs<NamedDecl>("from_decl");
auto IsInMovedNs = [&](const NamedDecl *D) {
if (!llvm::StringRef(D->getQualifiedNameAsString())
.starts_with(OldNamespace + "::"))
return false;
auto ExpansionLoc = Result.SourceManager->getExpansionLoc(D->getBeginLoc());
if (ExpansionLoc.isInvalid())
return false;
llvm::StringRef Filename = Result.SourceManager->getFilename(ExpansionLoc);
return FilePatternRE.match(Filename);
};
if (auto *Typedef = Type.getType()->getAs<TypedefType>()) {
FromDecl = Typedef->getDecl();
if (IsInMovedNs(FromDecl))
return;
} else if (auto *TemplateType =
Type.getType()->getAs<TemplateSpecializationType>()) {
if (TemplateType->isTypeAlias()) {
FromDecl = TemplateType->getTemplateName().getAsTemplateDecl();
if (IsInMovedNs(FromDecl))
return;
}
}
const auto *DeclCtx = Result.Nodes.getNodeAs<Decl>("dc");
assert(DeclCtx && "Empty decl context.");
replaceQualifiedSymbolInDeclContext(Result, DeclCtx->getDeclContext(), Start,
End, FromDecl);
}
void ChangeNamespaceTool::fixUsingShadowDecl(
const ast_matchers::MatchFinder::MatchResult &Result,
const UsingDecl *UsingDeclaration) {
SourceLocation Start = UsingDeclaration->getBeginLoc();
SourceLocation End = UsingDeclaration->getEndLoc();
if (Start.isInvalid() || End.isInvalid())
return;
assert(UsingDeclaration->shadow_size() > 0);
const NamedDecl *TargetDecl =
UsingDeclaration->shadow_begin()->getTargetDecl();
std::string TargetDeclName = TargetDecl->getQualifiedNameAsString();
addReplacementOrDie(Start, End, "using ::" + TargetDeclName,
*Result.SourceManager, &FileToReplacements);
}
void ChangeNamespaceTool::fixDeclRefExpr(
const ast_matchers::MatchFinder::MatchResult &Result,
const DeclContext *UseContext, const NamedDecl *From,
const DeclRefExpr *Ref) {
SourceRange RefRange = Ref->getSourceRange();
replaceQualifiedSymbolInDeclContext(Result, UseContext, RefRange.getBegin(),
RefRange.getEnd(), From);
}
void ChangeNamespaceTool::onEndOfTranslationUnit() {
for (const auto &FileAndNsMoves : MoveNamespaces) {
auto &NsMoves = FileAndNsMoves.second;
if (NsMoves.empty())
continue;
const std::string &FilePath = FileAndNsMoves.first;
auto &Replaces = FileToReplacements[FilePath];
auto &SM = *NsMoves.begin()->SourceMgr;
llvm::StringRef Code = SM.getBufferData(NsMoves.begin()->FID);
auto ChangedCode = tooling::applyAllReplacements(Code, Replaces);
if (!ChangedCode) {
llvm::errs() << llvm::toString(ChangedCode.takeError()) << "\n";
continue;
}
tooling::Replacements NewReplacements;
for (const auto &NsMove : NsMoves) {
const unsigned NewOffset = Replaces.getShiftedCodePosition(NsMove.Offset);
const unsigned NewLength =
Replaces.getShiftedCodePosition(NsMove.Offset + NsMove.Length) -
NewOffset;
tooling::Replacement Deletion(FilePath, NewOffset, NewLength, "");
std::string MovedCode = ChangedCode->substr(NewOffset, NewLength);
std::string MovedCodeWrappedInNewNs =
wrapCodeInNamespace(DiffNewNamespace, MovedCode);
unsigned NewInsertionOffset =
Replaces.getShiftedCodePosition(NsMove.InsertionOffset);
tooling::Replacement Insertion(FilePath, NewInsertionOffset, 0,
MovedCodeWrappedInNewNs);
addOrMergeReplacement(Deletion, &NewReplacements);
addOrMergeReplacement(Insertion, &NewReplacements);
}
const auto &FwdDeclInsertions = InsertFwdDecls[FilePath];
for (const auto &FwdDeclInsertion : FwdDeclInsertions) {
unsigned NewInsertionOffset =
Replaces.getShiftedCodePosition(FwdDeclInsertion.InsertionOffset);
tooling::Replacement Insertion(FilePath, NewInsertionOffset, 0,
FwdDeclInsertion.ForwardDeclText);
addOrMergeReplacement(Insertion, &NewReplacements);
}
Replaces = Replaces.merge(NewReplacements);
auto Style =
format::getStyle(format::DefaultFormatStyle, FilePath, FallbackStyle);
if (!Style) {
llvm::errs() << llvm::toString(Style.takeError()) << "\n";
continue;
}
auto CleanReplacements =
format::cleanupAroundReplacements(Code, Replaces, *Style);
if (!CleanReplacements) {
llvm::errs() << llvm::toString(CleanReplacements.takeError()) << "\n";
continue;
}
FileToReplacements[FilePath] = *CleanReplacements;
}
for (auto &Entry : FileToReplacements)
if (!FilePatternRE.match(Entry.first))
Entry.second.clear();
}
}
}