[llvm-branch-commits] [clang] c755e41 - Fix -Wno-error= parsing in clang-format.
Joachim Meyer via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Dec 17 13:26:14 PST 2020
Author: Joachim Meyer
Date: 2020-12-17T22:23:42+01:00
New Revision: c755e41c336c898873db6c3c58a2819a982f60de
URL: https://github.com/llvm/llvm-project/commit/c755e41c336c898873db6c3c58a2819a982f60de
DIFF: https://github.com/llvm/llvm-project/commit/c755e41c336c898873db6c3c58a2819a982f60de.diff
LOG: 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
Added:
clang/test/Format/error-config.cpp
Modified:
clang/docs/ClangFormat.rst
clang/tools/clang-format/ClangFormat.cpp
Removed:
################################################################################
diff --git a/clang/docs/ClangFormat.rst b/clang/docs/ClangFormat.rst
index b746ed3453df..d5333c0032b4 100644
--- a/clang/docs/ClangFormat.rst
+++ b/clang/docs/ClangFormat.rst
@@ -31,12 +31,13 @@ to format C/C++/Java/JavaScript/Objective-C/Protobuf/C# code.
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
-
diff ering format depending on an option being
- supported or not.
+ --Wno-error=<value> - 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
+
diff ering format depending on an option being
+ supported or not.
--assume-filename=<string> - 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
index 000000000000..7fbc869f3a3c
--- /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
index a1b42a6d0940..64f0e2badf33 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -104,18 +104,6 @@ static cl::opt<bool> SortIncludes(
"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<bool> 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"
- "
diff ering format depending on an option being\n"
- "supported or not."),
- cl::init(false), cl::cat(ClangFormatCategory));
-
static cl::opt<bool>
Verbose("verbose", cl::desc("If set, shows the list of processed files"),
cl::cat(ClangFormatCategory));
@@ -156,6 +144,23 @@ static cl::opt<bool>
cl::desc("If set, changes formatting warnings to errors"),
cl::cat(ClangFormatCategory));
+namespace {
+enum class WNoError { Unknown };
+}
+
+static cl::bits<WNoError> 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"
+ "
diff ering format depending on an option being\n"
+ "supported or not.")),
+ cl::cat(ClangFormatCategory));
+
static cl::opt<bool>
ShowColors("fcolor-diagnostics",
cl::desc("If set, and on a color-capable terminal controls "
@@ -391,7 +396,7 @@ static bool format(StringRef FileName) {
llvm::Expected<FormatStyle> 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;
More information about the llvm-branch-commits
mailing list