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). llvm-svn: 174378
This commit is contained in:
		
							parent
							
								
									22174f5d5a
								
							
						
					
					
						commit
						8c651f0974
					
				| 
						 | 
				
			
			@ -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());
 | 
			
		||||
  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(Offset);
 | 
			
		||||
  SourceLocation End = Sources.getLocForEndOfFile(ID);
 | 
			
		||||
  if (Length != -1)
 | 
			
		||||
    End = Start.getLocWithOffset(Length);
 | 
			
		||||
  std::vector<CharSourceRange> Ranges(
 | 
			
		||||
      1, CharSourceRange::getCharRange(Start, End));
 | 
			
		||||
        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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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  ;
 | 
			
		||||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
		Loading…
	
		Reference in New Issue