diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index dfaa1df21..5922b443b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2295,7 +2295,7 @@ void Tokenizer::simplifyTemplates() //--------------------------------------------------------------------------- -static bool setVarIdParseDeclaration(const Token **tok, const std::map &variableId, bool executableScope, bool cpp) +static bool setVarIdParseDeclaration(const Token **tok, const std::map &variableId, bool executableScope, bool cpp, bool c) { const Token *tok2 = *tok; @@ -2312,7 +2312,7 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::mapisName()) { if (cpp && Token::Match(tok2, "namespace|public|private|protected")) return false; - if (Token::Match(tok2, "struct|union") || (cpp && Token::Match(tok2, "class|typename"))) { + if (Token::Match(tok2, "struct|union") || (!c && Token::Match(tok2, "class|typename"))) { hasstruct = true; typeCount = 0; singleNameCount = 0; @@ -2648,7 +2648,7 @@ void Tokenizer::setVarId() if (Token::simpleMatch(tok2, "const new") && !isC()) continue; - bool decl = setVarIdParseDeclaration(&tok2, variableId, executableScope.top(), isCPP()); + bool decl = setVarIdParseDeclaration(&tok2, variableId, executableScope.top(), isCPP(), isC()); if (decl) { const Token* prev2 = tok2->previous(); if (Token::Match(prev2, "%type% [;[=,)]") && tok2->previous()->str() != "const") diff --git a/test/testvarid.cpp b/test/testvarid.cpp index ffb490a95..5ddf73102 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -137,6 +137,7 @@ private: TEST_CASE(varid_pointerToArray); // #2645 TEST_CASE(varid_cpp11initialization); // #4344 TEST_CASE(varid_inheritedMembers); // #4101 + TEST_CASE(varid_header); // #6386 TEST_CASE(varidclass1); TEST_CASE(varidclass2); @@ -1965,6 +1966,18 @@ private: "};")); } + void varid_header() { + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A ;\n" + "2: struct B {\n" + "3: void setData ( const A & a@1 ) ;\n" + "4: } ;\n", + tokenize("class A;\n" + "struct B {\n" + " void setData(const A & a);\n" + "}; ", false, "test.h")); + } + void varidclass1() { const std::string actual = tokenize( "class Fred\n"