Skip to content

Commit 336f7b4

Browse files
committed
fix nits
1 parent aeec46e commit 336f7b4

5 files changed

Lines changed: 28 additions & 5 deletions

File tree

‎lib/token.cpp‎

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

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

‎lib/token.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,12 @@ class CPPCHECKLIB Token {
499499
void isStandardType(const bool b) {
500500
setFlag(fIsStandardType, b);
501501
}
502+
/** macro code at this location (typedef-copied tokens excluded, see hasMacroName) */
502503
bool isExpandedMacro() const {
504+
return mImpl->mMacroName && !isSimplifiedTypedef();
505+
}
506+
/** token text originated from a macro (also true for typedef-copied tokens); for dump/copying, not checks */
507+
bool hasMacroName() const {
503508
return !!mImpl->mMacroName;
504509
}
505510
bool isCast() const {

‎lib/tokenize.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ namespace {
10341034
to = to->next();
10351035
to->isSimplifiedTypedef(true);
10361036
to->isExternC(from->isExternC());
1037-
if (from->isExpandedMacro())
1037+
if (from->hasMacroName())
10381038
to->setMacroName(from->getMacroName());
10391039
}
10401040
return to;
@@ -6384,7 +6384,7 @@ void Tokenizer::dump(std::ostream &out) const
63846384
outs += " isCast=\"true\"";
63856385
if (tok->isExternC())
63866386
outs += " externLang=\"C\"";
6387-
if (tok->isExpandedMacro())
6387+
if (tok->hasMacroName())
63886388
outs += " macroName=\"" + tok->getMacroName() + "\"";
63896389
if (tok->isTemplateArg()) {
63906390
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
@@ -7508,6 +7508,16 @@ class TestOther : public TestFixture {
75087508
" {}\n"
75097509
"}\n");
75107510
ASSERT_EQUALS("", errout_str());
7511+
7512+
// typedef of macro, same typedef in both branches
7513+
checkP("#define INT int\n"
7514+
"typedef INT T;\n"
7515+
"void g(T);\n"
7516+
"void f(bool c) {\n"
7517+
" if (c) { T x = 1; g(x); }\n"
7518+
" else { T x = 1; g(x); }\n"
7519+
"}\n");
7520+
ASSERT_EQUALS("[test.cpp:6:3] -> [test.cpp:5:3]: (style, inconclusive) Found duplicate branches for 'if' and 'else'. [duplicateBranch]\n", errout_str());
75117521
}
75127522

75137523
void duplicateBranch2() {
@@ -7634,6 +7644,14 @@ class TestOther : public TestFixture {
76347644
}
76357645

76367646
void duplicateExpression1() {
7647+
// typedef of macro is not macro code at the point of use
7648+
checkP("#define INT int\n"
7649+
"typedef INT T;\n"
7650+
"void foo(int a) {\n"
7651+
" if ((T)a == (T)a) { }\n"
7652+
"}\n");
7653+
ASSERT_EQUALS("[test.cpp:4:14]: (style) Same expression on both sides of '=='. [duplicateExpression]\n", errout_str());
7654+
76377655
check("void foo(int a) {\n"
76387656
" if (a == a) { }\n"
76397657
"}\n");

0 commit comments

Comments
 (0)