cmWhileCommand.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmWhileCommand.h"
  4. #include "cm_memory.hxx"
  5. #include "cmConditionEvaluator.h"
  6. #include "cmExecutionStatus.h"
  7. #include "cmExpandedCommandArgument.h"
  8. #include "cmMakefile.h"
  9. #include "cmMessageType.h"
  10. #include "cmSystemTools.h"
  11. #include <string>
  12. #include <utility>
  13. cmWhileFunctionBlocker::cmWhileFunctionBlocker(cmMakefile* mf)
  14. : Makefile(mf)
  15. , Depth(0)
  16. {
  17. this->Makefile->PushLoopBlock();
  18. }
  19. cmWhileFunctionBlocker::~cmWhileFunctionBlocker()
  20. {
  21. this->Makefile->PopLoopBlock();
  22. }
  23. bool cmWhileFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff,
  24. cmMakefile& mf,
  25. cmExecutionStatus& inStatus)
  26. {
  27. // at end of for each execute recorded commands
  28. if (lff.Name.Lower == "while") {
  29. // record the number of while commands past this one
  30. this->Depth++;
  31. } else if (lff.Name.Lower == "endwhile") {
  32. // if this is the endwhile for this while loop then execute
  33. if (!this->Depth) {
  34. // Remove the function blocker for this scope or bail.
  35. std::unique_ptr<cmFunctionBlocker> fb(
  36. mf.RemoveFunctionBlocker(this, lff));
  37. if (!fb) {
  38. return false;
  39. }
  40. std::string errorString;
  41. std::vector<cmExpandedCommandArgument> expandedArguments;
  42. mf.ExpandArguments(this->Args, expandedArguments);
  43. MessageType messageType;
  44. cmListFileContext execContext = this->GetStartingContext();
  45. cmCommandContext commandContext;
  46. commandContext.Line = execContext.Line;
  47. commandContext.Name = execContext.Name;
  48. cmConditionEvaluator conditionEvaluator(mf, this->GetStartingContext(),
  49. mf.GetBacktrace(commandContext));
  50. bool isTrue =
  51. conditionEvaluator.IsTrue(expandedArguments, errorString, messageType);
  52. while (isTrue) {
  53. if (!errorString.empty()) {
  54. std::string err = "had incorrect arguments: ";
  55. for (cmListFileArgument const& arg : this->Args) {
  56. err += (arg.Delim ? "\"" : "");
  57. err += arg.Value;
  58. err += (arg.Delim ? "\"" : "");
  59. err += " ";
  60. }
  61. err += "(";
  62. err += errorString;
  63. err += ").";
  64. mf.IssueMessage(messageType, err);
  65. if (messageType == MessageType::FATAL_ERROR) {
  66. cmSystemTools::SetFatalErrorOccured();
  67. return true;
  68. }
  69. }
  70. // Invoke all the functions that were collected in the block.
  71. for (cmListFileFunction const& fn : this->Functions) {
  72. cmExecutionStatus status(mf);
  73. mf.ExecuteCommand(fn, status);
  74. if (status.GetReturnInvoked()) {
  75. inStatus.SetReturnInvoked();
  76. return true;
  77. }
  78. if (status.GetBreakInvoked()) {
  79. return true;
  80. }
  81. if (status.GetContinueInvoked()) {
  82. break;
  83. }
  84. if (cmSystemTools::GetFatalErrorOccured()) {
  85. return true;
  86. }
  87. }
  88. expandedArguments.clear();
  89. mf.ExpandArguments(this->Args, expandedArguments);
  90. isTrue = conditionEvaluator.IsTrue(expandedArguments, errorString,
  91. messageType);
  92. }
  93. return true;
  94. }
  95. // decrement for each nested while that ends
  96. this->Depth--;
  97. }
  98. // record the command
  99. this->Functions.push_back(lff);
  100. // always return true
  101. return true;
  102. }
  103. bool cmWhileFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  104. cmMakefile&)
  105. {
  106. if (lff.Name.Lower == "endwhile") {
  107. // if the endwhile has arguments, then make sure
  108. // they match the arguments of the matching while
  109. if (lff.Arguments.empty() || lff.Arguments == this->Args) {
  110. return true;
  111. }
  112. }
  113. return false;
  114. }
  115. bool cmWhileCommand(std::vector<cmListFileArgument> const& args,
  116. cmExecutionStatus& status)
  117. {
  118. if (args.empty()) {
  119. status.SetError("called with incorrect number of arguments");
  120. return false;
  121. }
  122. // create a function blocker
  123. {
  124. cmMakefile& makefile = status.GetMakefile();
  125. auto fb = cm::make_unique<cmWhileFunctionBlocker>(&makefile);
  126. fb->Args = args;
  127. makefile.AddFunctionBlocker(std::move(fb));
  128. }
  129. return true;
  130. }