Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Token: cache isCpp() and isC() values #6933

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,7 @@ void SymbolDatabase::setArrayDimensionsUsingValueFlow()
// In template arguments, there might not be AST
// Determine size by using the "raw tokens"
TokenList tokenList(&mSettings);
tokenList.setLang(dimension.tok->isCpp() ? Standards::Language::CPP : Standards::Language::C);
tokenList.addtoken(";", 0, 0, 0, false);
bool fail = false;
for (const Token *tok = dimension.tok; tok && !Token::Match(tok, "[,>]"); tok = tok->next()) {
Expand Down
16 changes: 4 additions & 12 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ namespace {

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

Token::Token(TokensFrontBack &tokensFrontBack) :
mTokensFrontBack(tokensFrontBack)
Token::Token(TokensFrontBack &tokensFrontBack)
: mTokensFrontBack(tokensFrontBack)
, mIsC(mTokensFrontBack.list.isC())
, mIsCpp(mTokensFrontBack.list.isCPP())
{
mImpl = new TokenImpl();
}
Expand Down Expand Up @@ -2652,13 +2654,3 @@ Token* findLambdaEndScope(Token* tok)
const Token* findLambdaEndScope(const Token* tok) {
return findLambdaEndScope(const_cast<Token*>(tok));
}

bool Token::isCpp() const
{
return mTokensFrontBack.list.isCPP();
}

bool Token::isC() const
{
return mTokensFrontBack.list.isC();
}
13 changes: 11 additions & 2 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,9 @@ class CPPCHECKLIB Token {
/** Internal helper function to avoid excessive string allocations */
void astStringVerboseRecursive(std::string& ret, nonneg int indent1 = 0, nonneg int indent2 = 0) const;

bool mIsC : 1;
bool mIsCpp : 1;

public:
void astOperand1(Token *tok);
void astOperand2(Token *tok);
Expand Down Expand Up @@ -1546,9 +1549,15 @@ class CPPCHECKLIB Token {
mImpl->mDebug = td;
}

bool isCpp() const;
bool isCpp() const
{
return mIsCpp;
}

bool isC() const;
bool isC() const
{
return mIsC;
}
};

Token* findTypeEnd(Token* tok);
Expand Down
8 changes: 5 additions & 3 deletions test/testtokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@

class TestTokenList : public TestFixture {
public:
TestTokenList() : TestFixture("TestTokenList") {}
TestTokenList() : TestFixture("TestTokenList") {
settings.enforcedLang = Standards::Language::C;
}

private:
const Settings settings;
/*const*/ Settings settings;

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

void testaddtoken2() const {
const std::string code = "0xF0000000";
/*const*/ Settings settings1;
/*const*/ Settings settings1 = settings;
settings1.platform.int_bit = 32;
TokenList tokenlist(&settings1);
tokenlist.addtoken(code, 1, 1, false);
Expand Down
Loading