Skip to content

Commit 5a4e437

Browse files
Handle auto * const (#4994)
* Handle auto as first token * Set varid when initialized by function * Fix TODO from #11444 * Fix function parsing * Add parentheses * Format * Handle auto * const * Fix test on different platforms * simpleMatch * simpleMatch
1 parent ad464c4 commit 5a4e437

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/symboldatabase.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6202,6 +6202,8 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, Source
62026202
autoTok = var1Tok->previous();
62036203
else if (Token::Match(var1Tok->tokAt(-2), "auto *|&|&&"))
62046204
autoTok = var1Tok->tokAt(-2);
6205+
else if (Token::simpleMatch(var1Tok->tokAt(-3), "auto * const"))
6206+
autoTok = var1Tok->tokAt(-3);
62056207
if (autoTok) {
62066208
ValueType vt(*vt2);
62076209
if (vt.constness & (1 << vt.pointer))
@@ -6226,6 +6228,8 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, Source
62266228
vt2_.constness |= (1 << vt2->pointer);
62276229
if (!Token::Match(autoTok->tokAt(1), "*|&"))
62286230
vt2_.constness = vt.constness;
6231+
if (Token::simpleMatch(autoTok->tokAt(1), "* const"))
6232+
vt2_.constness |= (1 << vt2->pointer);
62296233
var->setValueType(vt2_);
62306234
if (vt2->typeScope && vt2->typeScope->definedType) {
62316235
var->type(vt2->typeScope->definedType);

test/testsymboldatabase.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8401,6 +8401,24 @@ class TestSymbolDatabase : public TestFixture {
84018401
ASSERT(tok && tok->valueType());
84028402
ASSERT_EQUALS("signed int", tok->valueType()->str());
84038403
}
8404+
{
8405+
GET_SYMBOL_DB("void f(const std::string& s) {\n"
8406+
" const auto* const p = s.data();\n"
8407+
"}\n");
8408+
ASSERT_EQUALS("", errout.str());
8409+
8410+
const Token* tok = tokenizer.tokens();
8411+
tok = Token::findsimplematch(tok, "auto");
8412+
ASSERT(tok && tok->valueType());
8413+
ASSERT_EQUALS(ValueType::CHAR, tok->valueType()->type);
8414+
ASSERT_EQUALS(1, tok->valueType()->constness);
8415+
ASSERT_EQUALS(0, tok->valueType()->pointer);
8416+
tok = Token::findsimplematch(tok, "p");
8417+
ASSERT(tok && tok->variable() && tok->variable()->valueType());
8418+
ASSERT_EQUALS(ValueType::CHAR, tok->variable()->valueType()->type);
8419+
ASSERT_EQUALS(3, tok->variable()->valueType()->constness);
8420+
ASSERT_EQUALS(1, tok->variable()->valueType()->pointer);
8421+
}
84048422
}
84058423

84068424
void valueTypeThis() {

0 commit comments

Comments
 (0)