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 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
75 changes: 75 additions & 0 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3762,3 +3762,78 @@ bool isUnreachableOperand(const Token *tok)

return false;
}

static bool unknownLeafValuesAreTemplateArgs(const Token *tok)
{
if (!tok)
return true;

if (!tok->astOperand1() && !tok->astOperand2())
return tok->isTemplateArg() || tok->hasKnownIntValue();

return unknownLeafValuesAreTemplateArgs(tok->astOperand1())
&& unknownLeafValuesAreTemplateArgs(tok->astOperand2());
}

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

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

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

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

return tok;
}

static const Token *skipUnreachableElseBranch(const Token *tok)
{
if (Token::simpleMatch(tok->previous(), ")"))
return tok;

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

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

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

condTok = condTok->astOperand2();

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

return tok;
}

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

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

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

return tok;
}
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);

const Token *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()) {
tok = skipUnreachableBranch(tok);

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