2016-09-20 01:40:32 +08:00
|
|
|
//===-- ChangeNamespace.cpp - Change namespace implementation -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "ChangeNamespace.h"
|
2018-10-22 20:48:49 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2016-09-20 01:40:32 +08:00
|
|
|
#include "clang/Format/Format.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
2018-10-22 20:48:49 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2016-11-17 00:54:53 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2016-09-20 01:40:32 +08:00
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace change_namespace {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline std::string
|
|
|
|
joinNamespaces(const llvm::SmallVectorImpl<StringRef> &Namespaces) {
|
|
|
|
if (Namespaces.empty())
|
|
|
|
return "";
|
|
|
|
std::string Result = Namespaces.front();
|
|
|
|
for (auto I = Namespaces.begin() + 1, E = Namespaces.end(); I != E; ++I)
|
|
|
|
Result += ("::" + *I).str();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-03-21 20:41:59 +08:00
|
|
|
// Given "a::b::c", returns {"a", "b", "c"}.
|
|
|
|
llvm::SmallVector<llvm::StringRef, 4> splitSymbolName(llvm::StringRef Name) {
|
|
|
|
llvm::SmallVector<llvm::StringRef, 4> Splitted;
|
|
|
|
Name.split(Splitted, "::", /*MaxSplit=*/-1,
|
|
|
|
/*KeepEmpty=*/false);
|
|
|
|
return Splitted;
|
|
|
|
}
|
|
|
|
|
2016-09-20 01:40:32 +08:00
|
|
|
SourceLocation startLocationForType(TypeLoc TLoc) {
|
|
|
|
// For elaborated types (e.g. `struct a::A`) we want the portion after the
|
|
|
|
// `struct` but including the namespace qualifier, `a::`.
|
|
|
|
if (TLoc.getTypeLocClass() == TypeLoc::Elaborated) {
|
|
|
|
NestedNameSpecifierLoc NestedNameSpecifier =
|
|
|
|
TLoc.castAs<ElaboratedTypeLoc>().getQualifierLoc();
|
|
|
|
if (NestedNameSpecifier.getNestedNameSpecifier())
|
|
|
|
return NestedNameSpecifier.getBeginLoc();
|
|
|
|
TLoc = TLoc.getNextTypeLoc();
|
|
|
|
}
|
2018-08-10 06:42:26 +08:00
|
|
|
return TLoc.getBeginLoc();
|
2016-09-20 01:40:32 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 01:25:55 +08:00
|
|
|
SourceLocation endLocationForType(TypeLoc TLoc) {
|
2016-09-20 01:40:32 +08:00
|
|
|
// Dig past any namespace or keyword qualifications.
|
|
|
|
while (TLoc.getTypeLocClass() == TypeLoc::Elaborated ||
|
|
|
|
TLoc.getTypeLocClass() == TypeLoc::Qualified)
|
|
|
|
TLoc = TLoc.getNextTypeLoc();
|
|
|
|
|
|
|
|
// The location for template specializations (e.g. Foo<int>) includes the
|
|
|
|
// templated types in its location range. We want to restrict this to just
|
|
|
|
// before the `<` character.
|
|
|
|
if (TLoc.getTypeLocClass() == TypeLoc::TemplateSpecialization)
|
|
|
|
return TLoc.castAs<TemplateSpecializationTypeLoc>()
|
|
|
|
.getLAngleLoc()
|
|
|
|
.getLocWithOffset(-1);
|
|
|
|
return TLoc.getEndLoc();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the containing namespace of `InnerNs` by skipping `PartialNsName`.
|
2016-11-11 02:29:01 +08:00
|
|
|
// If the `InnerNs` does not have `PartialNsName` as suffix, or `PartialNsName`
|
|
|
|
// is empty, nullptr is returned.
|
2016-09-20 01:40:32 +08:00
|
|
|
// For example, if `InnerNs` is "a::b::c" and `PartialNsName` is "b::c", then
|
|
|
|
// the NamespaceDecl of namespace "a" will be returned.
|
|
|
|
const NamespaceDecl *getOuterNamespace(const NamespaceDecl *InnerNs,
|
|
|
|
llvm::StringRef PartialNsName) {
|
2016-11-11 02:29:01 +08:00
|
|
|
if (!InnerNs || PartialNsName.empty())
|
|
|
|
return nullptr;
|
2016-09-20 01:40:32 +08:00
|
|
|
const auto *CurrentContext = llvm::cast<DeclContext>(InnerNs);
|
|
|
|
const auto *CurrentNs = InnerNs;
|
2017-03-21 20:41:59 +08:00
|
|
|
auto PartialNsNameSplitted = splitSymbolName(PartialNsName);
|
2016-09-20 01:40:32 +08:00
|
|
|
while (!PartialNsNameSplitted.empty()) {
|
|
|
|
// Get the inner-most namespace in CurrentContext.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-12 20:34:18 +08:00
|
|
|
static std::unique_ptr<Lexer>
|
|
|
|
getLexerStartingFromLoc(SourceLocation Loc, const SourceManager &SM,
|
|
|
|
const LangOptions &LangOpts) {
|
2016-09-20 01:40:32 +08:00
|
|
|
if (Loc.isMacroID() &&
|
|
|
|
!Lexer::isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc))
|
2016-10-12 20:34:18 +08:00
|
|
|
return nullptr;
|
2016-09-20 01:40:32 +08:00
|
|
|
// Break down the source location.
|
|
|
|
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
|
|
|
|
// Try to load the file buffer.
|
|
|
|
bool InvalidTemp = false;
|
|
|
|
llvm::StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp);
|
|
|
|
if (InvalidTemp)
|
2016-10-12 20:34:18 +08:00
|
|
|
return nullptr;
|
2016-09-20 01:40:32 +08:00
|
|
|
|
|
|
|
const char *TokBegin = File.data() + LocInfo.second;
|
|
|
|
// Lex from the start of the given location.
|
2016-10-12 20:34:18 +08:00
|
|
|
return llvm::make_unique<Lexer>(SM.getLocForStartOfFile(LocInfo.first),
|
|
|
|
LangOpts, File.begin(), TokBegin, File.end());
|
|
|
|
}
|
2016-09-20 01:40:32 +08:00
|
|
|
|
2016-10-12 20:34:18 +08:00
|
|
|
// FIXME: get rid of this helper function if this is supported in clang-refactor
|
|
|
|
// library.
|
|
|
|
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();
|
2016-09-20 01:40:32 +08:00
|
|
|
llvm::SmallVector<char, 16> Line;
|
|
|
|
// FIXME: this is a bit hacky to get ReadToEndOfLine work.
|
2016-10-12 20:34:18 +08:00
|
|
|
Lex->setParsingPreprocessorDirective(true);
|
|
|
|
Lex->ReadToEndOfLine(&Line);
|
2016-10-04 18:35:53 +08:00
|
|
|
auto End = Loc.getLocWithOffset(Line.size());
|
2016-10-12 20:34:18 +08:00
|
|
|
return SM.getLocForEndOfFile(SM.getDecomposedLoc(Loc).first) == End
|
|
|
|
? End
|
|
|
|
: End.getLocWithOffset(1);
|
2016-09-20 01:40:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns `R` with new range that refers to code after `Replaces` being
|
|
|
|
// applied.
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a replacement `R` into `Replaces` or merges it into `Replaces` by
|
|
|
|
// applying all existing Replaces first if there is conflict.
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-12-15 01:01:52 +08:00
|
|
|
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)[R.getFilePath()].add(R);
|
|
|
|
if (Err)
|
|
|
|
llvm_unreachable(llvm::toString(std::move(Err)).c_str());
|
|
|
|
}
|
|
|
|
|
2016-09-20 01:40:32 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the shortest qualified name for declaration `DeclName` in the
|
|
|
|
// namespace `NsName`. For example, if `DeclName` is "a::b::X" and `NsName`
|
|
|
|
// is "a::c::d", then "b::X" will be returned.
|
2017-01-26 23:08:44 +08:00
|
|
|
// Note that if `DeclName` is `::b::X` and `NsName` is `::a::b`, this returns
|
|
|
|
// "::b::X" instead of "b::X" since there will be a name conflict otherwise.
|
2016-10-05 23:52:39 +08:00
|
|
|
// \param DeclName A fully qualified name, "::a::b::X" or "a::b::X".
|
|
|
|
// \param NsName A fully qualified name, "::a::b" or "a::b". Global namespace
|
|
|
|
// will have empty name.
|
2016-09-20 01:40:32 +08:00
|
|
|
std::string getShortestQualifiedNameInNamespace(llvm::StringRef DeclName,
|
|
|
|
llvm::StringRef NsName) {
|
2016-10-05 23:52:39 +08:00
|
|
|
DeclName = DeclName.ltrim(':');
|
|
|
|
NsName = NsName.ltrim(':');
|
|
|
|
if (DeclName.find(':') == llvm::StringRef::npos)
|
2016-11-09 06:44:17 +08:00
|
|
|
return DeclName;
|
2016-10-05 23:52:39 +08:00
|
|
|
|
2017-03-21 20:41:59 +08:00
|
|
|
auto NsNameSplitted = splitSymbolName(NsName);
|
|
|
|
auto DeclNsSplitted = splitSymbolName(DeclName);
|
2017-01-26 23:08:44 +08:00
|
|
|
llvm::StringRef UnqualifiedDeclName = DeclNsSplitted.pop_back_val();
|
|
|
|
// If the Decl is in global namespace, there is no need to shorten it.
|
|
|
|
if (DeclNsSplitted.empty())
|
|
|
|
return UnqualifiedDeclName;
|
|
|
|
// If NsName is the global namespace, we can simply use the DeclName sans
|
|
|
|
// leading "::".
|
|
|
|
if (NsNameSplitted.empty())
|
|
|
|
return DeclName;
|
|
|
|
|
|
|
|
if (NsNameSplitted.front() != DeclNsSplitted.front()) {
|
|
|
|
// The DeclName must be fully-qualified, but we still need to decide if a
|
|
|
|
// leading "::" is necessary. For example, if `NsName` is "a::b::c" and the
|
|
|
|
// `DeclName` is "b::X", then the reference must be qualified as "::b::X"
|
|
|
|
// to avoid conflict.
|
|
|
|
if (llvm::is_contained(NsNameSplitted, DeclNsSplitted.front()))
|
|
|
|
return ("::" + DeclName).str();
|
|
|
|
return DeclName;
|
|
|
|
}
|
|
|
|
// Since there is already an overlap namespace, we know that `DeclName` can be
|
|
|
|
// shortened, so we reduce the longest common prefix.
|
|
|
|
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) {
|
2016-09-20 01:40:32 +08:00
|
|
|
}
|
2017-01-26 23:08:44 +08:00
|
|
|
return (DeclI == DeclE)
|
|
|
|
? UnqualifiedDeclName.str()
|
|
|
|
: (llvm::join(DeclI, DeclE, "::") + "::" + UnqualifiedDeclName)
|
|
|
|
.str();
|
2016-09-20 01:40:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string wrapCodeInNamespace(StringRef NestedNs, std::string Code) {
|
|
|
|
if (Code.back() != '\n')
|
|
|
|
Code += "\n";
|
2017-03-21 20:41:59 +08:00
|
|
|
auto NsSplitted = splitSymbolName(NestedNs);
|
2016-09-20 01:40:32 +08:00
|
|
|
while (!NsSplitted.empty()) {
|
|
|
|
// FIXME: consider code style for comments.
|
|
|
|
Code = ("namespace " + NsSplitted.back() + " {\n" + Code +
|
|
|
|
"} // namespace " + NsSplitted.back() + "\n")
|
|
|
|
.str();
|
|
|
|
NsSplitted.pop_back();
|
|
|
|
}
|
|
|
|
return Code;
|
|
|
|
}
|
|
|
|
|
2016-11-09 06:44:17 +08:00
|
|
|
// Returns true if \p D is a nested DeclContext in \p Context
|
|
|
|
bool isNestedDeclContext(const DeclContext *D, const DeclContext *Context) {
|
|
|
|
while (D) {
|
|
|
|
if (D == Context)
|
|
|
|
return true;
|
|
|
|
D = D->getParent();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if \p D is visible at \p Loc with DeclContext \p DeclCtx.
|
|
|
|
bool isDeclVisibleAtLocation(const SourceManager &SM, const Decl *D,
|
|
|
|
const DeclContext *DeclCtx, SourceLocation Loc) {
|
2018-08-10 06:42:26 +08:00
|
|
|
SourceLocation DeclLoc = SM.getSpellingLoc(D->getBeginLoc());
|
2016-11-09 06:44:17 +08:00
|
|
|
Loc = SM.getSpellingLoc(Loc);
|
|
|
|
return SM.isBeforeInTranslationUnit(DeclLoc, Loc) &&
|
|
|
|
(SM.getFileID(DeclLoc) == SM.getFileID(Loc) &&
|
|
|
|
isNestedDeclContext(DeclCtx, D->getDeclContext()));
|
|
|
|
}
|
|
|
|
|
2017-03-21 20:41:59 +08:00
|
|
|
// Given a qualified symbol name, returns true if the symbol will be
|
2018-10-22 20:48:49 +08:00
|
|
|
// incorrectly qualified without leading "::". For example, a symbol
|
|
|
|
// "nx::ny::Foo" in namespace "na::nx::ny" without leading "::"; a symbol
|
|
|
|
// "util::X" in namespace "na" can potentially conflict with "na::util" (if this
|
|
|
|
// exists).
|
|
|
|
bool conflictInNamespace(const ASTContext &AST, llvm::StringRef QualifiedSymbol,
|
2017-03-21 20:41:59 +08:00
|
|
|
llvm::StringRef Namespace) {
|
|
|
|
auto SymbolSplitted = splitSymbolName(QualifiedSymbol.trim(":"));
|
|
|
|
assert(!SymbolSplitted.empty());
|
|
|
|
SymbolSplitted.pop_back(); // We are only interested in namespaces.
|
|
|
|
|
2018-10-22 20:48:49 +08:00
|
|
|
if (SymbolSplitted.size() >= 1 && !Namespace.empty()) {
|
|
|
|
auto SymbolTopNs = SymbolSplitted.front();
|
2017-03-21 20:41:59 +08:00
|
|
|
auto NsSplitted = splitSymbolName(Namespace.trim(":"));
|
|
|
|
assert(!NsSplitted.empty());
|
2018-10-22 20:48:49 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
// We do not check the outermost namespace since it would not be a
|
|
|
|
// conflict if it equals to the symbol's outermost namespace and the
|
|
|
|
// symbol name would have been shortened.
|
|
|
|
const NamedDecl *Scope =
|
|
|
|
LookupDecl(*AST.getTranslationUnitDecl(), NsSplitted.front());
|
2017-03-21 20:41:59 +08:00
|
|
|
for (auto I = NsSplitted.begin() + 1, E = NsSplitted.end(); I != E; ++I) {
|
2018-10-22 20:48:49 +08:00
|
|
|
if (*I == SymbolTopNs) // Handles "::ny" in "::nx::ny" case.
|
2017-03-21 20:41:59 +08:00
|
|
|
return true;
|
2018-10-22 20:48:49 +08:00
|
|
|
// Handles "::util" and "::nx::util" conflicts.
|
|
|
|
if (Scope) {
|
|
|
|
if (LookupDecl(*Scope, SymbolTopNs))
|
|
|
|
return true;
|
|
|
|
Scope = LookupDecl(*Scope, *I);
|
|
|
|
}
|
2017-03-21 20:41:59 +08:00
|
|
|
}
|
2018-10-22 20:48:49 +08:00
|
|
|
if (Scope && LookupDecl(*Scope, SymbolTopNs))
|
|
|
|
return true;
|
2017-03-21 20:41:59 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-03 01:40:38 +08:00
|
|
|
AST_MATCHER(EnumDecl, isScoped) {
|
|
|
|
return Node.isScoped();
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:05:39 +08:00
|
|
|
bool isTemplateParameter(TypeLoc Type) {
|
|
|
|
while (!Type.isNull()) {
|
|
|
|
if (Type.getTypeLocClass() == TypeLoc::SubstTemplateTypeParm)
|
|
|
|
return true;
|
|
|
|
Type = Type.getNextTypeLoc();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-20 01:40:32 +08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
ChangeNamespaceTool::ChangeNamespaceTool(
|
|
|
|
llvm::StringRef OldNs, llvm::StringRef NewNs, llvm::StringRef FilePattern,
|
2017-02-24 19:54:45 +08:00
|
|
|
llvm::ArrayRef<std::string> WhiteListedSymbolPatterns,
|
2016-09-20 01:40:32 +08:00
|
|
|
std::map<std::string, tooling::Replacements> *FileToReplacements,
|
|
|
|
llvm::StringRef FallbackStyle)
|
|
|
|
: FallbackStyle(FallbackStyle), FileToReplacements(*FileToReplacements),
|
|
|
|
OldNamespace(OldNs.ltrim(':')), NewNamespace(NewNs.ltrim(':')),
|
2016-12-02 01:25:55 +08:00
|
|
|
FilePattern(FilePattern), FilePatternRE(FilePattern) {
|
2016-09-20 01:40:32 +08:00
|
|
|
FileToReplacements->clear();
|
2017-03-21 20:41:59 +08:00
|
|
|
auto OldNsSplitted = splitSymbolName(OldNamespace);
|
|
|
|
auto NewNsSplitted = splitSymbolName(NewNamespace);
|
2016-09-20 01:40:32 +08:00
|
|
|
// Calculates `DiffOldNamespace` and `DiffNewNamespace`.
|
|
|
|
while (!OldNsSplitted.empty() && !NewNsSplitted.empty() &&
|
|
|
|
OldNsSplitted.front() == NewNsSplitted.front()) {
|
|
|
|
OldNsSplitted.erase(OldNsSplitted.begin());
|
|
|
|
NewNsSplitted.erase(NewNsSplitted.begin());
|
|
|
|
}
|
|
|
|
DiffOldNamespace = joinNamespaces(OldNsSplitted);
|
|
|
|
DiffNewNamespace = joinNamespaces(NewNsSplitted);
|
2017-02-24 19:54:45 +08:00
|
|
|
|
|
|
|
for (const auto &Pattern : WhiteListedSymbolPatterns)
|
|
|
|
WhiteListedSymbolRegexes.emplace_back(Pattern);
|
2016-09-20 01:40:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChangeNamespaceTool::registerMatchers(ast_matchers::MatchFinder *Finder) {
|
|
|
|
std::string FullOldNs = "::" + OldNamespace;
|
2016-11-09 06:44:17 +08:00
|
|
|
// Prefix is the outer-most namespace in DiffOldNamespace. For example, if the
|
|
|
|
// OldNamespace is "a::b::c" and DiffOldNamespace is "b::c", then Prefix will
|
|
|
|
// be "a::b". Declarations in this namespace will not be visible in the new
|
|
|
|
// namespace. If DiffOldNamespace is empty, Prefix will be a invalid name "-".
|
|
|
|
llvm::SmallVector<llvm::StringRef, 4> DiffOldNsSplitted;
|
2016-12-05 19:17:04 +08:00
|
|
|
llvm::StringRef(DiffOldNamespace)
|
|
|
|
.split(DiffOldNsSplitted, "::", /*MaxSplit=*/-1,
|
|
|
|
/*KeepEmpty=*/false);
|
2016-11-09 06:44:17 +08:00
|
|
|
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)))));
|
|
|
|
// Match using declarations.
|
|
|
|
Finder->addMatcher(
|
|
|
|
usingDecl(isExpansionInFileMatching(FilePattern), IsVisibleInNewNs)
|
|
|
|
.bind("using"),
|
|
|
|
this);
|
|
|
|
// Match using namespace declarations.
|
|
|
|
Finder->addMatcher(usingDirectiveDecl(isExpansionInFileMatching(FilePattern),
|
|
|
|
IsVisibleInNewNs)
|
|
|
|
.bind("using_namespace"),
|
|
|
|
this);
|
2016-12-23 18:47:09 +08:00
|
|
|
// Match namespace alias declarations.
|
|
|
|
Finder->addMatcher(namespaceAliasDecl(isExpansionInFileMatching(FilePattern),
|
|
|
|
IsVisibleInNewNs)
|
|
|
|
.bind("namespace_alias"),
|
|
|
|
this);
|
2016-11-09 06:44:17 +08:00
|
|
|
|
|
|
|
// Match old namespace blocks.
|
2016-09-20 01:40:32 +08:00
|
|
|
Finder->addMatcher(
|
|
|
|
namespaceDecl(hasName(FullOldNs), isExpansionInFileMatching(FilePattern))
|
|
|
|
.bind("old_ns"),
|
|
|
|
this);
|
|
|
|
|
2016-12-07 22:20:52 +08:00
|
|
|
// Match class forward-declarations in the old namespace.
|
|
|
|
// Note that forward-declarations in classes are not matched.
|
|
|
|
Finder->addMatcher(cxxRecordDecl(unless(anyOf(isImplicit(), isDefinition())),
|
|
|
|
IsInMovedNs, hasParent(namespaceDecl()))
|
|
|
|
.bind("class_fwd_decl"),
|
|
|
|
this);
|
|
|
|
|
|
|
|
// Match template class forward-declarations in the old namespace.
|
2016-09-20 01:40:32 +08:00
|
|
|
Finder->addMatcher(
|
2016-12-07 22:20:52 +08:00
|
|
|
classTemplateDecl(unless(hasDescendant(cxxRecordDecl(isDefinition()))),
|
|
|
|
IsInMovedNs, hasParent(namespaceDecl()))
|
|
|
|
.bind("template_class_fwd_decl"),
|
2016-09-20 01:40:32 +08:00
|
|
|
this);
|
|
|
|
|
|
|
|
// Match references to types that are not defined in the old namespace.
|
|
|
|
// Forward-declarations in the old namespace are also matched since they will
|
|
|
|
// be moved back to the old namespace.
|
|
|
|
auto DeclMatcher = namedDecl(
|
|
|
|
hasAncestor(namespaceDecl()),
|
|
|
|
unless(anyOf(
|
2016-09-27 20:54:48 +08:00
|
|
|
isImplicit(), hasAncestor(namespaceDecl(isAnonymous())),
|
2016-09-20 01:40:32 +08:00
|
|
|
hasAncestor(cxxRecordDecl()),
|
|
|
|
allOf(IsInMovedNs, unless(cxxRecordDecl(unless(isDefinition())))))));
|
2016-09-27 20:54:48 +08:00
|
|
|
|
2016-12-08 01:04:07 +08:00
|
|
|
// Using shadow declarations in classes always refers to base class, which
|
|
|
|
// does not need to be qualified since it can be inferred from inheritance.
|
|
|
|
// Note that this does not match using alias declarations.
|
|
|
|
auto UsingShadowDeclInClass =
|
|
|
|
usingDecl(hasAnyUsingShadowDecl(decl()), hasParent(cxxRecordDecl()));
|
|
|
|
|
2016-09-20 01:40:32 +08:00
|
|
|
// Match TypeLocs on the declaration. Carefully match only the outermost
|
2016-10-31 16:28:29 +08:00
|
|
|
// TypeLoc and template specialization arguments (which are not outermost)
|
|
|
|
// that are directly linked to types matching `DeclMatcher`. Nested name
|
|
|
|
// specifier locs are handled separately below.
|
2016-09-20 01:40:32 +08:00
|
|
|
Finder->addMatcher(
|
|
|
|
typeLoc(IsInMovedNs,
|
|
|
|
loc(qualType(hasDeclaration(DeclMatcher.bind("from_decl")))),
|
2016-10-31 16:28:29 +08:00
|
|
|
unless(anyOf(hasParent(typeLoc(loc(qualType(
|
|
|
|
allOf(hasDeclaration(DeclMatcher),
|
|
|
|
unless(templateSpecializationType())))))),
|
2016-12-08 01:04:07 +08:00
|
|
|
hasParent(nestedNameSpecifierLoc()),
|
|
|
|
hasAncestor(isImplicit()),
|
2017-10-16 16:20:10 +08:00
|
|
|
hasAncestor(UsingShadowDeclInClass),
|
|
|
|
hasAncestor(functionDecl(isDefaulted())))),
|
2016-09-20 01:40:32 +08:00
|
|
|
hasAncestor(decl().bind("dc")))
|
|
|
|
.bind("type"),
|
|
|
|
this);
|
2016-09-27 20:54:48 +08:00
|
|
|
|
2016-09-21 23:06:12 +08:00
|
|
|
// Types in `UsingShadowDecl` is not matched by `typeLoc` above, so we need to
|
|
|
|
// special case it.
|
2016-12-08 01:04:07 +08:00
|
|
|
// Since using declarations inside classes must have the base class in the
|
|
|
|
// nested name specifier, we leave it to the nested name specifier matcher.
|
|
|
|
Finder->addMatcher(usingDecl(IsInMovedNs, hasAnyUsingShadowDecl(decl()),
|
|
|
|
unless(UsingShadowDeclInClass))
|
2016-11-09 06:44:17 +08:00
|
|
|
.bind("using_with_shadow"),
|
|
|
|
this);
|
2016-09-27 20:54:48 +08:00
|
|
|
|
2016-11-17 00:54:53 +08:00
|
|
|
// Handle types in nested name specifier. Specifiers that are in a TypeLoc
|
|
|
|
// matched above are not matched, e.g. "A::" in "A::A" is not matched since
|
|
|
|
// "A::A" would have already been fixed.
|
2016-12-08 01:04:07 +08:00
|
|
|
Finder->addMatcher(
|
|
|
|
nestedNameSpecifierLoc(
|
|
|
|
hasAncestor(decl(IsInMovedNs).bind("dc")),
|
|
|
|
loc(nestedNameSpecifier(
|
|
|
|
specifiesType(hasDeclaration(DeclMatcher.bind("from_decl"))))),
|
|
|
|
unless(anyOf(hasAncestor(isImplicit()),
|
|
|
|
hasAncestor(UsingShadowDeclInClass),
|
2017-10-16 16:20:10 +08:00
|
|
|
hasAncestor(functionDecl(isDefaulted())),
|
2016-12-08 01:04:07 +08:00
|
|
|
hasAncestor(typeLoc(loc(qualType(hasDeclaration(
|
|
|
|
decl(equalsBoundNode("from_decl"))))))))))
|
|
|
|
.bind("nested_specifier_loc"),
|
|
|
|
this);
|
2016-09-22 19:54:00 +08:00
|
|
|
|
2016-11-17 00:54:53 +08:00
|
|
|
// Matches base class initializers in constructors. TypeLocs of base class
|
|
|
|
// initializers do not need to be fixed. For example,
|
|
|
|
// class X : public a::b::Y {
|
|
|
|
// public:
|
|
|
|
// X() : Y::Y() {} // Y::Y do not need namespace specifier.
|
|
|
|
// };
|
|
|
|
Finder->addMatcher(
|
|
|
|
cxxCtorInitializer(isBaseInitializer()).bind("base_initializer"), this);
|
|
|
|
|
2016-09-22 19:54:00 +08:00
|
|
|
// Handle function.
|
2016-09-27 20:54:48 +08:00
|
|
|
// Only handle functions that are defined in a namespace excluding member
|
|
|
|
// function, static methods (qualified by nested specifier), and functions
|
|
|
|
// defined in the global namespace.
|
2016-09-22 19:54:00 +08:00
|
|
|
// Note that the matcher does not exclude calls to out-of-line static method
|
|
|
|
// definitions, so we need to exclude them in the callback handler.
|
2016-09-27 20:54:48 +08:00
|
|
|
auto FuncMatcher =
|
|
|
|
functionDecl(unless(anyOf(cxxMethodDecl(), IsInMovedNs,
|
|
|
|
hasAncestor(namespaceDecl(isAnonymous())),
|
|
|
|
hasAncestor(cxxRecordDecl()))),
|
|
|
|
hasParent(namespaceDecl()));
|
2018-03-15 22:45:02 +08:00
|
|
|
Finder->addMatcher(
|
|
|
|
expr(allOf(hasAncestor(decl().bind("dc")), IsInMovedNs,
|
|
|
|
unless(hasAncestor(isImplicit())),
|
|
|
|
anyOf(callExpr(callee(FuncMatcher)).bind("call"),
|
|
|
|
declRefExpr(to(FuncMatcher.bind("func_decl")))
|
|
|
|
.bind("func_ref")))),
|
|
|
|
this);
|
2016-09-30 12:32:39 +08:00
|
|
|
|
|
|
|
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);
|
2017-02-03 01:40:38 +08:00
|
|
|
|
|
|
|
// Handle unscoped enum constant.
|
|
|
|
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);
|
2016-09-20 01:40:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChangeNamespaceTool::run(
|
|
|
|
const ast_matchers::MatchFinder::MatchResult &Result) {
|
2016-11-09 06:44:17 +08:00
|
|
|
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);
|
2016-12-23 18:47:09 +08:00
|
|
|
} else if (const auto *NamespaceAlias =
|
|
|
|
Result.Nodes.getNodeAs<NamespaceAliasDecl>(
|
|
|
|
"namespace_alias")) {
|
|
|
|
NamespaceAliasDecls.insert(NamespaceAlias);
|
2016-11-09 06:44:17 +08:00
|
|
|
} else if (const auto *NsDecl =
|
|
|
|
Result.Nodes.getNodeAs<NamespaceDecl>("old_ns")) {
|
2016-09-20 01:40:32 +08:00
|
|
|
moveOldNamespace(Result, NsDecl);
|
|
|
|
} else if (const auto *FwdDecl =
|
2016-12-07 22:20:52 +08:00
|
|
|
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));
|
2016-11-09 06:44:17 +08:00
|
|
|
} else if (const auto *UsingWithShadow =
|
|
|
|
Result.Nodes.getNodeAs<UsingDecl>("using_with_shadow")) {
|
|
|
|
fixUsingShadowDecl(Result, UsingWithShadow);
|
2016-09-21 23:06:12 +08:00
|
|
|
} else if (const auto *Specifier =
|
|
|
|
Result.Nodes.getNodeAs<NestedNameSpecifierLoc>(
|
|
|
|
"nested_specifier_loc")) {
|
|
|
|
SourceLocation Start = Specifier->getBeginLoc();
|
2016-12-02 01:25:55 +08:00
|
|
|
SourceLocation End = endLocationForType(Specifier->getTypeLoc());
|
2016-09-21 23:06:12 +08:00
|
|
|
fixTypeLoc(Result, Start, End, Specifier->getTypeLoc());
|
2016-11-17 00:54:53 +08:00
|
|
|
} else if (const auto *BaseInitializer =
|
|
|
|
Result.Nodes.getNodeAs<CXXCtorInitializer>(
|
|
|
|
"base_initializer")) {
|
|
|
|
BaseCtorInitializerTypeLocs.push_back(
|
|
|
|
BaseInitializer->getTypeSourceInfo()->getTypeLoc());
|
2016-09-22 19:54:00 +08:00
|
|
|
} else if (const auto *TLoc = Result.Nodes.getNodeAs<TypeLoc>("type")) {
|
2016-12-15 18:42:35 +08:00
|
|
|
// This avoids fixing types with record types as qualifier, which is not
|
|
|
|
// filtered by matchers in some cases, e.g. the type is templated. We should
|
|
|
|
// handle the record type qualifier instead.
|
2016-12-15 21:02:41 +08:00
|
|
|
TypeLoc Loc = *TLoc;
|
|
|
|
while (Loc.getTypeLocClass() == TypeLoc::Qualified)
|
|
|
|
Loc = Loc.getNextTypeLoc();
|
|
|
|
if (Loc.getTypeLocClass() == TypeLoc::Elaborated) {
|
2016-12-15 18:42:35 +08:00
|
|
|
NestedNameSpecifierLoc NestedNameSpecifier =
|
2016-12-15 21:02:41 +08:00
|
|
|
Loc.castAs<ElaboratedTypeLoc>().getQualifierLoc();
|
2017-12-08 18:06:16 +08:00
|
|
|
// This happens for friend declaration of a base class with injected class
|
|
|
|
// name.
|
|
|
|
if (!NestedNameSpecifier.getNestedNameSpecifier())
|
|
|
|
return;
|
2016-12-15 18:42:35 +08:00
|
|
|
const Type *SpecifierType =
|
|
|
|
NestedNameSpecifier.getNestedNameSpecifier()->getAsType();
|
|
|
|
if (SpecifierType && SpecifierType->isRecordType())
|
|
|
|
return;
|
|
|
|
}
|
2016-12-15 21:02:41 +08:00
|
|
|
fixTypeLoc(Result, startLocationForType(Loc), endLocationForType(Loc), Loc);
|
2016-11-08 15:50:19 +08:00
|
|
|
} else if (const auto *VarRef =
|
|
|
|
Result.Nodes.getNodeAs<DeclRefExpr>("var_ref")) {
|
2016-09-30 12:32:39 +08:00
|
|
|
const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var_decl");
|
|
|
|
assert(Var);
|
|
|
|
if (Var->getCanonicalDecl()->isStaticDataMember())
|
|
|
|
return;
|
2016-11-29 22:15:14 +08:00
|
|
|
const auto *Context = Result.Nodes.getNodeAs<Decl>("dc");
|
2016-09-30 12:32:39 +08:00
|
|
|
assert(Context && "Empty decl context.");
|
2016-11-29 22:15:14 +08:00
|
|
|
fixDeclRefExpr(Result, Context->getDeclContext(),
|
|
|
|
llvm::cast<NamedDecl>(Var), VarRef);
|
2017-02-03 01:40:38 +08:00
|
|
|
} else if (const auto *EnumConstRef =
|
|
|
|
Result.Nodes.getNodeAs<DeclRefExpr>("enum_const_ref")) {
|
|
|
|
// Do not rename the reference if it is already scoped by the EnumDecl name.
|
|
|
|
if (EnumConstRef->hasQualifier() &&
|
2017-02-03 03:46:12 +08:00
|
|
|
EnumConstRef->getQualifier()->getKind() ==
|
|
|
|
NestedNameSpecifier::SpecifierKind::TypeSpec &&
|
2017-02-03 01:40:38 +08:00
|
|
|
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.");
|
|
|
|
// FIXME: this would qualify "ns::VALUE" as "ns::EnumValue::VALUE". Fix it
|
|
|
|
// if it turns out to be an issue.
|
|
|
|
fixDeclRefExpr(Result, Context->getDeclContext(),
|
|
|
|
llvm::cast<NamedDecl>(EnumConstDecl), EnumConstRef);
|
2016-11-29 22:15:14 +08:00
|
|
|
} else if (const auto *FuncRef =
|
|
|
|
Result.Nodes.getNodeAs<DeclRefExpr>("func_ref")) {
|
2016-12-20 22:39:04 +08:00
|
|
|
// If this reference has been processed as a function call, we do not
|
|
|
|
// process it again.
|
|
|
|
if (ProcessedFuncRefs.count(FuncRef))
|
|
|
|
return;
|
|
|
|
ProcessedFuncRefs.insert(FuncRef);
|
2016-11-29 22:15:14 +08:00
|
|
|
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);
|
2016-09-22 19:54:00 +08:00
|
|
|
} else {
|
2016-11-29 22:15:14 +08:00
|
|
|
const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
|
2016-11-08 15:50:19 +08:00
|
|
|
assert(Call != nullptr && "Expecting callback for CallExpr.");
|
2016-12-20 22:39:04 +08:00
|
|
|
const auto *CalleeFuncRef =
|
|
|
|
llvm::cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit());
|
|
|
|
ProcessedFuncRefs.insert(CalleeFuncRef);
|
2016-11-29 22:15:14 +08:00
|
|
|
const FunctionDecl *Func = Call->getDirectCallee();
|
2016-09-22 19:54:00 +08:00
|
|
|
assert(Func != nullptr);
|
2016-12-20 22:39:04 +08:00
|
|
|
// FIXME: ignore overloaded operators. This would miss cases where operators
|
|
|
|
// are called by qualified names (i.e. "ns::operator <"). Ignore such
|
|
|
|
// cases for now.
|
|
|
|
if (Func->isOverloadedOperator())
|
|
|
|
return;
|
2016-09-22 19:54:00 +08:00
|
|
|
// Ignore out-of-line static methods since they will be handled by nested
|
|
|
|
// name specifiers.
|
|
|
|
if (Func->getCanonicalDecl()->getStorageClass() ==
|
2016-11-29 22:15:14 +08:00
|
|
|
StorageClass::SC_Static &&
|
2016-09-22 19:54:00 +08:00
|
|
|
Func->isOutOfLine())
|
|
|
|
return;
|
2016-11-29 22:15:14 +08:00
|
|
|
const auto *Context = Result.Nodes.getNodeAs<Decl>("dc");
|
2016-09-22 19:54:00 +08:00
|
|
|
assert(Context && "Empty decl context.");
|
2016-11-29 22:15:14 +08:00
|
|
|
SourceRange CalleeRange = Call->getCallee()->getSourceRange();
|
2016-11-09 06:44:17 +08:00
|
|
|
replaceQualifiedSymbolInDeclContext(
|
|
|
|
Result, Context->getDeclContext(), CalleeRange.getBegin(),
|
2016-11-11 02:15:34 +08:00
|
|
|
CalleeRange.getEnd(), llvm::cast<NamedDecl>(Func));
|
2016-09-20 01:40:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 20:34:18 +08:00
|
|
|
static SourceLocation getLocAfterNamespaceLBrace(const NamespaceDecl *NsDecl,
|
|
|
|
const SourceManager &SM,
|
|
|
|
const LangOptions &LangOpts) {
|
|
|
|
std::unique_ptr<Lexer> Lex =
|
2018-08-10 06:42:26 +08:00
|
|
|
getLexerStartingFromLoc(NsDecl->getBeginLoc(), SM, LangOpts);
|
2016-10-12 20:34:18 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-09-20 01:40:32 +08:00
|
|
|
// Stores information about a moved namespace in `MoveNamespaces` and leaves
|
|
|
|
// the actual movement to `onEndOfTranslationUnit()`.
|
|
|
|
void ChangeNamespaceTool::moveOldNamespace(
|
|
|
|
const ast_matchers::MatchFinder::MatchResult &Result,
|
|
|
|
const NamespaceDecl *NsDecl) {
|
|
|
|
// If the namespace is empty, do nothing.
|
|
|
|
if (Decl::castToDeclContext(NsDecl)->decls_empty())
|
|
|
|
return;
|
|
|
|
|
2017-01-04 22:49:08 +08:00
|
|
|
const SourceManager &SM = *Result.SourceManager;
|
2016-09-20 01:40:32 +08:00
|
|
|
// Get the range of the code in the old namespace.
|
2017-01-04 22:49:08 +08:00
|
|
|
SourceLocation Start =
|
|
|
|
getLocAfterNamespaceLBrace(NsDecl, SM, Result.Context->getLangOpts());
|
2016-10-12 20:34:18 +08:00
|
|
|
assert(Start.isValid() && "Can't find l_brace for namespace.");
|
2016-09-20 01:40:32 +08:00
|
|
|
MoveNamespace MoveNs;
|
2017-01-04 22:49:08 +08:00
|
|
|
MoveNs.Offset = SM.getFileOffset(Start);
|
|
|
|
// The range of the moved namespace is from the location just past the left
|
|
|
|
// brace to the location right before the right brace.
|
|
|
|
MoveNs.Length = SM.getFileOffset(NsDecl->getRBraceLoc()) - MoveNs.Offset;
|
2016-09-20 01:40:32 +08:00
|
|
|
|
|
|
|
// Insert the new namespace after `DiffOldNamespace`. For example, if
|
|
|
|
// `OldNamespace` is "a::b::c" and `NewNamespace` is `a::x::y`, then
|
|
|
|
// "x::y" will be inserted inside the existing namespace "a" and after "a::b".
|
|
|
|
// `OuterNs` is the first namespace in `DiffOldNamespace`, e.g. "namespace b"
|
|
|
|
// in the above example.
|
2016-11-11 02:29:01 +08:00
|
|
|
// If there is no outer namespace (i.e. DiffOldNamespace is empty), the new
|
|
|
|
// namespace will be a nested namespace in the old namespace.
|
2016-09-20 01:40:32 +08:00
|
|
|
const NamespaceDecl *OuterNs = getOuterNamespace(NsDecl, DiffOldNamespace);
|
2016-11-11 02:29:01 +08:00
|
|
|
SourceLocation InsertionLoc = Start;
|
|
|
|
if (OuterNs) {
|
2017-01-04 22:49:08 +08:00
|
|
|
SourceLocation LocAfterNs = getStartOfNextLine(
|
|
|
|
OuterNs->getRBraceLoc(), SM, Result.Context->getLangOpts());
|
2016-11-11 02:29:01 +08:00
|
|
|
assert(LocAfterNs.isValid() &&
|
|
|
|
"Failed to get location after DiffOldNamespace");
|
|
|
|
InsertionLoc = LocAfterNs;
|
|
|
|
}
|
2017-01-04 22:49:08 +08:00
|
|
|
MoveNs.InsertionOffset = SM.getFileOffset(SM.getSpellingLoc(InsertionLoc));
|
|
|
|
MoveNs.FID = SM.getFileID(Start);
|
2016-09-20 01:58:59 +08:00
|
|
|
MoveNs.SourceMgr = Result.SourceManager;
|
2017-01-04 22:49:08 +08:00
|
|
|
MoveNamespaces[SM.getFilename(Start)].push_back(MoveNs);
|
2016-09-20 01:40:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removes a class forward declaration from the code in the moved namespace and
|
|
|
|
// creates an `InsertForwardDeclaration` to insert the forward declaration back
|
|
|
|
// into the old namespace after moving code from the old namespace to the new
|
|
|
|
// namespace.
|
|
|
|
// For example, changing "a" to "x":
|
|
|
|
// Old code:
|
|
|
|
// namespace a {
|
|
|
|
// class FWD;
|
|
|
|
// class A { FWD *fwd; }
|
|
|
|
// } // a
|
|
|
|
// New code:
|
|
|
|
// namespace a {
|
|
|
|
// class FWD;
|
|
|
|
// } // a
|
|
|
|
// namespace x {
|
|
|
|
// class A { a::FWD *fwd; }
|
|
|
|
// } // x
|
|
|
|
void ChangeNamespaceTool::moveClassForwardDeclaration(
|
|
|
|
const ast_matchers::MatchFinder::MatchResult &Result,
|
2016-12-07 22:20:52 +08:00
|
|
|
const NamedDecl *FwdDecl) {
|
2018-08-10 06:42:26 +08:00
|
|
|
SourceLocation Start = FwdDecl->getBeginLoc();
|
2018-08-10 06:43:02 +08:00
|
|
|
SourceLocation End = FwdDecl->getEndLoc();
|
2017-03-01 18:29:39 +08:00
|
|
|
const SourceManager &SM = *Result.SourceManager;
|
2016-09-20 01:40:32 +08:00
|
|
|
SourceLocation AfterSemi = Lexer::findLocationAfterToken(
|
2017-03-01 18:29:39 +08:00
|
|
|
End, tok::semi, SM, Result.Context->getLangOpts(),
|
2016-09-20 01:40:32 +08:00
|
|
|
/*SkipTrailingWhitespaceAndNewLine=*/true);
|
|
|
|
if (AfterSemi.isValid())
|
|
|
|
End = AfterSemi.getLocWithOffset(-1);
|
|
|
|
// Delete the forward declaration from the code to be moved.
|
2017-03-01 18:29:39 +08:00
|
|
|
addReplacementOrDie(Start, End, "", SM, &FileToReplacements);
|
2016-09-20 01:40:32 +08:00
|
|
|
llvm::StringRef Code = Lexer::getSourceText(
|
2017-03-01 18:29:39 +08:00
|
|
|
CharSourceRange::getTokenRange(SM.getSpellingLoc(Start),
|
|
|
|
SM.getSpellingLoc(End)),
|
|
|
|
SM, Result.Context->getLangOpts());
|
2016-09-20 01:40:32 +08:00
|
|
|
// Insert the forward declaration back into the old namespace after moving the
|
|
|
|
// code from old namespace to new namespace.
|
|
|
|
// Insertion information is stored in `InsertFwdDecls` and actual
|
|
|
|
// insertion will be performed in `onEndOfTranslationUnit`.
|
|
|
|
// Get the (old) namespace that contains the forward declaration.
|
|
|
|
const auto *NsDecl = Result.Nodes.getNodeAs<NamespaceDecl>("ns_decl");
|
|
|
|
// The namespace contains the forward declaration, so it must not be empty.
|
|
|
|
assert(!NsDecl->decls_empty());
|
2017-03-01 18:29:39 +08:00
|
|
|
const auto Insertion = createInsertion(
|
|
|
|
getLocAfterNamespaceLBrace(NsDecl, SM, Result.Context->getLangOpts()),
|
|
|
|
Code, SM);
|
2016-09-20 01:40:32 +08:00
|
|
|
InsertForwardDeclaration InsertFwd;
|
|
|
|
InsertFwd.InsertionOffset = Insertion.getOffset();
|
|
|
|
InsertFwd.ForwardDeclText = Insertion.getReplacementText().str();
|
|
|
|
InsertFwdDecls[Insertion.getFilePath()].push_back(InsertFwd);
|
|
|
|
}
|
|
|
|
|
2016-11-09 06:44:17 +08:00
|
|
|
// Replaces a qualified symbol (in \p DeclCtx) that refers to a declaration \p
|
|
|
|
// FromDecl with the shortest qualified name possible when the reference is in
|
|
|
|
// `NewNamespace`.
|
2016-09-20 01:40:32 +08:00
|
|
|
void ChangeNamespaceTool::replaceQualifiedSymbolInDeclContext(
|
2016-11-09 06:44:17 +08:00
|
|
|
const ast_matchers::MatchFinder::MatchResult &Result,
|
|
|
|
const DeclContext *DeclCtx, SourceLocation Start, SourceLocation End,
|
|
|
|
const NamedDecl *FromDecl) {
|
|
|
|
const auto *NsDeclContext = DeclCtx->getEnclosingNamespaceContext();
|
2016-12-15 01:01:52 +08:00
|
|
|
if (llvm::isa<TranslationUnitDecl>(NsDeclContext)) {
|
|
|
|
// This should not happen in usual unless the TypeLoc is in function type
|
|
|
|
// parameters, e.g `std::function<void(T)>`. In this case, DeclContext of
|
|
|
|
// `T` will be the translation unit. We simply use fully-qualified name
|
|
|
|
// here.
|
|
|
|
// Note that `FromDecl` must not be defined in the old namespace (according
|
|
|
|
// to `DeclMatcher`), so its fully-qualified name will not change after
|
|
|
|
// changing the namespace.
|
|
|
|
addReplacementOrDie(Start, End, FromDecl->getQualifiedNameAsString(),
|
|
|
|
*Result.SourceManager, &FileToReplacements);
|
|
|
|
return;
|
|
|
|
}
|
2016-11-11 02:15:34 +08:00
|
|
|
const auto *NsDecl = llvm::cast<NamespaceDecl>(NsDeclContext);
|
2016-09-20 01:40:32 +08:00
|
|
|
// Calculate the name of the `NsDecl` after it is moved to new namespace.
|
|
|
|
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());
|
2016-11-09 06:44:17 +08:00
|
|
|
std::string FromDeclName = FromDecl->getQualifiedNameAsString();
|
2017-02-24 19:54:45 +08:00
|
|
|
for (llvm::Regex &RE : WhiteListedSymbolRegexes)
|
|
|
|
if (RE.match(FromDeclName))
|
|
|
|
return;
|
2016-09-20 01:40:32 +08:00
|
|
|
std::string ReplaceName =
|
2016-11-09 06:44:17 +08:00
|
|
|
getShortestQualifiedNameInNamespace(FromDeclName, NewNs);
|
|
|
|
// Checks if there is any using namespace declarations that can shorten the
|
|
|
|
// qualified name.
|
|
|
|
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 = FromDeclNameRef;
|
|
|
|
}
|
|
|
|
}
|
2016-12-23 18:47:09 +08:00
|
|
|
// Checks if there is any namespace alias declarations that can shorten the
|
|
|
|
// qualified name.
|
|
|
|
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();
|
|
|
|
// We only consider namespace aliases define in the global namepspace or
|
|
|
|
// in namespaces that are directly visible from the reference, i.e.
|
|
|
|
// ancestor of the `OldNs`. Note that declarations in ancestor namespaces
|
|
|
|
// but not visible in the new namespace is filtered out by
|
|
|
|
// "IsVisibleInNewNs" matcher.
|
|
|
|
if (AliasQualifiedName != AliasName) {
|
|
|
|
// The alias is defined in some namespace.
|
|
|
|
assert(StringRef(AliasQualifiedName).endswith("::" + AliasName));
|
|
|
|
llvm::StringRef AliasNs =
|
|
|
|
StringRef(AliasQualifiedName).drop_back(AliasName.size() + 2);
|
|
|
|
if (!llvm::StringRef(OldNs).startswith(AliasNs))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::string NameWithAliasNamespace =
|
|
|
|
(AliasName + "::" + FromDeclNameRef).str();
|
|
|
|
if (NameWithAliasNamespace.size() < ReplaceName.size())
|
|
|
|
ReplaceName = NameWithAliasNamespace;
|
|
|
|
}
|
|
|
|
}
|
2016-11-09 06:44:17 +08:00
|
|
|
// Checks if there is any using shadow declarations that can shorten the
|
|
|
|
// qualified name.
|
|
|
|
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();
|
2017-02-02 23:29:54 +08:00
|
|
|
if (TargetDecl->getQualifiedNameAsString() ==
|
|
|
|
FromDecl->getQualifiedNameAsString()) {
|
2016-11-09 06:44:17 +08:00
|
|
|
ReplaceName = FromDecl->getNameAsString();
|
|
|
|
Matched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-22 20:48:49 +08:00
|
|
|
bool Conflict = conflictInNamespace(DeclCtx->getParentASTContext(),
|
|
|
|
ReplaceName, NewNamespace);
|
2016-09-20 01:40:32 +08:00
|
|
|
// If the new nested name in the new namespace is the same as it was in the
|
2018-10-22 20:48:49 +08:00
|
|
|
// old namespace, we don't create replacement unless there can be ambiguity.
|
|
|
|
if ((NestedName == ReplaceName && !Conflict) ||
|
2017-01-27 00:31:32 +08:00
|
|
|
(NestedName.startswith("::") && NestedName.drop_front(2) == ReplaceName))
|
2016-09-20 01:40:32 +08:00
|
|
|
return;
|
2016-12-08 04:08:02 +08:00
|
|
|
// If the reference need to be fully-qualified, add a leading "::" unless
|
|
|
|
// NewNamespace is the global namespace.
|
2018-10-22 20:48:49 +08:00
|
|
|
if (ReplaceName == FromDeclName && !NewNamespace.empty() && Conflict)
|
2016-12-08 04:08:02 +08:00
|
|
|
ReplaceName = "::" + ReplaceName;
|
2016-12-15 01:01:52 +08:00
|
|
|
addReplacementOrDie(Start, End, ReplaceName, *Result.SourceManager,
|
|
|
|
&FileToReplacements);
|
2016-09-20 01:40:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the [Start, End] of `Type` with the shortest qualified name when the
|
|
|
|
// `Type` is in `NewNamespace`.
|
|
|
|
void ChangeNamespaceTool::fixTypeLoc(
|
|
|
|
const ast_matchers::MatchFinder::MatchResult &Result, SourceLocation Start,
|
|
|
|
SourceLocation End, TypeLoc Type) {
|
|
|
|
// FIXME: do not rename template parameter.
|
|
|
|
if (Start.isInvalid() || End.isInvalid())
|
|
|
|
return;
|
2016-11-17 00:54:53 +08:00
|
|
|
// Types of CXXCtorInitializers do not need to be fixed.
|
|
|
|
if (llvm::is_contained(BaseCtorInitializerTypeLocs, Type))
|
|
|
|
return;
|
2017-03-17 22:05:39 +08:00
|
|
|
if (isTemplateParameter(Type))
|
|
|
|
return;
|
2016-09-20 01:40:32 +08:00
|
|
|
// The declaration which this TypeLoc refers to.
|
|
|
|
const auto *FromDecl = Result.Nodes.getNodeAs<NamedDecl>("from_decl");
|
|
|
|
// `hasDeclaration` gives underlying declaration, but if the type is
|
|
|
|
// a typedef type, we need to use the typedef type instead.
|
2016-12-15 18:42:35 +08:00
|
|
|
auto IsInMovedNs = [&](const NamedDecl *D) {
|
|
|
|
if (!llvm::StringRef(D->getQualifiedNameAsString())
|
|
|
|
.startswith(OldNamespace + "::"))
|
|
|
|
return false;
|
2018-08-10 06:42:26 +08:00
|
|
|
auto ExpansionLoc = Result.SourceManager->getExpansionLoc(D->getBeginLoc());
|
2016-12-15 18:42:35 +08:00
|
|
|
if (ExpansionLoc.isInvalid())
|
|
|
|
return false;
|
|
|
|
llvm::StringRef Filename = Result.SourceManager->getFilename(ExpansionLoc);
|
|
|
|
return FilePatternRE.match(Filename);
|
|
|
|
};
|
|
|
|
// Make `FromDecl` the immediate declaration that `Type` refers to, i.e. if
|
|
|
|
// `Type` is an alias type, we make `FromDecl` the type alias declaration.
|
|
|
|
// Also, don't fix the \p Type if it refers to a type alias decl in the moved
|
|
|
|
// namespace since the alias decl will be moved along with the type reference.
|
2016-11-15 03:37:55 +08:00
|
|
|
if (auto *Typedef = Type.getType()->getAs<TypedefType>()) {
|
2016-09-20 01:40:32 +08:00
|
|
|
FromDecl = Typedef->getDecl();
|
2016-11-15 03:37:55 +08:00
|
|
|
if (IsInMovedNs(FromDecl))
|
|
|
|
return;
|
2016-12-15 18:42:35 +08:00
|
|
|
} else if (auto *TemplateType =
|
|
|
|
Type.getType()->getAs<TemplateSpecializationType>()) {
|
|
|
|
if (TemplateType->isTypeAlias()) {
|
|
|
|
FromDecl = TemplateType->getTemplateName().getAsTemplateDecl();
|
|
|
|
if (IsInMovedNs(FromDecl))
|
|
|
|
return;
|
|
|
|
}
|
2016-11-15 03:37:55 +08:00
|
|
|
}
|
2016-12-14 23:29:23 +08:00
|
|
|
const auto *DeclCtx = Result.Nodes.getNodeAs<Decl>("dc");
|
2016-09-20 01:40:32 +08:00
|
|
|
assert(DeclCtx && "Empty decl context.");
|
2016-11-09 06:44:17 +08:00
|
|
|
replaceQualifiedSymbolInDeclContext(Result, DeclCtx->getDeclContext(), Start,
|
|
|
|
End, FromDecl);
|
2016-09-20 01:40:32 +08:00
|
|
|
}
|
|
|
|
|
2016-09-21 23:06:12 +08:00
|
|
|
void ChangeNamespaceTool::fixUsingShadowDecl(
|
|
|
|
const ast_matchers::MatchFinder::MatchResult &Result,
|
|
|
|
const UsingDecl *UsingDeclaration) {
|
2018-08-10 06:42:26 +08:00
|
|
|
SourceLocation Start = UsingDeclaration->getBeginLoc();
|
2018-08-10 06:43:02 +08:00
|
|
|
SourceLocation End = UsingDeclaration->getEndLoc();
|
2016-11-08 15:50:19 +08:00
|
|
|
if (Start.isInvalid() || End.isInvalid())
|
|
|
|
return;
|
2016-09-21 23:06:12 +08:00
|
|
|
|
|
|
|
assert(UsingDeclaration->shadow_size() > 0);
|
|
|
|
// FIXME: it might not be always accurate to use the first using-decl.
|
|
|
|
const NamedDecl *TargetDecl =
|
|
|
|
UsingDeclaration->shadow_begin()->getTargetDecl();
|
|
|
|
std::string TargetDeclName = TargetDecl->getQualifiedNameAsString();
|
|
|
|
// FIXME: check if target_decl_name is in moved ns, which doesn't make much
|
|
|
|
// sense. If this happens, we need to use name with the new namespace.
|
|
|
|
// Use fully qualified name in UsingDecl for now.
|
2016-12-15 01:01:52 +08:00
|
|
|
addReplacementOrDie(Start, End, "using ::" + TargetDeclName,
|
|
|
|
*Result.SourceManager, &FileToReplacements);
|
2016-09-21 23:06:12 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 22:15:14 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-09-20 01:40:32 +08:00
|
|
|
void ChangeNamespaceTool::onEndOfTranslationUnit() {
|
|
|
|
// Move namespace blocks and insert forward declaration to old namespace.
|
|
|
|
for (const auto &FileAndNsMoves : MoveNamespaces) {
|
|
|
|
auto &NsMoves = FileAndNsMoves.second;
|
|
|
|
if (NsMoves.empty())
|
|
|
|
continue;
|
|
|
|
const std::string &FilePath = FileAndNsMoves.first;
|
|
|
|
auto &Replaces = FileToReplacements[FilePath];
|
2016-09-20 01:58:59 +08:00
|
|
|
auto &SM = *NsMoves.begin()->SourceMgr;
|
|
|
|
llvm::StringRef Code = SM.getBufferData(NsMoves.begin()->FID);
|
2016-09-20 01:40:32 +08:00
|
|
|
auto ChangedCode = tooling::applyAllReplacements(Code, Replaces);
|
|
|
|
if (!ChangedCode) {
|
|
|
|
llvm::errs() << llvm::toString(ChangedCode.takeError()) << "\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Replacements on the changed code for moving namespaces and inserting
|
|
|
|
// forward declarations to old namespaces.
|
|
|
|
tooling::Replacements NewReplacements;
|
|
|
|
// Cut the changed code from the old namespace and paste the code in the new
|
|
|
|
// namespace.
|
|
|
|
for (const auto &NsMove : NsMoves) {
|
|
|
|
// Calculate the range of the old namespace block in the changed
|
|
|
|
// code.
|
|
|
|
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);
|
|
|
|
// Calculate the new offset at which the code will be inserted in the
|
|
|
|
// changed code.
|
|
|
|
unsigned NewInsertionOffset =
|
|
|
|
Replaces.getShiftedCodePosition(NsMove.InsertionOffset);
|
|
|
|
tooling::Replacement Insertion(FilePath, NewInsertionOffset, 0,
|
|
|
|
MovedCodeWrappedInNewNs);
|
|
|
|
addOrMergeReplacement(Deletion, &NewReplacements);
|
|
|
|
addOrMergeReplacement(Insertion, &NewReplacements);
|
|
|
|
}
|
|
|
|
// After moving namespaces, insert forward declarations back to old
|
|
|
|
// namespaces.
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
// Add replacements referring to the changed code to existing replacements,
|
|
|
|
// which refers to the original code.
|
|
|
|
Replaces = Replaces.merge(NewReplacements);
|
2018-08-02 18:30:56 +08:00
|
|
|
auto Style =
|
|
|
|
format::getStyle(format::DefaultFormatStyle, FilePath, FallbackStyle);
|
2017-01-17 08:13:32 +08:00
|
|
|
if (!Style) {
|
|
|
|
llvm::errs() << llvm::toString(Style.takeError()) << "\n";
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-20 01:40:32 +08:00
|
|
|
// Clean up old namespaces if there is nothing in it after moving.
|
|
|
|
auto CleanReplacements =
|
2017-01-17 08:13:32 +08:00
|
|
|
format::cleanupAroundReplacements(Code, Replaces, *Style);
|
2016-09-20 01:40:32 +08:00
|
|
|
if (!CleanReplacements) {
|
|
|
|
llvm::errs() << llvm::toString(CleanReplacements.takeError()) << "\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
FileToReplacements[FilePath] = *CleanReplacements;
|
|
|
|
}
|
2016-12-02 01:25:55 +08:00
|
|
|
|
|
|
|
// Make sure we don't generate replacements for files that do not match
|
|
|
|
// FilePattern.
|
|
|
|
for (auto &Entry : FileToReplacements)
|
|
|
|
if (!FilePatternRE.match(Entry.first))
|
|
|
|
Entry.second.clear();
|
2016-09-20 01:40:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace change_namespace
|
|
|
|
} // namespace clang
|