[clang-tools-extra] r283424 - [clang-move] Cleanup around replacements.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 6 01:29:33 PDT 2016


Author: hokein
Date: Thu Oct  6 03:29:32 2016
New Revision: 283424

URL: http://llvm.org/viewvc/llvm-project?rev=283424&view=rev
Log:
[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

Modified:
    clang-tools-extra/trunk/clang-move/ClangMove.cpp
    clang-tools-extra/trunk/clang-move/ClangMove.h
    clang-tools-extra/trunk/clang-move/tool/ClangMoveMain.cpp
    clang-tools-extra/trunk/test/clang-move/move-class.cpp
    clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp

Modified: clang-tools-extra/trunk/clang-move/ClangMove.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-move/ClangMove.cpp?rev=283424&r1=283423&r2=283424&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-move/ClangMove.cpp (original)
+++ clang-tools-extra/trunk/clang-move/ClangMove.cpp Thu Oct  6 03:29:32 2016
@@ -240,11 +240,12 @@ ClangMoveAction::CreateASTConsumer(clang
 }
 
 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::St
 
 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;
   }
 }
 

Modified: clang-tools-extra/trunk/clang-move/ClangMove.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-move/ClangMove.h?rev=283424&r1=283423&r2=283424&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-move/ClangMove.h (original)
+++ clang-tools-extra/trunk/clang-move/ClangMove.h Thu Oct  6 03:29:32 2016
@@ -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

Modified: clang-tools-extra/trunk/clang-move/tool/ClangMoveMain.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-move/tool/ClangMoveMain.cpp?rev=283424&r1=283423&r2=283424&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-move/tool/ClangMoveMain.cpp (original)
+++ clang-tools-extra/trunk/clang-move/tool/ClangMoveMain.cpp Thu Oct  6 03:29:32 2016
@@ -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)

Modified: clang-tools-extra/trunk/test/clang-move/move-class.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-move/move-class.cpp?rev=283424&r1=283423&r2=283424&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-move/move-class.cpp (original)
+++ clang-tools-extra/trunk/test/clang-move/move-class.cpp Thu Oct  6 03:29:32 2016
@@ -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

Modified: clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp?rev=283424&r1=283423&r2=283424&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp Thu Oct  6 03:29:32 2016
@@ -161,7 +161,7 @@ runClangMoveOnCode(const move::ClangMove
   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",




More information about the cfe-commits mailing list