[clang-tools-extra] r174378 - Implements support for specifying multiple ranges.

Manuel Klimek klimek at google.com
Tue Feb 5 07:25:34 PST 2013


Author: klimek
Date: Tue Feb  5 09:25:34 2013
New Revision: 174378

URL: http://llvm.org/viewvc/llvm-project?rev=174378&view=rev
Log:
Implements support for specifying multiple ranges.

This is backwards compatible with earlier integrations.
Also adds a basic test and a test for the ranges integration.

You can now run:
clang-format -offset=42 -length=15 -offset=150 -length=22
To re-format the ranges (42, +15) and (150, +22).

Added:
    clang-tools-extra/trunk/test/clang-format/basic.cpp
    clang-tools-extra/trunk/test/clang-format/ranges.cpp
Modified:
    clang-tools-extra/trunk/clang-format/ClangFormat.cpp

Modified: clang-tools-extra/trunk/clang-format/ClangFormat.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-format/ClangFormat.cpp?rev=174378&r1=174377&r2=174378&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-format/ClangFormat.cpp (original)
+++ clang-tools-extra/trunk/clang-format/ClangFormat.cpp Tue Feb  5 09:25:34 2013
@@ -27,12 +27,10 @@ using namespace llvm;
 
 static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
 
-static cl::opt<int> Offset(
-    "offset", cl::desc("Format a range starting at this file offset."),
-    cl::init(0));
-static cl::opt<int> Length(
-    "length", cl::desc("Format a range of this length, -1 for end of file."),
-    cl::init(-1));
+static cl::list<int> Offsets(
+    "offset", cl::desc("Format a range starting at this file offset."));
+static cl::list<int> Lengths(
+    "length", cl::desc("Format a range of this length, -1 for end of file."));
 static cl::opt<std::string> Style(
     "style",
     cl::desc("Coding style, currently supports: LLVM, Google, Chromium."),
@@ -86,13 +84,25 @@ static void format() {
   }
   FileID ID = createInMemoryFile(FileName, Code.get(), Sources, Files);
   Lexer Lex(ID, Sources.getBuffer(ID), Sources, getFormattingLangOpts());
-  SourceLocation Start =
-      Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset);
-  SourceLocation End = Sources.getLocForEndOfFile(ID);
-  if (Length != -1)
-    End = Start.getLocWithOffset(Length);
-  std::vector<CharSourceRange> Ranges(
-      1, CharSourceRange::getCharRange(Start, End));
+  if (Offsets.empty())
+    Offsets.push_back(0);
+  if (Offsets.size() != Lengths.size() &&
+      !(Offsets.size() == 1 && Lengths.empty())) {
+    llvm::errs() << "Number of -offset and -length arguments must match.\n";
+    return;
+  }
+  std::vector<CharSourceRange> Ranges;
+  for (cl::list<int>::size_type i = 0, e = Offsets.size(); i != e; ++i) {
+    SourceLocation Start =
+        Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]);
+    SourceLocation End;
+    if (i < Lengths.size()) {
+      End = Start.getLocWithOffset(Lengths[i]);
+    } else {
+      End = Sources.getLocForEndOfFile(ID);
+    }
+    Ranges.push_back(CharSourceRange::getCharRange(Start, End));
+  }
   tooling::Replacements Replaces = reformat(getStyle(), Lex, Sources, Ranges);
   Rewriter Rewrite(Sources, LangOptions());
   tooling::applyAllReplacements(Replaces, Rewrite);

Added: clang-tools-extra/trunk/test/clang-format/basic.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-format/basic.cpp?rev=174378&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-format/basic.cpp (added)
+++ clang-tools-extra/trunk/test/clang-format/basic.cpp Tue Feb  5 09:25:34 2013
@@ -0,0 +1,6 @@
+// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
+// RUN: clang-format -i %t.cpp
+// RUN: FileCheck -input-file=%t.cpp %s
+
+// CHECK: {{^int \*i;}}
+ int   *  i  ;

Added: clang-tools-extra/trunk/test/clang-format/ranges.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-format/ranges.cpp?rev=174378&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-format/ranges.cpp (added)
+++ clang-tools-extra/trunk/test/clang-format/ranges.cpp Tue Feb  5 09:25:34 2013
@@ -0,0 +1,11 @@
+// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
+// RUN: clang-format -offset=2 -length=0 -offset=28 -length=0 -i %t.cpp
+// RUN: FileCheck -input-file=%t.cpp %s
+// CHECK: {{^int \*i;$}}
+  int*i;
+
+// CHECK: {{^  int  \*  i;$}}
+  int  *  i; 
+
+// CHECK: {{^int \*i;$}}
+  int   *   i;





More information about the cfe-commits mailing list