[llvm-branch-commits] [clang-tools-extra] 68e642c - [clang-tidy] Support all YAML supported spellings for bools in CheckOptions.

Nathan James via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Dec 15 13:19:31 PST 2020


Author: Nathan James
Date: 2020-12-15T21:15:16Z
New Revision: 68e642cad02468ce3efb18e7648cf82f2611e7a3

URL: https://github.com/llvm/llvm-project/commit/68e642cad02468ce3efb18e7648cf82f2611e7a3
DIFF: https://github.com/llvm/llvm-project/commit/68e642cad02468ce3efb18e7648cf82f2611e7a3.diff

LOG: [clang-tidy] Support all YAML supported spellings for bools in CheckOptions.

Depends on D92755

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D92756

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/ClangTidyCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/ClangTidyCheck.cpp b/clang-tools-extra/clang-tidy/ClangTidyCheck.cpp
index af19da2419ab..3567aac42c06 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidyCheck.cpp
@@ -11,6 +11,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/WithColor.h"
+#include "llvm/Support/YAMLParser.h"
 #include "llvm/Support/raw_ostream.h"
 
 namespace clang {
@@ -108,13 +109,14 @@ ClangTidyCheck::OptionsView::getLocalOrGlobal(StringRef LocalName) const {
 
 static llvm::Expected<bool> getAsBool(StringRef Value,
                                       const llvm::Twine &LookupName) {
-  if (Value == "true")
-    return true;
-  if (Value == "false")
-    return false;
-  bool Result;
-  if (!Value.getAsInteger(10, Result))
-    return Result;
+
+  if (llvm::Optional<bool> Parsed = llvm::yaml::parseBool(Value))
+    return *Parsed;
+  // To maintain backwards compatability, we support parsing numbers as
+  // booleans, even though its not supported in YAML.
+  long long Number;
+  if (!Value.getAsInteger(10, Number))
+    return Number != 0;
   return llvm::make_error<UnparseableIntegerOptionError>(LookupName.str(),
                                                          Value.str(), true);
 }

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 062216697111..a15ca304070e 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -73,6 +73,9 @@ Improvements to clang-tidy
   <clang-tidy/checks/cppcoreguidelines-init-variables>` and
   :doc:`modernize-make-unique <clang-tidy/checks/modernize-make-unique>`.
 
+- CheckOptions that take boolean values now support all spellings supported in 
+  the `YAML format <https://yaml.org/type/bool.html>`_.
+
 New modules
 ^^^^^^^^^^^
 

diff  --git a/clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp b/clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp
index 40cd9a5eff15..db9624684dfe 100644
--- a/clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp
+++ b/clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp
@@ -195,8 +195,9 @@ TEST(CheckOptionsValidation, ValidIntOptions) {
   CheckOptions["test.BoolIFalseValue"] = "0";
   CheckOptions["test.BoolTrueValue"] = "true";
   CheckOptions["test.BoolFalseValue"] = "false";
+  CheckOptions["test.BoolTrueShort"] = "Y";
+  CheckOptions["test.BoolFalseShort"] = "N";
   CheckOptions["test.BoolUnparseable"] = "Nothing";
-  CheckOptions["test.BoolCaseMismatch"] = "True";
 
   ClangTidyContext Context(std::make_unique<DefaultOptionsProvider>(
       ClangTidyGlobalOptions(), Options));
@@ -227,12 +228,11 @@ TEST(CheckOptionsValidation, ValidIntOptions) {
   CHECK_VAL(TestCheck.getIntLocal<bool>("BoolIFalseValue"), false);
   CHECK_VAL(TestCheck.getIntLocal<bool>("BoolTrueValue"), true);
   CHECK_VAL(TestCheck.getIntLocal<bool>("BoolFalseValue"), false);
+  CHECK_VAL(TestCheck.getIntLocal<bool>("BoolTrueShort"), true);
+  CHECK_VAL(TestCheck.getIntLocal<bool>("BoolFalseShort"), false);
   CHECK_ERROR_INT(TestCheck.getIntLocal<bool>("BoolUnparseable"),
                   "invalid configuration value 'Nothing' for option "
                   "'test.BoolUnparseable'; expected a bool");
-  CHECK_ERROR_INT(TestCheck.getIntLocal<bool>("BoolCaseMismatch"),
-                  "invalid configuration value 'True' for option "
-                  "'test.BoolCaseMismatch'; expected a bool");
 
 #undef CHECK_ERROR_INT
 }


        


More information about the llvm-branch-commits mailing list