Fixed #8085 (Token::expressionString: unsigned long long)

This commit is contained in:
Daniel Marjamäki 2017-07-21 09:16:42 +02:00
parent 76a50e60b9
commit 255d0410a4
3 changed files with 22 additions and 5 deletions

View File

@ -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();
}

View File

@ -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());
}

View File

@ -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)