From ad6beec131636b84b9ad46c731902548379338bc Mon Sep 17 00:00:00 2001 From: Andreas Wilhelm Date: Fri, 27 Mar 2026 08:53:05 +0100 Subject: [PATCH] Fix syntaxError when not/compl is used as identifiers (C) --- lib/tokenize.cpp | 6 +++++- test/testtokenize.cpp | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2e4662ea755..a02d970105e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7805,7 +7805,7 @@ bool Tokenizer::simplifyCAlternativeTokens() alt.push_back(tok); // Is this a variable declaration.. - if (isC() && Token::Match(tok->previous(), "%type%|* %name% [;,=]")) + if (isC() && Token::Match(tok->previous(), "%type%|* %name% [;,=)]")) return false; if (!Token::Match(tok->previous(), "%name%|%num%|%char%|%str%|)|]|> %name% %name%|%num%|%char%|%op%|%str%|(")) @@ -7823,6 +7823,10 @@ bool Tokenizer::simplifyCAlternativeTokens() } else if (Token::Match(tok, "not|compl")) { alt.push_back(tok); + // Is this a variable/parameter declaration + if (isC() && Token::Match(tok->previous(), "%type%|* %name% [;,=)]")) + return false; + if ((Token::Match(tok->previous(), "%assign%") || Token::Match(tok->next(), "%num%")) && !Token::Match(tok->next(), ".|->")) { replaceAll = true; continue; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index f7437cdf2e4..303821d9fd6 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -5449,6 +5449,20 @@ class TestTokenizer : public TestFixture { ASSERT_EQUALS(exp, tokenizeAndStringify(code, dinit(TokenizeOptions, $.cpp = false))); } + { // not used as parameter name in C + const char code[] = "static void foo(int not, int test) {" + " test = not;" + "}"; + const char exp[] = "static void foo ( int not , int test ) { test = not ; }"; + ASSERT_EQUALS(exp, tokenizeAndStringify(code, dinit(TokenizeOptions, $.cpp = false))); + } + + { // binary alt token as last parameter in C + const char code[] = "void f(int or) { if (or) {} }"; + const char exp[] = "void f ( int or ) { if ( or ) { } }"; + ASSERT_EQUALS(exp, tokenizeAndStringify(code, dinit(TokenizeOptions, $.cpp = false))); + } + //ASSERT_EQUALS("", filter_valueflow(errout_str())); ignore_errout(); }