Skip to content

Commit 3304846

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

5 files changed

Lines changed: 32 additions & 9 deletions

File tree

lib/tokenize.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3543,7 +3543,7 @@ void VariableMap::addVariable(const std::string& varname, bool globalNamespace)
35433543
}
35443544

35453545

3546-
static bool setVarIdParseDeclaration(const Token **tok, const VariableMap& variableMap, bool executableScope, bool cpp, bool c)
3546+
static bool setVarIdParseDeclaration(const Token **tok, const VariableMap& variableMap, bool executableScope, bool cpp, bool c, bool header)
35473547
{
35483548
const Token *tok2 = *tok;
35493549
if (!tok2->isName())
@@ -3563,7 +3563,7 @@ static bool setVarIdParseDeclaration(const Token **tok, const VariableMap& varia
35633563
tok2 = tok2->linkAt(1)->next();
35643564
continue;
35653565
}
3566-
if (Token::Match(tok2, "struct|union|enum") || (!c && Token::Match(tok2, "class|typename"))) {
3566+
if (Token::Match(tok2, "struct|union|enum") || ((!c || header) && Token::Match(tok2, "class|typename"))) {
35673567
hasstruct = true;
35683568
typeCount = 0;
35693569
singleNameCount = 0;
@@ -3579,8 +3579,8 @@ static bool setVarIdParseDeclaration(const Token **tok, const VariableMap& varia
35793579
++typeCount;
35803580
++singleNameCount;
35813581
}
3582-
} else if (!c && ((TemplateSimplifier::templateParameters(tok2) > 0) ||
3583-
Token::simpleMatch(tok2, "< >") /* Ticket #4764 */)) {
3582+
} else if ((!c || header) && ((TemplateSimplifier::templateParameters(tok2) > 0) ||
3583+
Token::simpleMatch(tok2, "< >") /* Ticket #4764 */)) {
35843584
const Token *start = *tok;
35853585
if (Token::Match(start->previous(), "%or%|%oror%|&&|&|^|+|-|*|/"))
35863586
return false;
@@ -4053,7 +4053,7 @@ void Tokenizer::setVarIdPass1()
40534053
}
40544054

40554055
try { /* Ticket #8151 */
4056-
decl = setVarIdParseDeclaration(&tok2, variableMap, scopeStack.top().isExecutable, isCPP(), isC());
4056+
decl = setVarIdParseDeclaration(&tok2, variableMap, scopeStack.top().isExecutable, isCPP(), isC(), isHeader());
40574057
} catch (const Token * errTok) {
40584058
syntaxError(errTok);
40594059
}

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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ TokenList::TokenList(const Settings* settings) :
4949
mTokensFrontBack(),
5050
mSettings(settings),
5151
mIsC(false),
52-
mIsCpp(false)
52+
mIsCpp(false),
53+
mIsHeader(false)
5354
{
5455
mTokensFrontBack.list = this;
5556
mKeywords.insert("asm");
@@ -118,13 +119,14 @@ void TokenList::deallocateTokens()
118119
void TokenList::determineCppC()
119120
{
120121
Settings::Language lang;
122+
bool header = false;
123+
lang = Path::identify(getSourceFilePath(), &header);
121124
if (mSettings && mSettings->enforcedLang != Settings::Language::None)
122125
lang = mSettings->enforcedLang;
123-
else
124-
lang = Path::identify(getSourceFilePath());
125126

126127
mIsC = lang == Settings::Language::C;
127128
mIsCpp = lang == Settings::Language::CPP;
129+
mIsHeader = header;
128130

129131
if (mIsCpp) {
130132
//mKeywords.insert("bool"); // type

lib/tokenlist.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ class CPPCHECKLIB TokenList {
6868
return mIsCpp;
6969
}
7070

71+
/** Is it a header. Used for bailouts */
72+
bool isHeader() const {
73+
return mIsHeader;
74+
}
75+
7176
/**
7277
* Delete all tokens in given token list
7378
* @param tok token list to delete
@@ -214,9 +219,12 @@ class CPPCHECKLIB TokenList {
214219

215220
std::unordered_set<std::string> mKeywords;
216221

217-
/** File is known to be C/C++ code */
222+
/** File is known to be C/C++ source file */
218223
bool mIsC;
219224
bool mIsCpp;
225+
226+
/** File is known to be C/C++ header */
227+
bool mIsHeader;
220228
};
221229

222230
/// @}

test/testvarid.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,6 +2685,14 @@ class TestVarID : public TestFixture {
26852685
"struct B {\n"
26862686
" void setData(const A & a);\n"
26872687
"}; ", "test.h"));
2688+
ASSERT_EQUALS("1: class A ;\n"
2689+
"2: struct B {\n"
2690+
"3: void setData ( const A & a@1 ) ;\n"
2691+
"4: } ;\n",
2692+
tokenize("class A;\n"
2693+
"struct B {\n"
2694+
" void setData(const A & a);\n"
2695+
"}; ", "test.hpp"));
26882696
}
26892697

26902698
void varid_rangeBasedFor() {

0 commit comments

Comments
 (0)