Skip to content

Commit 550243a

Browse files
authored
Token: cache isCpp() and isC() values (#6933)
1 parent 9130b3b commit 550243a

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

lib/symboldatabase.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,6 +1804,7 @@ void SymbolDatabase::setArrayDimensionsUsingValueFlow()
18041804
// In template arguments, there might not be AST
18051805
// Determine size by using the "raw tokens"
18061806
TokenList tokenList(&mSettings);
1807+
tokenList.setLang(dimension.tok->isCpp() ? Standards::Language::CPP : Standards::Language::C);
18071808
tokenList.addtoken(";", 0, 0, 0, false);
18081809
bool fail = false;
18091810
for (const Token *tok = dimension.tok; tok && !Token::Match(tok, "[,>]"); tok = tok->next()) {

lib/token.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ namespace {
5757

5858
const std::list<ValueFlow::Value> TokenImpl::mEmptyValueList;
5959

60-
Token::Token(TokensFrontBack &tokensFrontBack) :
61-
mTokensFrontBack(tokensFrontBack)
60+
Token::Token(TokensFrontBack &tokensFrontBack)
61+
: mTokensFrontBack(tokensFrontBack)
62+
, mIsC(mTokensFrontBack.list.isC())
63+
, mIsCpp(mTokensFrontBack.list.isCPP())
6264
{
6365
mImpl = new TokenImpl();
6466
}
@@ -2653,16 +2655,6 @@ const Token* findLambdaEndScope(const Token* tok) {
26532655
return findLambdaEndScope(const_cast<Token*>(tok));
26542656
}
26552657

2656-
bool Token::isCpp() const
2657-
{
2658-
return mTokensFrontBack.list.isCPP();
2659-
}
2660-
2661-
bool Token::isC() const
2662-
{
2663-
return mTokensFrontBack.list.isC();
2664-
}
2665-
26662658
const std::string& Token::fileName() const {
26672659
return mTokensFrontBack.list.getFiles()[mImpl->mFileIndex];
26682660
}

lib/token.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,10 @@ class CPPCHECKLIB Token {
14391439
/** Internal helper function to avoid excessive string allocations */
14401440
void astStringVerboseRecursive(std::string& ret, nonneg int indent1 = 0, nonneg int indent2 = 0) const;
14411441

1442+
// cppcheck-suppress premium-misra-cpp-2023-12.2.1
1443+
bool mIsC : 1;
1444+
bool mIsCpp : 1;
1445+
14421446
public:
14431447
void astOperand1(Token *tok);
14441448
void astOperand2(Token *tok);
@@ -1548,9 +1552,15 @@ class CPPCHECKLIB Token {
15481552
mImpl->mDebug = td;
15491553
}
15501554

1551-
bool isCpp() const;
1555+
bool isCpp() const
1556+
{
1557+
return mIsCpp;
1558+
}
15521559

1553-
bool isC() const;
1560+
bool isC() const
1561+
{
1562+
return mIsC;
1563+
}
15541564
};
15551565

15561566
Token* findTypeEnd(Token* tok);

test/testtokenlist.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535

3636
class TestTokenList : public TestFixture {
3737
public:
38-
TestTokenList() : TestFixture("TestTokenList") {}
38+
TestTokenList() : TestFixture("TestTokenList") {
39+
settings.enforcedLang = Standards::Language::C;
40+
}
3941

4042
private:
41-
const Settings settings;
43+
/*const*/ Settings settings;
4244

4345
void run() override {
4446
TEST_CASE(testaddtoken1);
@@ -59,7 +61,7 @@ class TestTokenList : public TestFixture {
5961

6062
void testaddtoken2() const {
6163
const std::string code = "0xF0000000";
62-
/*const*/ Settings settings1;
64+
/*const*/ Settings settings1 = settings;
6365
settings1.platform.int_bit = 32;
6466
TokenList tokenlist(&settings1);
6567
tokenlist.addtoken(code, 1, 1, false);

0 commit comments

Comments
 (0)