From c1ea873e1c671af4a4c885323f311cb580cb55a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 8 Jun 2017 08:15:05 +0200 Subject: [PATCH] TemplateSimplifier::simplifyCalculations: Add some more tests and make sure they pass --- lib/templatesimplifier.cpp | 6 +++--- test/testtokenize.cpp | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 813b14752..bffdc97b9 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -1152,10 +1152,10 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens) ret = true; } - if (Token::simpleMatch(tok->previous(), "( 0 |") || - Token::simpleMatch(tok->previous(), "| 0 )")) { + if (Token::Match(tok->previous(), "( 0 [|+]") || + Token::Match(tok->previous(), "[|+-] 0 )")) { tok = tok->previous(); - if (tok->str() == "|") + if (Token::Match(tok, "[|+-]")) tok = tok->previous(); tok->deleteNext(2); ret = true; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 7e4a53687..333473293 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -5753,6 +5753,18 @@ private: void simplifyCalculations() { ASSERT_EQUALS("void foo ( char str [ ] ) { char x ; x = * str ; }", tokenizeAndStringify("void foo ( char str [ ] ) { char x = 0 | ( * str ) ; }", true)); + ASSERT_EQUALS("void foo ( ) { if ( b ) { } }", + tokenizeAndStringify("void foo ( ) { if (b + 0) { } }", true)); + ASSERT_EQUALS("void foo ( ) { if ( b ) { } }", + tokenizeAndStringify("void foo ( ) { if (0 + b) { } }", true)); + ASSERT_EQUALS("void foo ( ) { if ( b ) { } }", + tokenizeAndStringify("void foo ( ) { if (b - 0) { } }", true)); + ASSERT_EQUALS("void foo ( ) { if ( b ) { } }", + tokenizeAndStringify("void foo ( ) { if (b * 1) { } }", true)); + ASSERT_EQUALS("void foo ( ) { if ( b ) { } }", + tokenizeAndStringify("void foo ( ) { if (1 * b) { } }", true)); + //ASSERT_EQUALS("void foo ( ) { if ( b ) { } }", + // tokenizeAndStringify("void foo ( ) { if (b / 1) { } }", true)); ASSERT_EQUALS("void foo ( ) { if ( b ) { } }", tokenizeAndStringify("void foo ( ) { if (b | 0) { } }", true)); ASSERT_EQUALS("void foo ( ) { if ( b ) { } }",