diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 0b2ec9205..82730bf4f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -9484,6 +9484,26 @@ void Tokenizer::findGarbageCode() const } } + // Keywords in global scope + std::set nonGlobalKeywords{"break", + "continue", + "for", + "goto", + "if", + "return", + "switch", + "while"}; + if (isCPP()) { + nonGlobalKeywords.insert("try"); + nonGlobalKeywords.insert("catch"); + } + for (const Token *tok = tokens(); tok; tok = tok->next()) { + if (tok->str() == "{") + tok = tok->link(); + else if (tok->isName() && nonGlobalKeywords.count(tok->str()) && !Token::Match(tok->tokAt(-2), "operator %str%")) + syntaxError(tok, "keyword '" + tok->str() + "' is not allowed in global scope"); + } + // keyword keyword const std::set nonConsecutiveKeywords{"break", "continue", diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index dbc84edd2..f82a201cc 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -483,7 +483,7 @@ private: } void garbageCode10() { // #6127 - checkCode("for( rl=reslist; rl!=NULL; rl=rl->next )"); + ASSERT_THROW(checkCode("for( rl=reslist; rl!=NULL; rl=rl->next )"), InternalError); } void garbageCode12() { // do not crash diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 6a402dc28..5c1f4a989 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -4086,7 +4086,7 @@ private: // ok code (ticket #1985) tok("void f()\n" - "try { ;x ( p ) ;", tok("cast(p);")); - ASSERT_EQUALS("return ( a + b ) * c ;", tok("return (a+b)*c;")); + ASSERT_EQUALS("void f ( ) { return ( a + b ) * c ; }", tok("void f(){return (a+b)*c;}")); ASSERT_EQUALS("void f ( ) { int p ; if ( 2 * p == 0 ) { } }", tok("void f(){int p; if (2*p == 0) {}}")); ASSERT_EQUALS("void f ( ) { DIR * f ; f = opendir ( dirname ) ; if ( closedir ( f ) ) { } }", tok("void f(){DIR * f = opendir(dirname);if (closedir(f)){}}")); ASSERT_EQUALS("void foo ( int p ) { if ( p >= 0 ) { ; } }", tok("void foo(int p){if((p)>=0);}")); @@ -2147,11 +2146,11 @@ private: } void elseif1() { - const char code[] = "else if(ab) { cd } else { ef }gh;"; - ASSERT_EQUALS("\n\n##file 0\n1: else { if ( ab ) { cd } else { ef } } gh ;\n", tokenizeDebugListing(code)); + const char code[] = "void f(){ if(x) {} else if(ab) { cd } else { ef }gh; }"; + ASSERT_EQUALS("\n\n##file 0\n1: void f ( ) { if ( x ) { } else { if ( ab ) { cd } else { ef } } gh ; }\n", tokenizeDebugListing(code)); // syntax error: assert there is no segmentation fault - ASSERT_EQUALS("\n\n##file 0\n1: else if ( x ) { }\n", tokenizeDebugListing("else if (x) { }")); + ASSERT_EQUALS("\n\n##file 0\n1: void f ( ) { if ( x ) { } else { if ( x ) { } } }\n", tokenizeDebugListing("void f(){ if(x) {} else if (x) { } }")); { const char src[] = "void f(int g,int f) {\n" @@ -2263,11 +2262,13 @@ private: void sizeof5() { const char code[] = + "{" "const char * names[2];" "for (int i = 0; i != sizeof(names[0]); i++)" - "{}"; + "{}" + "}"; std::ostringstream expected; - expected << "const char * names [ 2 ] ; for ( int i = 0 ; i != " << sizeofFromTokenizer("*") << " ; i ++ ) { }"; + expected << "{ const char * names [ 2 ] ; for ( int i = 0 ; i != " << sizeofFromTokenizer("*") << " ; i ++ ) { } }"; ASSERT_EQUALS(expected.str(), tok(code)); } @@ -2923,10 +2924,10 @@ private: } void ifassign1() { - ASSERT_EQUALS("; a = b ; if ( a ) { ; }", simplifyIfAndWhileAssign(";if(a=b);")); - ASSERT_EQUALS("; a = b ( ) ; if ( a ) { ; }", simplifyIfAndWhileAssign(";if((a=b()));")); - ASSERT_EQUALS("; a = b ( ) ; if ( ! ( a ) ) { ; }", simplifyIfAndWhileAssign(";if(!(a=b()));")); - ASSERT_EQUALS("; a . x = b ( ) ; if ( ! ( a . x ) ) { ; }", simplifyIfAndWhileAssign(";if(!(a->x=b()));")); + ASSERT_EQUALS("{ a = b ; if ( a ) { ; } }", simplifyIfAndWhileAssign("{if(a=b);}")); + ASSERT_EQUALS("{ a = b ( ) ; if ( a ) { ; } }", simplifyIfAndWhileAssign("{if((a=b()));}")); + ASSERT_EQUALS("{ a = b ( ) ; if ( ! ( a ) ) { ; } }", simplifyIfAndWhileAssign("{if(!(a=b()));}")); + ASSERT_EQUALS("{ a . x = b ( ) ; if ( ! ( a . x ) ) { ; } }", simplifyIfAndWhileAssign("{if(!(a->x=b()));}")); ASSERT_EQUALS("void f ( ) { A ( ) a = b ; if ( a ) { ; } }", simplifyIfAndWhileAssign("void f() { A() if(a=b); }")); ASSERT_EQUALS("void foo ( int a ) { a = b ( ) ; if ( a >= 0 ) { ; } }", tok("void foo(int a) {if((a=b())>=0);}")); TODO_ASSERT_EQUALS("void foo ( A a ) { a . c = b ( ) ; if ( 0 <= a . c ) { ; } }", @@ -2956,17 +2957,21 @@ private: } void whileAssign1() { - ASSERT_EQUALS("; a = b ; while ( a ) { b = 0 ; a = b ; }", simplifyIfAndWhileAssign(";while(a=b) { b = 0; }")); - ASSERT_EQUALS("; a . b = c ; while ( a . b ) { c = 0 ; a . b = c ; }", simplifyIfAndWhileAssign(";while(a.b=c) { c=0; }")); - ASSERT_EQUALS("struct hfs_bnode * node ; " + ASSERT_EQUALS("{ a = b ; while ( a ) { b = 0 ; a = b ; } }", simplifyIfAndWhileAssign("{while(a=b) { b = 0; }}")); + ASSERT_EQUALS("{ a . b = c ; while ( a . b ) { c = 0 ; a . b = c ; } }", simplifyIfAndWhileAssign("{while(a.b=c) { c=0; }}")); + ASSERT_EQUALS("{ " + "struct hfs_bnode * node ; " "struct hfs_btree * tree ; " "node = tree . node_hash [ i ++ ] ; " - "while ( node ) { node = tree . node_hash [ i ++ ] ; }", - tok("struct hfs_bnode *node;" + "while ( node ) { node = tree . node_hash [ i ++ ] ; } " + "}", + tok("{" + "struct hfs_bnode *node;" "struct hfs_btree *tree;" - "while ((node = tree->node_hash[i++])) { }")); - ASSERT_EQUALS("char * s ; s = new char [ 10 ] ; while ( ! s ) { s = new char [ 10 ] ; }", - tok("char *s; while (0 == (s=new char[10])) { }")); + "while ((node = tree->node_hash[i++])) { }" + "}")); + ASSERT_EQUALS("{ char * s ; s = new char [ 10 ] ; while ( ! s ) { s = new char [ 10 ] ; } }", + tok("{ char *s; while (0 == (s=new char[10])) { } }")); } void whileAssign2() { @@ -2999,11 +3004,11 @@ private: errout.str(""); Tokenizer tokenizer(&settings0, this); - std::istringstream istr("; while (!(m = q->push(x))) {}"); + std::istringstream istr("{ while (!(m = q->push(x))) {} }"); tokenizer.tokenize(istr, "test.cpp"); tokenizer.simplifyTokenList2(); - ASSERT_EQUALS("; m = q . push < Message > ( x ) ; while ( ! m ) { m = q . push < Message > ( x ) ; }", tokenizer.tokens()->stringifyList(nullptr, false)); + ASSERT_EQUALS("{ m = q . push < Message > ( x ) ; while ( ! m ) { m = q . push < Message > ( x ) ; } }", tokenizer.tokens()->stringifyList(nullptr, false)); ASSERT(tokenizer.tokens()->tokAt(26) != nullptr); if (tokenizer.tokens()->tokAt(26)) { ASSERT(tokenizer.tokens()->linkAt(6) == tokenizer.tokens()->tokAt(8)); @@ -3012,18 +3017,22 @@ private: } void doWhileAssign() { - ASSERT_EQUALS("; do { a = b ; } while ( a ) ;", simplifyIfAndWhileAssign(";do { } while(a=b);")); - ASSERT_EQUALS("; do { a . a = 0 ; a . b = c ; } while ( a . b ) ;", simplifyIfAndWhileAssign(";do { a.a = 0; } while(a.b=c);")); - ASSERT_EQUALS("struct hfs_bnode * node ; " + ASSERT_EQUALS("{ do { a = b ; } while ( a ) ; }", simplifyIfAndWhileAssign("{ do { } while(a=b); }")); + ASSERT_EQUALS("{ do { a . a = 0 ; a . b = c ; } while ( a . b ) ; }", simplifyIfAndWhileAssign("{ do { a.a = 0; } while(a.b=c); }")); + ASSERT_EQUALS("{ " + "struct hfs_bnode * node ; " "struct hfs_btree * tree ; " - "do { node = tree . node_hash [ i ++ ] ; } while ( node ) ;", - tok("struct hfs_bnode *node;" + "do { node = tree . node_hash [ i ++ ] ; } while ( node ) ; " + "}", + tok("{" + "struct hfs_bnode *node;" "struct hfs_btree *tree;" - "do { } while((node = tree->node_hash[i++]));")); - ASSERT_EQUALS("char * s ; do { s = new char [ 10 ] ; } while ( ! s ) ;", - tok("char *s; do { } while (0 == (s=new char[10]));")); + "do { } while((node = tree->node_hash[i++]));" + "}")); + ASSERT_EQUALS("void foo ( ) { char * s ; do { s = new char [ 10 ] ; } while ( ! s ) ; }", + tok("void foo() { char *s; do { } while (0 == (s=new char[10])); }")); // #4911 - ASSERT_EQUALS("; do { current = f ( ) ; } while ( ( current ) != NULL ) ;", simplifyIfAndWhileAssign(";do { } while((current=f()) != NULL);")); + ASSERT_EQUALS("void foo ( ) { do { current = f ( ) ; } while ( ( current ) != NULL ) ; }", simplifyIfAndWhileAssign("void foo() { do { } while((current=f()) != NULL); }")); } void not1() { @@ -3255,7 +3264,7 @@ private: } { - ASSERT_EQUALS("; return a ? ( b = c , d ) : e ;", tok("; return a ? b = c , d : e ;")); // Keep comma + ASSERT_EQUALS("{ return a ? ( b = c , d ) : e ; }", tok("{ return a ? b = c , d : e ; }")); // Keep comma } { @@ -3486,9 +3495,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*5tokAt(5); ASSERT_EQUALS(false, tokenizer.duplicateDefinition(&x_token)); @@ -4255,29 +4257,34 @@ private: } void simplifyErrNoInWhile() { - ASSERT_EQUALS("; while ( f ( ) ) { }", - tok("; while (f() && errno == EINTR) { }")); - ASSERT_EQUALS("; while ( f ( ) ) { }", - tok("; while (f() && (errno == EINTR)) { }")); + ASSERT_EQUALS("{ while ( f ( ) ) { } }", + tok("{ while (f() && errno == EINTR) { } }")); + ASSERT_EQUALS("{ while ( f ( ) ) { } }", + tok("{ while (f() && (errno == EINTR)) { } }")); } void simplifyFuncInWhile() { - ASSERT_EQUALS("int cppcheck:r1 = fclose ( f ) ; " + ASSERT_EQUALS("{ " + "int cppcheck:r1 = fclose ( f ) ; " "while ( cppcheck:r1 ) " "{ " "foo ( ) ; " "cppcheck:r1 = fclose ( f ) ; " + "} " "}", - tok("while(fclose(f))foo();")); + tok("{while(fclose(f))foo();}")); - ASSERT_EQUALS("int cppcheck:r1 = fclose ( f ) ; " + ASSERT_EQUALS("{ " + "int cppcheck:r1 = fclose ( f ) ; " "while ( cppcheck:r1 ) " "{ " "; cppcheck:r1 = fclose ( f ) ; " + "} " "}", - tok("while(fclose(f));")); + tok("{while(fclose(f));}")); - ASSERT_EQUALS("int cppcheck:r1 = fclose ( f ) ; " + ASSERT_EQUALS("{ " + "int cppcheck:r1 = fclose ( f ) ; " "while ( cppcheck:r1 ) " "{ " "; cppcheck:r1 = fclose ( f ) ; " @@ -4286,8 +4293,9 @@ private: "while ( cppcheck:r2 ) " "{ " "; cppcheck:r2 = fclose ( g ) ; " + "} " "}", - tok("while(fclose(f)); while(fclose(g));")); + tok("{while(fclose(f)); while(fclose(g));}")); } void simplifyStructDecl1() { diff --git a/test/testsizeof.cpp b/test/testsizeof.cpp index 743cbb3d5..20902985c 100644 --- a/test/testsizeof.cpp +++ b/test/testsizeof.cpp @@ -390,36 +390,54 @@ private: } void suspiciousSizeofCalculation() { - check("int* p;\n" - "return sizeof(p)/5;"); - ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Division of result of sizeof() on pointer type.\n", errout.str()); + check("void f() {\n" + " int* p;\n" + " return sizeof(p)/5;\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Division of result of sizeof() on pointer type.\n", errout.str()); - check("unknown p;\n" - "return sizeof(p)/5;"); + check("void f() {\n" + " unknown p;\n" + " return sizeof(p)/5;\n" + "}"); ASSERT_EQUALS("", errout.str()); - check("return sizeof(unknown)/5;"); + check("void f() {\n" + " return sizeof(unknown)/5;\n" + "}"); ASSERT_EQUALS("", errout.str()); - check("int p;\n" - "return sizeof(p)/5;"); + check("void f() {\n" + " int p;\n" + " return sizeof(p)/5;\n" + "}"); ASSERT_EQUALS("", errout.str()); - check("int* p[5];\n" - "return sizeof(p)/5;"); + check("void f() {\n" + " int* p[5];\n" + " return sizeof(p)/5;\n" + "}"); ASSERT_EQUALS("", errout.str()); - check("return sizeof(foo)*sizeof(bar);"); - ASSERT_EQUALS("[test.cpp:1]: (warning, inconclusive) Multiplying sizeof() with sizeof() indicates a logic error.\n", errout.str()); + check("void f() {\n" + " return sizeof(foo)*sizeof(bar);\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Multiplying sizeof() with sizeof() indicates a logic error.\n", errout.str()); - check("return (foo)*sizeof(bar);"); + check("void f() {\n" + " return (foo)*sizeof(bar);\n" + "}"); ASSERT_EQUALS("", errout.str()); - check("return sizeof(foo)*bar;"); + check("void f() {\n" + " return sizeof(foo)*bar;\n" + "}"); ASSERT_EQUALS("", errout.str()); - check("return (end - source) / sizeof(encode_block_type) * sizeof(encode_block_type);"); + check("void f() {\n" + " return (end - source) / sizeof(encode_block_type) * sizeof(encode_block_type);\n" + "}"); ASSERT_EQUALS("", errout.str()); } diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 54e2787a4..daa561bf5 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -2648,7 +2648,7 @@ private: void symboldatabase17() { // ticket #2657 - segmentation fault - check("return f(){}"); + check("{return f(){}}"); ASSERT_EQUALS("", errout.str()); } @@ -6826,7 +6826,7 @@ private: string.arrayLike_indexOp = string.stdStringLike = true; set.library.containers["test::string"] = string; ASSERT_EQUALS("signed int", typeOf("Vector v; v[0]=3;", "[", "test.cpp", &set)); - ASSERT_EQUALS("container(test :: string)", typeOf("return test::string();", "(", "test.cpp", &set)); + ASSERT_EQUALS("container(test :: string)", typeOf("{return test::string();}", "(", "test.cpp", &set)); ASSERT_EQUALS("container(test :: string)", typeOf("void foo(Vector v) { for (auto s: v) { x=s+s; } }", "s", "test.cpp", &set)); ASSERT_EQUALS("container(test :: string)", typeOf("void foo(Vector v) { for (auto s: v) { x=s+s; } }", "+", "test.cpp", &set)); } diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 9e03c4d8d..cd64d507e 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -1108,14 +1108,14 @@ private: const Token *const tok2 = Token::findsimplematch(var2.tokens(), "*"); ASSERT_EQUALS("*((unsigned long long*)x)", tok2->expressionString()); - givenACodeSampleToTokenize data3("return (t){1,2};"); - ASSERT_EQUALS("return(t){1,2}", data3.tokens()->expressionString()); + givenACodeSampleToTokenize data3("void f() { return (t){1,2}; }"); + ASSERT_EQUALS("return(t){1,2}", data3.tokens()->tokAt(5)->expressionString()); - givenACodeSampleToTokenize data4("return L\"a\";"); - ASSERT_EQUALS("returnL\"a\"", data4.tokens()->expressionString()); + givenACodeSampleToTokenize data4("void f() { return L\"a\"; }"); + ASSERT_EQUALS("returnL\"a\"", data4.tokens()->tokAt(5)->expressionString()); - givenACodeSampleToTokenize data5("return U\"a\";"); - ASSERT_EQUALS("returnU\"a\"", data5.tokens()->expressionString()); + givenACodeSampleToTokenize data5("void f() { return U\"a\"; }"); + ASSERT_EQUALS("returnU\"a\"", data5.tokens()->tokAt(5)->expressionString()); } void hasKnownIntValue() { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d2fca04f0..eb1bfb213 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -1018,8 +1018,8 @@ private: void simplifyCasts4() { // ticket #970 - const char code[] = "if (a >= (unsigned)(b)) {}"; - const char expected[] = "if ( a >= ( unsigned int ) ( b ) ) { }"; + const char code[] = "{if (a >= (unsigned)(b)) {}}"; + const char expected[] = "{ if ( a >= ( unsigned int ) ( b ) ) { } }"; ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); } @@ -1091,8 +1091,8 @@ private: } void simplifyCasts17() { // #6110 - don't remove any parentheses in 'a(b)(c)' - ASSERT_EQUALS("if ( a ( b ) ( c ) >= 3 )", - tokenizeAndStringify("if (a(b)(c) >= 3)", true)); + ASSERT_EQUALS("{ if ( a ( b ) ( c ) >= 3 ) { } }", + tokenizeAndStringify("{ if (a(b)(c) >= 3) { } }", true)); } void simplifyAt() { @@ -1151,8 +1151,8 @@ private: ASSERT_THROW(ASSERT_EQUALS("; return f ( a [ b = c ] , asm ( \"^{}\" ) ) ;", tokenizeAndStringify("; return f(a[b=c],^{});")), InternalError); // #7185 - ASSERT_EQUALS("; return f ( asm ( \"^(void){somecode}\" ) ) ;", - tokenizeAndStringify("; return f(^(void){somecode});")); + ASSERT_EQUALS("{ return f ( asm ( \"^(void){somecode}\" ) ) ; }", + tokenizeAndStringify("{ return f(^(void){somecode}); }")); ASSERT_THROW(ASSERT_EQUALS("; asm ( \"a?(b?(c,asm(\"^{}\")):0):^{}\" ) ;", tokenizeAndStringify(";a?(b?(c,^{}):0):^{};")), InternalError); ASSERT_EQUALS("template < typename T > " @@ -1387,28 +1387,28 @@ private: } void whileAddBraces() { - const char code[] = ";while(a);"; - ASSERT_EQUALS("; while ( a ) { ; }", tokenizeAndStringify(code, true)); + const char code[] = "{while(a);}"; + ASSERT_EQUALS("{ while ( a ) { ; } }", tokenizeAndStringify(code, true)); } void doWhileAddBraces() { { - const char code[] = "do ; while (0);"; - const char result[] = "do { ; } while ( 0 ) ;"; + const char code[] = "{do ; while (0);}"; + const char result[] = "{ do { ; } while ( 0 ) ; }"; ASSERT_EQUALS(result, tokenizeAndStringify(code, false)); } { - const char code[] = "UNKNOWN_MACRO ( do ) ; while ( a -- ) ;"; - const char result[] = "UNKNOWN_MACRO ( do ) ; while ( a -- ) { ; }"; + const char code[] = "{ UNKNOWN_MACRO ( do ) ; while ( a -- ) ; }"; + const char result[] = "{ UNKNOWN_MACRO ( do ) ; while ( a -- ) { ; } }"; ASSERT_EQUALS(result, tokenizeAndStringify(code, true)); } { - const char code[] = "UNKNOWN_MACRO ( do , foo ) ; while ( a -- ) ;"; - const char result[] = "UNKNOWN_MACRO ( do , foo ) ; while ( a -- ) { ; }"; + const char code[] = "{ UNKNOWN_MACRO ( do , foo ) ; while ( a -- ) ; }"; + const char result[] = "{ UNKNOWN_MACRO ( do , foo ) ; while ( a -- ) { ; } }"; ASSERT_EQUALS(result, tokenizeAndStringify(code, true)); } @@ -3345,7 +3345,7 @@ private: } void removeParentheses14() { - ASSERT_EQUALS("; if ( ( i & 1 ) == 0 ) { ; } ;", tokenizeAndStringify("; if ( (i & 1) == 0 ); ;", false)); + ASSERT_EQUALS("{ if ( ( i & 1 ) == 0 ) { ; } }", tokenizeAndStringify("{ if ( (i & 1) == 0 ); }", false)); } void removeParentheses15() { @@ -4592,7 +4592,7 @@ private: { // if (a < b || c > d) { } - const char code[] = "if (a < b || c > d);"; + const char code[] = "{ if (a < b || c > d); }"; errout.str(""); Tokenizer tokenizer(&settings0, this); std::istringstream istr(code); @@ -4628,7 +4628,7 @@ private: { // if (a < ... > d) { } - const char code[] = "if (a < b || c == 3 || d > e);"; + const char code[] = "{ if (a < b || c == 3 || d > e); }"; errout.str(""); Tokenizer tokenizer(&settings0, this); std::istringstream istr(code); @@ -5512,8 +5512,8 @@ private: } { - const char code[] = "return doSomething(X), 0;"; - ASSERT_EQUALS("return doSomething ( X ) , 0 ;", tokenizeAndStringify(code, false)); + const char code[] = "{ return doSomething(X), 0; }"; + ASSERT_EQUALS("{ return doSomething ( X ) , 0 ; }", tokenizeAndStringify(code, false)); ASSERT_EQUALS("", errout.str()); } @@ -5896,10 +5896,14 @@ private: ASSERT_EQUALS(expected, tokenizeAndStringify(code, false)); code = "using namespace std;\n" - "try { }\n" - "catch(std::exception &exception) { }"; - expected = "try { }\n" - "catch ( std :: exception & exception ) { }"; + "void f() {\n" + " try { }\n" + " catch(std::exception &exception) { }\n" + "}"; + expected = "void f ( ) {\n" + "try { }\n" + "catch ( std :: exception & exception ) { }\n" + "}"; ASSERT_EQUALS(expected, tokenizeAndStringify(code, false)); // #5773 (Don't prepend 'std ::' to function definitions) @@ -6361,8 +6365,8 @@ private: const char code1[] = "using a::operator=;"; ASSERT_EQUALS("using a :: operator= ;", tokenizeAndStringify(code1)); - const char code2[] = "return &Fred::operator!=;"; - ASSERT_EQUALS("return & Fred :: operator!= ;", tokenizeAndStringify(code2)); + const char code2[] = "{ return &Fred::operator!=; }"; + ASSERT_EQUALS("{ return & Fred :: operator!= ; }", tokenizeAndStringify(code2)); } void simplifyOperatorName11() { // #8889 @@ -8000,6 +8004,9 @@ private: } void findGarbageCode() { // Test Tokenizer::findGarbageCode() + // C++ try/catch in global scope + ASSERT_THROW_EQUALS(tokenizeAndStringify("void f() try { }"), InternalError, "syntax error: keyword 'try' is not allowed in global scope"); + // before if|for|while|switch ASSERT_NO_THROW(tokenizeAndStringify("void f() { do switch (a) {} while (1); }")) ASSERT_NO_THROW(tokenizeAndStringify("void f() { label: switch (a) {} }"));