From 662b0d2dbe2a612ac95292b7d73548c95106add6 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sun, 26 Aug 2012 10:03:05 +0200 Subject: [PATCH] Simplify 0[foo] to *(foo) (fixes #4083) --- lib/tokenize.cpp | 9 +++++++++ test/testtokenize.cpp | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 65aec0782..4415a1da1 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1693,6 +1693,15 @@ bool Tokenizer::tokenize(std::istream &code, } } + // Simplify: 0[foo] -> *(foo) + for (Token* tok = list.front(); tok; tok = tok->next()) { + if (Token::simpleMatch(tok, "0 [") && tok->linkAt(1)) { + tok->str("*"); + tok->next()->str("("); + tok->linkAt(1)->str(")"); + } + } + // Remove "volatile", "inline", "register", and "restrict" simplifyKeyword(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 4b235b32f..cd1eae898 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -424,6 +424,8 @@ private: TEST_CASE(simplifyOperatorName5); TEST_CASE(simplifyOperatorName6); // ticket #3194 + TEST_CASE(simplifyNullArray); + // Some simple cleanups of unhandled macros in the global scope TEST_CASE(removeMacrosInGlobalScope); @@ -6700,6 +6702,10 @@ private: ASSERT_EQUALS(result2, tokenizeAndStringify(code2,false)); } + void simplifyNullArray() { + ASSERT_EQUALS("* ( foo . bar [ 5 ] ) = x ;", tokenizeAndStringify("0[foo.bar[5]] = x;")); + } + void removeMacrosInGlobalScope() { // remove some unhandled macros in the global scope. ASSERT_EQUALS("void f ( ) { }", tokenizeAndStringify("void f() NOTHROW { }"));