njames93 updated this revision to Diff 311388. njames93 added a comment. Fix test failures and added some to demonstrate this behaviour. Haven't added exhaustive cases as they are present in the YAML parser tests.
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D92756/new/ https://reviews.llvm.org/D92756 Files: clang-tools-extra/clang-tidy/ClangTidyCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp Index: clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp =================================================================== --- clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp +++ clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp @@ -195,8 +195,9 @@ 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 @@ 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 } Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -73,6 +73,9 @@ <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 ^^^^^^^^^^^ Index: clang-tools-extra/clang-tidy/ClangTidyCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/ClangTidyCheck.cpp +++ 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 @@ 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); }
Index: clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp =================================================================== --- clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp +++ clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp @@ -195,8 +195,9 @@ 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 @@ 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 } Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -73,6 +73,9 @@ <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 ^^^^^^^^^^^ Index: clang-tools-extra/clang-tidy/ClangTidyCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/ClangTidyCheck.cpp +++ 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 @@ 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); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits