[clang] [clang-format] Add --fail-on-incomplete-format. (PR #84346)
Roberto Bampi via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 7 09:19:25 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] [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;
}
More information about the cfe-commits
mailing list