[clang] [clang-format] Add --fail-on-incomplete-format. (PR #84346)

Roberto Bampi via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 8 05:51:55 PST 2024


https://github.com/gigaroby updated https://github.com/llvm/llvm-project/pull/84346

>From 4b0ef9a83db7f8309ea0d8f79fabbfbad50ae6f8 Mon Sep 17 00:00:00 2001
From: Roberto Bampi <bampi at 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/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..a2615ba0e21fca 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::init(false), 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;
 }

>From 2d6036587cb31eea0080661ead4108f7517e7724 Mon Sep 17 00:00:00 2001
From: Roberto Bampi <gigaroby at 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 <github at 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 a2615ba0e21fca..00aba43ed9d4e7 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -541,10 +541,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



More information about the cfe-commits mailing list