[CMake] Regex help: multi-line matching and matching backslashes

Ben Medina ben.medina at gmail.com
Fri Jan 7 14:55:17 EST 2011


I need to parse a C++ file for Google Test macros. (I'm aware the
GTEST_ADD_TESTS provided by FindGtest.cmake, but I need the test list
for my own purposes). I had been using a regex similar to the one in
GTEST_ADD_TESTS to match tests:

string (REGEX MATCHALL "TEST_?F?\\([A-Za-z_0-9 ,]+)\\)" found_tests ${contents})

But this doesn't work when the test is split onto two lines, like this:

TEST(SampleTest,\
     MultilineTest)

So, I've been trying to build a regex that will match tests split onto
multiple lines. This should work:

string (REGEX MATCHALL "TEST_?F?\\([A-Za-z_0-9
]+,[\\s\\\\]*[A-Za-z_0-9 ]+\\)" found_tests ${contents})

After the comma, the regex should greedily match all whitespace
(including newlines) via the "\\s" and backslashes via the "\\\\"
(Note that all special characters are escaped). This works in other
regex engines, but fails in CMake.

Is this possible in CMake, or do I need to use another tool?

Here is my test file (named tests.cpp):

TEST(SampleTest, SingleLineTest)
TEST(SampleTest,\
     MultilineTest)
TEST_F(SampleTest, SingleLineFixtureTest)
TEST_F(SampleTest,\
     MultilineFixtureTest)

And my CMakeLists.txt:

cmake_minimum_required (VERSION 2.8)

file (READ "tests.cpp" contents)
string (REGEX MATCHALL "TEST_?F?\\([A-Za-z_0-9
]+,[\\s\\\\]*[A-Za-z_0-9 ]+\\)" found_tests ${contents})
message ("Found tests:")
foreach (test ${found_tests})
    message ("${test}")
endforeach ()

Thanks,
Ben


More information about the CMake mailing list