Skip to content

Commit

Permalink
Fix #12718 restrict wrongly treated as keyword (#6390)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed May 9, 2024
1 parent f562ff2 commit 89f5657
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 38 deletions.
36 changes: 1 addition & 35 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,38 +100,6 @@ static const std::unordered_set<std::string> controlFlowKeywords = {
"return"
};

// TODO: replace with Keywords::getX()?
// Another list of keywords
static const std::unordered_set<std::string> baseKeywords = {
"asm",
"auto",
"break",
"case",
"const",
"continue",
"default",
"do",
"else",
"enum",
"extern",
"for",
"goto",
"if",
"inline",
"register",
"restrict",
"return",
"sizeof",
"static",
"struct",
"switch",
"typedef",
"union",
"volatile",
"while",
"void"
};

void Token::update_property_info()
{
setFlag(fIsControlFlowKeyword, controlFlowKeywords.find(mStr) != controlFlowKeywords.end());
Expand All @@ -146,9 +114,7 @@ void Token::update_property_info()
else if (std::isalpha((unsigned char)mStr[0]) || mStr[0] == '_' || mStr[0] == '$') { // Name
if (mImpl->mVarId)
tokType(eVariable);
else if (mTokensFrontBack.list.isKeyword(mStr))
tokType(eKeyword);
else if (baseKeywords.count(mStr) > 0)
else if (mTokensFrontBack.list.isKeyword(mStr) || mStr == "asm") // TODO: not a keyword
tokType(eKeyword);
else if (mTokType != eVariable && mTokType != eFunction && mTokType != eType && mTokType != eKeyword)
tokType(eName);
Expand Down
6 changes: 3 additions & 3 deletions test/testincompletestatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class TestIncompleteStatement : public TestFixture {
const Settings settings = settingsBuilder().severity(Severity::warning).build();

#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
void check_(const char* file, int line, const char code[], bool inconclusive = false) {
void check_(const char* file, int line, const char code[], bool inconclusive = false, bool cpp = true) {
const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, inconclusive).build();

std::vector<std::string> files(1, "test.cpp");
std::vector<std::string> files(1, cpp ? "test.cpp" : "test.c");
Tokenizer tokenizer(settings1, *this);
PreprocessorHelper::preprocess(code, files, tokenizer, *this);

Expand Down Expand Up @@ -743,7 +743,7 @@ class TestIncompleteStatement : public TestFixture {
check("void f() { char * const * a, * const * b; }", true);
ASSERT_EQUALS("", errout_str());

check("void f() { char * const * a = 0, * volatile restrict * b; }", true);
check("void f() { char * const * a = 0, * volatile restrict * b; }", true, /*cpp*/ false);
ASSERT_EQUALS("", errout_str());

check("void f() { char * const * a = 0, * volatile const * b; }", true);
Expand Down
3 changes: 3 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7151,6 +7151,9 @@ class TestTokenizer : public TestFixture {

ASSERT_NO_THROW(tokenizeAndStringify("typedef struct { typedef int T; } S;")); // #12700

ASSERT_NO_THROW(tokenizeAndStringify("class A { bool restrict() const; };\n"
"bool A::restrict() const { return true; }")); // #12718

ignore_errout();
}

Expand Down

0 comments on commit 89f5657

Please sign in to comment.