Skip to content

Fix #13847 (simplifyTypedef should not simplify all using statements) #7527

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

Merged
merged 1 commit into from
May 17, 2025
Merged
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
33 changes: 5 additions & 28 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,31 +503,6 @@ Token *Tokenizer::processFunc(Token *tok2, bool inOperator)
return const_cast<Token*>(processFunc(const_cast<const Token*>(tok2), inOperator));
}

void Tokenizer::simplifyUsingToTypedef()
{
if (!isCPP() || mSettings.standards.cpp < Standards::CPP11)
return;

for (Token *tok = list.front(); tok; tok = tok->next()) {
// using a::b; => typedef a::b b;
if ((Token::Match(tok, "[;{}] using %name% :: %name% ::|;") && !tok->tokAt(2)->isKeyword()) ||
(Token::Match(tok, "[;{}] using :: %name% :: %name% ::|;") && !tok->tokAt(3)->isKeyword())) {
Token *endtok = tok->tokAt(5);
if (Token::Match(endtok, "%name%"))
endtok = endtok->next();
while (Token::Match(endtok, ":: %name%"))
endtok = endtok->tokAt(2);
if (endtok && endtok->str() == ";") {
if (endtok->strAt(-1) == endtok->strAt(-3))
continue;
tok->next()->str("typedef");
endtok = endtok->previous();
endtok->insertToken(endtok->str());
}
}
}
}

void Tokenizer::simplifyTypedefLHS()
{
if (!list.front())
Expand Down Expand Up @@ -1157,9 +1132,6 @@ void Tokenizer::simplifyTypedefCpp()
// add global namespace
std::vector<Space> spaceInfo(1);

// Convert "using a::b;" to corresponding typedef statements
simplifyUsingToTypedef();

const std::time_t maxTime = mSettings.typedefMaxTime > 0 ? std::time(nullptr) + mSettings.typedefMaxTime: 0;
const bool doProgress = (mSettings.reportProgress != -1) && !list.getFiles().empty();

Expand Down Expand Up @@ -1975,6 +1947,11 @@ void Tokenizer::simplifyTypedefCpp()
}
if (Token::Match(tok2->tokAt(-1), "class|struct|union") && tok2->strAt(-1) == typeStart->str())
tok2->deletePrevious();

if (cpp && Token::Match(tok2->previous(), "using %name% ::|;")) {
tok2->previous()->str("typedef");
tok2->insertToken(tok2->str());
}
tok2->str(typeStart->str());

// restore qualification if it was removed
Expand Down
3 changes: 0 additions & 3 deletions lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,6 @@ class CPPCHECKLIB Tokenizer {
*/
Token * simplifyAddBracesPair(Token *tok, bool commandWithCondition);

// Convert "using ...;" to corresponding typedef
void simplifyUsingToTypedef();

/**
* typedef A mytype;
* mytype c;
Expand Down
10 changes: 9 additions & 1 deletion test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(simplifyTypedefTokenColumn3);

TEST_CASE(typedefInfo1);

TEST_CASE(typedefInfo2);
TEST_CASE(typedefInfo3);
}

struct TokOptions
Expand Down Expand Up @@ -4562,6 +4562,14 @@ class TestSimplifyTypedef : public TestFixture {
" <info name=\"pfp16\" file=\"file.c\" line=\"4\" column=\"20\" used=\"0\" isFunctionPointer=\"1\"/>\n"
" </typedef-info>\n",xml);
}

void typedefInfo3() {
const std::string xml = dumpTypedefInfo("int main() {\n"
" using x::a;\n"
" b = a + 2;\n"
"}\n");
ASSERT_EQUALS("",xml);
}
};

REGISTER_TEST(TestSimplifyTypedef)
13 changes: 13 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class TestTokenizer : public TestFixture {
TEST_CASE(tokenize38); // #9569
TEST_CASE(tokenize39); // #9771
TEST_CASE(tokenize40); // #13181
TEST_CASE(tokenize41); // #13847

TEST_CASE(validate);

Expand Down Expand Up @@ -860,6 +861,18 @@ class TestTokenizer : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void tokenize41() { // #13847
const char code[] = "int main() {\n"
" using x::a;\n"
" b = a + 2;\n"
"}\n";
ASSERT_EQUALS("int main ( ) {\n"
"\n"
"b = x :: a + 2 ;\n"
"}", tokenizeAndStringify(code));
(void)errout_str();
}

void validate() {
// C++ code in C file
ASSERT_THROW_INTERNAL(tokenizeAndStringify(";using namespace std;",false,Platform::Type::Native,false), SYNTAX);
Expand Down
Loading