Merge topic 'cached-regex-clear-fixed'

ceecd790 cmMakefile: store the number of last matches in a CMake var
7878d061 test: add a test for clearing regex results
This commit is contained in:
Brad King 2014-12-04 10:23:26 -05:00 committed by CMake Topic Stage
commit c7bd275393
13 changed files with 199 additions and 43 deletions

View File

@ -47,6 +47,7 @@ Variables that Provide Information
/variable/CMAKE_LINK_LIBRARY_SUFFIX /variable/CMAKE_LINK_LIBRARY_SUFFIX
/variable/CMAKE_MAJOR_VERSION /variable/CMAKE_MAJOR_VERSION
/variable/CMAKE_MAKE_PROGRAM /variable/CMAKE_MAKE_PROGRAM
/variable/CMAKE_MATCH_COUNT
/variable/CMAKE_MINIMUM_REQUIRED_VERSION /variable/CMAKE_MINIMUM_REQUIRED_VERSION
/variable/CMAKE_MINOR_VERSION /variable/CMAKE_MINOR_VERSION
/variable/CMAKE_PARENT_LIST_FILE /variable/CMAKE_PARENT_LIST_FILE

View File

@ -0,0 +1,5 @@
cached-regex-clear-fixed
------------------------
* Add :variable:`CMAKE_MATCH_COUNT` for the number of matches made in the last
regular expression.

View File

@ -0,0 +1,8 @@
CMAKE_MATCH_COUNT
-----------------
The number of matches with the last regular expression.
When a regular expression match is used, CMake fills in ``CMAKE_MATCH_<n>``
variables with the match contents. The ``CMAKE_MATCH_COUNT`` variable holds
the number of match expressions when these are filled.

View File

