diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 6bc179efa..7fc0ac63c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8921,11 +8921,12 @@ void Tokenizer::simplifyComparisonOrder() void Tokenizer::simplifyConst() { for (Token *tok = _tokens; tok; tok = tok->next()) { - if (Token::Match(tok, "[;{}(,] %type% const") && - tok->next()->str().find(":") == std::string::npos && - tok->next()->str() != "operator") { - tok->tokAt(2)->str(tok->next()->str()); - tok->next()->str("const"); + if (Token::Match(tok, "%type% const") && + (!tok->previous() || Token::Match(tok->previous(), "[;{}(,]")) && + tok->str().find(":") == std::string::npos && + tok->str() != "operator") { + tok->next()->str(tok->str()); + tok->str("const"); } } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index edb373ab4..f4d57395c 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -4196,7 +4196,7 @@ private: "a = SZ;\n" "}\n"; const char expected[] = - "static const char str [ 5 ] = \"abcd\" ;\n\nvoid f ( ) {\na = 5 ;\n}"; + "const static char str [ 5 ] = \"abcd\" ;\n\nvoid f ( ) {\na = 5 ;\n}"; ASSERT_EQUALS(expected, tokenizeAndStringify(code,true)); } @@ -4890,6 +4890,7 @@ private: ASSERT_EQUALS("void foo ( ) { int * const x ; }", tokenizeAndStringify("void foo(){ int * const x;}")); + ASSERT_EQUALS("const int foo ( ) ;", tokenizeAndStringify("int const foo ();")); } void switchCase() {