Skip to content

Commit 6332bbf

Browse files
committed
use seperate flag for copied macro names
1 parent 0087f08 commit 6332bbf

4 files changed

Lines changed: 23 additions & 7 deletions

File tree

‎lib/token.h‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,12 @@ class CPPCHECKLIB Token {
500500
setFlag(fIsStandardType, b);
501501
}
502502
/**
503-
* Is this token macro code at this location? Tokens copied from a
504-
* typedef are excluded, see hasMacroName().
503+
* Is this token macro code at this location? Tokens whose macro name was
504+
* copied from another token (e.g. a typedef definition) are excluded,
505+
* see hasMacroName().
505506
*/
506507
bool isExpandedMacro() const {
507-
return mImpl->mMacroName && !isSimplifiedTypedef();
508+
return mImpl->mMacroName && !getFlag(fIsMacroNameCopied);
508509
}
509510
/**
510511
* Did the token text originate from a macro? Also true for tokens copied
@@ -877,6 +878,12 @@ class CPPCHECKLIB Token {
877878
else
878879
*mImpl->mMacroName = std::move(name);
879880
}
881+
void copyMacroName(const Token* from) {
882+
if (from->hasMacroName()) {
883+
setMacroName(from->getMacroName());
884+
setFlag(fIsMacroNameCopied, true);
885+
}
886+
}
880887

881888
template<size_t count>
882889
static const Token *findsimplematch(const Token * const startTok, const char (&pattern)[count]) {
@@ -1533,6 +1540,7 @@ class CPPCHECKLIB Token {
15331540
fIsInitBracket = (1ULL << 44), // Is this bracket used as a part of variable initialization i.e: int a{5}, b(2);
15341541
fIsAnonymous = (1ULL << 45), // Is this a token added for an unnamed member
15351542
fIsInsertedBrace = (1ULL << 46), // brace added when simplifying e.g. if (x) f(); => if (x) { f(); }
1543+
fIsMacroNameCopied = (1ULL << 47), // macro name copied from another token, e.g. a typedef definition
15361544
};
15371545

15381546
enum : std::uint8_t {

‎lib/tokenize.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,8 +1033,7 @@ namespace {
10331033
to = to->next();
10341034
to->isSimplifiedTypedef(true);
10351035
to->isExternC(from->isExternC());
1036-
if (from->hasMacroName())
1037-
to->setMacroName(from->getMacroName());
1036+
to->copyMacroName(from);
10381037
}
10391038
return to;
10401039
}

‎lib/tokenlist.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ Token *TokenList::copyTokens(Token *dest, const Token *first, const Token *last,
267267
tok2->flags(tok->flags());
268268
tok2->varId(tok->varId());
269269
tok2->setTokenDebug(tok->getTokenDebug());
270-
if (tok->hasMacroName())
271-
tok2->setMacroName(tok->getMacroName());
270+
tok2->copyMacroName(tok);
272271

273272
// Check for links and fix them up
274273
if (Token::Match(tok2, "(|[|{"))

‎test/testtokenize.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ class TestTokenizer : public TestFixture {
536536
TEST_CASE(constFunctionPtrTypedef); // #12135
537537

538538
TEST_CASE(simplifyPlatformTypes);
539+
TEST_CASE(platformTypeExpandedMacro);
539540

540541
TEST_CASE(dumpFallthrough);
541542
TEST_CASE(dumpAttributeUsed);
@@ -9390,6 +9391,15 @@ class TestTokenizer : public TestFixture {
93909391
ASSERT_EQUALS("void ( * const f ) ( ) ;", tokenizeAndStringify("typedef void (*fp_t)(); fp_t const f;\n"));
93919392
}
93929393

9394+
void platformTypeExpandedMacro() {
9395+
const char code[] = "#define A DWORD\n"
9396+
"A x;\n";
9397+
SimpleTokenizer2 tokenizer(settings_win32a, *this, code, "test.c");
9398+
ASSERT(tokenizer.simplifyTokens1(""));
9399+
const Token* tok = Token::findsimplematch(tokenizer.tokens(), "x");
9400+
ASSERT(tok && tok->previous()->isExpandedMacro());
9401+
}
9402+
93939403
void simplifyPlatformTypes() {
93949404
{
93959405
const char code[] = "size_t f();\n";

0 commit comments

Comments
 (0)