Skip to content

Commit 62c30ad

Browse files
committed
Add Valuetype type and sign for stdint macros
1 parent c0b4c73 commit 62c30ad

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

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
@@ -7531,6 +7531,40 @@ static const Function* getFunction(const Token* tok) {
75317531
return nullptr;
75327532
}
75337533

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

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