Skip to content

Commit c755e41

Browse files
committed
Fix -Wno-error= parsing in clang-format.
As noted in https://reviews.llvm.org/D86137#2460135 parsing of the clang-format parameter -Wno-error=unknown fails. This currently is done by having `-Wno-error=unknown` as an option. In this patch this is changed to make `-Wno-error=` parse an enum into a bit set. This way the parsing is fixed and also we can possibly add new options easily. Reviewed By: MyDeveloperDay Differential Revision: https://reviews.llvm.org/D93459
1 parent 6340f89 commit c755e41

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

clang/docs/ClangFormat.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ to format C/C++/Java/JavaScript/Objective-C/Protobuf/C# code.
3131
Clang-format options:
3232
3333
--Werror - If set, changes formatting warnings to errors
34-
--Wno-error=unknown - If set, unknown format options are only warned about.
35-
This can be used to enable formatting, even if the
36-
configuration contains unknown (newer) options.
37-
Use with caution, as this might lead to dramatically
38-
differing format depending on an option being
39-
supported or not.
34+
--Wno-error=<value> - If set don't error out on the specified warning type.
35+
=unknown - If set, unknown format options are only warned about.
36+
This can be used to enable formatting, even if the
37+
configuration contains unknown (newer) options.
38+
Use with caution, as this might lead to dramatically
39+
differing format depending on an option being
40+
supported or not.
4041
--assume-filename=<string> - Override filename used to determine the language.
4142
When reading from stdin, clang-format assumes this
4243
filename to determine the language.

clang/test/Format/error-config.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: clang-format %s --Wno-error=unknown --style="{UnknownKey: true}" 2>&1 | FileCheck %s -check-prefix=CHECK
2+
// RUN: not clang-format %s --style="{UnknownKey: true}" 2>&1 | FileCheck %s -check-prefix=CHECK-FAIL
3+
4+
// CHECK: YAML:1:2: warning: unknown key 'UnknownKey'
5+
// CHECK-NEXT: {UnknownKey: true}
6+
// CHECK-NEXT: ^~~~~~~~~~
7+
// CHECK-FAIL: YAML:1:2: error: unknown key 'UnknownKey'
8+
// CHECK-FAIL-NEXT: {UnknownKey: true}
9+
// CHECK-FAIL-NEXT: ^~~~~~~~~~
10+
11+
int i ;

clang/tools/clang-format/ClangFormat.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,6 @@ static cl::opt<bool> SortIncludes(
104104
"SortIncludes style flag"),
105105
cl::cat(ClangFormatCategory));
106106

107-
// using the full param name as Wno-error probably won't be a common use case in
108-
// clang-format
109-
static cl::opt<bool> AllowUnknownOptions(
110-
"Wno-error=unknown",
111-
cl::desc("If set, unknown format options are only warned about.\n"
112-
"This can be used to enable formatting, even if the\n"
113-
"configuration contains unknown (newer) options.\n"
114-
"Use with caution, as this might lead to dramatically\n"
115-
"differing format depending on an option being\n"
116-
"supported or not."),
117-
cl::init(false), cl::cat(ClangFormatCategory));
118-
119107
static cl::opt<bool>
120108
Verbose("verbose", cl::desc("If set, shows the list of processed files"),
121109
cl::cat(ClangFormatCategory));
@@ -156,6 +144,23 @@ static cl::opt<bool>
156144
cl::desc("If set, changes formatting warnings to errors"),
157145
cl::cat(ClangFormatCategory));
158146

147+
namespace {
148+
enum class WNoError { Unknown };
149+
}
150+
151+
static cl::bits<WNoError> WNoErrorList(
152+
"Wno-error",
153+
cl::desc("If set don't error out on the specified warning type."),
154+
cl::values(
155+
clEnumValN(WNoError::Unknown, "unknown",
156+
"If set, unknown format options are only warned about.\n"
157+
"This can be used to enable formatting, even if the\n"
158+
"configuration contains unknown (newer) options.\n"
159+
"Use with caution, as this might lead to dramatically\n"
160+
"differing format depending on an option being\n"
161+
"supported or not.")),
162+
cl::cat(ClangFormatCategory));
163+
159164
static cl::opt<bool>
160165
ShowColors("fcolor-diagnostics",
161166
cl::desc("If set, and on a color-capable terminal controls "
@@ -391,7 +396,7 @@ static bool format(StringRef FileName) {
391396

392397
llvm::Expected<FormatStyle> FormatStyle =
393398
getStyle(Style, AssumedFileName, FallbackStyle, Code->getBuffer(),
394-
nullptr, AllowUnknownOptions.getValue());
399+
nullptr, WNoErrorList.isSet(WNoError::Unknown));
395400
if (!FormatStyle) {
396401
llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n";
397402
return true;

0 commit comments

Comments
 (0)