Skip to content
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
4 changes: 2 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4229,7 +4229,7 @@ static bool setVarIdParseDeclaration(Token*& tok, const VariableMap& variableMap
}
if (tok2->isCpp() && Token::Match(tok2, "namespace|public|private|protected"))
return false;
if (tok2->isCpp() && Token::simpleMatch(tok2, "decltype (")) {
if (tok2->isCpp() && Token::Match(tok2, "decltype|typeof|__typeof (")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also a GCC extension (https://gcc.gnu.org/onlinedocs/gcc/Typeof.html). But I'm not sure if we should check the standard here at all, it would silentlty fail if it's not configured correctly, and I don't know if there's any point in ever not setting the varid.

Copy link
Collaborator

@chrchr-github chrchr-github Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My point was that the code is currently only executed for C++. Maybe we should handle typeof etc. in C as well.

typeCount = 1;
tok2 = tok2->linkAt(1)->next();
continue;
Expand Down Expand Up @@ -4782,7 +4782,7 @@ void Tokenizer::setVarIdPass1()
variableMap.map(true),
mTemplateVarIdUsage);
}
if (Token *declTypeTok = Token::findsimplematch(tok, "decltype (", tok2)) {
if (Token *declTypeTok = Token::findmatch(tok, "decltype|typeof|__typeof (", tok2)) {
for (Token *declTok = declTypeTok->linkAt(1); declTok != declTypeTok; declTok = declTok->previous()) {
if (declTok->isName() && !Token::Match(declTok->previous(), "::|.") && variableMap.hasVariable(declTok->str()))
declTok->varId(variableMap.map(false).find(declTok->str())->second);
Expand Down
29 changes: 29 additions & 0 deletions test/testvarid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ class TestVarID : public TestFixture {
TEST_CASE(decltype1);
TEST_CASE(decltype2);

TEST_CASE(typeof1);
TEST_CASE(typeof2);
TEST_CASE(typeof3);
TEST_CASE(typeof4);

TEST_CASE(exprid1);
TEST_CASE(exprid2);
TEST_CASE(exprid3);
Expand Down Expand Up @@ -4189,6 +4194,30 @@ class TestVarID : public TestFixture {
ASSERT_EQUALS(expected, tokenize(code));
}

void typeof1() {
const char code[] = "int x; typeof(x) y;";
const char expected[] = "1: int x@1 ; typeof ( x@1 ) y@2 ;\n";
ASSERT_EQUALS(expected, tokenize(code));
}

void typeof2() {
const char code[] = "int x; typeof(x) *y;";
const char expected[] = "1: int x@1 ; typeof ( x@1 ) * y@2 ;\n";
ASSERT_EQUALS(expected, tokenize(code));
}

void typeof3() {
const char code[] = "int x; const typeof(x) *const y;";
const char expected[] = "1: int x@1 ; const typeof ( x@1 ) * const y@2 ;\n";
ASSERT_EQUALS(expected, tokenize(code));
}

void typeof4() {
const char code[] = "int x; __typeof(x) y;";
const char expected[] = "1: int x@1 ; __typeof ( x@1 ) y@2 ;\n";
ASSERT_EQUALS(expected, tokenize(code));
}

void exprid1() {
const std::string actual = tokenizeExpr(
"struct A {\n"
Expand Down