Skip to content

Commit 7bbdc95

Browse files
Fix #11473 FP constVariable / #11448 FP constParameter / #11188 FP danglingTempReference (#4680)
* Fix #11473 FP constVariable with range-based for / #11448 FP constParameter with unused non-const range loop variable * Fix ValueType / #11188 FP danglingTempReference with auto * Fix ValueType in range-based for * Update symboldatabase.cpp
1 parent a77f0d9 commit 7bbdc95

5 files changed

Lines changed: 62 additions & 8 deletions

File tree

lib/checkother.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,13 @@ void CheckOther::checkConstVariable()
14641464
break;
14651465
}
14661466
}
1467+
if (astIsRangeBasedForDecl(tok) && Token::Match(tok->astParent()->astOperand2(), "%varid%", var->declarationId())) {
1468+
const Variable* refvar = tok->astParent()->astOperand1()->variable();
1469+
if (refvar && refvar->isReference() && !refvar->isConst()) {
1470+
usedInAssignment = true;
1471+
break;
1472+
}
1473+
}
14671474
}
14681475
if (usedInAssignment)
14691476
continue;

lib/symboldatabase.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6132,7 +6132,7 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, Source
61326132
if (autoTok->strAt(1) == "*" && vt.pointer)
61336133
vt.pointer--;
61346134
if (Token::Match(autoTok->tokAt(-1), "const|constexpr"))
6135-
vt.constness |= 1;
6135+
vt.constness |= (1 << vt.pointer);
61366136
setValueType(autoTok, vt);
61376137
setAutoTokenProperties(autoTok);
61386138
if (vt2->pointer > vt.pointer)
@@ -6338,8 +6338,10 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, Source
63386338
setValueType(autoToken, autovt);
63396339
setAutoTokenProperties(autoToken);
63406340
ValueType varvt(autovt);
6341+
if (autoToken->strAt(1) == "*" && autovt.pointer)
6342+
autovt.pointer--;
63416343
if (isconst)
6342-
varvt.constness |= 1;
6344+
varvt.constness |= (1 << autovt.pointer);
63436345
setValueType(parent->previous(), varvt);
63446346
Variable * var = const_cast<Variable *>(parent->previous()->variable());
63456347
if (var) {

test/testautovariables.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,13 @@ class TestAutoVariables : public TestFixture {
20222022
" const int& x = (*e)[1];\n"
20232023
"}\n");
20242024
ASSERT_EQUALS("", errout.str());
2025+
2026+
check("int* g();\n" // #11188
2027+
"void f() {\n"
2028+
" const auto& p = g();\n"
2029+
" if (p != nullptr) {}\n"
2030+
"}\n");
2031+
ASSERT_EQUALS("", errout.str());
20252032
}
20262033

