Skip to content

Commit

Permalink
Fix #12178 extern "C++" scope generates valueflow (#5654)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Nov 14, 2023
1 parent 9228b9e commit 6ac804d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
7 changes: 4 additions & 3 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3592,17 +3592,18 @@ void Tokenizer::simplifyExternC()

// Add attributes to all tokens within `extern "C"` inlines and blocks, and remove the `extern "C"` tokens.
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::simpleMatch(tok, "extern \"C\"")) {
if (Token::Match(tok, "extern \"C\"|\"C++\"")) {
Token *tok2 = tok->next();
const bool isExtC = tok->next()->str().size() == 3;
if (tok->strAt(2) == "{") {
tok2 = tok2->next(); // skip {
while ((tok2 = tok2->next()) && tok2 != tok->linkAt(2))
tok2->isExternC(true);
tok2->isExternC(isExtC);
tok->linkAt(2)->deleteThis(); // }
tok->deleteNext(2); // "C" {
} else {
while ((tok2 = tok2->next()) && !Token::Match(tok2, "[;{]"))
tok2->isExternC(true);
tok2->isExternC(isExtC);
tok->deleteNext(); // "C"
}
tok->deleteThis(); // extern
Expand Down
3 changes: 2 additions & 1 deletion test/testsimplifytemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4163,7 +4163,8 @@ class TestSimplifyTemplate : public TestFixture {

void template163() { // #9685 syntax error
const char code[] = "extern \"C++\" template < typename T > T * test ( ) { return nullptr ; }";
ASSERT_EQUALS(code, tok(code));
const char expected[] = "template < typename T > T * test ( ) { return nullptr ; }";
ASSERT_EQUALS(expected, tok(code));
}

void template164() { // #9394
Expand Down
43 changes: 41 additions & 2 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1548,8 +1548,47 @@ class TestTokenizer : public TestFixture {


void simplifyExternC() {
ASSERT_EQUALS("int foo ( ) ;", tokenizeAndStringify("extern \"C\" int foo();"));
ASSERT_EQUALS("int foo ( ) ;", tokenizeAndStringify("extern \"C\" { int foo(); }"));
const char expected[] = "int foo ( ) ;";
{
const char code[] = "extern \"C\" int foo();";
// tokenize..
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
ASSERT(tokenizer.tokenize(istr, "test.cpp"));
// Expected result..
ASSERT_EQUALS(expected, tokenizer.tokens()->stringifyList(nullptr, false));
ASSERT(tokenizer.tokens()->next()->isExternC());
}
{
const char code[] = "extern \"C\" { int foo(); }";
// tokenize..
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
ASSERT(tokenizer.tokenize(istr, "test.cpp"));
// Expected result..
ASSERT_EQUALS(expected, tokenizer.tokens()->stringifyList(nullptr, false));
ASSERT(tokenizer.tokens()->next()->isExternC());
}
{
const char code[] = "extern \"C++\" int foo();";
// tokenize..
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
ASSERT(tokenizer.tokenize(istr, "test.cpp"));
// Expected result..
ASSERT_EQUALS(expected, tokenizer.tokens()->stringifyList(nullptr, false));
ASSERT(!tokenizer.tokens()->next()->isExternC());
}
{
const char code[] = "extern \"C++\" { int foo(); }";
// tokenize..
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
ASSERT(tokenizer.tokenize(istr, "test.cpp"));
// Expected result..
ASSERT_EQUALS(expected, tokenizer.tokens()->stringifyList(nullptr, false));
ASSERT(!tokenizer.tokens()->next()->isExternC());
}
}

void simplifyFunctionParameters() {
Expand Down

0 comments on commit 6ac804d

Please sign in to comment.