SymbolDatabase: Fix valuetype with constexpr and auto (#3577)

This commit is contained in:
Rikard Falkeborn 2021-11-24 16:51:40 +01:00 committed by GitHub
parent a6b8339990
commit 085d25f1b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 3 deletions

View File

@ -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);

View File

@ -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() {

View File

@ -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\";", "["));