20272034
void testglobalnamespace() {

test/testother.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,8 +2349,7 @@ class TestOther : public TestFixture {
23492349
check("void f(std::vector<int>& v) {\n"
23502350
" for(auto& x:v) {}\n"
23512351
"}");
2352-
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'v' can be declared as reference to const\n"
2353-
"[test.cpp:2]: (style) Variable 'x' can be declared as reference to const\n",
2352+
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'x' can be declared as reference to const\n",
23542353
errout.str());
23552354

23562355
check("void f(std::vector<int>& v) {\n" // #10980
@@ -3019,6 +3018,16 @@ class TestOther : public TestFixture {
30193018
" std::cout << m[0] << std::endl;\n"
30203019
"};\n");
30213020
ASSERT_EQUALS("", errout.str());
3021+
3022+
check("struct S { int i; };\n" // #11473
3023+
"void f(std::vector<std::vector<S>>&m, int*& p) {\n"
3024+
" auto& a = m[0];\n"
3025+
" for (auto& s : a) {\n"
3026+
" p = &s.i;\n"
3027+
" return;\n"
3028+
" }\n"
3029+
"}\n");
3030+
ASSERT_EQUALS("", errout.str());
30223031
}
30233032

30243033
void constParameterCallback() {

test/testsymboldatabase.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ class TestSymbolDatabase : public TestFixture {
500500
TEST_CASE(auto15); // C++17 auto deduction from braced-init-list
501501
TEST_CASE(auto16);
502502
TEST_CASE(auto17); // #11163
503+
TEST_CASE(auto18);
503504

504505
TEST_CASE(unionWithConstructor);
505506

@@ -8469,14 +8470,14 @@ class TestSymbolDatabase : public TestFixture {
84698470
autotok = Token::findsimplematch(autotok, "auto v4");
84708471
ASSERT(autotok);
84718472
ASSERT(autotok->valueType());
8472-
TODO_ASSERT_EQUALS(0, 1, autotok->valueType()->constness);
8473+
ASSERT_EQUALS(3, autotok->valueType()->constness);
84738474
ASSERT_EQUALS(1, autotok->valueType()->pointer);
84748475
ASSERT_EQUALS(ValueType::SIGNED, autotok->valueType()->sign);
84758476
ASSERT_EQUALS(ValueType::INT, autotok->valueType()->type);
84768477
vartok = Token::findsimplematch(autotok, "v4 =");
84778478
ASSERT(autotok);
84788479
ASSERT(autotok->valueType());
8479-
ASSERT_EQUALS(1, vartok->valueType()->constness);
8480+
ASSERT_EQUALS(3, vartok->valueType()->constness);
84808481
ASSERT_EQUALS(1, vartok->valueType()->pointer);
84818482
ASSERT_EQUALS(ValueType::SIGNED, vartok->valueType()->sign);
84828483
ASSERT_EQUALS(ValueType::INT, vartok->valueType()->type);
@@ -8549,14 +8550,14 @@ class TestSymbolDatabase : public TestFixture {
85498550
autotok = Token::findsimplematch(autotok, "auto v9");
85508551
ASSERT(autotok);
85518552
ASSERT(autotok->valueType());
8552-
TODO_ASSERT_EQUALS(0, 1, autotok->valueType()->constness);
8553+
ASSERT_EQUALS(3, autotok->valueType()->constness);
85538554
ASSERT_EQUALS(1, autotok->valueType()->pointer);
85548555
ASSERT_EQUALS(ValueType::SIGNED, autotok->valueType()->sign);
85558556
ASSERT_EQUALS(ValueType::INT, autotok->valueType()->type);
85568557
vartok = Token::findsimplematch(autotok, "v9 =");
85578558
ASSERT(autotok);
85588559
ASSERT(autotok->valueType());
8559-
ASSERT_EQUALS(1, vartok->valueType()->constness);
8560+
ASSERT_EQUALS(3, vartok->valueType()->constness);
85608561
ASSERT_EQUALS(1, vartok->valueType()->pointer);
85618562
ASSERT_EQUALS(ValueType::SIGNED, vartok->valueType()->sign);
85628563
ASSERT_EQUALS(ValueType::INT, vartok->valueType()->type);
@@ -8750,6 +8751,34 @@ class TestSymbolDatabase : public TestFixture {
87508751
ASSERT_EQUALS(5, db->variableList().size());
87518752
}
87528753

8754+
void auto18() {
8755+
GET_SYMBOL_DB("void f(const int* p) {\n"
8756+
" const int* const& r = p;\n"
8757+
" const auto& s = p;\n"
8758+
"}\n");
8759+
ASSERT_EQUALS(4, db->variableList().size());
8760+
8761+
const Variable* r = db->variableList()[2];
8762+
ASSERT(r->isReference());
8763+
ASSERT(r->isConst());
8764+
ASSERT(r->isPointer());
8765+
const Token* varTok = Token::findsimplematch(tokenizer.tokens(), "r");
8766+
ASSERT(varTok && varTok->valueType());
8767+
ASSERT_EQUALS(varTok->valueType()->constness, 3);
8768+
ASSERT_EQUALS(varTok->valueType()->pointer, 1);
8769+
ASSERT(varTok->valueType()->reference == Reference::LValue);
8770+
8771+
const Variable* s = db->variableList()[3];
8772+
ASSERT(s->isReference());
8773+
ASSERT(s->isConst());
8774+
ASSERT(s->isPointer());
8775+
const Token* autoTok = Token::findsimplematch(tokenizer.tokens(), "auto");
8776+
ASSERT(autoTok && autoTok->valueType());
8777+
ASSERT_EQUALS(autoTok->valueType()->constness, 3);
8778+
ASSERT_EQUALS(autoTok->valueType()->pointer, 1);
8779+
TODO_ASSERT(autoTok->valueType()->reference == Reference::LValue);
8780+
}
8781+
87538782
void unionWithConstructor() {
87548783
GET_SYMBOL_DB("union Fred {\n"
87558784
" Fred(int x) : i(x) { }\n"

0 commit comments

Comments
 (0)