diff --git a/clang/docs/ClangFormat.rst b/clang/docs/ClangFormat.rst --- a/clang/docs/ClangFormat.rst +++ b/clang/docs/ClangFormat.rst @@ -31,12 +31,13 @@ Clang-format options: --Werror - If set, changes formatting warnings to errors - --Wno-error=unknown - If set, unknown format options are only warned about. - This can be used to enable formatting, even if the - configuration contains unknown (newer) options. - Use with caution, as this might lead to dramatically - differing format depending on an option being - supported or not. + --Wno-error= - If set don't error out on the specified warning type. + =unknown - If set, unknown format options are only warned about. + This can be used to enable formatting, even if the + configuration contains unknown (newer) options. + Use with caution, as this might lead to dramatically + differing format depending on an option being + supported or not. --assume-filename= - Override filename used to determine the language. When reading from stdin, clang-format assumes this filename to determine the language. diff --git a/clang/test/Format/error-config.cpp b/clang/test/Format/error-config.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Format/error-config.cpp @@ -0,0 +1,11 @@ +// RUN: clang-format %s --Wno-error=unknown --style="{UnknownKey: true}" 2>&1 | FileCheck %s -check-prefix=CHECK +// RUN: not clang-format %s --style="{UnknownKey: true}" 2>&1 | FileCheck %s -check-prefix=CHECK-FAIL + +// CHECK: YAML:1:2: warning: unknown key 'UnknownKey' +// CHECK-NEXT: {UnknownKey: true} +// CHECK-NEXT: ^~~~~~~~~~ +// CHECK-FAIL: YAML:1:2: error: unknown key 'UnknownKey' +// CHECK-FAIL-NEXT: {UnknownKey: true} +// CHECK-FAIL-NEXT: ^~~~~~~~~~ + +int i ; diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -104,18 +104,6 @@ "SortIncludes style flag"), cl::cat(ClangFormatCategory)); -// using the full param name as Wno-error probably won't be a common use case in -// clang-format -static cl::opt AllowUnknownOptions( - "Wno-error=unknown", - cl::desc("If set, unknown format options are only warned about.\n" - "This can be used to enable formatting, even if the\n" - "configuration contains unknown (newer) options.\n" - "Use with caution, as this might lead to dramatically\n" - "differing format depending on an option being\n" - "supported or not."), - cl::init(false), cl::cat(ClangFormatCategory)); - static cl::opt Verbose("verbose", cl::desc("If set, shows the list of processed files"), cl::cat(ClangFormatCategory)); @@ -156,6 +144,23 @@ cl::desc("If set, changes formatting warnings to errors"), cl::cat(ClangFormatCategory)); +namespace { +enum class WNoError { Unknown }; +} + +static cl::bits WNoErrorList( + "Wno-error", + cl::desc("If set don't error out on the specified warning type."), + cl::values( + clEnumValN(WNoError::Unknown, "unknown", + "If set, unknown format options are only warned about.\n" + "This can be used to enable formatting, even if the\n" + "configuration contains unknown (newer) options.\n" + "Use with caution, as this might lead to dramatically\n" + "differing format depending on an option being\n" + "supported or not.")), + cl::cat(ClangFormatCategory)); + static cl::opt ShowColors("fcolor-diagnostics", cl::desc("If set, and on a color-capable terminal controls " @@ -391,7 +396,7 @@ llvm::Expected FormatStyle = getStyle(Style, AssumedFileName, FallbackStyle, Code->getBuffer(), - nullptr, AllowUnknownOptions.getValue()); + nullptr, WNoErrorList.isSet(WNoError::Unknown)); if (!FormatStyle) { llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; return true;