[PATCH] clang-format support for multiple input files.
Alexander Kornienko
alexfh at google.com
Wed Apr 24 04:37:45 PDT 2013
Hi djasper, klimek,
Added support for multiple input files, that can be used both with and
without in-place edit (-i) option. Added checks for -offset and -length options:
don't allow them on multiple files, check that they don't fall outside input
file, made both options unsigned, so that there's no need to check for negative
values. Removed "-1 can be used for end-of-file" from -length description.
http://llvm-reviews.chandlerc.com/D719
Files:
test/Format/multiple-inputs-error.cpp
test/Format/multiple-inputs-inplace.cpp
test/Format/multiple-inputs.cpp
tools/clang-format/ClangFormat.cpp
Index: test/Format/multiple-inputs-error.cpp
===================================================================
--- /dev/null
+++ test/Format/multiple-inputs-error.cpp
@@ -0,0 +1,6 @@
+// RUN: cp %s %t-1.cpp
+// RUN: cp %s %t-2.cpp
+// RUN: clang-format 2>&1 >/dev/null -offset=1 -length=0 %t-1.cpp %t-2.cpp |FileCheck %s
+// CHECK: error: "-offset" and "-length" can only be used for single file.
+
+int i ;
Index: test/Format/multiple-inputs-inplace.cpp
===================================================================
--- /dev/null
+++ test/Format/multiple-inputs-inplace.cpp
@@ -0,0 +1,8 @@
+// RUN: cp %s %t-1.cpp
+// RUN: cp %s %t-2.cpp
+// RUN: clang-format -style=LLVM -i %t-1.cpp %t-2.cpp
+// RUN: FileCheck -strict-whitespace -input-file=%t-1.cpp %s
+// RUN: FileCheck -strict-whitespace -input-file=%t-2.cpp %s
+
+// CHECK: {{^int\ \*i;}}
+ int * i ;
Index: test/Format/multiple-inputs.cpp
===================================================================
--- /dev/null
+++ test/Format/multiple-inputs.cpp
@@ -0,0 +1,7 @@
+// RUN: cp %s %t-1.cpp
+// RUN: cp %s %t-2.cpp
+// RUN: clang-format -style=LLVM %t-1.cpp %t-2.cpp|FileCheck -strict-whitespace %s
+
+// CHECK: {{^int\ \*i;}}
+// CHECK: {{^int\ \*i;}}
+ int * i ;
Index: tools/clang-format/ClangFormat.cpp
===================================================================
--- tools/clang-format/ClangFormat.cpp
+++ tools/clang-format/ClangFormat.cpp
@@ -27,22 +27,25 @@
static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
-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::list<unsigned>
+Offsets("offset", cl::desc("Format a range starting at this file offset. Can "
+ "only be used with one input file."));
+static cl::list<unsigned>
+Lengths("length", cl::desc("Format a range of this length. "
+ "When it's not specified, end of file is used. "
+ "Can only be used with one input file."));
static cl::opt<std::string> Style(
"style",
cl::desc("Coding style, currently supports: LLVM, Google, Chromium."),
cl::init("LLVM"));
static cl::opt<bool> Inplace("i",
- cl::desc("Inplace edit <file>, if specified."));
+ cl::desc("Inplace edit <file>s, if specified."));
static cl::opt<bool> OutputXML(
"output-replacements-xml", cl::desc("Output replacements as XML."));
-static cl::opt<std::string> FileName(cl::Positional, cl::desc("[<file>]"),
- cl::init("-"));
+static cl::list<std::string> FileNames(cl::Positional,
+ cl::desc("[<file> ...]"));
namespace clang {
namespace format {
@@ -65,41 +68,55 @@
return TheStyle;
}
-static void format() {
+// Returns true on error.
+static bool format(std::string FileName) {
FileManager Files((FileSystemOptions()));
DiagnosticsEngine Diagnostics(
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
new DiagnosticOptions);
SourceManager Sources(Diagnostics, Files);
OwningPtr<MemoryBuffer> Code;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(FileName, Code)) {
llvm::errs() << ec.message() << "\n";
- return;
+ return true;
}
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;
+ llvm::errs()
+ << "error: number of -offset and -length arguments must match.\n";
+ return true;
}
std::vector<CharSourceRange> Ranges;
- for (cl::list<int>::size_type i = 0, e = Offsets.size(); i != e; ++i) {
+ for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
+ if (Offsets[i] >= Code->getBufferSize()) {
+ llvm::errs() << "error: offset " << Offsets[i]
+ << " is outside the file\n";
+ return true;
+ }
SourceLocation Start =
Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]);
SourceLocation End;
if (i < Lengths.size()) {
+ if (Offsets[i] + Lengths[i] > Code->getBufferSize()) {
+ llvm::errs() << "error: invalid length " << Lengths[i]
+ << ", offset + length (" << Offsets[i] + Lengths[i]
+ << ") is outside the file.\n";
+ return true;
+ }
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);
if (OutputXML) {
- llvm::outs() << "<?xml version='1.0'?>\n<replacements xml:space='preserve'>\n";
+ llvm::outs()
+ << "<?xml version='1.0'?>\n<replacements xml:space='preserve'>\n";
for (tooling::Replacements::const_iterator I = Replaces.begin(),
E = Replaces.end();
I != E; ++I) {
@@ -114,21 +131,22 @@
tooling::applyAllReplacements(Replaces, Rewrite);
if (Inplace) {
if (Replaces.size() == 0)
- return; // Nothing changed, don't touch the file.
+ return false; // Nothing changed, don't touch the file.
std::string ErrorInfo;
llvm::raw_fd_ostream FileStream(FileName.c_str(), ErrorInfo,
llvm::raw_fd_ostream::F_Binary);
if (!ErrorInfo.empty()) {
llvm::errs() << "Error while writing file: " << ErrorInfo << "\n";
- return;
+ return true;
}
Rewrite.getEditBuffer(ID).write(FileStream);
FileStream.flush();
} else {
Rewrite.getEditBuffer(ID).write(outs());
}
}
+ return false;
}
} // namespace format
@@ -141,11 +159,30 @@
"A tool to format C/C++/Obj-C code.\n\n"
"If no arguments are specified, it formats the code from standard input\n"
"and writes the result to the standard output.\n"
- "If <file> is given, it reformats the file. If -i is specified together\n"
- "with <file>, the file is edited in-place. Otherwise, the result is\n"
- "written to the standard output.\n");
+ "If <file>s are given, it reformats the files. If -i is specified \n"
+ "together with <file>s, the files are edited in-place. Otherwise, the \n"
+ "result is written to the standard output.\n");
+
if (Help)
cl::PrintHelpMessage();
- clang::format::format();
- return 0;
+
+ bool Error = false;
+ switch (FileNames.size()) {
+ case 0:
+ Error = clang::format::format("-");
+ break;
+ case 1:
+ Error = clang::format::format(FileNames[0]);
+ break;
+ default:
+ if (!Offsets.empty() || !Lengths.empty()) {
+ llvm::errs() << "error: \"-offset\" and \"-length\" can only be used for "
+ "single file.\n";
+ return 1;
+ }
+ for (unsigned i = 0; i < FileNames.size(); ++i)
+ Error |= clang::format::format(FileNames[i]);
+ break;
+ }
+ return Error ? 1 : 0;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D719.1.patch
Type: text/x-patch
Size: 7415 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130424/af4935ee/attachment.bin>
More information about the cfe-commits
mailing list