Skip to content

Commit 085d25f

Browse files
SymbolDatabase: Fix valuetype with constexpr and auto (#3577)
1 parent a6b8339 commit 085d25f

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5817,17 +5817,17 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype)
58175817
Token::Match(parent->tokAt(-1), "%var% ="))) {
58185818
Token *var1Tok = parent->strAt(-2) == ";" ? parent->tokAt(-3) : parent->tokAt(-1);
58195819
Token *autoTok = nullptr;
5820-
if (Token::Match(var1Tok->tokAt(-2), ";|{|}|(|const auto"))
5820+
if (Token::Match(var1Tok->tokAt(-2), ";|{|}|(|const|constexpr auto"))
58215821
autoTok = var1Tok->previous();
5822-
else if (Token::Match(var1Tok->tokAt(-3), ";|{|}|(|const auto *"))
5822+
else if (Token::Match(var1Tok->tokAt(-3), ";|{|}|(|const|constexpr auto *"))
58235823
autoTok = var1Tok->tokAt(-2);
58245824
if (autoTok) {
58255825
ValueType vt(*vt2);
58265826
if (vt.constness & (1 << vt.pointer))
58275827
vt.constness &= ~(1 << vt.pointer);
58285828
if (autoTok->strAt(1) == "*" && vt.pointer)
58295829
vt.pointer--;
5830-
if (autoTok->strAt(-1) == "const")
5830+
if (Token::Match(autoTok->tokAt(-1), "const|constexpr"))
58315831
vt.constness |= 1;
58325832
setValueType(autoTok, vt);
58335833
setAutoTokenProperties(autoTok);

test/testother.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6415,6 +6415,14 @@ class TestOther : public TestFixture {
64156415
" return i >= 0;\n"
64166416
"}\n", &settings1);
64176417
ASSERT_EQUALS("", errout.str());
6418+
6419+
// #10612
6420+
check("void f(void) {\n"
6421+
" const uint32_t x = 0;\n"
6422+
" constexpr const auto y = 0xFFFFU;\n"
6423+
" if (y < x) {}\n"
6424+
"}");
6425+
ASSERT_EQUALS("[test.cpp:4]: (style) Checking if unsigned expression 'y' is less than zero.\n", errout.str());
64186426
}
64196427

64206428
void checkSignOfPointer() {

test/testsymboldatabase.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7543,12 +7543,21 @@ class TestSymbolDatabase : public TestFixture {
75437543
// auto variables
75447544
ASSERT_EQUALS("signed int", typeOf("; auto x = 3;", "x"));
75457545
ASSERT_EQUALS("signed int *", typeOf("; auto *p = (int *)0;", "p"));
7546+
ASSERT_EQUALS("const signed int *", typeOf("; auto *p = (const int *)0;", "p"));
7547+
ASSERT_EQUALS("const signed int *", typeOf("; auto *p = (constexpr int *)0;", "p"));
7548+
ASSERT_EQUALS("const signed int *", typeOf("; const auto *p = (int *)0;", "p"));
7549+
ASSERT_EQUALS("const signed int *", typeOf("; constexpr auto *p = (int *)0;", "p"));
7550+
ASSERT_EQUALS("const signed int *", typeOf("; const auto *p = (const int *)0;", "p"));
7551+
ASSERT_EQUALS("const signed int *", typeOf("; constexpr auto *p = (constexpr int *)0;", "p"));
7552+
ASSERT_EQUALS("const signed int *", typeOf("; const constexpr auto *p = (int *)0;", "p"));
75467553
ASSERT_EQUALS("signed int *", typeOf("; auto data = new int[100];", "data"));
75477554
ASSERT_EQUALS("signed int", typeOf("; auto data = new X::Y; int x=1000; x=x/5;", "/")); // #7970
75487555
ASSERT_EQUALS("signed int *", typeOf("; auto data = new (nothrow) int[100];", "data"));
75497556
ASSERT_EQUALS("signed int *", typeOf("; auto data = new (std::nothrow) int[100];", "data"));
75507557
ASSERT_EQUALS("const signed short", typeOf("short values[10]; void f() { for (const auto *x : values); }", "x"));
75517558
ASSERT_EQUALS("const signed int", typeOf("; const auto x = 3;", "x"));
7559+
ASSERT_EQUALS("const signed int", typeOf("; constexpr auto x = 3;", "x"));
7560+
ASSERT_EQUALS("const signed int", typeOf("; const constexpr auto x = 3;", "x"));
75527561

75537562
// Variable declaration
75547563
ASSERT_EQUALS("char *", typeOf("; char abc[] = \"abc\";", "["));

0 commit comments

Comments
 (0)