https://github.com/gigaroby updated https://github.com/llvm/llvm-project/pull/84346
>From e19f49ca2660cbcd64fb81aae0428e899d61cac6 Mon Sep 17 00:00:00 2001 From: Roberto Bampi <ba...@google.com> Date: Thu, 7 Mar 2024 18:10:56 +0100 Subject: [PATCH 1/2] [clang-format] Add --fail-on-incomplete-format. At the moment clang-format will return exit code 0 on incomplete results. In scripts it would sometimes be useful if clang-format would instead fail in those cases, signalling that there was something wrong with the code being formatted. --- clang/docs/ClangFormat.rst | 1 + clang/test/Format/fail-on-incomplete.cpp | 4 ++++ clang/tools/clang-format/ClangFormat.cpp | 14 +++++++++++--- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 clang/test/Format/fail-on-incomplete.cpp diff --git a/clang/docs/ClangFormat.rst b/clang/docs/ClangFormat.rst index 819d9ee9f9cde1..80dc38a075c8fc 100644 --- a/clang/docs/ClangFormat.rst +++ b/clang/docs/ClangFormat.rst @@ -61,6 +61,7 @@ to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code. --dry-run - If set, do not actually make the formatting changes --dump-config - Dump configuration options to stdout and exit. Can be used with -style option. + --fail-on-incomplete-format - If set, fail with exit code 1 on incomplete format. --fallback-style=<string> - The name of the predefined style used as a fallback in case clang-format is invoked with -style=file, but can not find the .clang-format diff --git a/clang/test/Format/fail-on-incomplete.cpp b/clang/test/Format/fail-on-incomplete.cpp new file mode 100644 index 00000000000000..42ddea66faf6ee --- /dev/null +++ b/clang/test/Format/fail-on-incomplete.cpp @@ -0,0 +1,4 @@ +// RUN: not clang-format %s -style=LLVM -fail-on-incomplete-format +// RUN: clang-format %s -style=LLVM +int a( + diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index e122cea50f7268..58027af5d9e091 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -205,6 +205,11 @@ static cl::list<std::string> FileNames(cl::Positional, cl::desc("[@<file>] [<file> ...]"), cl::cat(ClangFormatCategory)); +static cl::opt<bool> FailOnIncompleteFormat( + "fail-on-incomplete-format", + cl::desc("If set, fail with exit code 1 on incomplete format."), + cl::init(false), cl::cat(ClangFormatCategory)); + namespace clang { namespace format { @@ -399,7 +404,7 @@ class ClangFormatDiagConsumer : public DiagnosticConsumer { }; // Returns true on error. -static bool format(StringRef FileName) { +static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) { const bool IsSTDIN = FileName == "-"; if (!OutputXML && Inplace && IsSTDIN) { errs() << "error: cannot use -i when reading from stdin.\n"; @@ -535,6 +540,9 @@ static bool format(StringRef FileName) { Rewrite.getEditBuffer(ID).write(outs()); } } + if (ErrorOnIncompleteFormat && !Status.FormatComplete) + return true; + return false; } @@ -699,7 +707,7 @@ int main(int argc, const char **argv) { } if (FileNames.empty()) - return clang::format::format("-"); + return clang::format::format("-", FailOnIncompleteFormat); if (FileNames.size() > 1 && (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) { @@ -717,7 +725,7 @@ int main(int argc, const char **argv) { errs() << "Formatting [" << FileNo++ << "/" << FileNames.size() << "] " << FileName << "\n"; } - Error |= clang::format::format(FileName); + Error |= clang::format::format(FileName, FailOnIncompleteFormat); } return Error ? 1 : 0; } >From af442672b7957593d37a1640b316fa4d8ec00467 Mon Sep 17 00:00:00 2001 From: Roberto Bampi <gigar...@users.noreply.github.com> Date: Fri, 8 Mar 2024 14:51:49 +0100 Subject: [PATCH 2/2] Update clang/tools/clang-format/ClangFormat.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Björn Schäpers <git...@hazardy.de> --- clang/tools/clang-format/ClangFormat.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index 58027af5d9e091..ed401135ad8433 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -540,10 +540,7 @@ static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) { Rewrite.getEditBuffer(ID).write(outs()); } } - if (ErrorOnIncompleteFormat && !Status.FormatComplete) - return true; - - return false; + return ErrorOnIncompleteFormat && !Status.FormatComplete; } } // namespace format _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits