diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index c0af12049..9d766c2e0 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -148,9 +148,9 @@ static bool isVariableComparison(const Token *tok, const std::string &comp, cons // Invert comparator std::string s = tok->str(); if (s[0] == '>') - s[1] = '<'; + s[0] = '<'; else if (s[0] == '<') - s[1] = '>'; + s[0] = '>'; if (s == comp) { *vartok = tok->astOperand2(); } diff --git a/tools/extract_and_run_more_tests.sh b/tools/extract_and_run_more_tests.sh new file mode 100755 index 000000000..c564f9f17 --- /dev/null +++ b/tools/extract_and_run_more_tests.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +cd ~/cppcheck +rm -rf test1 +python tools/extracttests.py --code=test1 test/testleakautovar.cpp +cd ~/cppcheck/test1 +~/cppcheck/tools/run_more_tests.sh + diff --git a/tools/extracttests.py b/tools/extracttests.py index d8c4ff729..41dcc8948 100755 --- a/tools/extracttests.py +++ b/tools/extracttests.py @@ -47,6 +47,7 @@ class Extract: testclass = None functionName = None + code = None fin = open(filename, 'r') for line in fin: @@ -76,19 +77,20 @@ class Extract: code = res.group(1) # code.. - res = re.match('\\s+' + string, line) - if res is not None: - code = code + res.group(1) + if code is not None: + res = re.match('\\s+' + string, line) + if res is not None: + code = code + res.group(1) # assert res = re.match('\\s+ASSERT_EQUALS\\(\\"([^"]*)\\",', line) - if res is not None and len(code) > 10: + if res is not None and code is not None: node = {'testclass': testclass, 'functionName': functionName, 'code': code, 'expected': res.group(1)} self.nodes.append(node) - code = '' + code = None # close test file fin.close() diff --git a/tools/run_more_tests.sh b/tools/run_more_tests.sh new file mode 100755 index 000000000..46baac8eb --- /dev/null +++ b/tools/run_more_tests.sh @@ -0,0 +1,89 @@ +#!/bin/bash + +~/cppcheck/cppcheck -q . 2> 1.txt + + +# (!x) => (x==0) +sed -ri 's/([(&][ ]*)\!([a-z]+)([ ]*[&)])/\1\2==0\3/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (x==0) => (0==x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*==[ ]*0([ ]*[&)])/\10==\2\3/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (0==x) => (!x) +sed -ri 's/([(&][ ]*)0[ ]*==[ ]*([a-z]+)([ ]*[&)])/\1!\2\3/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + + + + +# if (x) => (x!=0) +sed -ri 's/(if[ ]*\([ ]*[a-z]+)([ ]*[&)])/\1!=0\2/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# while (x) => (x!=0) +sed -ri 's/(while[ ]*\([ ]*[a-z]+)([ ]*[&)])/\1!=0\2/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (x!=0) => (0!=x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*!=[ ]*0([ ]*[&)])/\10!=\2\3/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (0!=x) => (x) +sed -ri 's/([(&][ ]*)0[ ]*!=[ ]*([a-z]+[ ]*[&)])/\1\2/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + + +# (x < 0) => (0 > x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*<[ ]*(\-?[0-9]+)([ ]*[&)])/\1\3>\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (x <= 0) => (0 >= x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*<=[ ]*(\-?[0-9]+)([ ]*[&)])/\1\3>=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (x > 0) => (0 < x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*<=[ ]*(\-?[0-9]+)([ ]*[&)])/\1\3>=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (x >= 0) => (0 <= x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*<=[ ]*(\-?[0-9]+)([ ]*[&)])/\1\3>=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (x == 123) => (123 == x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*==[ ]*(\-?[0-9]+)([ ]*[&)])/\1\3==\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (x != 123) => (123 != x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*\!=[ ]*(\-?[0-9]+)([ ]*[&)])/\1\3!=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + + + +# (0 < x) => (x > 0) +sed -ri 's/([(&][ ]*)(\-?[0-9]+)[ ]*<[ ]*([a-z]+)([ ]*[&)])/\1\3>\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (0 <= x) => (x >= 0) +sed -ri 's/([(&][ ]*)(\-?[0-9]+)[ ]*<=[ ]*([a-z]+)([ ]*[&)])/\1\3>=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (0 > x) => (x < 0) +sed -ri 's/([(&][ ]*)(\-?[0-9]+)[ ]*<=[ ]*([a-z]+)([ ]*[&)])/\1\3>=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (0 >= x) => (x <= 0) +sed -ri 's/([(&][ ]*)(\-?[0-9]+)[ ]*<=[ ]*([a-z]+)([ ]*[&)])/\1\3>=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (123 == x) => (x == 123) +sed -ri 's/([(&][ ]*)(\-?[0-9]+)[ ]*==[ ]*([a-z]+)([ ]*[&)])/\1\3==\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (123 != x) => (x <= 123) +sed -ri 's/([(&][ ]*)(\-?[0-9]+)[ ]*\!=[ ]*([a-z]+)([ ]*[&)])/\1\3!=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + + +