diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 379b6c71c..ecd35539b 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -5817,9 +5817,9 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype) Token::Match(parent->tokAt(-1), "%var% ="))) { Token *var1Tok = parent->strAt(-2) == ";" ? parent->tokAt(-3) : parent->tokAt(-1); Token *autoTok = nullptr; - if (Token::Match(var1Tok->tokAt(-2), ";|{|}|(|const auto")) + if (Token::Match(var1Tok->tokAt(-2), ";|{|}|(|const|constexpr auto")) autoTok = var1Tok->previous(); - else if (Token::Match(var1Tok->tokAt(-3), ";|{|}|(|const auto *")) + else if (Token::Match(var1Tok->tokAt(-3), ";|{|}|(|const|constexpr auto *")) autoTok = var1Tok->tokAt(-2); if (autoTok) { ValueType vt(*vt2); @@ -5827,7 +5827,7 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype) vt.constness &= ~(1 << vt.pointer); if (autoTok->strAt(1) == "*" && vt.pointer) vt.pointer--; - if (autoTok->strAt(-1) == "const") + if (Token::Match(autoTok->tokAt(-1), "const|constexpr")) vt.constness |= 1; setValueType(autoTok, vt); setAutoTokenProperties(autoTok); diff --git a/test/testother.cpp b/test/testother.cpp index 235ac48ff..8a61df3ee 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -6415,6 +6415,14 @@ private: " return i >= 0;\n" "}\n", &settings1); ASSERT_EQUALS("", errout.str()); + + // #10612 + check("void f(void) {\n" + " const uint32_t x = 0;\n" + " constexpr const auto y = 0xFFFFU;\n" + " if (y < x) {}\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (style) Checking if unsigned expression 'y' is less than zero.\n", errout.str()); } void checkSignOfPointer() { diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 067d5c939..a20b78174 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -7543,12 +7543,21 @@ private: // auto variables ASSERT_EQUALS("signed int", typeOf("; auto x = 3;", "x")); ASSERT_EQUALS("signed int *", typeOf("; auto *p = (int *)0;", "p")); + ASSERT_EQUALS("const signed int *", typeOf("; auto *p = (const int *)0;", "p")); + ASSERT_EQUALS("const signed int *", typeOf("; auto *p = (constexpr int *)0;", "p")); + ASSERT_EQUALS("const signed int *", typeOf("; const auto *p = (int *)0;", "p")); + ASSERT_EQUALS("const signed int *", typeOf("; constexpr auto *p = (int *)0;", "p")); + ASSERT_EQUALS("const signed int *", typeOf("; const auto *p = (const int *)0;", "p")); + ASSERT_EQUALS("const signed int *", typeOf("; constexpr auto *p = (constexpr int *)0;", "p")); + ASSERT_EQUALS("const signed int *", typeOf("; const constexpr auto *p = (int *)0;", "p")); ASSERT_EQUALS("signed int *", typeOf("; auto data = new int[100];", "data")); ASSERT_EQUALS("signed int", typeOf("; auto data = new X::Y; int x=1000; x=x/5;", "/")); // #7970 ASSERT_EQUALS("signed int *", typeOf("; auto data = new (nothrow) int[100];", "data")); ASSERT_EQUALS("signed int *", typeOf("; auto data = new (std::nothrow) int[100];", "data")); ASSERT_EQUALS("const signed short", typeOf("short values[10]; void f() { for (const auto *x : values); }", "x")); ASSERT_EQUALS("const signed int", typeOf("; const auto x = 3;", "x")); + ASSERT_EQUALS("const signed int", typeOf("; constexpr auto x = 3;", "x")); + ASSERT_EQUALS("const signed int", typeOf("; const constexpr auto x = 3;", "x")); // Variable declaration ASSERT_EQUALS("char *", typeOf("; char abc[] = \"abc\";", "["));