Skip to content

Commit 2d1a0ae

Browse files
Partial fix for #13747 assertion in Token::update_property_info() (handle true/false as variables in C) (danmar#7456)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 1bd5f86 commit 2d1a0ae

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

lib/token.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,15 @@ void Token::update_property_info()
114114
isStandardType(false);
115115

116116
if (!mStr.empty()) {
117-
if (mStr == "true" || mStr == "false")
118-
tokType(eBoolean);
117+
if (mStr == "true" || mStr == "false") {
118+
if (mImpl->mVarId) {
119+
if (mIsCpp)
120+
throw InternalError(this, "Internal error. VarId set for bool literal.");
121+
tokType(eVariable);
122+
}
123+
else
124+
tokType(eBoolean);
125+
}
119126
else if (isStringLiteral(mStr)) {
120127
tokType(eString);
121128
isLong(isPrefixStringCharLiteral(mStr, '"', "L"));

lib/valueflow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ static void valueFlowNumber(TokenList &tokenlist, const Settings& settings)
562562

563563
if (tokenlist.isCPP() || settings.standards.c >= Standards::C23) {
564564
for (Token *tok = tokenlist.front(); tok; tok = tok->next()) {
565-
if (tok->isName() && !tok->varId() && Token::Match(tok, "false|true")) {
565+
if (tok->isName() && !tok->varId() && Token::Match(tok, "%bool%")) {
566566
ValueFlow::Value value(tok->str() == "true");
567567
if (!tok->isTemplateArg())
568568
value.setKnown();
@@ -1208,7 +1208,7 @@ static void valueFlowImpossibleValues(TokenList& tokenList, const Settings& sett
12081208
for (Token* tok = tokenList.front(); tok; tok = tok->next()) {
12091209
if (tok->hasKnownIntValue())
12101210
continue;
1211-
if (Token::Match(tok, "true|false"))
1211+
if (Token::Match(tok, "%bool%"))
12121212
continue;
12131213
if (astIsBool(tok) || Token::Match(tok, "%comp%")) {
12141214
ValueFlow::Value lower{-1};

test/testvarid.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class TestVarID : public TestFixture {
108108
TEST_CASE(varid_cpp_keywords_in_c_code);
109109
TEST_CASE(varid_cpp_keywords_in_c_code2); // #5373: varid=0 for argument called "delete"
110110
TEST_CASE(varid_cpp_keywords_in_c_code3);
111+
TEST_CASE(varid_cpp_keywords_in_c_code4);
111112
TEST_CASE(varidFunctionCall1);
112113
TEST_CASE(varidFunctionCall2);
113114
TEST_CASE(varidFunctionCall3);
@@ -1468,6 +1469,17 @@ class TestVarID : public TestFixture {
14681469
ASSERT_EQUALS(expected, tokenize(code, dinit(TokenizeOptions, $.cpp = false)));
14691470
}
14701471

1472+
void varid_cpp_keywords_in_c_code4() { // #12120
1473+
{
1474+
const char code[] = "int false = 0;";
1475+
ASSERT_THROW_INTERNAL_EQUALS(tokenize(code, dinit(TokenizeOptions, $.cpp = true)), INTERNAL, "Internal error. VarId set for bool literal.");
1476+
}
1477+
{
1478+
const char code[] = "int false = 0;";
1479+
ASSERT_EQUALS("1: int false@1 ; false@1 = 0 ;\n", tokenize(code, dinit(TokenizeOptions, $.cpp = false)));
1480+
}
1481+
}
1482+
14711483
void varidFunctionCall1() {
14721484
const char code[] ="void f() {\n"
14731485
" int x;\n"

0 commit comments

Comments
 (0)