Skip to content

Commit 88cefb3

Browse files
committed
fixed assumption in setVarIdParseDeclaration() that isC() == false implies isHeader() == true / added TokenList::isHeader() and Tokenizer::isHeader()
1 parent ea1c1ed commit 88cefb3

5 files changed

Lines changed: 38 additions & 8 deletions

File tree

lib/tokenize.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4079,7 +4079,7 @@ void VariableMap::addVariable(const std::string& varname, bool globalNamespace)
40794079
it->second = ++mVarId;
40804080
}
40814081

4082-
static bool setVarIdParseDeclaration(Token** tok, const VariableMap& variableMap, bool executableScope, bool cpp, bool c)
4082+
static bool setVarIdParseDeclaration(Token** tok, const VariableMap& variableMap, bool executableScope, bool cpp, bool c, bool header)
40834083
{
40844084
const Token* const tok1 = *tok;
40854085
Token* tok2 = *tok;
@@ -4104,7 +4104,7 @@ static bool setVarIdParseDeclaration(Token** tok, const VariableMap& variableMap
41044104
tok2 = tok2->linkAt(1)->next();
41054105
continue;
41064106
}
4107-
if (Token::Match(tok2, "struct|union|enum") || (!c && Token::Match(tok2, "class|typename"))) {
4107+
if (Token::Match(tok2, "struct|union|enum") || ((!c || header) && Token::Match(tok2, "class|typename"))) {
41084108
hasstruct = true;
41094109
typeCount = 0;
41104110
singleNameCount = 0;
@@ -4120,8 +4120,8 @@ static bool setVarIdParseDeclaration(Token** tok, const VariableMap& variableMap
41204120
++typeCount;
41214121
++singleNameCount;
41224122
}
4123-
} else if (!c && ((TemplateSimplifier::templateParameters(tok2) > 0) ||
4124-
Token::simpleMatch(tok2, "< >") /* Ticket #4764 */)) {
4123+
} else if ((!c || header) && ((TemplateSimplifier::templateParameters(tok2) > 0) ||
4124+
Token::simpleMatch(tok2, "< >") /* Ticket #4764 */)) {
41254125
const Token *start = *tok;
41264126
if (Token::Match(start->previous(), "%or%|%oror%|&&|&|^|+|-|*|/"))
41274127
return false;
@@ -4601,7 +4601,7 @@ void Tokenizer::setVarIdPass1()
46014601
}
46024602

46034603
try { /* Ticket #8151 */
4604-
decl = setVarIdParseDeclaration(&tok2, variableMap, scopeStack.top().isExecutable, isCPP(), isC());
4604+
decl = setVarIdParseDeclaration(&tok2, variableMap, scopeStack.top().isExecutable, isCPP(), isC(), isHeader());
46054605
} catch (const Token * errTok) {
46064606
syntaxError(errTok);
46074607
}

lib/tokenize.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ class CPPCHECKLIB Tokenizer {
7777
return list.isCPP();
7878
}
7979

80+
/** Is it a header. Used for bailouts */
81+
bool isHeader() const {
82+
return list.isHeader();
83+
}
84+
8085
/**
8186
* Check if inner scope ends with a call to a noreturn function
8287
* \param endScopeToken The '}' token

lib/tokenlist.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ void TokenList::deallocateTokens()
8383
void TokenList::determineCppC()
8484
{
8585
Settings::Language lang;
86+
bool header = false;
87+
lang = Path::identify(getSourceFilePath(), &header);
8688
if (mSettings && mSettings->enforcedLang != Settings::Language::None)
8789
lang = mSettings->enforcedLang;
88-
else
89-
lang = Path::identify(getSourceFilePath());
9090

9191
mIsC = lang == Settings::Language::C;
9292
mIsCpp = lang == Settings::Language::CPP;
93+
mIsHeader = header;
9394
}
9495

9596
int TokenList::appendFileIfNew(std::string fileName)

lib/tokenlist.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class CPPCHECKLIB TokenList {
6363
return mIsCpp;
6464
}
6565

66+
/** Is it a header. Used for bailouts */
67+
bool isHeader() const {
68+
return mIsHeader;
69+
}
70+
6671
/**
6772
* Delete all tokens in given token list
6873
* @param tok token list to delete
@@ -207,9 +212,12 @@ class CPPCHECKLIB TokenList {
207212
/** settings */
208213
const Settings* mSettings{};
209214

210-
/** File is known to be C/C++ code */
215+
/** File is known to be C/C++ source file */
211216
bool mIsC{};
212217
bool mIsCpp{};
218+
219+
/** File is known to be C/C++ header */
220+
bool mIsHeader;
213221
};
214222

215223
/// @}

test/testvarid.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,6 +2737,14 @@ class TestVarID : public TestFixture {
27372737
}
27382738

27392739
void varid_header() {
2740+
ASSERT_EQUALS("1: class A ;\n"
2741+
"2: struct B {\n"
2742+
"3: void setData ( const A & a@1 ) ;\n"
2743+
"4: } ;\n",
2744+
tokenize("class A;\n"
2745+
"struct B {\n"
2746+
" void setData(const A & a);\n"
2747+
"}; ", "test.hpp"));
27402748
ASSERT_EQUALS("1: class A ;\n"
27412749
"2: struct B {\n"
27422750
"3: void setData ( const A & a@1 ) ;\n"
@@ -2745,6 +2753,14 @@ class TestVarID : public TestFixture {
27452753
"struct B {\n"
27462754
" void setData(const A & a);\n"
27472755
"}; ", "test.h"));
2756+
ASSERT_EQUALS("1: void f ( )\n"
2757+
"2: {\n"
2758+
"3: int class@1 ;\n"
2759+
"4: }\n",
2760+
tokenize("void f()\n"
2761+
"{\n"
2762+
" int class;\n"
2763+
"}", "test.h"));
27482764
}
27492765

27502766
void varid_rangeBasedFor() {

0 commit comments

Comments
 (0)