From 206ff74f5e548f244da6042a8c5cff270c199d84 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Mon, 14 Jun 2021 22:18:41 -0500 Subject: [PATCH] [Target] Allow 'true' and 'false' strings in conversions to integer (#8254) * [Target] Allow 'true' and 'false' strings in conversions to integer This will allow Bool parameters to take true/false values instead of 0 and 1 only. * Convert the string to lowercase. * Reserve memory for lowercase string * Add include --- src/target/target.cc | 13 ++++++++++++- tests/python/unittest/test_target_target.py | 11 +++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/target/target.cc b/src/target/target.cc index 396e264ede4d..546a3596297a 100644 --- a/src/target/target.cc +++ b/src/target/target.cc @@ -28,6 +28,7 @@ #include #include +#include #include #include "../runtime/object_internal.h" @@ -210,7 +211,17 @@ ObjectRef TargetInternal::ParseType(const std::string& str, // Parsing integer int v; if (!(is >> v)) { - throw Error(": Cannot parse into type \"Integer\" from string: " + str); + std::string lower(str.size(), '\x0'); + std::transform(str.begin(), str.end(), lower.begin(), + [](unsigned char c) { return std::tolower(c); }); + // Bool is a subclass of IntImm, so allow textual boolean values. + if (lower == "true") { + v = 1; + } else if (lower == "false") { + v = 0; + } else { + throw Error(": Cannot parse into type \"Integer\" from string: " + str); + } } return Integer(v); } else if (info.type_index == String::ContainerType::_GetOrAllocRuntimeTypeIndex()) { diff --git a/tests/python/unittest/test_target_target.py b/tests/python/unittest/test_target_target.py index 98a9edc7a517..3ed275800bd2 100644 --- a/tests/python/unittest/test_target_target.py +++ b/tests/python/unittest/test_target_target.py @@ -299,5 +299,16 @@ def test_check_and_update_host_consist_3(): assert target.host == host +def test_target_attr_bool_value(): + target0 = Target("llvm --link-params=True") + assert target0.attrs["link-params"] == 1 + target1 = Target("llvm --link-params=true") + assert target1.attrs["link-params"] == 1 + target2 = Target("llvm --link-params=False") + assert target2.attrs["link-params"] == 0 + target3 = Target("llvm --link-params=false") + assert target3.attrs["link-params"] == 0 + + if __name__ == "__main__": sys.exit(pytest.main([__file__] + sys.argv[1:]))