[clang-move] Cleanup around replacements.

Summary:
cleanup the remaining empty namespace after moving out the
class defintitions.

Reviewers: ioeric

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D25282

llvm-svn: 283424
This commit is contained in:
Haojian Wu 2016-10-06 08:29:32 +00:00
parent 6215fad0e9
commit 253d596d4a
5 changed files with 39 additions and 23 deletions

View File

@ -240,11 +240,12 @@ ClangMoveAction::CreateASTConsumer(clang::CompilerInstance &Compiler,
}
ClangMoveTool::ClangMoveTool(
const MoveDefinitionSpec &MoveSpec,
std::map<std::string, tooling::Replacements> &FileToReplacements,
llvm::StringRef OriginalRunningDirectory)
: Spec(MoveSpec), FileToReplacements(FileToReplacements),
OriginalRunningDirectory(OriginalRunningDirectory) {
const MoveDefinitionSpec &MoveSpec,
std::map<std::string, tooling::Replacements> &FileToReplacements,
llvm::StringRef OriginalRunningDirectory, llvm::StringRef FallbackStyle)
: Spec(MoveSpec), FileToReplacements(FileToReplacements),
OriginalRunningDirectory(OriginalRunningDirectory),
FallbackStyle(FallbackStyle) {
Spec.Name = llvm::StringRef(Spec.Name).ltrim(':');
if (!Spec.NewHeader.empty())
CCIncludes.push_back("#include \"" + Spec.NewHeader + "\"\n");
@ -364,13 +365,27 @@ void ClangMoveTool::addIncludes(llvm::StringRef IncludeHeader,
void ClangMoveTool::removeClassDefinitionInOldFiles() {
for (const auto &MovedDecl : RemovedDecls) {
auto EndLoc = getLocForEndOfDecl(MovedDecl.Decl, MovedDecl.SM);
const auto &SM = *MovedDecl.SM;
auto EndLoc = getLocForEndOfDecl(MovedDecl.Decl, &SM);
clang::tooling::Replacement RemoveReplacement(
*MovedDecl.SM, clang::CharSourceRange::getTokenRange(
MovedDecl.Decl->getLocStart(), EndLoc),
SM, clang::CharSourceRange::getTokenRange(MovedDecl.Decl->getLocStart(),
EndLoc),
"");
std::string FilePath = RemoveReplacement.getFilePath().str();
addOrMergeReplacement(RemoveReplacement, &FileToReplacements[FilePath]);
llvm::StringRef Code =
SM.getBufferData(SM.getFileID(MovedDecl.Decl->getLocation()));
format::FormatStyle Style =
format::getStyle("file", FilePath, FallbackStyle);
auto CleanReplacements = format::cleanupAroundReplacements(
Code, FileToReplacements[FilePath], Style);
if (!CleanReplacements) {
llvm::errs() << llvm::toString(CleanReplacements.takeError()) << "\n";
continue;
}
FileToReplacements[FilePath] = *CleanReplacements;
}
}

View File

@ -50,7 +50,7 @@ public:
ClangMoveTool(
const MoveDefinitionSpec &MoveSpec,
std::map<std::string, tooling::Replacements> &FileToReplacements,
llvm::StringRef OriginalRunningDirectory);
llvm::StringRef OriginalRunningDirectory, llvm::StringRef Style);
void registerMatchers(ast_matchers::MatchFinder *Finder);
@ -95,6 +95,8 @@ private:
// directory when analyzing the source file. We save the original working
// directory in order to get the absolute file path for the fields in Spec.
std::string OriginalRunningDirectory;
// The name of a predefined code style.
std::string FallbackStyle;
};
class ClangMoveAction : public clang::ASTFrontendAction {
@ -102,8 +104,9 @@ public:
ClangMoveAction(
const ClangMoveTool::MoveDefinitionSpec &spec,
std::map<std::string, tooling::Replacements> &FileToReplacements,
llvm::StringRef OriginalRunningDirectory)
: MoveTool(spec, FileToReplacements, OriginalRunningDirectory) {
llvm::StringRef OriginalRunningDirectory, llvm::StringRef FallbackStyle)
: MoveTool(spec, FileToReplacements, OriginalRunningDirectory,
FallbackStyle) {
MoveTool.registerMatchers(&MatchFinder);
}
@ -123,18 +126,21 @@ public:
ClangMoveActionFactory(
const ClangMoveTool::MoveDefinitionSpec &Spec,
std::map<std::string, tooling::Replacements> &FileToReplacements,
llvm::StringRef OriginalRunningDirectory)
llvm::StringRef OriginalRunningDirectory, llvm::StringRef FallbackStyle)
: Spec(Spec), FileToReplacements(FileToReplacements),
OriginalRunningDirectory(OriginalRunningDirectory) {}
OriginalRunningDirectory(OriginalRunningDirectory),
FallbackStyle(FallbackStyle) {}
clang::FrontendAction *create() override {
return new ClangMoveAction(Spec, FileToReplacements, OriginalRunningDirectory);
return new ClangMoveAction(Spec, FileToReplacements,
OriginalRunningDirectory, FallbackStyle);
}
private:
const ClangMoveTool::MoveDefinitionSpec &Spec;
std::map<std::string, tooling::Replacements> &FileToReplacements;
std::string OriginalRunningDirectory;
std::string FallbackStyle;
};
} // namespace move

