This revision was automatically updated to reflect the committed changes.
Closed by commit rL283424: [clang-move] Cleanup around replacements. (authored 
by hokein).

Changed prior to commit:
  https://reviews.llvm.org/D25282?vs=73663&id=73786#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D25282

Files:
  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

Index: clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp
===================================================================
--- clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp
+++ clang-tools-extra/trunk/unittests/clang-move/ClangMoveTests.cpp
@@ -161,7 +161,7 @@
   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",
Index: clang-tools-extra/trunk/clang-move/tool/ClangMoveMain.cpp
===================================================================
--- clang-tools-extra/trunk/clang-move/tool/ClangMoveMain.cpp
+++ clang-tools-extra/trunk/clang-move/tool/ClangMoveMain.cpp
@@ -86,7 +86,7 @@
                              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)
Index: clang-tools-extra/trunk/clang-move/ClangMove.cpp
===================================================================
--- clang-tools-extra/trunk/clang-move/ClangMove.cpp
+++ clang-tools-extra/trunk/clang-move/ClangMove.cpp
@@ -240,11 +240,12 @@
 }
 
 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::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;
   }
 }
 
Index: clang-tools-extra/trunk/clang-move/ClangMove.h
===================================================================
--- clang-tools-extra/trunk/clang-move/ClangMove.h
+++ clang-tools-extra/trunk/clang-move/ClangMove.h
@@ -50,7 +50,7 @@
   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,15 +95,18 @@
   // 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 {
 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 @@
   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
Index: clang-tools-extra/trunk/test/clang-move/move-class.cpp
===================================================================
--- clang-tools-extra/trunk/test/clang-move/move-class.cpp
+++ clang-tools-extra/trunk/test/clang-move/move-class.cpp
@@ -7,15 +7,15 @@
 // 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
 // RUN: clang-move -name="a::Foo" -new_cc=%T/clang-move/new_test.cpp -new_header=%T/clang-move/new_test.h -old_cc=%T/clang-move/test.cpp -old_header=%T/clang-move/test.h %T/clang-move/test.cpp
 // 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
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to