Skip to content

Commit e4b51d7

Browse files
authored
Fix #13847 (simplifyTypedef should not simplify all using statements) (#7527)
1 parent 69a11d6 commit e4b51d7

File tree

4 files changed

+27
-32
lines changed

4 files changed

+27
-32
lines changed

lib/tokenize.cpp

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -503,31 +503,6 @@ Token *Tokenizer::processFunc(Token *tok2, bool inOperator)
503503
return const_cast<Token*>(processFunc(const_cast<const Token*>(tok2), inOperator));
504504
}
505505

506-
void Tokenizer::simplifyUsingToTypedef()
507-
{
508-
if (!isCPP() || mSettings.standards.cpp < Standards::CPP11)
509-
return;
510-
511-
for (Token *tok = list.front(); tok; tok = tok->next()) {
512-
// using a::b; => typedef a::b b;
513-
if ((Token::Match(tok, "[;{}] using %name% :: %name% ::|;") && !tok->tokAt(2)->isKeyword()) ||
514-
(Token::Match(tok, "[;{}] using :: %name% :: %name% ::|;") && !tok->tokAt(3)->isKeyword())) {
515-
Token *endtok = tok->tokAt(5);
516-
if (Token::Match(endtok, "%name%"))
517-
endtok = endtok->next();
518-
while (Token::Match(endtok, ":: %name%"))
519-
endtok = endtok->tokAt(2);
520-
if (endtok && endtok->str() == ";") {
521-
if (endtok->strAt(-1) == endtok->strAt(-3))
522-
continue;
523-
tok->next()->str("typedef");
524-
endtok = endtok->previous();
525-
endtok->insertToken(endtok->str());
526-
}
527-
}
528-
}
529-
}
530-
531506
void Tokenizer::simplifyTypedefLHS()
532507
{
533508
if (!list.front())
@@ -1157,9 +1132,6 @@ void Tokenizer::simplifyTypedefCpp()
11571132
// add global namespace
11581133
std::vector<Space> spaceInfo(1);
11591134

1160-
// Convert "using a::b;" to corresponding typedef statements
1161-
simplifyUsingToTypedef();
1162-
11631135
const std::time_t maxTime = mSettings.typedefMaxTime > 0 ? std::time(nullptr) + mSettings.typedefMaxTime: 0;
11641136
const bool doProgress = (mSettings.reportProgress != -1) && !list.getFiles().empty();
11651137

@@ -1975,6 +1947,11 @@ void Tokenizer::simplifyTypedefCpp()
19751947
}
19761948
if (Token::Match(tok2->tokAt(-1), "class|struct|union") && tok2->strAt(-1) == typeStart->str())
19771949
tok2->deletePrevious();
1950+
1951+
if (cpp && Token::Match(tok2->previous(), "using %name% ::|;")) {
1952+
tok2->previous()->str("typedef");
1953+
tok2->insertToken(tok2->str());
1954+
}
19781955
tok2->str(typeStart->str());
19791956

19801957
// restore qualification if it was removed

lib/tokenize.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,6 @@ class CPPCHECKLIB Tokenizer {
218218
*/
219219
Token * simplifyAddBracesPair(Token *tok, bool commandWithCondition);
220220

221-
// Convert "using ...;" to corresponding typedef
222-
void simplifyUsingToTypedef();
223-
224221
/**
225222
* typedef A mytype;
226223
* mytype c;

test/testsimplifytypedef.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ class TestSimplifyTypedef : public TestFixture {
249249
TEST_CASE(simplifyTypedefTokenColumn3);
250250

251251
TEST_CASE(typedefInfo1);
252-
253252
TEST_CASE(typedefInfo2);
253+
TEST_CASE(typedefInfo3);
254254
}
255255

256256
struct TokOptions
@@ -4562,6 +4562,14 @@ class TestSimplifyTypedef : public TestFixture {
45624562
" <info name=\"pfp16\" file=\"file.c\" line=\"4\" column=\"20\" used=\"0\" isFunctionPointer=\"1\"/>\n"
45634563
" </typedef-info>\n",xml);
45644564
}
4565+
4566+
void typedefInfo3() {
4567+
const std::string xml = dumpTypedefInfo("int main() {\n"
4568+
" using x::a;\n"
4569+
" b = a + 2;\n"
4570+
"}\n");
4571+
ASSERT_EQUALS("",xml);
4572+
}
45654573
};
45664574

45674575
REGISTER_TEST(TestSimplifyTypedef)

test/testtokenize.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class TestTokenizer : public TestFixture {
8080
TEST_CASE(tokenize38); // #9569
8181
TEST_CASE(tokenize39); // #9771
8282
TEST_CASE(tokenize40); // #13181
83+
TEST_CASE(tokenize41); // #13847
8384

8485
TEST_CASE(validate);
8586

@@ -860,6 +861,18 @@ class TestTokenizer : public TestFixture {
860861
ASSERT_EQUALS("", errout_str());
861862
}
862863

864+
void tokenize41() { // #13847
865+
const char code[] = "int main() {\n"
866+
" using x::a;\n"
867+
" b = a + 2;\n"
868+
"}\n";
869+
ASSERT_EQUALS("int main ( ) {\n"
870+
"\n"
871+
"b = x :: a + 2 ;\n"
872+
"}", tokenizeAndStringify(code));
873+
(void)errout_str();
874+
}
875+
863876
void validate() {
864877
// C++ code in C file
865878
ASSERT_THROW_INTERNAL(tokenizeAndStringify(";using namespace std;",false,Platform::Type::Native,false), SYNTAX);

0 commit comments

Comments
 (0)