[PATCH] clang-format: Fix whitespace surrounding diff removals

Alp Toker alp at nuanti.com
Mon Oct 21 11:27:08 PDT 2013


Hi Daniel,

This patch is the continuation of r191820. The purpose is to properly
reformat whitespace surrounding line removals in clang-format-diff.py
without reformatting unrelated code as was happening before.

For example, the input:

diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp

index a71d3c0..a75d66f 100644

--- a/lib/Sema/SemaDeclAttr.cpp

+++ b/lib/Sema/SemaDeclAttr.cpp

@@ -1681,10 +1681,10 @@ static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {

     return;

   }

 

-  // FIXME: check if target symbol exists in current file

 

   D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,

                                          Attr.getAttributeSpellingListIndex()));

+  S.Aliases.push_back(D);

 }

 

 static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {


Now yields:

--- lib/Sema/SemaDeclAttr.cpp    (before formatting)

+++ lib/Sema/SemaDeclAttr.cpp    (after formatting)

@@ -1680,7 +1680,6 @@

     S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);

     return;

   }

-

 

   D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,

                                          Attr.getAttributeSpellingListIndex()));


Even though we're not yet testing the scripts, I'm aware this needs a
test added to test/Format/line-ranges.cpp. Unfortunately I'm struggling
to write one and found the existing tests hard to read. Could you give
me a hand with that?

I wonder if we could move towards multiple input files for a single test
-- just trying to wrap my head around line-ranges.cpp, FileCheck doesn't
feel like the right tool for this. I wonder if GNU diffutils has a
better format for tests that we could adopt to start testing
clang-format-diff.py.

Alp.

-- 
http://www.nuanti.com
the browser experts

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131021/8529365b/attachment.html>
-------------- next part --------------
diff --git a/tools/clang-format/ClangFormat.cpp b/tools/clang-format/ClangFormat.cpp
index 2ca2bed..2af59ca 100644
--- a/tools/clang-format/ClangFormat.cpp
+++ b/tools/clang-format/ClangFormat.cpp
@@ -127,14 +127,20 @@ static bool fillRanges(SourceManager &Sources, FileID ID,
         llvm::errs() << "error: invalid <start line>:<end line> pair\n";
         return true;
       }
-      if (FromLine > ToLine) {
-        llvm::errs() << "error: start line should be less than end line\n";
-        return true;
-      }
+
       SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1);
-      SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX);
-      if (Start.isInvalid() || End.isInvalid())
+      SourceLocation End = ToLine > 0
+                               ? Sources.translateLineCol(ID, ToLine, UINT_MAX)
+                               : Sources.getLocForStartOfFile(ID);
+
+      if (Start.isInvalid() || End.isInvalid()) {
+        llvm::errs() << "Invalid range\n";
         return true;
+      }
+
+      if (FromLine > ToLine)
+        std::swap(Start, End);
+
       Ranges.push_back(CharSourceRange::getCharRange(Start, End));
     }
     return false;
diff --git a/tools/clang-format/clang-format-diff.py b/tools/clang-format/clang-format-diff.py
index 60b8fb7..30e6fee 100755
--- a/tools/clang-format/clang-format-diff.py
+++ b/tools/clang-format/clang-format-diff.py
@@ -71,7 +71,7 @@ def main():
       if match.group(3):
         line_count = int(match.group(3))
       if line_count == 0:
-        continue
+        start_line += 1
       end_line = start_line + line_count - 1;
       lines_by_file.setdefault(filename, []).extend(
           ['-lines', str(start_line) + ':' + str(end_line)])
diff --git a/tools/clang-format/git-clang-format b/tools/clang-format/git-clang-format
index b0737ed..1f1940d 100755
--- a/tools/clang-format/git-clang-format
+++ b/tools/clang-format/git-clang-format
@@ -283,8 +283,9 @@ def extract_lines(patch_file):
       line_count = 1
       if match.group(3):
         line_count = int(match.group(3))
-      if line_count > 0:
-        matches.setdefault(filename, []).append(Range(start_line, line_count))
+      if line_count == 0:
+        start_line += 1
+      matches.setdefault(filename, []).append(Range(start_line, line_count))
   return matches
 
 


More information about the cfe-commits mailing list