From 1f20bc17d005fb37816fac95955ec4a36f9de26f Mon Sep 17 00:00:00 2001 From: Paul Hoad Date: Sat, 12 Oct 2019 15:36:05 +0000 Subject: [clang-format] Proposal for clang-format to give compiler style warnings Summary: Related somewhat to {D29039} On seeing a quote on twitter by @invalidop > If it's not formatted with clang-format it's a build error. This made me want to change the way I use clang-format into a tool that could optionally show me where my source code violates clang-format syle. When I'm making a change to clang-format itself, one thing I like to do to test the change is to ensure I didn't cause a huge wave of changes, what I want to do is simply run this on a known formatted directory and see if any new differences arrive in a manner I'm used to. This started me thinking that we should allow build systems to run clang-format on a whole tree and emit compiler style warnings about files that fail clang-format in a form that would make them as a warning in most build systems and because those build systems range in their construction I don't think its unreasonable to NOT expect them to have to do the directory searching or parsing the output replacements themselves, but simply transform that into an error code when there are changes required. I am starting this by suggesing adding a -n or -dry-run command line argument which would emit a warning/error of the form Support for various common compiler command line argumuments like '-Werror' and '-ferror-limit' could make this very flexible to be integrated into build systems and CI systems. ``` > $ /usr/bin/clang-format --dry-run ClangFormat.cpp -ferror-limit=3 -fcolor-diagnostics > ClangFormat.cpp:54:29: warning: code should be clang-formatted [-Wclang-format-violations] > static cl::list > ^ > ClangFormat.cpp:55:20: warning: code should be clang-formatted [-Wclang-format-violations] > LineRanges("lines", cl::desc(": - format a range of\n" > ^ > ClangFormat.cpp:55:77: warning: code should be clang-formatted [-Wclang-format-violations] > LineRanges("lines", cl::desc(": - format a range of\n" > ^ ``` Reviewers: mitchell-stellar, klimek, owenpan Reviewed By: klimek Subscribers: mgorny, cfe-commits Tags: #clang-format, #clang-tools-extra, #clang Differential Revision: https://reviews.llvm.org/D68554 llvm-svn: 374663 --- clang/test/Format/dry-run.cpp | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 clang/test/Format/dry-run.cpp (limited to 'clang/test/Format/dry-run.cpp') diff --git a/clang/test/Format/dry-run.cpp b/clang/test/Format/dry-run.cpp new file mode 100644 index 00000000000..9d6cd34204c --- /dev/null +++ b/clang/test/Format/dry-run.cpp @@ -0,0 +1,4 @@ +// RUN: clang-format -style=LLVM -i --dry-run %s 2> %t.stderr +// RUN: grep -E "*code should be clang-formatted*" %t.stderr + +int a ; -- cgit v1.2.3