Fixed #9094 (Tokenizer::createLinks2 problem with 'x%x<--a==x>x')

This commit is contained in:
Daniel Marjamäki 2019-05-11 19:11:40 +02:00
parent 1e2f1bac1f
commit 4d9b1e6c3d
4 changed files with 38 additions and 5 deletions

View File

@ -3792,6 +3792,9 @@ void Tokenizer::createLinks2()
const Token * templateToken = nullptr;
bool isStruct = false;
std::map<std::string, bool> templateNames;
templateNames["template"] = true;
std::stack<Token*> type;
for (Token *token = list.front(); token; token = token->next()) {
if (Token::Match(token, "%name%|> %name% [:<]"))
@ -3848,6 +3851,28 @@ void Tokenizer::createLinks2()
while (!type.empty() && type.top()->str() == "<")
type.pop();
} else if (token->str() == "<" && token->previous() && token->previous()->isName() && !token->previous()->varId()) {
if (!Token::simpleMatch(token->tokAt(-2), "::")) {
std::map<std::string, bool>::const_iterator it = templateNames.find(token->previous()->str());
if (it == templateNames.end()) {
const std::string &name = token->previous()->str();
bool isTemplateType = true;
for (const Token *tok2 = list.front(); tok2; tok2 = tok2->next()) {
if (!tok2->isName() || tok2->str() != name)
continue;
if (Token::Match(tok2->previous(), "class|struct"))
break;
if (!Token::Match(tok2->next(), "[<:({]")) {
isTemplateType = false;
break;
}
}
templateNames[name] = isTemplateType;
if (!isTemplateType)
continue;
} else if (!it->second) {
continue;
}
}
type.push(token);
if (!templateToken && (token->previous()->str() == "template"))
templateToken = token;

View File

@ -1372,9 +1372,8 @@ private:
" delete [] (double*)f;\n"
" delete [] (long double const*)(new float[10]);\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (portability) Casting between float* and double* which have an incompatible binary data representation.\n"
"[test.cpp:4]: (portability) Casting between float* and const long double* which have an incompatible binary data representation.\n",
"[test.cpp:3]: (portability) Casting between float* and double* which have an incompatible binary data representation.\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (portability) Casting between float* and double* which have an incompatible binary data representation.\n"
"[test.cpp:4]: (portability) Casting between float* and const long double* which have an incompatible binary data representation.\n", errout.str());
checkInvalidPointerCast("void test(const float* f) {\n"
" double *d = (double*)f;\n"

View File

@ -1881,8 +1881,8 @@ private:
check("void f()\n"
"{\n"
" vector<int> v;\n"
" vector.push_back(1);\n"
" vector.push_back(2);\n"
" v.push_back(1);\n"
" v.push_back(2);\n"
" for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)\n"
" {\n"
" if (*it == 1)\n"

View File

@ -4768,6 +4768,15 @@ private:
tokenizer.tokenize(istr, "test.cpp");
ASSERT_EQUALS(true, Token::simpleMatch(tokenizer.tokens()->next()->link(), "> void"));
}
{
// #9094
const char code[] = "a = f(x%x<--a==x>x);";
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
ASSERT(nullptr == Token::findsimplematch(tokenizer.tokens(), "<")->link());
}
}
void simplifyString() {