I'm working on a test script. I want a particular test to run when another test does not return a failure. Here's is the basic test I am using, and its assigned to a shell script variable:
cpp -dM -fsanitize=undefined < /dev/null 2>&1 | grep -i -c error
If its GCC 4.8 or Clang 3.2 (or above), then the Undefined Behavior sanitizer is available and the value returned from
grep is 0. If the compiler lacks the support, then the value is non-0. For example:
# Clang 3.1 $ cpp -dM -fsanitize=undefined < /dev/null clang: error: unsupported argument 'undefined' to option 'fsanitize='
If I try to use
grep -v -c, then lines not matching "error" are counted, which effectively counts the lines dumped by the preprocessor. So I get a result of 248 (no error) and 1 (with error), but I never get a 0-value on failure.
Later, for readability, I perform:
if [ "$HAVE_UBSAN" -ne "0" ]; then export CXXFLAGS="-DNDEBUG -g2 -O2 -fsanitize=undefined $MY_CXXFLAGS" make ... # Run self tests, scrape output for failures fi
The problem is I must negate the count returned from the test
grep -c error to maintain readability. That is, I must turn the result into 0 (does not have) or non-0 (does have).
In C/C++, I would perform the equivalent to (notice the
! on the expression):
!(`cpp -dM -fsanitize=undefined < /dev/null 2>&1 | grep -i -c error`)
How do I invert the count returned from
I don't have this problem with the Valgrind test because I can use the count directly:
HAVE_VALGRIND=`which valgrind | grep -i -c valgrind`