diff options
author | Alex Turbov <i.zaufi@gmail.com> | 2021-07-27 20:05:08 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2021-08-03 14:55:47 (GMT) |
commit | 0dd77957063f07d5ab8dc8ddb7a56930c688d523 (patch) | |
tree | 7739fc87026d34b176facd40a9bb5d2196b05256 /Source/cmConditionEvaluator.cxx | |
parent | 866b0595f6f3e26542148984e22f508fb1b3eb1e (diff) | |
download | CMake-0dd77957063f07d5ab8dc8ddb7a56930c688d523.zip CMake-0dd77957063f07d5ab8dc8ddb7a56930c688d523.tar.gz CMake-0dd77957063f07d5ab8dc8ddb7a56930c688d523.tar.bz2 |
Refactor: Extract the logic of testing for special variable to func
Introduce `looksLikeSpecialVariable`. It makes `if()` blocks readable.
Diffstat (limited to 'Source/cmConditionEvaluator.cxx')
-rw-r--r-- | Source/cmConditionEvaluator.cxx | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/Source/cmConditionEvaluator.cxx b/Source/cmConditionEvaluator.cxx index 444d029..a239fcc 100644 --- a/Source/cmConditionEvaluator.cxx +++ b/Source/cmConditionEvaluator.cxx @@ -91,6 +91,16 @@ std::string bool2string(bool const value) { return std::string(std::size_t(1), static_cast<char>('0' + int(value))); } + +bool looksLikeSpecialVariable(const std::string& var, + cm::static_string_view prefix, + const std::size_t varNameLen) +{ + // NOTE Expecting a variable name at least 1 char length: + // <prefix> + `{` + <varname> + `}` + return ((prefix.size() + 3) <= varNameLen) && + cmHasPrefix(var, cmStrCat(prefix, '{')) && var[varNameLen - 1] == '}'; +} } // anonymous namespace #if defined(__SUNPRO_CC) @@ -580,21 +590,24 @@ bool cmConditionEvaluator::HandleLevel1(cmArgumentList& newArgs, std::string&, } // is a variable defined else if (this->IsKeyword(keyDEFINED, *args.current)) { - const auto argP1len = args.next->GetValue().size(); - auto bdef = false; - if (argP1len > 4 && cmHasLiteralPrefix(args.next->GetValue(), "ENV{") && - args.next->GetValue().operator[](argP1len - 1) == '}') { - const auto env = args.next->GetValue().substr(4, argP1len - 5); - bdef = cmSystemTools::HasEnv(env); - } else if (argP1len > 6 && - cmHasLiteralPrefix(args.next->GetValue(), "CACHE{") && - args.next->GetValue().operator[](argP1len - 1) == '}') { - const auto cache = args.next->GetValue().substr(6, argP1len - 7); - bdef = bool(this->Makefile.GetState()->GetCacheEntryValue(cache)); - } else { - bdef = this->Makefile.IsDefinitionSet(args.next->GetValue()); + const auto& var = args.next->GetValue(); + const auto varNameLen = var.size(); + + auto result = false; + if (looksLikeSpecialVariable(var, "ENV"_s, varNameLen)) { + const auto env = args.next->GetValue().substr(4, varNameLen - 5); + result = cmSystemTools::HasEnv(env); + } + + else if (looksLikeSpecialVariable(var, "CACHE"_s, varNameLen)) { + const auto cache = args.next->GetValue().substr(6, varNameLen - 7); + result = bool(this->Makefile.GetState()->GetCacheEntryValue(cache)); + } + + else { + result = this->Makefile.IsDefinitionSet(args.next->GetValue()); } - newArgs.ReduceOneArg(bdef, args); + newArgs.ReduceOneArg(result, args); } // does a test exist else if (this->IsKeyword(keyTEST, *args.current)) { |