Skip to content

Commit a29e9e3

Browse files
gigarobyHazardyKnusperkeksowenca
authored
[clang-format] Add --fail-on-incomplete-format. (llvm#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. --------- Co-authored-by: Björn Schäpers <[email protected]> Co-authored-by: Owen Pan <[email protected]>
1 parent adda597 commit a29e9e3

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

clang/docs/ClangFormat.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.
6161
--dry-run - If set, do not actually make the formatting changes
6262
--dump-config - Dump configuration options to stdout and exit.
6363
Can be used with -style option.
64+
--fail-on-incomplete-format - If set, fail with exit code 1 on incomplete format.
6465
--fallback-style=<string> - The name of the predefined style used as a
6566
fallback in case clang-format is invoked with
6667
-style=file, but can not find the .clang-format
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: not clang-format -style=LLVM -fail-on-incomplete-format %s
2+
// RUN: clang-format -style=LLVM %s
3+
4+
int a(

clang/tools/clang-format/ClangFormat.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ static cl::list<std::string> FileNames(cl::Positional,
205205
cl::desc("[@<file>] [<file> ...]"),
206206
cl::cat(ClangFormatCategory));
207207

208+
static cl::opt<bool> FailOnIncompleteFormat(
209+
"fail-on-incomplete-format",
210+
cl::desc("If set, fail with exit code 1 on incomplete format."),
211+
cl::init(false), cl::cat(ClangFormatCategory));
212+
208213
namespace clang {
209214
namespace format {
210215

@@ -399,7 +404,7 @@ class ClangFormatDiagConsumer : public DiagnosticConsumer {
399404
};
400405

401406
// Returns true on error.
402-
static bool format(StringRef FileName) {
407+
static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
403408
const bool IsSTDIN = FileName == "-";
404409
if (!OutputXML && Inplace && IsSTDIN) {
405410
errs() << "error: cannot use -i when reading from stdin.\n";
@@ -535,7 +540,7 @@ static bool format(StringRef FileName) {
535540
Rewrite.getEditBuffer(ID).write(outs());
536541
}
537542
}
538-
return false;
543+
return ErrorOnIncompleteFormat && !Status.FormatComplete;
539544
}
540545

541546
} // namespace format
@@ -699,7 +704,7 @@ int main(int argc, const char **argv) {
699704
}
700705

701706
if (FileNames.empty())
702-
return clang::format::format("-");
707+
return clang::format::format("-", FailOnIncompleteFormat);
703708

704709
if (FileNames.size() > 1 &&
705710
(!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) {
@@ -717,7 +722,7 @@ int main(int argc, const char **argv) {
717722
errs() << "Formatting [" << FileNo++ << "/" << FileNames.size() << "] "
718723
<< FileName << "\n";
719724
}
720-
Error |= clang::format::format(FileName);
725+
Error |= clang::format::format(FileName, FailOnIncompleteFormat);
721726
}
722727
return Error ? 1 : 0;
723728
}

0 commit comments

Comments
 (0)