From a0d664ee8254f0d18196f612e76b07d8d6096b06 Mon Sep 17 00:00:00 2001 From: Jean Abou Samra Date: Mon, 15 Aug 2022 00:23:27 +0200 Subject: [PATCH] CFamilyLexer: refuse quotes between parentheses for function definitions and declarations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Something like id id2("){ ... }"); is no longer wrongly recognized as a "function" id id2(") { ... } "); As the difference in the tests shows, this has the unfortunate side effect that we no longer highlight something like int f(param="default"); as a function declaration, but it is hard to imagine another way to fix this (cf. “most vexing parse” problem). Fixes #2207 --- pygments/lexers/c_cpp.py | 8 ++++---- tests/examplefiles/cpp/example.cpp.output | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pygments/lexers/c_cpp.py b/pygments/lexers/c_cpp.py index 6deb8bf8bb..865065ba4b 100644 --- a/pygments/lexers/c_cpp.py +++ b/pygments/lexers/c_cpp.py @@ -132,9 +132,9 @@ class CFamilyLexer(RegexLexer): r'(' + _possible_comments + r')' # possible comments r'(' + _namespaced_ident + r')' # method name r'(' + _possible_comments + r')' # possible comments - r'(\([^;]*?\))' # signature + r'(\([^;"\']*?\))' # signature r'(' + _possible_comments + r')' # possible comments - r'([^;{/]*)(\{)', + r'([^;{/"\']*)(\{)', bygroups(using(this), using(this, state='whitespace'), Name.Function, using(this, state='whitespace'), using(this), using(this, state='whitespace'), using(this), Punctuation), 'function'), @@ -143,9 +143,9 @@ class CFamilyLexer(RegexLexer): r'(' + _possible_comments + r')' # possible comments r'(' + _namespaced_ident + r')' # method name r'(' + _possible_comments + r')' # possible comments - r'(\([^;]*?\))' # signature + r'(\([^;"\']*?\))' # signature r'(' + _possible_comments + r')' # possible comments - r'([^;/]*)(;)', + r'([^;/"\']*)(;)', bygroups(using(this), using(this, state='whitespace'), Name.Function, using(this, state='whitespace'), using(this), using(this, state='whitespace'), using(this), Punctuation)), include('types'), diff --git a/tests/examplefiles/cpp/example.cpp.output b/tests/examplefiles/cpp/example.cpp.output index b82dd0955b..1e2444fce8 100644 --- a/tests/examplefiles/cpp/example.cpp.output +++ b/tests/examplefiles/cpp/example.cpp.output @@ -944,7 +944,7 @@ ' ' Text.Whitespace 'string' Name ' ' Text.Whitespace -'getOpenTag' Name.Function +'getOpenTag' Name '(' Punctuation 'const' Keyword ' ' Text.Whitespace