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

2024-03-14 Thread Roberto Bampi via cfe-commits

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

>From f65709e2a353fa19919df291d6c3a60f5e1d1bd4 Mon Sep 17 00:00:00 2001
From: Roberto Bampi 
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/docs/ClangFormat.rst   |  1 +
 clang/tools/clang-format/ClangFormat.cpp | 14 +++---
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/clang/docs/ClangFormat.rst b/clang/docs/ClangFormat.rst
index 819d9ee9f9cde1..80dc38a075c8fc 100644
--- a/clang/docs/ClangFormat.rst
+++ b/clang/docs/ClangFormat.rst
@@ -61,6 +61,7 @@ to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# 
code.
 --dry-run  - If set, do not actually make the 
formatting changes
 --dump-config  - Dump configuration options to stdout and 
exit.
  Can be used with -style option.
+--fail-on-incomplete-format- If set, fail with exit code 1 on 
incomplete format.
 --fallback-style=  - The name of the predefined style used as a
  fallback in case clang-format is invoked 
with
  -style=file, but can not find the 
.clang-format
diff --git a/clang/tools/clang-format/ClangFormat.cpp 
b/clang/tools/clang-format/ClangFormat.cpp
index e122cea50f7268..58027af5d9e091 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -205,6 +205,11 @@ static cl::list FileNames(cl::Positional,
cl::desc("[@] [ ...]"),
cl::cat(ClangFormatCategory));
 
+static cl::opt FailOnIncompleteFormat(
+"fail-on-incomplete-format",
+cl::desc("If set, fail with exit code 1 on incomplete format."),
+cl::init(false), cl::cat(ClangFormatCategory));
+
 namespace clang {
 namespace format {
 
@@ -399,7 +404,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 +540,9 @@ static bool format(StringRef FileName) {
   Rewrite.getEditBuffer(ID).write(outs());
 }
   }
+  if (ErrorOnIncompleteFormat && !Status.FormatComplete)
+return true;
+
   return false;
 }
 
@@ -699,7 +707,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 +725,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 51523aae1d42b916d70ca8a2fe7ead82323f38bd Mon Sep 17 00:00:00 2001
From: Roberto Bampi 
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 
---
 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 58027af5d9e091..ed401135ad8433 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -540,10 +540,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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-03-14 Thread Roberto Bampi via cfe-commits

gigaroby wrote:

> > I think you need to run `clang/docs/tools/dump_format_help.py`.

Done.

https://github.com/llvm/llvm-project/pull/84346
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-03-18 Thread Roberto Bampi via cfe-commits

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

>From ee4f8f1c4abaa20d4eedd3451fd96f3306990d0e Mon Sep 17 00:00:00 2001
From: Roberto Bampi 
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/docs/ClangFormat.rst   |  1 +
 clang/test/Format/fail-on-incomplete.cpp |  5 +
 clang/tools/clang-format/ClangFormat.cpp | 14 +++---
 3 files changed, 17 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Format/fail-on-incomplete.cpp

diff --git a/clang/docs/ClangFormat.rst b/clang/docs/ClangFormat.rst
index 819d9ee9f9cde1..80dc38a075c8fc 100644
--- a/clang/docs/ClangFormat.rst
+++ b/clang/docs/ClangFormat.rst
@@ -61,6 +61,7 @@ to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# 
code.
 --dry-run  - If set, do not actually make the 
formatting changes
 --dump-config  - Dump configuration options to stdout and 
exit.
  Can be used with -style option.
+--fail-on-incomplete-format- If set, fail with exit code 1 on 
incomplete format.
 --fallback-style=  - The name of the predefined style used as a
  fallback in case clang-format is invoked 
with
  -style=file, but can not find the 
