diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index dd76cd1d9..b4622ee42 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5010,8 +5010,9 @@ bool Tokenizer::simplifyKnownVariables() // Variable is used in calculation.. if (Token::Match(tok3, "[=+-*/[] %varid% [?+-*/;]]", varid) || - Token::Match(tok3, "[=+-*/[] %varid% <<", varid) || - Token::Match(tok3, "<< %varid% [+-*/;]]", varid)) + Token::Match(tok3, "[(=+-*/[] %varid% <<|>>", varid) || + Token::Match(tok3, "<< %varid% [+-*/;])]", varid) || + Token::Match(tok3, ">> %varid% [+-*/])]", varid)) { tok3 = tok3->next(); tok3->str(value); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index bb7d631b0..78ad3d1ea 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -1063,21 +1063,50 @@ private: void simplifyKnownVariables22() { // This testcase is related to ticket #1169 - const char code[] = "void foo()\n" - "{\n" - " int n = 10;\n" - " i = (n >> 1);\n" - "}\n"; + { + const char code[] = "void foo()\n" + "{\n" + " int n = 10;\n" + " i = (n >> 1);\n" + "}\n"; - // Wanted result - Ticket #1169 can probably be closed when this works - TODO_ASSERT_EQUALS( - "void foo ( ) { int n ; n = 10 ; i = ( 10 >> 1 ) ; }", - simplifyKnownVariables(code)); + ASSERT_EQUALS( + "void foo ( ) { int n ; n = 10 ; i = ( 10 >> 1 ) ; }", + simplifyKnownVariables(code)); + } + { + const char code[] = "void foo()\n" + "{\n" + " int n = 10;\n" + " i = (n << 1);\n" + "}\n"; - // Current result - ASSERT_EQUALS( - "void foo ( ) { int n ; n = 10 ; i = ( n >> 1 ) ; }", - simplifyKnownVariables(code)); + ASSERT_EQUALS( + "void foo ( ) { int n ; n = 10 ; i = ( 10 << 1 ) ; }", + simplifyKnownVariables(code)); + } + { + const char code[] = "void foo()\n" + "{\n" + " int n = 10;\n" + " i = (1 << n);\n" + "}\n"; + + ASSERT_EQUALS( + "void foo ( ) { int n ; n = 10 ; i = ( 1 << 10 ) ; }", + simplifyKnownVariables(code)); + } + { + const char code[] = "void foo()\n" + "{\n" + " int n = 10;\n" + " i = (1 >> n);\n" + "}\n"; + + ASSERT_EQUALS( + "void foo ( ) { int n ; n = 10 ; i = ( 1 >> 10 ) ; }", + simplifyKnownVariables(code)); + } } void simplifyKnownVariables23()