Skip to content

Commit f1c0820

Browse files
Fix #15033 Unary plus should not be simplified (#8855)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 4b08731 commit f1c0820

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎lib/tokenize.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3774,7 +3774,8 @@ void Tokenizer::concatenateNegativeNumberAndAnyPositive()
37743774
if (!tok->tokAt(2) || (tok->tokAt(2)->isOp() && !Token::Match(tok->tokAt(2), "[+-*]")))
37753775
syntaxError(tok);
37763776

3777-
while (tok->str() != ">" && tok->next() && tok->strAt(1) == "+" && (!Token::Match(tok->tokAt(2), "%name% (|;") || Token::Match(tok, "%op%")))
3777+
while ((tok->isArithmeticalOp() || tok->isComparisonOp()) && tok->str() != ">" && tok->next() && tok->strAt(1) == "+" &&
3778+
(!Token::Match(tok->tokAt(2), "%name% (|;") || Token::Match(tok, "%op%")))
37783779
tok->deleteNext();
37793780

37803781
if (Token::Match(tok->next(), "+|- %num%")) {

‎test/testsymboldatabase.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ class TestSymbolDatabase : public TestFixture {
546546
TEST_CASE(findFunction62); // #14272 - pointer passed to function is const
547547
TEST_CASE(findFunction63); // #14937 - member function of type returned by operator()
548548
TEST_CASE(findFunction64); // overloaded operator()
549+
TEST_CASE(findFunction65);
549550
TEST_CASE(findFunctionRef1);
550551
TEST_CASE(findFunctionRef2); // #13328
551552
TEST_CASE(findFunctionContainer);
@@ -8990,6 +8991,20 @@ class TestSymbolDatabase : public TestFixture {
89908991
}
89918992
}
89928993

8994+
void findFunction65()
8995+
{
8996+
{
8997+
GET_SYMBOL_DB("bool g(char) { return true; }\n" // #15033
8998+
"bool g(int) { return false; }\n"
8999+
"void f(char c) {\n"
9000+
" if (g(+c)) {}\n"
9001+
"}\n");
9002+
const Token* g = Token::findsimplematch(tokenizer.tokens(), "g ( +");
9003+
ASSERT(g && g->function());
9004+
ASSERT_EQUALS(2, g->function()->tokenDef->linenr());
9005+
}
9006+
}
9007+
89939008
void findFunctionRef1() {
89949009
GET_SYMBOL_DB("struct X {\n"
89959010
" const std::vector<int> getInts() const & { return mInts; }\n"

‎test/testtokenize.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ class TestTokenizer : public TestFixture {
11041104
ASSERT_EQUALS("int f ( ) { return -2 ; }", tokenizeAndStringify("int f(){return -2;}\n"));
11051105
ASSERT_EQUALS("int x [ 2 ] = { -2 , 1 }", tokenizeAndStringify("int x[2] = {-2,1}\n"));
11061106

1107-
ASSERT_EQUALS("f ( 123 )", tokenizeAndStringify("f(+123)\n"));
1107+
ASSERT_EQUALS("f ( +123 )", tokenizeAndStringify("f(+123)\n"));
11081108

11091109
ASSERT_EQUALS("std :: extent_v < A > - 1 ;", tokenizeAndStringify("std::extent_v<A> - 1;\n")); // #11341
11101110
}

‎test/testvalueflow.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ class TestValueFlow : public TestFixture {
609609
}
610610

611611
ASSERT_EQUALS(63, valueOfTok("x = 3 * uint32_t{21};\n", "*").intvalue);
612+
ASSERT_EQUALS(123, valueOfTok("x = +123;\n", "=").intvalue);
612613
}
613614

614615
void valueFlowString() {
@@ -1837,6 +1838,14 @@ class TestValueFlow : public TestFixture {
18371838
ASSERT_EQUALS(1U, values.size());
18381839
ASSERT_EQUALS(3 * settings.platform.sizeof_int, values.back().intvalue);
18391840
ASSERT_EQUALS_ENUM(ValueFlow::Value::ValueKind::Known, values.back().valueKind);
1841+
1842+
code = "int f(char c) {\n" // #15033
1843+
" return sizeof(+c);\n"
1844+
"}\n";
1845+
values = tokenValues(code, "( +");
1846+
ASSERT_EQUALS(1U, values.size());
1847+
ASSERT_EQUALS(settings.platform.sizeof_int, values.back().intvalue);
1848+
ASSERT_EQUALS_ENUM(ValueFlow::Value::ValueKind::Known, values.back().valueKind);
18401849
}
18411850

18421851
void valueFlowComma()

0 commit comments

Comments
 (0)