Skip to content

Commit 68454d6

Browse files
authored
#14173: Add Valuetype type and sign for stdint macros (danmar#7869)
1 parent e3c8bdc commit 68454d6

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

addons/test/misra/misra-test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ static void misra_10_3(uint32_t u32a, uint32_t u32b) {
725725
res = 0.1f; // 10.3
726726
const char c = '0'; // no-warning
727727
bool b = true; // no-warning
728-
uint32_t u = UINT32_C(10); // 17.3 no-warning
728+
uint32_t u = UINT32_C(10); // no-warning
729729
}
730730

731731
static void misra_10_4(u32 x, s32 y) {

lib/symboldatabase.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7530,6 +7530,40 @@ static const Function* getFunction(const Token* tok) {
75307530
return nullptr;
75317531
}
75327532

7533+
static int getIntegerConstantMacroWidth(const Token* tok) {
7534+
if (!Token::Match(tok, "%name% (") || Token::simpleMatch(tok->next()->astOperand2(), ","))
7535+
return 0;
7536+
const std::string &name = tok->str();
7537+
if (name.back() != 'C')
7538+
return 0;
7539+
size_t pos = (name[0] == 'U') ? 1 : 0;
7540+
if (name[pos] != 'I' || name[pos + 1] != 'N' || name[pos + 2] != 'T')
7541+
return 0;
7542+
pos += 3;
7543+
int intnum = 0;
7544+
if (name[pos] == '8') {
7545+
++pos;
7546+
intnum = 8;
7547+
}
7548+
else if (name[pos] == '1' && name[pos + 1] == '6') {
7549+
pos += 2;
7550+
intnum = 16;
7551+
}
7552+
else if (name[pos] == '3' && name[pos + 1] == '2') {
7553+
pos += 2;
7554+
intnum = 32;
7555+
}
7556+
else if (name[pos] == '6' && name[pos + 1] == '4') {
7557+
pos += 2;
7558+
intnum = 64;
7559+
}
7560+
else
7561+
return 0;
7562+
if (pos + 2 != name.size() || name[pos] != '_')
7563+
return 0;
7564+
return intnum;
7565+
}
7566+
75337567
void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *tokens)
75347568
{
75357569
if (!tokens)
@@ -7671,6 +7705,29 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
76717705
}
76727706
}
76737707

7708+
// functions from stdint.h
7709+
else if (const int macroWidth = getIntegerConstantMacroWidth(tok->previous())) {
7710+
ValueType valuetype;
7711+
if (macroWidth == mSettings.platform.char_bit)
7712+
valuetype.type = ValueType::Type::CHAR;
7713+
else if (macroWidth == mSettings.platform.short_bit)
7714+
valuetype.type = ValueType::Type::SHORT;
7715+
else if (macroWidth == mSettings.platform.int_bit)
7716+
valuetype.type = ValueType::Type::INT;
7717+
else if (macroWidth == mSettings.platform.long_bit)
7718+
valuetype.type = ValueType::Type::LONG;
7719+
else if (macroWidth == mSettings.platform.long_long_bit)
7720+
valuetype.type = ValueType::Type::LONGLONG;
7721+
else
7722+
valuetype.type = ValueType::Type::INT;
7723+
7724+
if (tok->strAt(-1)[0] == 'U')
7725+
valuetype.sign = ValueType::Sign::UNSIGNED;
7726+
else
7727+
valuetype.sign = ValueType::Sign::SIGNED;
7728+
setValueType(tok, valuetype);
7729+
}
7730+
76747731
// function style cast
76757732
else if (tok->previous() && tok->previous()->isStandardType()) {
76767733
ValueType valuetype;

test/testsymboldatabase.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,8 @@ class TestSymbolDatabase : public TestFixture {
622622
TEST_CASE(dumpFriend); // Check if isFriend added to dump file
623623

624624
TEST_CASE(smartPointerLookupCtor); // #13719);
625+
626+
TEST_CASE(stdintFunction);
625627
}
626628

627629
void array() {
@@ -11317,6 +11319,14 @@ class TestSymbolDatabase : public TestFixture {
1131711319

1131811320
ASSERT(db);
1131911321
}
11322+
11323+
void stdintFunction() {
11324+
GET_SYMBOL_DB("a = UINT32_C(60);");
11325+
const Token* tok = Token::findsimplematch(tokenizer.tokens(), "UINT32_C (");
11326+
ASSERT(tok != nullptr);
11327+
ASSERT_EQUALS(tok->next()->valueType()->sign, ValueType::Sign::UNSIGNED);
11328+
ASSERT_EQUALS(tok->next()->valueType()->type, ValueType::Type::INT);
11329+
}
1132011330
};
1132111331

1132211332
REGISTER_TEST(TestSymbolDatabase)

0 commit comments

Comments
 (0)