From 255d0410a4b64874a94bf5b342c3fc9080f15920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 21 Jul 2017 09:16:42 +0200 Subject: [PATCH] Fixed #8085 (Token::expressionString: unsigned long long) --- lib/token.cpp | 13 +++++++++---- test/testcondition.cpp | 2 +- test/testtoken.cpp | 12 ++++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index ca0358022..eacb14183 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1202,15 +1202,20 @@ static const Token* goToRightParenthesis(const Token* start, const Token* end) static std::string stringFromTokenRange(const Token* start, const Token* end) { std::ostringstream ret; + if (end) + end = end->next(); for (const Token *tok = start; tok && tok != end; tok = tok->next()) { - if (tok->originalName() == "->") - ret << "->"; - else + if (tok->isUnsigned()) + ret << "unsigned "; + if (tok->isLong()) + ret << (tok->isLiteral() ? "L" : "long "); + if (tok->originalName().empty()) { ret << tok->str(); + } else + ret << tok->originalName(); if (Token::Match(tok, "%name%|%num% %name%|%num%")) ret << ' '; } - ret << end->str(); return ret.str(); } diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 4bf1c7e0a..aadc7d71b 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -1892,7 +1892,7 @@ private: " int x = 'd' ? 1 : 2;\n" "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Condition ''a'' is always true\n" - "[test.cpp:3]: (style) Condition ''b'' is always true\n" + "[test.cpp:3]: (style) Condition 'L'b'' is always true\n" "[test.cpp:4]: (style) Condition ''c'' is always true\n" "[test.cpp:5]: (style) Condition ''d'' is always true\n", errout.str()); } diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 01afc53ea..f00114c9e 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -96,6 +96,8 @@ private: TEST_CASE(canFindMatchingBracketsOuterPair); TEST_CASE(canFindMatchingBracketsWithTooManyClosing); TEST_CASE(canFindMatchingBracketsWithTooManyOpening); + + TEST_CASE(expressionString); } void nextprevious() const { @@ -945,6 +947,16 @@ private: t = var.tokens()->tokAt(4)->findClosingBracket(); ASSERT(t == nullptr); } + + void expressionString() { + givenACodeSampleToTokenize var1("void f() { *((unsigned long long *)x) = 0; }"); + const Token *tok1 = Token::findsimplematch(var1.tokens(), "*"); + ASSERT_EQUALS("*((unsigned long long*)x)", tok1->expressionString()); + + givenACodeSampleToTokenize var2("typedef unsigned long long u64; void f() { *((u64 *)x) = 0; }"); + const Token *tok2 = Token::findsimplematch(var2.tokens(), "*"); + ASSERT_EQUALS("*((unsigned long long*)x)", tok2->expressionString()); + } }; REGISTER_TEST(TestToken)