diff --git a/clang-tools-extra/clang-tidy/ClangTidyCheck.cpp b/clang-tools-extra/clang-tidy/ClangTidyCheck.cpp index af19da2419ab21..3567aac42c0629 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 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 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(LookupName.str(), Value.str(), true); } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 062216697111ec..a15ca304070e1b 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -73,6 +73,9 @@ Improvements to clang-tidy ` and :doc:`modernize-make-unique `. +- CheckOptions that take boolean values now support all spellings supported in + the `YAML format `_. + New modules ^^^^^^^^^^^ diff --git a/clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp b/clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp index 40cd9a5eff1511..db9624684dfebd 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( ClangTidyGlobalOptions(), Options)); @@ -227,12 +228,11 @@ TEST(CheckOptionsValidation, ValidIntOptions) { CHECK_VAL(TestCheck.getIntLocal("BoolIFalseValue"), false); CHECK_VAL(TestCheck.getIntLocal("BoolTrueValue"), true); CHECK_VAL(TestCheck.getIntLocal("BoolFalseValue"), false); + CHECK_VAL(TestCheck.getIntLocal("BoolTrueShort"), true); + CHECK_VAL(TestCheck.getIntLocal("BoolFalseShort"), false); CHECK_ERROR_INT(TestCheck.getIntLocal("BoolUnparseable"), "invalid configuration value 'Nothing' for option " "'test.BoolUnparseable'; expected a bool"); - CHECK_ERROR_INT(TestCheck.getIntLocal("BoolCaseMismatch"), - "invalid configuration value 'True' for option " - "'test.BoolCaseMismatch'; expected a bool"); #undef CHECK_ERROR_INT }