From 2fc067e6abfcdb8ad2c9a15eab5f93269262ade0 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Sun, 4 Dec 2011 16:46:03 +0100 Subject: [PATCH] Tokenizer::simplifyConst : handle also '%type% const' when the 'type' token is the first one in the list. --- lib/tokenize.cpp | 11 ++++++----- test/testtokenize.cpp | 3 ++- 2 files changed, 8 insertions(+), 6 deletions(-) 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() {