From 8fd62e0cf9c4a6bf17a209cea19039c64cab0ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 20 Nov 2015 19:43:11 +0100 Subject: [PATCH] Fixed #4931 (Wrong calculation of constants (simplifying: +,<<,% operations)) --- lib/tokenize.cpp | 8 ++++++++ test/testtokenize.cpp | 3 +++ 2 files changed, 11 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 011c0c474..57fc6ceef 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3734,6 +3734,14 @@ bool Tokenizer::simplifyTokenList2() simplifyCasts(); + for (Token *tok = list.front(); tok; tok = tok->next()) { + if (Token::Match(tok, "%oror%|&& %num% %oror%|&&|,|)") || + Token::Match(tok, "[(,] %num% %oror%|&&")) { + tok = tok->next(); + tok->str(MathLib::isNullValue(tok->str()) ? "0" : "1"); + } + } + // Simplify simple calculations before replace constants, this allows the replacement of constants that are calculated // e.g. const static int value = sizeof(X)/sizeof(Y); simplifyCalculations(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index dd3228970..812ca7244 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -5660,6 +5660,9 @@ private: // ticket #3964 - simplify numeric calculations in tokenization ASSERT_EQUALS("char a [ 10 ] ;", tokenizeAndStringify("char a[9+1];")); + + // ticket #4931 + ASSERT_EQUALS("dostuff ( 1 ) ;", tokenizeAndStringify("dostuff(9&&8);", true)); } void simplifyCompoundAssignment() {