[clang-tools-extra] r190050 - Fix writing of reformatted files.

Manuel Klimek klimek at google.com
Thu Sep 5 06:55:39 PDT 2013


Author: klimek
Date: Thu Sep  5 08:55:39 2013
New Revision: 190050

URL: http://llvm.org/viewvc/llvm-project?rev=190050&view=rev
Log:
Fix writing of reformatted files.

If transformations lead to changes that do not need reformatting, the
current strategy will not write those files. With this patch, we just
update the overall state with the changes due to reformatting, and then
write out everything.

Added:
    clang-tools-extra/trunk/test/clang-modernize/Core/NoReformattingNeeded.cpp
Modified:
    clang-tools-extra/trunk/clang-modernize/tool/ClangModernize.cpp

Modified: clang-tools-extra/trunk/clang-modernize/tool/ClangModernize.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-modernize/tool/ClangModernize.cpp?rev=190050&r1=190049&r2=190050&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-modernize/tool/ClangModernize.cpp (original)
+++ clang-tools-extra/trunk/clang-modernize/tool/ClangModernize.cpp Thu Sep  5 08:55:39 2013
@@ -238,7 +238,7 @@ static Reformatter *handleFormatStyle(co
 ///              disk.
 ///          \li false if reformatting could not be successfully applied or
 ///              if at least one file failed to write to disk.
-bool reformat(Reformatter &ChangesReformatter, const FileOverrides &Overrides,
+void reformat(Reformatter &ChangesReformatter, FileOverrides &Overrides,
               DiagnosticsEngine &Diagnostics) {
   FileManager Files((FileSystemOptions()));
   SourceManager SM(Diagnostics, Files);
@@ -250,16 +250,16 @@ bool reformat(Reformatter &ChangesReform
   replace::FileToReplacementsMap GroupedReplacements;
   if (!replace::mergeAndDeduplicate(AllReplacements, GroupedReplacements, SM)) {
     llvm::errs() << "Warning: Reformatting produced conflicts.\n";
-    return false;
+    return;
   }
 
   Rewriter DestRewriter(SM, LangOptions());
   if (!replace::applyReplacements(GroupedReplacements, DestRewriter)) {
     llvm::errs() << "Warning: Failed to apply reformatting conflicts!\n";
-    return false;
+    return;
   }
 
-  return replace::writeFiles(DestRewriter);
+  Overrides.updateState(DestRewriter);
 }
 
 bool serializeReplacements(const replace::TUReplacements &Replacements) {
@@ -452,13 +452,12 @@ int main(int argc, const char **argv) {
   }
 
   // Skip writing final file states to disk if we were asked to serialize
-  // replacements. Otherwise reformat changes if reformatting is enabled. If
-  // not enabled or if reformatting fails write un-formated changes to disk
-  // instead. reformat() takes care of writing successfully formatted changes.
-  if (!SerializeReplacements &&
-      (!ChangesReformatter ||
-       !reformat(*ChangesReformatter, FileStates, Diagnostics)))
+  // replacements. Otherwise reformat changes if reformatting is enabled.
+  if (!SerializeReplacements) {
+    if (ChangesReformatter)
+       reformat(*ChangesReformatter, FileStates, Diagnostics);
     FileStates.writeToDisk(Diagnostics);
+  }
 
   if (FinalSyntaxCheck)
     if (!doSyntaxCheck(*Compilations, SourcePaths, FileStates))

Added: clang-tools-extra/trunk/test/clang-modernize/Core/NoReformattingNeeded.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-modernize/Core/NoReformattingNeeded.cpp?rev=190050&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-modernize/Core/NoReformattingNeeded.cpp (added)
+++ clang-tools-extra/trunk/test/clang-modernize/Core/NoReformattingNeeded.cpp Thu Sep  5 08:55:39 2013
@@ -0,0 +1,10 @@
+// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
+// RUN: clang-modernize -format-style=LLVM -use-auto %t.cpp -- -std=c++11
+// RUN: FileCheck --strict-whitespace -input-file=%t.cpp %s
+
+class C {};
+
+void f() { //
+  C *a = new C();
+  // CHECK: {{^\ \ auto\ a\ \=\ new\ C\(\);}}
+}





More information about the cfe-commits mailing list