Fix issue 9211: No error on divide by zero outside template instatiation

I am not sure how to add a test for this.
This commit is contained in:
Paul Fultz II 2019-07-18 14:55:30 +02:00 committed by Daniel Marjamäki
parent 28e5133f50
commit 8cd8a2671c
3 changed files with 11 additions and 9 deletions

View File

@ -2153,7 +2153,7 @@ static bool isLowerEqualThanMulDiv(const Token* lower)
}
bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
bool TemplateSimplifier::simplifyNumericCalculations(Token *tok, bool isTemplate)
{
bool ret = false;
// (1-2)
@ -2179,8 +2179,10 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
break;
// Don't simplify "%num% / 0"
if (Token::Match(op, "[/%] 0"))
throw InternalError(op, "Instantiation error: Divide by zero in template instantiation.", InternalError::INSTANTIATION);
if (Token::Match(op, "[/%] 0")) {
if (isTemplate) throw InternalError(op, "Instantiation error: Divide by zero in template instantiation.", InternalError::INSTANTIATION);
else return ret;
}
// Integer operations
if (Token::Match(op, ">>|<<|&|^|%or%")) {
@ -2430,7 +2432,7 @@ static bool validTokenEnd(bool bounded, const Token *tok, const Token *backToken
// TODO: This is not the correct class for simplifyCalculations(), so it
// should be moved away.
bool TemplateSimplifier::simplifyCalculations(Token* frontToken, Token *backToken)
bool TemplateSimplifier::simplifyCalculations(Token* frontToken, Token *backToken, bool isTemplate)
{
bool ret = false;
const bool bounded = frontToken || backToken;
@ -2489,11 +2491,11 @@ bool TemplateSimplifier::simplifyCalculations(Token* frontToken, Token *backToke
if (tok && tok->isNumber()) {
if (validTokenEnd(bounded, tok, backToken, 2) &&
simplifyNumericCalculations(tok)) {
simplifyNumericCalculations(tok, isTemplate)) {
ret = true;
Token *prev = tok->tokAt(-2);
while (validTokenStart(bounded, tok, frontToken, -2) &&
prev && simplifyNumericCalculations(prev)) {
prev && simplifyNumericCalculations(prev, isTemplate)) {
tok = prev;
prev = prev->tokAt(-2);
}

View File

@ -286,7 +286,7 @@ public:
* @return true if modifications to token-list are done.
* false if no modifications are done.
*/
static bool simplifyNumericCalculations(Token *tok);
static bool simplifyNumericCalculations(Token *tok, bool isTemplate = true);
/**
* Simplify constant calculations such as "1+2" => "3".
@ -294,7 +294,7 @@ public:
* @return true if modifications to token-list are done.
* false if no modifications are done.
*/
bool simplifyCalculations(Token* frontToken = nullptr, Token *backToken = nullptr);
bool simplifyCalculations(Token* frontToken = nullptr, Token *backToken = nullptr, bool isTemplate = true);
/** Simplify template instantiation arguments.
* @param start first token of arguments

View File

@ -8059,7 +8059,7 @@ void Tokenizer::simplifyReference()
bool Tokenizer::simplifyCalculations()
{
return mTemplateSimplifier->simplifyCalculations();
return mTemplateSimplifier->simplifyCalculations(nullptr, nullptr, false);
}
void Tokenizer::simplifyOffsetPointerDereference()