Skip to content

Commit 0087f08

Browse files
committed
fix nits
1 parent 9cd865e commit 0087f08

5 files changed

Lines changed: 34 additions & 5 deletions

File tree

‎lib/token.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ std::string Token::stringify(const stringifyOptions& options) const
12821282
ret += "long ";
12831283
}
12841284
}
1285-
if (options.macro && isExpandedMacro())
1285+
if (options.macro && hasMacroName())
12861286
ret += '$';
12871287
if (isName() && mStr.find(' ') != std::string::npos) {
12881288
for (const char i : mStr) {
@@ -1698,7 +1698,7 @@ static void indent(std::string &str, const nonneg int indent1, const nonneg int
16981698

16991699
void Token::astStringVerboseRecursive(std::string& ret, const nonneg int indent1, const nonneg int indent2) const
17001700
{
1701-
if (isExpandedMacro())
1701+
if (hasMacroName())
17021702
ret += '$';
17031703
ret += mStr;
17041704
if (mImpl->mValueType)

‎lib/token.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,18 @@ class CPPCHECKLIB Token {
499499
void isStandardType(const bool b) {
500500
setFlag(fIsStandardType, b);
501501
}
502+
/**
503+
* Is this token macro code at this location? Tokens copied from a
504+
* typedef are excluded, see hasMacroName().
505+
*/
502506
bool isExpandedMacro() const {
507+
return mImpl->mMacroName && !isSimplifiedTypedef();
508+
}
509+
/**
510+
* Did the token text originate from a macro? Also true for tokens copied
511+
* from a typedef. Intended for dump output and copying tokens.
512+
*/
513+
bool hasMacroName() const {
503514
return !!mImpl->mMacroName;
504515
}
505516
bool isCast() const {

‎lib/tokenize.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ namespace {
10331033
to = to->next();
10341034
to->isSimplifiedTypedef(true);
10351035
to->isExternC(from->isExternC());
1036-
if (from->isExpandedMacro())
1036+
if (from->hasMacroName())
10371037
to->setMacroName(from->getMacroName());
10381038
}
10391039
return to;
@@ -6396,7 +6396,7 @@ void Tokenizer::dump(std::ostream &out) const
63966396
outs += " isCast=\"true\"";
63976397
if (tok->isExternC())
63986398
outs += " externLang=\"C\"";
6399-
if (tok->isExpandedMacro())
6399+
if (tok->hasMacroName())
64006400
outs += " macroName=\"" + tok->getMacroName() + "\"";
64016401
if (tok->isTemplateArg()) {
64026402
outs += " isTemplateArg=\"true\"";

‎lib/tokenlist.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +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->isExpandedMacro())
270+
if (tok->hasMacroName())
271271
tok2->setMacroName(tok->getMacroName());
272272

273273
// Check for links and fix them up

‎test/testother.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7565,6 +7565,16 @@ class TestOther : public TestFixture {
75657565
" {}\n"
75667566
"}\n");
75677567
ASSERT_EQUALS("", errout_str());
7568+
7569+
// typedef of macro, same typedef in both branches
7570+
checkP("#define INT int\n"
7571+
"typedef INT T;\n"
7572+
"void g(T);\n"
7573+
"void f(bool c) {\n"
7574+
" if (c) { T x = 1; g(x); }\n"
7575+
" else { T x = 1; g(x); }\n"
7576+
"}\n");
7577+
ASSERT_EQUALS("[test.cpp:6:3] -> [test.cpp:5:3]: (style, inconclusive) Found duplicate branches for 'if' and 'else'. [duplicateBranch]\n", errout_str());
75687578
}
75697579

75707580
void duplicateBranch2() {
@@ -7691,6 +7701,14 @@ class TestOther : public TestFixture {
76917701
}
76927702

76937703
void duplicateExpression1() {
7704+
// typedef of macro is not macro code at the point of use
7705+
checkP("#define INT int\n"
7706+
"typedef INT T;\n"
7707+
"void foo(int a) {\n"
7708+
" if ((T)a == (T)a) { }\n"
7709+
"}\n");
7710+
ASSERT_EQUALS("[test.cpp:4:14]: (style) Same expression on both sides of '=='. [duplicateExpression]\n", errout_str());
7711+
76947712
check("void foo(int a) {\n"
76957713
" if (a == a) { }\n"
76967714
"}\n");

0 commit comments

Comments
 (0)