diff --git a/addons/misra.py b/addons/misra.py index accbd74d7..899639d6e 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -1863,6 +1863,7 @@ class MisraChecker: return following # Zero arguments should be in form ( void ) + # TODO: Use rawTokens or add flag when void is removed def checkZeroArguments(func, startCall, endCall): if (len(func.argument) == 0): voidArg = startCall.next @@ -1935,7 +1936,7 @@ class MisraChecker: endCall = startCall.link if endCall is None or endCall.str != ')': continue - checkZeroArguments(func, startCall, endCall) + # checkZeroArguments(func, startCall, endCall) checkDefinitionArgumentsViolations(func, startCall, endCall) # Check arguments in function declaration @@ -1947,7 +1948,7 @@ class MisraChecker: endCall = startCall.link if endCall is None or endCall.str != ')': continue - checkZeroArguments(func, startCall, endCall) + # checkZeroArguments(func, startCall, endCall) if tokenImpl: checkDeclarationArgumentsViolations(func, startCall, endCall) else: diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index 78f283136..e7568bb75 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -337,7 +337,7 @@ const misra_8_1_a; // 8.1 8.4 static int misra_8_2_a (int n, ...); extern int misra_8_2_b (int n); extern int misra_8_2_c (int); // 8.2 -static int misra_8_2_d (); // 8.2 +static int misra_8_2_d (); // TODO: 8.2 static int misra_8_2_e (void); static int misra_8_2_f (vec, n ) int *vec; // 8.2 @@ -345,13 +345,13 @@ int n; // 8.2 { return vec[ n - 1 ]; } -static int misra_8_2_g ( /* comment */ ); // 8.2 -static int misra_8_2_h ( /* comment 1 */ /* comment 2 */ ); // 8.2 +static int misra_8_2_g ( /* comment */ ); // TODO: 8.2 +static int misra_8_2_h ( /* comment 1 */ /* comment 2 */ ); // TODO: 8.2 static int misra_8_2_i ( /* comment */ void); static int misra_8_2_j ( /* comment */ void /* comment */); static int misra_8_2_k ( // void); -static int misra_8_2_l ( // 8.2 +static int misra_8_2_l ( // TODO: 8.2 ); static void misra_8_2_m(uint8_t * const x); static void misra_8_2_m(uint8_t * const x) @@ -373,7 +373,7 @@ static int misra_8_2_p( const uint8_t *const a2 ); static int misra_8_2_q -(); // 8.2 +(); // TODO: 8.2 void misra_8_4_foo(void) {} // 8.4 extern void misra_8_4_func(void); diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 9f7173003..bfc94e66f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3108,6 +3108,16 @@ void Tokenizer::simplifyArrayAccessSyntax() } } +void Tokenizer::simplifyParameterVoid() +{ + for (Token* tok = list.front(); tok; tok = tok->next()) { + if (Token::Match(tok, "sizeof|decltype|typeof")) + continue; + if (Token::Match(tok, "%name% ( void )")) + tok->next()->deleteNext(); + } +} + void Tokenizer::simplifyRedundantConsecutiveBraces() { // Remove redundant consecutive braces, i.e. '.. { { .. } } ..' -> '.. { .. } ..'. @@ -5273,6 +5283,8 @@ bool Tokenizer::simplifyTokenList1(const char FileName[]) removeRedundantSemicolons(); + simplifyParameterVoid(); + simplifyRedundantConsecutiveBraces(); simplifyEmptyNamespaces(); diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index f3e9eb75d..7e81eefa4 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -217,6 +217,7 @@ private: TEST_CASE(template172); // #10258 crash TEST_CASE(template173); // #10332 crash TEST_CASE(template174); // #10506 hang + TEST_CASE(template175); // #10908 TEST_CASE(template_specialization_1); // #7868 - template specialization template struct S> {..}; TEST_CASE(template_specialization_2); // #7868 - template specialization template struct S> {..}; TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template) @@ -1138,7 +1139,7 @@ private: "return f1> ( 0 , reinterpret_cast < B * > ( E :: Int ( -1 ) ) ) ; " "} " "} ; " - "int main ( void ) { " + "int main ( ) { " "C ca ; " "return 0 ; " "} " @@ -4456,6 +4457,16 @@ private: ASSERT_EQUALS(exp, tok(code)); } + void template175() + { + const char code[] = "template T Get() {return value;}\n" + "char f() { Get(); }\n"; + const char exp[] = "int Get ( ) ; " + "char f ( ) { Get ( ) ; } " + "int Get ( ) { return 10 ; }"; + ASSERT_EQUALS(exp, tok(code)); + } + void template_specialization_1() { // #7868 - template specialization template struct S> {..}; const char code[] = "template struct C {};\n" "template struct S {a};\n" diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 475e45627..a929c934a 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -4832,7 +4832,7 @@ private: ASSERT(functok); ASSERT(functok->function()); ASSERT(functok->function()->name() == "foo1"); - functok = Token::findsimplematch(tokenizer.tokens(), "foo2 ( void ) { }"); + functok = Token::findsimplematch(tokenizer.tokens(), "foo2 ( ) { }"); ASSERT(functok); ASSERT(functok->function()); ASSERT(functok->function()->name() == "foo2"); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 9ebe5325b..27e423de4 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -2508,7 +2508,7 @@ private: " unsigned short const int x = 1;\n" " return x;\n" "}"; - ASSERT_EQUALS("unsigned short f ( void ) {\n" + ASSERT_EQUALS("unsigned short f ( ) {\n" "const unsigned short x ; x = 1 ;\n" "return x ;\n" "}", @@ -3571,11 +3571,11 @@ private: void simplifyFunctionPointers6() { const char code1[] = "void (*fp(void))(int) {}"; - const char expected1[] = "1: void * fp ( void ) { }\n"; + const char expected1[] = "1: void * fp ( ) { }\n"; ASSERT_EQUALS(expected1, tokenizeDebugListing(code1)); const char code2[] = "std::string (*fp(void))(int);"; - const char expected2[] = "1: std :: string * fp ( void ) ;\n"; + const char expected2[] = "1: std :: string * fp ( ) ;\n"; ASSERT_EQUALS(expected2, tokenizeDebugListing(code2)); } @@ -4071,7 +4071,7 @@ private: { const char code[] = "class S { int function(void); };"; - ASSERT_EQUALS("class S { int function ( void ) ; } ;", tokenizeAndStringify(code)); + ASSERT_EQUALS("class S { int function ( ) ; } ;", tokenizeAndStringify(code)); ASSERT_EQUALS("", errout.str()); } @@ -4083,7 +4083,7 @@ private: { const char code[] = "int function(void);"; - ASSERT_EQUALS("int function ( void ) ;", tokenizeAndStringify(code)); + ASSERT_EQUALS("int function ( ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("", errout.str()); } @@ -4095,13 +4095,13 @@ private: { const char code[] = "extern int function(void);"; - ASSERT_EQUALS("extern int function ( void ) ;", tokenizeAndStringify(code)); + ASSERT_EQUALS("extern int function ( ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("", errout.str()); } { const char code[] = "int function1(void); int function2(void);"; - ASSERT_EQUALS("int function1 ( void ) ; int function2 ( void ) ;", tokenizeAndStringify(code)); + ASSERT_EQUALS("int function1 ( ) ; int function2 ( ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("", errout.str()); } @@ -4746,7 +4746,7 @@ private: } void simplifyCAlternativeTokens() { - ASSERT_EQUALS("void or ( void ) ;", tokenizeAndStringify("void or(void);", true, Settings::Native, "test.c")); + ASSERT_EQUALS("void or ( ) ;", tokenizeAndStringify("void or(void);", true, Settings::Native, "test.c")); ASSERT_EQUALS("void f ( ) { if ( a && b ) { ; } }", tokenizeAndStringify("void f() { if (a and b); }", true, Settings::Native, "test.c")); ASSERT_EQUALS("void f ( ) { if ( a && b ) { ; } }", tokenizeAndStringify("void f() { if (a and b); }", true, Settings::Native, "test.cpp")); ASSERT_EQUALS("void f ( ) { if ( a || b ) { ; } }", tokenizeAndStringify("void f() { if (a or b); }", true, Settings::Native, "test.c")); diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 182378cb1..1ef2c72e9 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -6167,6 +6167,15 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + valueFlowUninit("template T Get() {return value;}\n" + "char f() {\n" + " char buf[10];\n" + " for(int i = 0; i < Get() ; ++i) \n" + " buf[i] = 0;\n" + " return buf[0];\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + valueFlowUninit("static void Foo(double* p) {\n" " p[0] = 0;\n" " p[1] = 0;\n" diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 19557f7e8..7b5461df4 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -1735,7 +1735,7 @@ private: "};"; ASSERT_EQUALS("1: class Foo {\n" "2: private:\n" - "3: void f ( void ) ;\n" + "3: void f ( ) ;\n" "4: } ;\n", tokenize(code)); }