From 7602b13a8e8b5656afd6327d112b76b39f836e5b Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 12 Aug 2017 15:15:10 +0000 Subject: [PATCH] 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 --- docs/ClangFormat.rst | 1 + docs/ReleaseNotes.rst | 3 +++ test/Format/verbose.cpp | 16 +++++++++++++++ tools/clang-format/ClangFormat.cpp | 32 +++++++++++++++--------------- 4 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 test/Format/verbose.cpp diff --git a/docs/ClangFormat.rst b/docs/ClangFormat.rst index 902afcd08e..b4030eac6f 100644 --- a/docs/ClangFormat.rst +++ b/docs/ClangFormat.rst @@ -71,6 +71,7 @@ to format C/C++/Obj-C code. Use -style="{key: value, ...}" to set specific parameters, e.g.: -style="{BasedOnStyle: llvm, IndentWidth: 8}" + -verbose - If set, shows the list of processed files Generic Options: diff --git a/docs/ReleaseNotes.rst b/docs/ReleaseNotes.rst index 64c1cf492f..be038234df 100644 --- a/docs/ReleaseNotes.rst +++ b/docs/ReleaseNotes.rst @@ -187,6 +187,9 @@ clang-format ... +* Option -verbose added to the command line. + Shows the list of processed files. + libclang -------- diff --git a/test/Format/verbose.cpp b/test/Format/verbose.cpp new file mode 100644 index 0000000000..dd625e3f67 --- /dev/null +++ b/test/Format/verbose.cpp @@ -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; diff --git a/tools/clang-format/ClangFormat.cpp b/tools/clang-format/ClangFormat.cpp index f8e2fe186b..37c2d8b78f 100644 --- a/tools/clang-format/ClangFormat.cpp +++ b/tools/clang-format/ClangFormat.cpp @@ -102,6 +102,10 @@ static cl::opt SortIncludes( "SortIncludes style flag"), cl::cat(ClangFormatCategory)); +static cl::opt + Verbose("verbose", cl::desc("If set, shows the list of processed files"), + cl::cat(ClangFormatCategory)); + static cl::list FileNames(cl::Positional, cl::desc("[ ...]"), cl::cat(ClangFormatCategory)); @@ -365,23 +369,19 @@ int main(int argc, const char **argv) { } bool Error = false; - switch (FileNames.size()) { - case 0: + if (FileNames.empty()) { Error = clang::format::format("-"); - break; - case 1: - Error = clang::format::format(FileNames[0]); - break; - default: - if (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty()) { - errs() << "error: -offset, -length and -lines 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; + } + if (FileNames.size() != 1 && (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) { + errs() << "error: -offset, -length and -lines can only be used for " + "single file.\n"; + return 1; + } + for (const auto &FileName : FileNames) { + if (Verbose) + errs() << "Formatting " << FileName << "\n"; + Error |= clang::format::format(FileName); } return Error ? 1 : 0; } - -- 2.40.0