Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #13326: FP shiftNegative for template argument #7045

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3762,3 +3762,72 @@ bool isUnreachableOperand(const Token *tok)

return false;
}

static bool tokenDependsOnTemplateArg(const Token *tok)
{
if (!tok)
return false;
if (tok->isTemplateArg())
return true;
return tokenDependsOnTemplateArg(tok->astOperand1())
|| tokenDependsOnTemplateArg(tok->astOperand2());
}

static void skipUnreachableIfBranch(const Token *&tok)
{
const Token *condTok = tok->linkAt(-1);
if (!condTok)
return;

if (!Token::simpleMatch(condTok->tokAt(-1), "if") && !Token::simpleMatch(condTok->tokAt(-2), "if constexpr"))
ludviggunne marked this conversation as resolved.
Show resolved Hide resolved
return;

condTok = condTok->astOperand2();
if (!condTok)
return;

if ((condTok->hasKnownIntValue() && condTok->getKnownIntValue() == 0)
|| (tokenDependsOnTemplateArg(condTok) && condTok->getValue(0))) {
tok = tok->link();
}
}

static void skipUnreachableElseBranch(const Token *&tok)
{
if (Token::simpleMatch(tok->previous(), ")"))
ludviggunne marked this conversation as resolved.
Show resolved Hide resolved
return;

const Token *condTok = tok->linkAt(-2);
if (!condTok)
return;

condTok = condTok->linkAt(-1);
if (!condTok)
return;

if (!Token::simpleMatch(condTok->tokAt(-1), "if") && !Token::simpleMatch(condTok->tokAt(-2), "if constexpr"))
return;

condTok = condTok->astOperand2();

if ((condTok->hasKnownIntValue() && condTok->getKnownIntValue() != 0)
|| (tokenDependsOnTemplateArg(condTok) && !condTok->getValue(0))) {
tok = tok->link();
}
}

void skipUnreachableBranch(const Token *&tok)
{
if (!Token::simpleMatch(tok, "{"))
return;

if (tok->scope()->type == Scope::ScopeType::eIf) {
skipUnreachableIfBranch(tok);
return;
}

if (tok->scope()->type == Scope::ScopeType::eElse) {
skipUnreachableElseBranch(tok);
return;
}
}
2 changes: 2 additions & 0 deletions lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,6 @@ bool isExhaustiveSwitch(const Token *startbrace);

bool isUnreachableOperand(const Token *tok);

void skipUnreachableBranch(const Token *&tok);

#endif // astutilsH
2 changes: 2 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3114,6 +3114,8 @@ void CheckOther::checkNegativeBitwiseShift()
logChecker("CheckOther::checkNegativeBitwiseShift");

for (const Token* tok = mTokenizer->tokens(); tok; tok = tok->next()) {
skipUnreachableBranch(tok);
ludviggunne marked this conversation as resolved.
Show resolved Hide resolved

if (!tok->astOperand1() || !tok->astOperand2())
continue;

Expand Down
29 changes: 29 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9369,6 +9369,35 @@ class TestOther : public TestFixture {
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());

// #13326
check("template<int b>\n"
"int f(int a)\n"
"{\n"
" if constexpr (b >= 0) {\n"
" return a << b;\n"
" } else {\n"
" return a << -b;\n"
" }\n"
"}\n"
"int g() {\n"
" return f<1>(2)\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("template<int b>\n"
"int f(int a)\n"
"{\n"
" if constexpr (b >= 0) {\n"
" return a << b;\n"
" } else {\n"
" return a << -b;\n"
" }\n"
"}\n"
"int g() {\n"
" return f<-1>(2)\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void incompleteArrayFill() {
Expand Down
Loading