View File

@ -86,7 +86,7 @@ int main(int argc, const char **argv) {
Twine(EC.message()));
auto Factory = llvm::make_unique<clang::move::ClangMoveActionFactory>(
Spec, Tool.getReplacements(), InitialDirectory.str());
Spec, Tool.getReplacements(), InitialDirectory.str(), Style);
int CodeStatus = Tool.run(Factory.get());
if (CodeStatus)

View File

@ -7,7 +7,7 @@
// RUN: FileCheck -input-file=%T/clang-move/new_test.cpp -check-prefix=CHECK-NEW-TEST-CPP %s
// RUN: FileCheck -input-file=%T/clang-move/new_test.h -check-prefix=CHECK-NEW-TEST-H %s
// RUN: FileCheck -input-file=%T/clang-move/test.cpp -check-prefix=CHECK-OLD-TEST-CPP %s
// RUN: FileCheck -input-file=%T/clang-move/test.h -check-prefix=CHECK-OLD-TEST-H %s
// RUN: FileCheck -input-file=%T/clang-move/test.h %s -implicit-check-not='{{namespace.*}}'
//
// RUN: cp %S/Inputs/test* %T/clang-move/
// RUN: cd %T/clang-move
@ -15,7 +15,7 @@
// RUN: FileCheck -input-file=%T/clang-move/new_test.cpp -check-prefix=CHECK-NEW-TEST-CPP %s
// RUN: FileCheck -input-file=%T/clang-move/new_test.h -check-prefix=CHECK-NEW-TEST-H %s
// RUN: FileCheck -input-file=%T/clang-move/test.cpp -check-prefix=CHECK-OLD-TEST-CPP %s
// RUN: FileCheck -input-file=%T/clang-move/test.h -check-prefix=CHECK-OLD-TEST-H %s
// RUN: FileCheck -input-file=%T/clang-move/test.h %s -implicit-check-not='{{namespace.*}}'
//
// CHECK-NEW-TEST-H: namespace a {
// CHECK-NEW-TEST-H: class Foo {
@ -30,10 +30,5 @@
// CHECK-NEW-TEST-CPP: int Foo::f() { return 0; }
// CHECK-NEW-TEST-CPP: } // namespace a
//
// CHECK-OLD-TEST-H: namespace a {
// CHECK-OLD-TEST-H: } // namespace a
//
// CHECK-OLD-TEST-CPP: #include "test.h"
// CHECK-OLD-TEST-CPP: #include "test2.h"
// CHECK-OLD-TEST-CPP: namespace a {
// CHECK-OLD-TEST-CPP: } // namespace a

View File

@ -161,7 +161,7 @@ runClangMoveOnCode(const move::ClangMoveTool::MoveDefinitionSpec &Spec) {
assert(!EC);
(void)EC;
auto Factory = llvm::make_unique<clang::move::ClangMoveActionFactory>(
Spec, FileToReplacements, InitialDirectory.str());
Spec, FileToReplacements, InitialDirectory.str(), "LLVM");
tooling::runToolOnCodeWithArgs(
Factory->create(), TestCC, {"-std=c++11"}, TestCCName, "clang-move",