https://github.com/gigaroby created https://github.com/llvm/llvm-project/pull/84346
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. >From 9d04b6fdd9d6a6468e8f81e5d070c773fe07a612 Mon Sep 17 00:00:00 2001 From: Roberto Bampi <ba...@google.com> Date: Thu, 7 Mar 2024 18:10:56 +0100 Subject: [PATCH] [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/tools/clang-format/ClangFormat.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index e122cea50f7268..104978ce4bba1d 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -205,6 +205,12 @@ 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, clang-format will fail with exit " + "code 1 on incomplete failures."), + cl::cat(ClangFormatCategory)); + namespace clang { namespace format { @@ -399,7 +405,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 +541,9 @@ static bool format(StringRef FileName) { Rewrite.getEditBuffer(ID).write(outs()); } } + if (ErrorOnIncompleteFormat && !Status.FormatComplete) + return true; + return false; } @@ -699,7 +708,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 +726,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; } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits