Skip to content

Commit 89f5657

Browse files
Fix #12718 restrict wrongly treated as keyword (cppcheck-opensource#6390)
1 parent f562ff2 commit 89f5657

3 files changed

Lines changed: 7 additions & 38 deletions

File tree

lib/token.cpp

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -100,38 +100,6 @@ static const std::unordered_set<std::string> controlFlowKeywords = {
100100
"return"
101101
};
102102

103-
// TODO: replace with Keywords::getX()?
104-
// Another list of keywords
105-
static const std::unordered_set<std::string> baseKeywords = {
106-
"asm",
107-
"auto",
108-
"break",
109-
"case",
110-
"const",
111-
"continue",
112-
"default",
113-
"do",
114-
"else",
115-
"enum",
116-
"extern",
117-
"for",
118-
"goto",
119-
"if",
120-
"inline",
121-
"register",
122-
"restrict",
123-
"return",
124-
"sizeof",
125-
"static",
126-
"struct",
127-
"switch",
128-
"typedef",
129-
"union",
130-
"volatile",
131-
"while",
132-
"void"
133-
};
134-
135103
void Token::update_property_info()
136104
{
137105
setFlag(fIsControlFlowKeyword, controlFlowKeywords.find(mStr) != controlFlowKeywords.end());
@@ -146,9 +114,7 @@ void Token::update_property_info()
146114
else if (std::isalpha((unsigned char)mStr[0]) || mStr[0] == '_' || mStr[0] == '$') { // Name
147115
if (mImpl->mVarId)
148116
tokType(eVariable);
149-
else if (mTokensFrontBack.list.isKeyword(mStr))
150-
tokType(eKeyword);
151-
else if (baseKeywords.count(mStr) > 0)
117+
else if (mTokensFrontBack.list.isKeyword(mStr) || mStr == "asm") // TODO: not a keyword
152118
tokType(eKeyword);
153119
else if (mTokType != eVariable && mTokType != eFunction && mTokType != eType && mTokType != eKeyword)
154120
tokType(eName);

test/testincompletestatement.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class TestIncompleteStatement : public TestFixture {
3434
const Settings settings = settingsBuilder().severity(Severity::warning).build();
3535

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

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

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

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

749749
check("void f() { char * const * a = 0, * volatile const * b; }", true);

test/testtokenize.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7151,6 +7151,9 @@ class TestTokenizer : public TestFixture {
71517151

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

7154+
ASSERT_NO_THROW(tokenizeAndStringify("class A { bool restrict() const; };\n"
7155+
"bool A::restrict() const { return true; }")); // #12718
7156+
71547157
ignore_errout();
71557158
}
71567159

0 commit comments

Comments
 (0)