From 963f6ce3eff8abb656922b6bb9951a1804ca6f25 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Sun, 15 Jan 2012 14:50:01 +0100 Subject: [PATCH] Tokenizer: report syntaxError if there's nothing after 'if|for|while ()' and if there's nothing inside round brackets; TestTokenizer: change test cases accordingly; TestSimplifyTokens: ditto. --- lib/tokenize.cpp | 11 +++++++--- test/testsimplifytokens.cpp | 44 ++++++++++++++++++------------------- test/testtokenize.cpp | 18 +++++++-------- 3 files changed, 39 insertions(+), 34 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 6adb5ac42..61667f707 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4144,6 +4144,12 @@ bool Tokenizer::simplifyIfAddBraces() } if (Token::Match(tok, "if|for|while|BOOST_FOREACH (")) { + + if (tok->strAt(2) == ")") { + //no arguments, abort + syntaxError(tok); + return false; + } // don't add "{}" around ";" in "do {} while();" (#609) const Token *prev = tok->previous(); if (Token::simpleMatch(prev, "} while") && @@ -4173,9 +4179,8 @@ bool Tokenizer::simplifyIfAddBraces() // If there is no code after the if(), abort if (!tok->next()) { - // This is a syntax error and we should call syntaxError() and return false but - // many tokenizer tests are written with this syntax error so just ignore it. - return true; + syntaxError(tok); + return false; } // insert open brace.. diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index acb3bf855..754dec327 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -447,7 +447,7 @@ private: void cast() { - ASSERT_EQUALS("if ( ! p )", tok("if (p == (char *)0)")); + ASSERT_EQUALS("if ( ! p ) { ; }", tok("if (p == (char *)0);")); ASSERT_EQUALS("return str ;", tok("return (char *)str;")); { @@ -469,7 +469,7 @@ private: ASSERT_EQUALS("if ( * a )", tok("if ((unsigned int)(unsigned char)*a)")); ASSERT_EQUALS("class A { A operator* ( int ) ; } ;", tok("class A { A operator *(int); };")); ASSERT_EQUALS("class A { A operator* ( int ) const ; } ;", tok("class A { A operator *(int) const; };")); - ASSERT_EQUALS("if ( ! p )", tok("if (p == (char *)(char *)0)")); + ASSERT_EQUALS("if ( ! p ) { ; }", tok("if (p == (char *)(char *)0);")); // no simplification as the cast may be important here. see #2897 for example ASSERT_EQUALS("; * ( ( char * ) p + 1 ) = 0 ;", tok("; *((char *)p + 1) = 0;")); @@ -2439,17 +2439,17 @@ private: } void ifnot() { - ASSERT_EQUALS("if ( ! x )", simplifyIfNot("if(0==x)")); - ASSERT_EQUALS("if ( ! x )", simplifyIfNot("if(x==0)")); - ASSERT_EQUALS("if ( ! ( a = b ) )", simplifyIfNot("if(0==(a=b))")); - ASSERT_EQUALS("if ( ! x )", simplifyIfNot("if(x==0)")); - ASSERT_EQUALS("if ( ! a && b ( ) )", simplifyIfNot("if( 0 == a && b() )")); - ASSERT_EQUALS("if ( b ( ) && ! a )", simplifyIfNot("if( b() && 0 == a )")); - ASSERT_EQUALS("if ( ! ( a = b ) )", simplifyIfNot("if((a=b)==0)")); - ASSERT_EQUALS("if ( ! x . y )", simplifyIfNot("if(x.y==0)")); - ASSERT_EQUALS("if ( ! x )", simplifyIfNot("if((x==0))")); - ASSERT_EQUALS("if ( ( ! x ) && ! y )", simplifyIfNot("if((x==0) && y==0)")); - ASSERT_EQUALS("if ( ! ( ! fclose ( fd ) ) )", simplifyIfNot("if(!(fclose(fd) == 0))")); + ASSERT_EQUALS("if ( ! x ) { ; }", simplifyIfNot("if(0==x);")); + ASSERT_EQUALS("if ( ! x ) { ; }", simplifyIfNot("if(x==0);")); + ASSERT_EQUALS("if ( ! ( a = b ) ) { ; }", simplifyIfNot("if(0==(a=b));")); + ASSERT_EQUALS("if ( ! x ) { ; }", simplifyIfNot("if(x==0);")); + ASSERT_EQUALS("if ( ! a && b ( ) ) { ; }", simplifyIfNot("if( 0 == a && b() );")); + ASSERT_EQUALS("if ( b ( ) && ! a ) { ; }", simplifyIfNot("if( b() && 0 == a );")); + ASSERT_EQUALS("if ( ! ( a = b ) ) { ; }", simplifyIfNot("if((a=b)==0);")); + ASSERT_EQUALS("if ( ! x . y ) { ; }", simplifyIfNot("if(x.y==0);")); + ASSERT_EQUALS("if ( ! x ) { ; }", simplifyIfNot("if((x==0));")); + ASSERT_EQUALS("if ( ( ! x ) && ! y ) { ; }", simplifyIfNot("if((x==0) && y==0);")); + ASSERT_EQUALS("if ( ! ( ! fclose ( fd ) ) ) { ; }", simplifyIfNot("if(!(fclose(fd) == 0));")); } @@ -2470,8 +2470,8 @@ private: } void not1() { - ASSERT_EQUALS("if ( ! p )", simplifyLogicalOperators("if (not p)")); - ASSERT_EQUALS("if ( p && ! q )", simplifyLogicalOperators("if (p && not q)")); + ASSERT_EQUALS("if ( ! p ) { ; }", simplifyLogicalOperators("if (not p);")); + ASSERT_EQUALS("if ( p && ! q ) { ; }", simplifyLogicalOperators("if (p && not q);")); ASSERT_EQUALS("void foo ( not i )", simplifyLogicalOperators("void foo ( not i )")); } @@ -2488,8 +2488,8 @@ private: ASSERT_EQUALS("if ( p && bar ( ) ) { ; }", simplifyLogicalOperators("if (p and bar()) ;")); - ASSERT_EQUALS("if ( p && ! q )", - simplifyLogicalOperators("if (p and not q)")); + ASSERT_EQUALS("if ( p && ! q ) { ; }", + simplifyLogicalOperators("if (p and not q) ;")); } void or1() { @@ -2505,8 +2505,8 @@ private: ASSERT_EQUALS("if ( p || bar ( ) ) { ; }", simplifyLogicalOperators("if (p or bar()) ;")); - ASSERT_EQUALS("if ( p || ! q )", - simplifyLogicalOperators("if (p or not q)")); + ASSERT_EQUALS("if ( p || ! q ) { ; }", + simplifyLogicalOperators("if (p or not q) ;")); } void comma_keyword() { @@ -2800,9 +2800,9 @@ private: ASSERT_EQUALS(";", tok("; x = x + 0;")); - ASSERT_EQUALS("if ( a == 2 )", tok("if (a==1+1)")); - ASSERT_EQUALS("if ( a + 2 != 6 )", tok("if (a+1+1!=1+2+3)")); - ASSERT_EQUALS("if ( 4 < a )", tok("if (14-2*5