From 5835b2665b6b154369378de0e3644a7bbfc7432f Mon Sep 17 00:00:00 2001 From: Zachary Blair Date: Fri, 23 Apr 2010 22:59:53 -0700 Subject: [PATCH] Fixed #1169 (improve check: divsion by zero not detected when using the shift (>>) operator) --- lib/tokenize.cpp | 5 ++-- test/testtokenize.cpp | 55 +++++++++++++++++++++++++++++++++---------- 2 files changed, 45 insertions(+), 15 deletions(-) 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()