Skip to content

Commit 6711549

Browse files
committed
Fixed #9739 (Tokenizer: simplifyTypedef: wrong simplification in using)
1 parent 7ff6923 commit 6711549

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

lib/tokenize.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,13 +546,34 @@ Token *Tokenizer::processFunc(Token *tok2, bool inOperator) const
546546
return tok2;
547547
}
548548

549+
void Tokenizer::simplifyUsingToTypedef()
550+
{
551+
for (Token *tok = list.front(); tok; tok = tok->next()) {
552+
// using a::b; => typedef a::b b;
553+
if (Token::Match(tok, "[;{}] using %name% :: %name% ::|;") && !tok->tokAt(2)->isKeyword()) {
554+
Token *endtok = tok->tokAt(5);
555+
while (Token::Match(endtok, ":: %name%"))
556+
endtok = endtok->tokAt(2);
557+
if (endtok && endtok->str() == ";") {
558+
tok->next()->str("typedef");
559+
endtok = endtok->previous();
560+
endtok->insertToken(endtok->str());
561+
}
562+
}
563+
}
564+
}
565+
549566
void Tokenizer::simplifyTypedef()
550567
{
551568
std::vector<Space> spaceInfo;
552569
bool isNamespace = false;
553570
std::string className;
554571
bool hasClass = false;
555572
bool goback = false;
573+
574+
// Convert "using a::b;" to corresponding typedef statements
575+
simplifyUsingToTypedef();
576+
556577
for (Token *tok = list.front(); tok; tok = tok->next()) {
557578
if (mErrorLogger && !list.getFiles().empty())
558579
mErrorLogger->reportProgress(list.getFiles()[0], "Tokenize (typedef)", tok->progressValue());

lib/tokenize.h

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

370+
// Convert "using ...;" to corresponding typedef
371+
void simplifyUsingToTypedef();
372+
370373
/**
371374
* typedef A mytype;
372375
* mytype c;

test/testsimplifytypedef.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class TestSimplifyTypedef : public TestFixture {
167167
TEST_CASE(simplifyTypedef129);
168168
TEST_CASE(simplifyTypedef130); // ticket #9446
169169
TEST_CASE(simplifyTypedef131); // ticket #9446
170+
TEST_CASE(simplifyTypedef132); // ticket #9739 - using
170171

171172
TEST_CASE(simplifyTypedefFunction1);
172173
TEST_CASE(simplifyTypedefFunction2); // ticket #1685
@@ -2630,6 +2631,27 @@ class TestSimplifyTypedef : public TestFixture {
26302631
ASSERT_EQUALS(exp, tok(code, false));
26312632
}
26322633

2634+
void simplifyTypedef132() {
2635+
const char code[] = "namespace NamespaceA {\n"
2636+
" typedef int MySpecialType;\n"
2637+
"}\n"
2638+
"\n"
2639+
"class A {\n"
2640+
" void DoSomething( NamespaceA::MySpecialType special );\n"
2641+
"};\n"
2642+
"\n"
2643+
"using NamespaceA::MySpecialType;\n"
2644+
"\n"
2645+
"void A::DoSomething( MySpecialType wrongName ) {}";
2646+
2647+
const char exp [] = "class A { "
2648+
"void DoSomething ( int special ) ; "
2649+
"} ; "
2650+
"void A :: DoSomething ( int wrongName ) { }";
2651+
2652+
ASSERT_EQUALS(exp, tok(code, false));
2653+
}
2654+
26332655
void simplifyTypedefFunction1() {
26342656
{
26352657
const char code[] = "typedef void (*my_func)();\n"

0 commit comments

Comments
 (0)