Skip to content

Commit 9c1e757

Browse files
authored
Fix #14629 syntaxError when not/compl is used as identifiers (C) (danmar#8384)
Fix false syntaxError when C alternative tokens (`not`/`compl`) are used as identifiers (parameters) **Minimal Example** ``` static void foo(int not, int test) { test = not; } ``` **Solution** Add C-mode declaration guard to the not/compl branch in `simplifyCAlternativeTokens()` and extend both guards to cover last-parameter position.
1 parent e2de428 commit 9c1e757

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
@@ -7819,7 +7819,7 @@ bool Tokenizer::simplifyCAlternativeTokens()
78197819
alt.push_back(tok);
78207820

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

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

7840+
// Is this a variable/parameter declaration
7841+
if (isC() && Token::Match(tok->previous(), "%type%|* %name% [;,=)]"))
7842+
return false;
7843+
78407844
if ((Token::Match(tok->previous(), "%assign%") || Token::Match(tok->next(), "%num%")) && !Token::Match(tok->next(), ".|->")) {
78417845
replaceAll = true;
78427846
continue;

test/testtokenize.cpp

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

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

0 commit comments

Comments
 (0)