From ac4c2f675a5607839a639d2297002c85351bfbe7 Mon Sep 17 00:00:00 2001 From: Ken Martin Date: Thu, 3 Jan 2008 11:22:33 -0500 Subject: [PATCH] ENH: change raise_scope signature to be safer for returned varuables --- Source/cmMakefile.cxx | 7 ++-- Source/cmMakefile.h | 2 +- Source/cmRaiseScopeCommand.cxx | 16 +++++++-- Source/cmRaiseScopeCommand.h | 9 ++--- Tests/FunctionTest/CMakeLists.txt | 33 +++++++++++++++---- Tests/FunctionTest/SubDirScope/CMakeLists.txt | 3 +- Tests/FunctionTest/Util.cmake | 4 +++ 7 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 Tests/FunctionTest/Util.cmake diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index 49fc0f1c4..c74cf4d86 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -2846,9 +2846,12 @@ void cmMakefile::PopScope() this->DefinitionStack.pop_back(); } -void cmMakefile::RaiseScope(const char *var) +void cmMakefile::RaiseScope(const char *var, const char *varDef) { - const char *varDef = this->GetDefinition(var); + if (!var || !strlen(var)) + { + return; + } // multiple scopes in this directory? if (this->DefinitionStack.size() > 1) diff --git a/Source/cmMakefile.h b/Source/cmMakefile.h index 2d4e61f76..3fa99513e 100644 --- a/Source/cmMakefile.h +++ b/Source/cmMakefile.h @@ -740,7 +740,7 @@ public: // push and pop variable scopes void PushScope(); void PopScope(); - void RaiseScope(const char *var); + void RaiseScope(const char *var, const char *value); protected: // add link libraries and directories to the target diff --git a/Source/cmRaiseScopeCommand.cxx b/Source/cmRaiseScopeCommand.cxx index ffaff8ebd..adf87d803 100644 --- a/Source/cmRaiseScopeCommand.cxx +++ b/Source/cmRaiseScopeCommand.cxx @@ -20,10 +20,20 @@ bool cmRaiseScopeCommand ::InitialPass(std::vector const& args) { - unsigned int i =0; - for(; i < args.size(); ++i) + if (args.size() < 1) { - this->Makefile->RaiseScope(args[i].c_str()); + this->SetError("called with incorrect number of arguments, " + "raise scope must have at least one argument"); + return false; + } + + if (args.size() == 1) + { + this->Makefile->RaiseScope(args[0].c_str(), 0); + } + else + { + this->Makefile->RaiseScope(args[0].c_str(), args[1].c_str()); } return true; } diff --git a/Source/cmRaiseScopeCommand.h b/Source/cmRaiseScopeCommand.h index 51d4f1ff2..0ff2e4e36 100644 --- a/Source/cmRaiseScopeCommand.h +++ b/Source/cmRaiseScopeCommand.h @@ -61,12 +61,13 @@ public: virtual const char* GetFullDocumentation() { return - " raise_scope(VAR VAR2 VAR...)\n" - "Pushes the current state of a variable into the scope above the " + " raise_scope(VAR [VALUE])\n" + "Sets the value of a variable in the scope above the " "current scope. Each new directory or function creates a new scope. " - "This command will push the current state of a variable into the " + "This command will set the value of a variable into the " "parent directory or calling function (whichever is applicable to " - "the case at hand)"; + "the case at hand) If VALUE is not specified then the variable is " + "removed from the parent scope."; } /** diff --git a/Tests/FunctionTest/CMakeLists.txt b/Tests/FunctionTest/CMakeLists.txt index 5ab9bcd92..1efdef1a8 100644 --- a/Tests/FunctionTest/CMakeLists.txt +++ b/Tests/FunctionTest/CMakeLists.txt @@ -44,16 +44,37 @@ FUNCTION(test_argn_function argument) ENDFUNCTION(test_argn_function) Test_Argn_Function(ignored 3) +# test argument naming and raise scope +function(track_find_variable cache_variable is_changed) + raise_scope("${is_changed}" changed) +endfunction(track_find_variable) +track_find_variable(testvar is_changed) +if ("${is_changed}" STREQUAL changed) + pass("same argument name test") +else ("${is_changed}" STREQUAL changed) + pass("same argument name test") +endif ("${is_changed}" STREQUAL changed) + +include("Util.cmake") +tester() +if (tester_res STREQUAL "${CMAKE_CURRENT_LIST_FILE}") + pass("CMAKE_CURRENT_LIST_FILE test") +else (tester_res STREQUAL "${CMAKE_CURRENT_LIST_FILE}") + pass("CMAKE_CURRENT_LIST_FILE test") +endif (tester_res STREQUAL "${CMAKE_CURRENT_LIST_FILE}") + + + # test recursion and return via raise_scope function (factorial argument result) if (argument LESS 2) - set (${result} 1) + set (lresult 1) else (argument LESS 2) math (EXPR temp "${argument} - 1") factorial (${temp} tresult) - math (EXPR ${result} "${argument}*${tresult}") + math (EXPR lresult "${argument}*${tresult}") endif (argument LESS 2) - raise_scope (${result}) + raise_scope ("${result}" "${lresult}") endfunction (factorial) factorial (5 fresult) @@ -67,8 +88,7 @@ endif (fresult EQUAL 120) # case test FUNCTION(strange_function m) - SET(${m} strange_function) - RAISE_SCOPE(${m}) + RAISE_SCOPE("${m}" strange_function) ENDFUNCTION(strange_function m) STRANGE_FUNCTION(var) set(second_var "second_var") @@ -85,8 +105,7 @@ ENDFUNCTION(ADD_EXECUTABLE) # var undef case FUNCTION(undef_var m) - SET(${m}) - RAISE_SCOPE(${m}) + RAISE_SCOPE("${m}") ENDFUNCTION(undef_var) SET(FUNCTION_UNDEFINED 1) undef_var(FUNCTION_UNDEFINED) diff --git a/Tests/FunctionTest/SubDirScope/CMakeLists.txt b/Tests/FunctionTest/SubDirScope/CMakeLists.txt index 174b9b006..9241941e9 100644 --- a/Tests/FunctionTest/SubDirScope/CMakeLists.txt +++ b/Tests/FunctionTest/SubDirScope/CMakeLists.txt @@ -1,3 +1,4 @@ SET(SUBDIR_DEFINED 1) SET(SUBDIR_UNDEFINED) -RAISE_SCOPE(SUBDIR_DEFINED SUBDIR_UNDEFINED) +RAISE_SCOPE(SUBDIR_DEFINED ${SUBDIR_DEFINED}) +RAISE_SCOPE(SUBDIR_UNDEFINED ${SUBDIR_UNDEFINED}) diff --git a/Tests/FunctionTest/Util.cmake b/Tests/FunctionTest/Util.cmake new file mode 100644 index 000000000..2b40cdf40 --- /dev/null +++ b/Tests/FunctionTest/Util.cmake @@ -0,0 +1,4 @@ +function(tester) + set (tester_res "${CMAKE_CURRENT_LIST_FILE}") + raise_scope(tester_res) +endfunction(tester)