.clang-format
diff --git a/clang/test/Format/fail-on-incomplete.cpp 
b/clang/test/Format/fail-on-incomplete.cpp
new file mode 100644
index 00..23220e654e4625
--- /dev/null
+++ b/clang/test/Format/fail-on-incomplete.cpp
@@ -0,0 +1,5 @@
+// RUN: cat %s | not clang-format --fail-on-incomplete-format | FileCheck %s
+// RUN: cat %s | clang-format | FileCheck %s
+int a([) {}
+
+// CHECK: int a([) {}
\ No newline at end of file
diff --git a/clang/tools/clang-format/ClangFormat.cpp 
b/clang/tools/clang-format/ClangFormat.cpp
index 60b0f5d1a0f414..e2600435b0a620 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -199,6 +199,11 @@ static cl::list FileNames(cl::Positional,
cl::desc("[@] [ ...]"),
cl::cat(ClangFormatCategory));
 
+static cl::opt FailOnIncompleteFormat(
+"fail-on-incomplete-format",
+cl::desc("If set, fail with exit code 1 on incomplete format."),
+cl::init(false), cl::cat(ClangFormatCategory));
+
 namespace clang {
 namespace format {
 
@@ -393,7 +398,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";
@@ -529,6 +534,9 @@ static bool format(StringRef FileName) {
   Rewrite.getEditBuffer(ID).write(outs());
 }
   }
+  if (ErrorOnIncompleteFormat && !Status.FormatComplete)
+return true;
+
   return false;
 }
 
@@ -693,7 +701,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())) {
@@ -711,7 +719,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 9763d8bb325cbaedf9af045e77d50f81c200a5f8 Mon Sep 17 00:00:00 2001
From: Roberto Bampi 
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 
---
 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 e2600435b0a620..0e54a478f94fd3 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -534,10 +534,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 form

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

2024-03-18 Thread Roberto Bampi via cfe-commits

gigaroby wrote:

> Can you add lit test cases?

Done.

https://github.com/llvm/llvm-project/pull/84346
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-03-18 Thread Roberto Bampi via cfe-commits

gigaroby wrote:

I think the documentation failure is related to 
https://github.com/llvm/llvm-project/commit/0c423af59c971ddf1aa12d94529edf8293608157
 but I might be wrong.

https://github.com/llvm/llvm-project/pull/84346
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-03-20 Thread Roberto Bampi via cfe-commits

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

>From e19f49ca2660cbcd64fb81aae0428e899d61cac6 Mon Sep 17 00:00:00 2001
From: Roberto Bampi 
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/docs/ClangFormat.rst   |  1 +
 clang/test/Format/fail-on-incomplete.cpp |  4 
 clang/tools/clang-format/ClangFormat.cpp | 14 +++---
 3 files changed, 16 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Format/fail-on-incomplete.cpp

diff --git a/clang/docs/ClangFormat.rst b/clang/docs/ClangFormat.rst
index 819d9ee9f9cde1..80dc38a075c8fc 100644
--- a/clang/docs/ClangFormat.rst
+++ b/clang/docs/ClangFormat.rst
@@ -61,6 +61,7 @@ to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# 
code.
 --dry-run  - If set, do not actually make the 
formatting changes
 --dump-config  - Dump configuration options to stdout and 
exit.
  Can be used with -style option.
+--fail-on-incomplete-format- If set, fail with exit code 1 on 
incomplete format.
 --fallback-style=  - The name of the predefined style used as a
  fallback in case clang-format is invoked 
with
  -style=file, but can not find the 
.clang-format
diff --git a/clang/test/Format/fail-on-incomplete.cpp 
b/clang/test/Format/fail-on-incomplete.cpp
new file mode 100644
index 00..42ddea66faf6ee
--- /dev/null
+++ b/clang/test/Format/fail-on-incomplete.cpp
@@ -0,0 +1,4 @@
+// RUN: not clang-format %s -style=LLVM -fail-on-incomplete-format
+// RUN: clang-format %s -style=LLVM
+int a(
+
diff --git a/clang/tools/clang-format/ClangFormat.cpp 
b/clang/tools/clang-format/ClangFormat.cpp
index e122cea50f7268..58027af5d9e091 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -205,6 +205,11 @@ static cl::list FileNames(cl::Positional,
cl::desc("[@] [ ...]"),
cl::cat(ClangFormatCategory));
 
+static cl::opt FailOnIncompleteFormat(
+"fail-on-incomplete-format",
+cl::desc("If set, fail with exit code 1 on incomplete format."),
+cl::init(false), cl::cat(ClangFormatCategory));
+
 namespace clang {
 namespace format {
 
@@ -399,7 +404,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 +540,9 @@ static bool format(StringRef FileName) {
   Rewrite.getEditBuffer(ID).write(outs());
 }
   }
+  if (ErrorOnIncompleteFormat && !Status.FormatComplete)
+return true;
+
   return false;
 }
 
@@ -699,7 +707,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 +725,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 af442672b7957593d37a1640b316fa4d8ec00467 Mon Sep 17 00:00:00 2001
From: Roberto Bampi 
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 
---
 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 58027af5d9e091..ed401135ad8433 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -540,10 +540,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

___
cfe-commits mailing list

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

2024-03-20 Thread Roberto Bampi via cfe-commits


@@ -0,0 +1,5 @@
+// RUN: cat %s | not clang-format --fail-on-incomplete-format | FileCheck %s
+// RUN: cat %s | clang-format | FileCheck %s
+int a([) {}
+
+// CHECK: int a([) {}

gigaroby wrote:

> It's missing the EOF. Also, use a single hyphen for the option like other 
> tests.

Done. It took me quite a while but `-style=LLVM` is also required as the 
.clang-format in the parent directory sets DisableFormat to true. Before I 
didn't notice because the `cat %s | clang-format` was bypassing the lookup for 
the config file.

https://github.com/llvm/llvm-project/pull/84346
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-03-07 Thread Roberto Bampi via cfe-commits

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 
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 FileNames(cl::Positional,
cl::desc("[@] [ ...]"),
cl::cat(ClangFormatCategory));
 
+static cl::opt
+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


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

2024-03-07 Thread Roberto Bampi via cfe-commits

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 
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 FileNames(cl::Positional,
cl::desc("[@] [ ...]"),
cl::cat(ClangFormatCategory));
 
+static cl::opt
+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;
 }

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-03-08 Thread Roberto Bampi via cfe-commits

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 
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 FileNames(cl::Positional,
cl::desc("[@] [ ...]"),
cl::cat(ClangFormatCategory));
 
+static cl::opt
+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 
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 
---
 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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-03-12 Thread Roberto Bampi via cfe-commits

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

>From 16d2b76c9c39dc0ae5cc6ddb1809e6326624cdde Mon Sep 17 00:00:00 2001
From: Roberto Bampi 
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 FileNames(cl::Positional,
cl::desc("[@] [ ...]"),
cl::cat(ClangFormatCategory));
 
+static cl::opt
+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 88544a2d8448d114228dd07ae60548181ea2c89c Mon Sep 17 00:00:00 2001
From: Roberto Bampi 
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 
---
 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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits