Skip to content
Merged
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
11 changes: 9 additions & 2 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,15 @@ void Token::update_property_info()
isStandardType(false);

if (!mStr.empty()) {
if (mStr == "true" || mStr == "false")
tokType(eBoolean);
if (mStr == "true" || mStr == "false") {
if (mImpl->mVarId) {
if (mIsCpp)
throw InternalError(this, "Internal error. VarId set for bool literal.");
tokType(eVariable);
}
else
tokType(eBoolean);
}
else if (isStringLiteral(mStr)) {
tokType(eString);
isLong(isPrefixStringCharLiteral(mStr, '"', "L"));
Expand Down
4 changes: 2 additions & 2 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ static void valueFlowNumber(TokenList &tokenlist, const Settings& settings)

if (tokenlist.isCPP() || settings.standards.c >= Standards::C23) {
for (Token *tok = tokenlist.front(); tok; tok = tok->next()) {
if (tok->isName() && !tok->varId() && Token::Match(tok, "false|true")) {
if (tok->isName() && !tok->varId() && Token::Match(tok, "%bool%")) {
ValueFlow::Value value(tok->str() == "true");
if (!tok->isTemplateArg())
value.setKnown();
Expand Down Expand Up @@ -1208,7 +1208,7 @@ static void valueFlowImpossibleValues(TokenList& tokenList, const Settings& sett
for (Token* tok = tokenList.front(); tok; tok = tok->next()) {
if (tok->hasKnownIntValue())
continue;
if (Token::Match(tok, "true|false"))
if (Token::Match(tok, "%bool%"))
continue;
if (astIsBool(tok) || Token::Match(tok, "%comp%")) {
ValueFlow::Value lower{-1};
Expand Down
12 changes: 12 additions & 0 deletions test/testvarid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class TestVarID : public TestFixture {
TEST_CASE(varid_cpp_keywords_in_c_code);
TEST_CASE(varid_cpp_keywords_in_c_code2); // #5373: varid=0 for argument called "delete"
TEST_CASE(varid_cpp_keywords_in_c_code3);
TEST_CASE(varid_cpp_keywords_in_c_code4);
TEST_CASE(varidFunctionCall1);
TEST_CASE(varidFunctionCall2);
TEST_CASE(varidFunctionCall3);
Expand Down Expand Up @@ -1468,6 +1469,17 @@ class TestVarID : public TestFixture {
ASSERT_EQUALS(expected, tokenize(code, dinit(TokenizeOptions, $.cpp = false)));
}

void varid_cpp_keywords_in_c_code4() { // #12120
{
const char code[] = "int false = 0;";
ASSERT_THROW_INTERNAL_EQUALS(tokenize(code, dinit(TokenizeOptions, $.cpp = true)), INTERNAL, "Internal error. VarId set for bool literal.");
}
{
const char code[] = "int false = 0;";
ASSERT_EQUALS("1: int false@1 ; false@1 = 0 ;\n", tokenize(code, dinit(TokenizeOptions, $.cpp = false)));
}
}

void varidFunctionCall1() {
const char code[] ="void f() {\n"
" int x;\n"
Expand Down
Loading