Skip to content

Commit

Permalink
CFamilyLexer: refuse quotes between parentheses for function definiti…
Browse files Browse the repository at this point in the history
…ons and declarations

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 pygments#2207
  • Loading branch information
jeanas committed Aug 14, 2022
1 parent 581f7ee commit 6b824a8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
8 changes: 4 additions & 4 deletions pygments/lexers/c_cpp.py
Expand Up @@ -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'),
Expand All @@ -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'),
Expand Down
2 changes: 1 addition & 1 deletion tests/examplefiles/cpp/example.cpp.output

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions tests/snippets/c/test_string_resembling_decl_end.txt
@@ -0,0 +1,41 @@
---input---
// This should not be recognized as a function declaration followed by
// garbage.
string xyz(");");

// This should not be recognized as a function definition.

string xyz("){ }");

---tokens---
'// This should not be recognized as a function declaration followed by\n' Comment.Single

'// garbage.\n' Comment.Single

'string' Name
' ' Text.Whitespace
'xyz' Name
'(' Punctuation
'"' Literal.String
');' Literal.String
'"' Literal.String
')' Punctuation
';' Punctuation
'\n' Text.Whitespace

'\n' Text.Whitespace

'// This should not be recognized as a function definition.\n' Comment.Single

'\n' Text.Whitespace

'string' Name
' ' Text.Whitespace
'xyz' Name
'(' Punctuation
'"' Literal.String
'){ }' Literal.String
'"' Literal.String
')' Punctuation
';' Punctuation
'\n' Text.Whitespace

0 comments on commit 6b824a8

Please sign in to comment.