@ -11,7 +11,6 @@
============================================================================*/ ============================================================================*/
#include "cmConditionEvaluator.h" #include "cmConditionEvaluator.h"
#include "cmStringCommand.h"
cmConditionEvaluator::cmConditionEvaluator(cmMakefile& makefile): cmConditionEvaluator::cmConditionEvaluator(cmMakefile& makefile):
Makefile(makefile), Makefile(makefile),
@ -556,7 +555,7 @@ bool cmConditionEvaluator::HandleLevel2(cmArgumentList &newArgs,
{ {
def = this->GetVariableOrString(*arg); def = this->GetVariableOrString(*arg);
const char* rex = argP2->c_str(); const char* rex = argP2->c_str();
cmStringCommand::ClearMatches(&this->Makefile); this->Makefile.ClearMatches();
cmsys::RegularExpression regEntry; cmsys::RegularExpression regEntry;
if ( !regEntry.compile(rex) ) if ( !regEntry.compile(rex) )
{ {
@ -568,7 +567,7 @@ bool cmConditionEvaluator::HandleLevel2(cmArgumentList &newArgs,
} }
if (regEntry.find(def)) if (regEntry.find(def))
{ {
cmStringCommand::StoreMatches(&this->Makefile, regEntry); this->Makefile.StoreMatches(regEntry);
*arg = cmExpandedCommandArgument("1", true); *arg = cmExpandedCommandArgument("1", true);
} }
else else

View File

@ -4851,6 +4851,64 @@ std::vector<cmSourceFile*> cmMakefile::GetQtUiFilesWithOptions() const
return this->QtUiFilesWithOptions; return this->QtUiFilesWithOptions;
} }
static std::string const matchVariables[] = {
"CMAKE_MATCH_0",
"CMAKE_MATCH_1",
"CMAKE_MATCH_2",
"CMAKE_MATCH_3",
"CMAKE_MATCH_4",
"CMAKE_MATCH_5",
"CMAKE_MATCH_6",
"CMAKE_MATCH_7",
"CMAKE_MATCH_8",
"CMAKE_MATCH_9"
};
static std::string const nMatchesVariable = "CMAKE_MATCH_COUNT";
//----------------------------------------------------------------------------
void cmMakefile::ClearMatches()
{
const char* nMatchesStr = this->GetDefinition(nMatchesVariable);
if (!nMatchesStr)
{
return;
}
int nMatches = atoi(nMatchesStr);
for (int i=0; i<=nMatches; i++)
{
std::string const& var = matchVariables[i];
std::string const& s = this->GetSafeDefinition(var);
if(!s.empty())
{
this->AddDefinition(var, "");
this->MarkVariableAsUsed(var);
}
}
this->AddDefinition(nMatchesVariable, "0");
this->MarkVariableAsUsed(nMatchesVariable);
}
//----------------------------------------------------------------------------
void cmMakefile::StoreMatches(cmsys::RegularExpression& re)
{
char highest = 0;
for (int i=0; i<10; i++)
{
std::string const& m = re.match(i);
if(!m.empty())
{
std::string const& var = matchVariables[i];
this->AddDefinition(var, m.c_str());
this->MarkVariableAsUsed(var);
highest = static_cast<char>('0' + i);
}
}
char nMatches[] = {highest, '\0'};
this->AddDefinition(nMatchesVariable, nMatches);
this->MarkVariableAsUsed(nMatchesVariable);
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
cmPolicies::PolicyStatus cmPolicies::PolicyStatus
cmMakefile::GetPolicyStatus(cmPolicies::PolicyID id) const cmMakefile::GetPolicyStatus(cmPolicies::PolicyID id) const

View File

@ -976,6 +976,9 @@ public:
void PopLoopBlock(); void PopLoopBlock();
bool IsLoopBlock() const; bool IsLoopBlock() const;
void ClearMatches();
void StoreMatches(cmsys::RegularExpression& re);
protected: protected:
// add link libraries and directories to the target // add link libraries and directories to the target
void AddGlobalLinkInformation(const std::string& name, cmTarget& target); void AddGlobalLinkInformation(const std::string& name, cmTarget& target);

View File

@ -310,7 +310,7 @@ bool cmStringCommand::RegexMatch(std::vector<std::string> const& args)
input += args[i]; input += args[i];
} }
this->ClearMatches(this->Makefile); this->Makefile->ClearMatches();
// Compile the regular expression. // Compile the regular expression.
cmsys::RegularExpression re; cmsys::RegularExpression re;
if(!re.compile(regex.c_str())) if(!re.compile(regex.c_str()))
@ -325,7 +325,7 @@ bool cmStringCommand::RegexMatch(std::vector<std::string> const& args)
std::string output; std::string output;
if(re.find(input.c_str())) if(re.find(input.c_str()))
{ {
this->StoreMatches(this->Makefile, re); this->Makefile->StoreMatches(re);
std::string::size_type l = re.start(); std::string::size_type l = re.start();
std::string::size_type r = re.end(); std::string::size_type r = re.end();
if(r-l == 0) if(r-l == 0)
@ -359,7 +359,7 @@ bool cmStringCommand::RegexMatchAll(std::vector<std::string> const& args)
input += args[i]; input += args[i];
} }
this->ClearMatches(this->Makefile); this->Makefile->ClearMatches();
// Compile the regular expression. // Compile the regular expression.
cmsys::RegularExpression re; cmsys::RegularExpression re;
if(!re.compile(regex.c_str())) if(!re.compile(regex.c_str()))
@ -376,7 +376,7 @@ bool cmStringCommand::RegexMatchAll(std::vector<std::string> const& args)
const char* p = input.c_str(); const char* p = input.c_str();
while(re.find(p)) while(re.find(p))
{ {
this->StoreMatches(this->Makefile, re); this->Makefile->StoreMatches(re);
std::string::size_type l = re.start(); std::string::size_type l = re.start();
std::string::size_type r = re.end(); std::string::size_type r = re.end();
if(r-l == 0) if(r-l == 0)
@ -463,7 +463,7 @@ bool cmStringCommand::RegexReplace(std::vector<std::string> const& args)
input += args[i]; input += args[i];
} }
this->ClearMatches(this->Makefile); this->Makefile->ClearMatches();
// Compile the regular expression. // Compile the regular expression.
cmsys::RegularExpression re; cmsys::RegularExpression re;
if(!re.compile(regex.c_str())) if(!re.compile(regex.c_str()))
@ -480,7 +480,7 @@ bool cmStringCommand::RegexReplace(std::vector<std::string> const& args)
std::string::size_type base = 0; std::string::size_type base = 0;
while(re.find(input.c_str()+base)) while(re.find(input.c_str()+base))
{ {
this->StoreMatches(this->Makefile, re); this->Makefile->StoreMatches(re);
std::string::size_type l2 = re.start(); std::string::size_type l2 = re.start();
std::string::size_type r = re.end(); std::string::size_type r = re.end();
@ -540,38 +540,6 @@ bool cmStringCommand::RegexReplace(std::vector<std::string> const& args)
return true; return true;
} }
//----------------------------------------------------------------------------
void cmStringCommand::ClearMatches(cmMakefile* mf)
{
for (unsigned int i=0; i<10; i++)
{
char name[128];
sprintf(name, "CMAKE_MATCH_%d", i);
const char* s = mf->GetDefinition(name);
if(s && *s != 0)
{
mf->AddDefinition(name, "");
mf->MarkVariableAsUsed(name);
}
}
}
//----------------------------------------------------------------------------
void cmStringCommand::StoreMatches(cmMakefile* mf,cmsys::RegularExpression& re)
{
for (unsigned int i=0; i<10; i++)
{
std::string m = re.match(i);
if(m.size() > 0)
{
char name[128];
sprintf(name, "CMAKE_MATCH_%d", i);
mf->AddDefinition(name, re.match(i).c_str());
mf->MarkVariableAsUsed(name);
}
}
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
bool cmStringCommand::HandleFindCommand(std::vector<std::string> const& bool cmStringCommand::HandleFindCommand(std::vector<std::string> const&
args) args)

View File

@ -53,8 +53,6 @@ public:
virtual std::string GetName() const { return "string";} virtual std::string GetName() const { return "string";}
cmTypeMacro(cmStringCommand, cmCommand); cmTypeMacro(cmStringCommand, cmCommand);
static void ClearMatches(cmMakefile* mf);
static void StoreMatches(cmMakefile* mf, cmsys::RegularExpression& re);
protected: protected:
bool HandleConfigureCommand(std::vector<std::string> const& args); bool HandleConfigureCommand(std::vector<std::string> const& args);
bool HandleAsciiCommand(std::vector<std::string> const& args); bool HandleAsciiCommand(std::vector<std::string> const& args);

View File

@ -0,0 +1,54 @@
^Matched string properly
results from: setting up initial state
CMAKE_MATCH_0: -->01<--
CMAKE_MATCH_1: -->0<--
CMAKE_MATCH_2: -->1<--
CMAKE_MATCH_COUNT: -->2<--
Matched string properly
results from: making a match inside of find_package
CMAKE_MATCH_0: -->01<--
CMAKE_MATCH_1: -->0<--
CMAKE_MATCH_2: -->1<--
CMAKE_MATCH_COUNT: -->2<--
Matched nothing properly
results from: making a failure inside of find_package
CMAKE_MATCH_0: --><--
CMAKE_MATCH_1: --><--
CMAKE_MATCH_2: --><--
CMAKE_MATCH_COUNT: -->0<--
Matched nothing properly
results from: checking after find_package
CMAKE_MATCH_0: --><--
CMAKE_MATCH_1: --><--
CMAKE_MATCH_2: --><--
CMAKE_MATCH_COUNT: -->0<--
Matched nothing properly
results from: clearing out results with a failing match
CMAKE_MATCH_0: --><--
CMAKE_MATCH_1: --><--
CMAKE_MATCH_2: --><--
CMAKE_MATCH_COUNT: -->0<--
Matched string properly
results from: making a successful match before add_subdirectory
CMAKE_MATCH_0: -->01<--
CMAKE_MATCH_1: -->0<--
CMAKE_MATCH_2: -->1<--
CMAKE_MATCH_COUNT: -->2<--
Matched string properly
results from: check for success in add_subdirectory
CMAKE_MATCH_0: -->01<--
CMAKE_MATCH_1: -->0<--
CMAKE_MATCH_2: -->1<--
CMAKE_MATCH_COUNT: -->2<--
Matched nothing properly
results from: failing inside of add_subdirectory
CMAKE_MATCH_0: --><--
CMAKE_MATCH_1: --><--
CMAKE_MATCH_2: --><--
CMAKE_MATCH_COUNT: -->0<--
Matched string properly
results from: ensuring the subdirectory did not interfere with the parent
CMAKE_MATCH_0: -->01<--
CMAKE_MATCH_1: -->0<--
CMAKE_MATCH_2: -->1<--
CMAKE_MATCH_COUNT: -->2<--$

View File

@ -0,0 +1,54 @@
cmake_minimum_required (VERSION 3.0)
project (RegexClear C)
function (output_results msg)
message("results from: ${msg}")
message("CMAKE_MATCH_0: -->${CMAKE_MATCH_0}<--")
message("CMAKE_MATCH_1: -->${CMAKE_MATCH_1}<--")
message("CMAKE_MATCH_2: -->${CMAKE_MATCH_2}<--")
message("CMAKE_MATCH_COUNT: -->${CMAKE_MATCH_COUNT}<--")
endfunction ()
function (check_for_success msg)
if (CMAKE_MATCH_1 STREQUAL "0" AND
CMAKE_MATCH_2 STREQUAL "1")
message("Matched string properly")
else ()
message("Failed to match properly")
endif ()
output_results("${msg}")
endfunction ()
function (check_for_failure msg)
if (CMAKE_MATCH_1 STREQUAL "" AND
CMAKE_MATCH_2 STREQUAL "")
message("Matched nothing properly")
else ()
message("Found a match where there should be none")
endif ()
output_results("${msg}")
endfunction ()
macro (do_regex_success msg)
string(REGEX MATCH "(0)(1)" output "01")
check_for_success("${msg}")
endmacro ()
macro (do_regex_failure msg)
string(REGEX MATCH "(0)(1)" output "12")
check_for_failure("${msg}")
endmacro ()
do_regex_success("setting up initial state")
list(INSERT CMAKE_MODULE_PATH 0
"${CMAKE_CURRENT_SOURCE_DIR}/cmake")
find_package(dummy) # Ensure cmMakefile::PushScope/PopScope work.
check_for_failure("checking after find_package")
do_regex_failure("clearing out results with a failing match")
do_regex_success("making a successful match before add_subdirectory")
add_subdirectory(subdir)
check_for_success("ensuring the subdirectory did not interfere with the parent") # Ensure that the subdir didn't mess with this scope.

View File

@ -10,3 +10,5 @@ run_cmake(UuidBadNamespace)
run_cmake(UuidMissingNameValue) run_cmake(UuidMissingNameValue)
run_cmake(UuidMissingTypeValue) run_cmake(UuidMissingTypeValue)
run_cmake(UuidBadType) run_cmake(UuidBadType)
run_cmake(RegexClear)

View File

@ -0,0 +1,4 @@
check_for_success("making a match inside of find_package")
do_regex_failure("making a failure inside of find_package")
set(dummy_FOUND 1)

View File

@ -0,0 +1,2 @@
check_for_success("check for success in add_subdirectory")
do_regex_failure("failing inside of add_subdirectory")