Skip to content

Commit 166a11f

Browse files
committed
fix nits
1 parent 3f5debd commit 166a11f

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;
@@ -6380,7 +6380,7 @@ void Tokenizer::dump(std::ostream &out) const
63806380
outs += " isCast=\"true\"";
63816381
if (tok->isExternC())
63826382
outs += " externLang=\"C\"";
6383-
if (tok->isExpandedMacro())
6383+
if (tok->hasMacroName())
63846384
outs += " macroName=\"" + tok->getMacroName() + "\"";
63856385
if (tok->isTemplateArg()) {
63866386
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
@@ -7556,6 +7556,16 @@ class TestOther : public TestFixture {
75567556
" {}\n"
75577557
"}\n");
75587558
ASSERT_EQUALS("", errout_str());
7559+
7560+
// typedef of macro, same typedef in both branches
7561+
checkP("#define INT int\n"
7562+
"typedef INT T;\n"
7563+
"void g(T);\n"
7564+
"void f(bool c) {\n"
7565+
" if (c) { T x = 1; g(x); }\n"
7566+
" else { T x = 1; g(x); }\n"
7567+
"}\n");
7568+
ASSERT_EQUALS("[test.cpp:6:3] -> [test.cpp:5:3]: (style, inconclusive) Found duplicate branches for 'if' and 'else'. [duplicateBranch]\n", errout_str());
75597569
}
75607570

75617571
void duplicateBranch2() {
@@ -7682,6 +7692,14 @@ class TestOther : public TestFixture {
76827692
}
76837693

76847694
void duplicateExpression1() {
7695+
// typedef of macro is not macro code at the point of use
7696+
checkP("#define INT int\n"
7697+
"typedef INT T;\n"
7698+
"void foo(int a) {\n"
7699+
" if ((T)a == (T)a) { }\n"
7700+
"}\n");
7701+
ASSERT_EQUALS("[test.cpp:4:14]: (style) Same expression on both sides of '=='. [duplicateExpression]\n", errout_str());
7702+
76857703
check("void foo(int a) {\n"
76867704
" if (a == a) { }\n"
76877705
"}\n");

0 commit comments

Comments
 (0)