From 498beb7d9abe9470c68d7625b49dca85d67c7deb Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sun, 4 Oct 2009 23:33:41 +0300 Subject: [PATCH] Fix #784 (Tokenizer: Simplify 2[a] -> a[2]) http://sourceforge.net/apps/trac/cppcheck/ticket/784 --- src/tokenize.cpp | 11 +++++++++++ test/testsimplifytokens.cpp | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 5a5f8ca17..78eac509f 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1748,6 +1748,17 @@ void Tokenizer::simplifyTokenList() } } + // 0[a] -> a[0] + for (Token *tok = _tokens; tok; tok = tok->next()) + { + if (Token::Match(tok, "%num% [ %var% ]")) + { + const std::string temp = tok->str(); + tok->str(tok->tokAt(2)->str()); + tok->tokAt(2)->str(temp); + } + } + simplifySizeof(); // Replace constants.. diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 6d16ede19..ee6be8f62 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -124,6 +124,7 @@ private: TEST_CASE(simplifyTypedef3) TEST_CASE(simplifyTypedef4) TEST_CASE(simplifyTypedef5) + TEST_CASE(reverseArraySyntax) } std::string tok(const char code[], bool simplify = true) @@ -1775,6 +1776,10 @@ private: ASSERT_EQUALS(expected, tok(code, false)); } + void reverseArraySyntax() + { + ASSERT_EQUALS("a [ 13 ]", tok("13[a]")); + } }; REGISTER_TEST(TestSimplifyTokens)