clang-format: add an option -verbose to list the files being processed

Reviewers: djasper

Reviewed By: djasper

Subscribers: klimek, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D34824

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@310778 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Sylvestre Ledru 2017-08-12 15:15:10 +00:00
parent 4e37891c39
commit 7602b13a8e
4 changed files with 36 additions and 16 deletions

View File

@ -71,6 +71,7 @@ to format C/C++/Obj-C code.
Use -style="{key: value, ...}" to set specific Use -style="{key: value, ...}" to set specific
parameters, e.g.: parameters, e.g.:
-style="{BasedOnStyle: llvm, IndentWidth: 8}" -style="{BasedOnStyle: llvm, IndentWidth: 8}"
-verbose - If set, shows the list of processed files
Generic Options: Generic Options:

View File

@ -187,6 +187,9 @@ clang-format
... ...
* Option -verbose added to the command line.
Shows the list of processed files.
libclang libclang
-------- --------

16
test/Format/verbose.cpp Normal file
View File

@ -0,0 +1,16 @@
// RUN: clang-format %s 2> %t.stderr
// RUN: not grep "Formatting" %t.stderr
// RUN: clang-format %s -verbose 2> %t.stderr
// RUN: grep -E "Formatting (.*)verbose.cpp(.*)" %t.stderr
// RUN: clang-format %s -verbose=false 2> %t.stderr
// RUN: not grep "Formatting" %t.stderr
int a;
// RUN: clang-format %s 2> %t.stderr
// RUN: not grep "Formatting" %t.stderr
// RUN: clang-format %s -verbose 2> %t.stderr
// RUN: grep -E "Formatting (.*)verbose.cpp(.*)" %t.stderr
// RUN: clang-format %s -verbose=false 2> %t.stderr
// RUN: not grep "Formatting" %t.stderr
int a;

View File

@ -102,6 +102,10 @@ static cl::opt<bool> SortIncludes(
"SortIncludes style flag"), "SortIncludes style flag"),
cl::cat(ClangFormatCategory)); cl::cat(ClangFormatCategory));
static cl::opt<bool>
Verbose("verbose", cl::desc("If set, shows the list of processed files"),
cl::cat(ClangFormatCategory));
static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"), static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"),
cl::cat(ClangFormatCategory)); cl::cat(ClangFormatCategory));
@ -365,23 +369,19 @@ int main(int argc, const char **argv) {
} }
bool Error = false; bool Error = false;
switch (FileNames.size()) { if (FileNames.empty()) {
case 0:
Error = clang::format::format("-"); Error = clang::format::format("-");
break; return Error ? 1 : 0;
case 1: }
Error = clang::format::format(FileNames[0]); if (FileNames.size() != 1 && (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) {
break;
default:
if (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty()) {
errs() << "error: -offset, -length and -lines can only be used for " errs() << "error: -offset, -length and -lines can only be used for "
"single file.\n"; "single file.\n";
return 1; return 1;
} }
for (unsigned i = 0; i < FileNames.size(); ++i) for (const auto &FileName : FileNames) {
Error |= clang::format::format(FileNames[i]); if (Verbose)
break; errs() << "Formatting " << FileName << "\n";
Error |= clang::format::format(FileName);
} }
return Error ? 1 : 0; return Error ? 1 : 0;
} }