diff --git a/Help/command/foreach.rst b/Help/command/foreach.rst index ae2afb2905..ecbfed3d9d 100644 --- a/Help/command/foreach.rst +++ b/Help/command/foreach.rst @@ -47,7 +47,7 @@ of undocumented behavior that may change in future releases. .. code-block:: cmake - foreach(loop_var IN [LISTS []] [ITEMS []]) + foreach( IN [LISTS []] [ITEMS []]) In this variant, ```` is a whitespace or semicolon separated list of list-valued variables. The ``foreach`` diff --git a/Source/cmForEachCommand.cxx b/Source/cmForEachCommand.cxx index 44392baa75..91cd4efd47 100644 --- a/Source/cmForEachCommand.cxx +++ b/Source/cmForEachCommand.cxx @@ -2,7 +2,12 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmForEachCommand.h" -#include +#include +#include +// NOTE The declaration of `std::abs` has moved to `cmath` since C++17 +// See https://en.cppreference.com/w/cpp/numeric/math/abs +// ALERT But IWYU used to lint `#include`s do not "understand" +// conditional compilation (i.e. `#if __cplusplus >= 201703L`) #include #include @@ -21,8 +26,6 @@ #include "cmSystemTools.h" namespace { -bool HandleInMode(std::vector const& args, cmMakefile& makefile); - class cmForEachFunctionBlocker : public cmFunctionBlocker { public: @@ -60,7 +63,8 @@ bool cmForEachFunctionBlocker::ArgumentsMatch(cmListFileFunction const& lff, { std::vector expandedArguments; mf.ExpandArguments(lff.Arguments, expandedArguments); - return expandedArguments.empty() || expandedArguments[0] == this->Args[0]; + return expandedArguments.empty() || + expandedArguments.front() == this->Args.front(); } bool cmForEachFunctionBlocker::Replay( @@ -70,13 +74,13 @@ bool cmForEachFunctionBlocker::Replay( // at end of for each execute recorded commands // store the old value std::string oldDef; - if (mf.GetDefinition(this->Args[0])) { - oldDef = mf.GetDefinition(this->Args[0]); + if (mf.GetDefinition(this->Args.front())) { + oldDef = mf.GetDefinition(this->Args.front()); } for (std::string const& arg : cmMakeRange(this->Args).advance(1)) { // set the variable to the loop value - mf.AddDefinition(this->Args[0], arg); + mf.AddDefinition(this->Args.front(), arg); // Invoke all the functions that were collected in the block. for (cmListFileFunction const& func : functions) { cmExecutionStatus status(mf); @@ -84,12 +88,12 @@ bool cmForEachFunctionBlocker::Replay( if (status.GetReturnInvoked()) { inStatus.SetReturnInvoked(); // restore the variable to its prior value - mf.AddDefinition(this->Args[0], oldDef); + mf.AddDefinition(this->Args.front(), oldDef); return true; } if (status.GetBreakInvoked()) { // restore the variable to its prior value - mf.AddDefinition(this->Args[0], oldDef); + mf.AddDefinition(this->Args.front(), oldDef); return true; } if (status.GetContinueInvoked()) { @@ -102,11 +106,48 @@ bool cmForEachFunctionBlocker::Replay( } // restore the variable to its prior value - mf.AddDefinition(this->Args[0], oldDef); + mf.AddDefinition(this->Args.front(), oldDef); return true; } + +bool HandleInMode(std::vector const& args, cmMakefile& makefile) +{ + auto fb = cm::make_unique(&makefile); + fb->Args.push_back(args.front()); + + enum Doing + { + DoingNone, + DoingLists, + DoingItems + }; + Doing doing = DoingNone; + for (std::string const& arg : cmMakeRange(args).advance(2)) { + if (arg == "LISTS") { + doing = DoingLists; + } else if (arg == "ITEMS") { + doing = DoingItems; + } else if (doing == DoingLists) { + auto const& value = makefile.GetSafeDefinition(arg); + if (!value.empty()) { + cmExpandList(value, fb->Args, true); + } + } else if (doing == DoingItems) { + fb->Args.push_back(arg); + } else { + makefile.IssueMessage(MessageType::FATAL_ERROR, + cmStrCat("Unknown argument:\n", " ", arg, "\n")); + return true; + } + } + + makefile.AddFunctionBlocker(std::move(fb)); + + return true; } +} // anonymous namespace + bool cmForEachCommand(std::vector const& args, cmExecutionStatus& status) { @@ -126,16 +167,16 @@ bool cmForEachCommand(std::vector const& args, int stop = 0; int step = 0; if (args.size() == 3) { - stop = atoi(args[2].c_str()); + stop = std::stoi(args[2]); } if (args.size() == 4) { - start = atoi(args[2].c_str()); - stop = atoi(args[3].c_str()); + start = std::stoi(args[2]); + stop = std::stoi(args[3]); } if (args.size() == 5) { - start = atoi(args[2].c_str()); - stop = atoi(args[3].c_str()); - step = atoi(args[4].c_str()); + start = std::stoi(args[2]); + stop = std::stoi(args[3]); + step = std::stoi(args[4]); } if (step == 0) { if (start > stop) { @@ -151,21 +192,24 @@ bool cmForEachCommand(std::vector const& args, ", stop ", stop, ", step ", step)); return false; } - std::vector range; - char buffer[100]; - range.push_back(args[0]); - int cc; - for (cc = start;; cc += step) { - if ((step > 0 && cc > stop) || (step < 0 && cc < stop)) { - break; - } - sprintf(buffer, "%d", cc); - range.emplace_back(buffer); - if (cc == stop) { - break; - } - } - fb->Args = range; + + // Calculate expected iterations count and reserve enough space + // in the `fb->Args` vector. The first item is the iteration variable + // name... + const std::size_t iter_cnt = 2u + + int(start < stop) * (stop - start) / std::abs(step) + + int(start > stop) * (start - stop) / std::abs(step); + fb->Args.resize(iter_cnt); + fb->Args.front() = args.front(); + auto cc = start; + auto generator = [&cc, step]() -> std::string { + auto result = std::to_string(cc); + cc += step; + return result; + }; + // Fill the `range` vector w/ generated string values + // (starting from 2nd position) + std::generate(++fb->Args.begin(), fb->Args.end(), generator); } else { fb->Args = args; } @@ -176,42 +220,3 @@ bool cmForEachCommand(std::vector const& args, return true; } - -namespace { -bool HandleInMode(std::vector const& args, cmMakefile& makefile) -{ - auto fb = cm::make_unique(&makefile); - fb->Args.push_back(args[0]); - - enum Doing - { - DoingNone, - DoingLists, - DoingItems - }; - Doing doing = DoingNone; - for (unsigned int i = 2; i < args.size(); ++i) { - if (doing == DoingItems) { - fb->Args.push_back(args[i]); - } else if (args[i] == "LISTS") { - doing = DoingLists; - } else if (args[i] == "ITEMS") { - doing = DoingItems; - } else if (doing == DoingLists) { - const char* value = makefile.GetDefinition(args[i]); - if (value && *value) { - cmExpandList(value, fb->Args, true); - } - } else { - makefile.IssueMessage( - MessageType::FATAL_ERROR, - cmStrCat("Unknown argument:\n", " ", args[i], "\n")); - return true; - } - } - - makefile.AddFunctionBlocker(std::move(fb)); - - return true; -} -} diff --git a/Tests/RunCMake/foreach/RunCMakeTest.cmake b/Tests/RunCMake/foreach/RunCMakeTest.cmake index 4b74cfe493..0f1fdd47d8 100644 --- a/Tests/RunCMake/foreach/RunCMakeTest.cmake +++ b/Tests/RunCMake/foreach/RunCMakeTest.cmake @@ -1,3 +1,4 @@ include(RunCMake) run_cmake(BadRangeInFunction) +run_cmake(foreach-all-test) diff --git a/Tests/RunCMake/foreach/foreach-all-test-stdout.txt b/Tests/RunCMake/foreach/foreach-all-test-stdout.txt new file mode 100644 index 0000000000..e8f622d6ee --- /dev/null +++ b/Tests/RunCMake/foreach/foreach-all-test-stdout.txt @@ -0,0 +1,44 @@ +-- foreach\(RANGE\): +-- \[0\.\.1\]/1 +-- < 0 +-- < 1 +-- \[1\.\.1\]/1 +-- < 1 +-- \[0\.\.10\]/2 +-- < 0 +-- < 2 +-- < 4 +-- < 6 +-- < 8 +-- < 10 +-- \[-10\.\.0\]/3 +-- < -10 +-- < -7 +-- < -4 +-- < -1 +-- \[0\.\.-10\]/-5 +-- < 0 +-- < -5 +-- < -10 +-- foreach\(IN ITEMS\): +-- < one +-- < two +-- < three +-- foreach\(IN LISTS\): +-- < satu +-- < dua +-- < tiga +-- foreach\(IN LISTS and ITEMS\): +-- < satu +-- < dua +-- < tiga +-- < one +-- < two +-- < three +-- foreach\(IN ITEMS and LISTS\): +-- < one +-- < two +-- < three +-- < satu +-- < dua +-- < tiga diff --git a/Tests/RunCMake/foreach/foreach-all-test.cmake b/Tests/RunCMake/foreach/foreach-all-test.cmake new file mode 100644 index 0000000000..2e377c8028 --- /dev/null +++ b/Tests/RunCMake/foreach/foreach-all-test.cmake @@ -0,0 +1,67 @@ +message(STATUS "foreach(RANGE):") +list(APPEND CMAKE_MESSAGE_INDENT " ") + +message(STATUS "[0..1]/1") +list(APPEND CMAKE_MESSAGE_INDENT " < ") +foreach(i RANGE 1) + message(STATUS ${i}) +endforeach() +list(POP_BACK CMAKE_MESSAGE_INDENT) + +message(STATUS "[1..1]/1") +list(APPEND CMAKE_MESSAGE_INDENT " < ") +foreach(i RANGE 1 1) + message(STATUS ${i}) +endforeach() +list(POP_BACK CMAKE_MESSAGE_INDENT) + +message(STATUS "[0..10]/2") +list(APPEND CMAKE_MESSAGE_INDENT " < ") +foreach(i RANGE 0 10 2) + message(STATUS ${i}) +endforeach() +list(POP_BACK CMAKE_MESSAGE_INDENT) + +message(STATUS "[-10..0]/3") +list(APPEND CMAKE_MESSAGE_INDENT " < ") +foreach(i RANGE -10 0 3) + message(STATUS ${i}) +endforeach() +list(POP_BACK CMAKE_MESSAGE_INDENT) + +message(STATUS "[0..-10]/-5") +list(APPEND CMAKE_MESSAGE_INDENT " < ") +foreach(i RANGE 0 -10 -5) + message(STATUS ${i}) +endforeach() +list(POP_BACK CMAKE_MESSAGE_INDENT) +list(POP_BACK CMAKE_MESSAGE_INDENT) + +message(STATUS "foreach(IN ITEMS):") +list(APPEND CMAKE_MESSAGE_INDENT " < ") +foreach(i IN ITEMS one two three) + message(STATUS ${i}) +endforeach() +list(POP_BACK CMAKE_MESSAGE_INDENT) + +message(STATUS "foreach(IN LISTS):") +list(APPEND CMAKE_MESSAGE_INDENT " < ") +list(APPEND count satu dua tiga) +foreach(i IN LISTS count) + message(STATUS ${i}) +endforeach() +list(POP_BACK CMAKE_MESSAGE_INDENT) + +message(STATUS "foreach(IN LISTS and ITEMS):") +list(APPEND CMAKE_MESSAGE_INDENT " < ") +foreach(i IN LISTS count ITEMS one two three) + message(STATUS ${i}) +endforeach() +list(POP_BACK CMAKE_MESSAGE_INDENT) + +message(STATUS "foreach(IN ITEMS and LISTS):") +list(APPEND CMAKE_MESSAGE_INDENT " < ") +foreach(i IN ITEMS one two three LISTS count) + message(STATUS ${i}) +endforeach() +list(POP_BACK CMAKE_MESSAGE_INDENT) diff --git a/Utilities/IWYU/mapping.imp b/Utilities/IWYU/mapping.imp index ef31e8befe..5e4c7ba586 100644 --- a/Utilities/IWYU/mapping.imp +++ b/Utilities/IWYU/mapping.imp @@ -24,6 +24,7 @@ { include: [ "", private, "", public ] }, { include: [ "", private, "", public ] }, { include: [ "", private, "", public ] }, + { include: [ "", private, "", public ] }, { include: [ "", private, "", public ] }, { include: [ "", private, "", public ] }, { include: [ "", private, "", public ] },