[clang] [clang-format] Add --fail-on-incomplete-format. (PR #84346)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 7 09:16:39 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Roberto Bampi (gigaroby)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/84346.diff
1 Files Affected:
- (modified) clang/tools/clang-format/ClangFormat.cpp (+12-3)
``````````diff
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;
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/84346
More information about the cfe-commits
mailing list