Skip to content

Commit ad6beec

Browse files
committed
Fix syntaxError when not/compl is used as identifiers (C)
1 parent 6c25934 commit ad6beec

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

lib/tokenize.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7805,7 +7805,7 @@ bool Tokenizer::simplifyCAlternativeTokens()
78057805
alt.push_back(tok);
78067806

78077807
// Is this a variable declaration..
7808-
if (isC() && Token::Match(tok->previous(), "%type%|* %name% [;,=]"))
7808+
if (isC() && Token::Match(tok->previous(), "%type%|* %name% [;,=)]"))
78097809
return false;
78107810

78117811
if (!Token::Match(tok->previous(), "%name%|%num%|%char%|%str%|)|]|> %name% %name%|%num%|%char%|%op%|%str%|("))
@@ -7823,6 +7823,10 @@ bool Tokenizer::simplifyCAlternativeTokens()
78237823
} else if (Token::Match(tok, "not|compl")) {
78247824
alt.push_back(tok);
78257825

7826+
// Is this a variable/parameter declaration
7827+
if (isC() && Token::Match(tok->previous(), "%type%|* %name% [;,=)]"))
7828+
return false;
7829+
78267830
if ((Token::Match(tok->previous(), "%assign%") || Token::Match(tok->next(), "%num%")) && !Token::Match(tok->next(), ".|->")) {
78277831
replaceAll = true;
78287832
continue;

test/testtokenize.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5449,6 +5449,20 @@ class TestTokenizer : public TestFixture {
54495449
ASSERT_EQUALS(exp, tokenizeAndStringify(code, dinit(TokenizeOptions, $.cpp = false)));
54505450
}
54515451

5452+
{ // not used as parameter name in C
5453+
const char code[] = "static void foo(int not, int test) {"
5454+
" test = not;"
5455+
"}";
5456+
const char exp[] = "static void foo ( int not , int test ) { test = not ; }";
5457+
ASSERT_EQUALS(exp, tokenizeAndStringify(code, dinit(TokenizeOptions, $.cpp = false)));
5458+
}
5459+
5460+
{ // binary alt token as last parameter in C
5461+
const char code[] = "void f(int or) { if (or) {} }";
5462+
const char exp[] = "void f ( int or ) { if ( or ) { } }";
5463+
ASSERT_EQUALS(exp, tokenizeAndStringify(code, dinit(TokenizeOptions, $.cpp = false)));
5464+
}
5465+
54525466
//ASSERT_EQUALS("", filter_valueflow(errout_str()));
54535467
ignore_errout();
54545468
}

0 commit comments

Comments
 (0)