Skip to content

Commit

Permalink
Fix C# regression in preprocessor directive parsing (#514)
Browse files Browse the repository at this point in the history
* Fix C# regression in preprocessor directive parsing
It was due to a left-over syntax extension workaround that no longer
applies and was causing conflicts.

* Fix parsing of typed metavariables
Test 'Typed metavariable 3' was failing and was being parsed as binary
'<' and '>' operations.
  • Loading branch information
mjambon authored Oct 11, 2024
1 parent 208d243 commit a55e4a6
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 18 deletions.
7 changes: 7 additions & 0 deletions lang/c-sharp-pro/test/ok/preproc_stmts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
void F()
{
#if DEBUG
a();
b();
#endif
}
20 changes: 2 additions & 18 deletions lang/semgrep-grammars/src/semgrep-c-sharp-pro/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ module.exports = grammar(standard_grammar, {
conflicts: ($, previous) => [
...previous,
[$.expression, $.parameter],
[$.file_scoped_namespace_declaration, $.preproc_if_in_top_level],
[$.file_scoped_namespace_declaration, $.preproc_else_in_top_level],
],

rules: {
Expand Down Expand Up @@ -77,20 +75,6 @@ module.exports = grammar(standard_grammar, {
)
},

// We do this because a file scoped namespace declaration is a top-level
// thing, but ellipses are more particular. We want ellipses to be used
// in conjunction with file scoped namespace declarations.
// Unfortunately, in the base grammar, we can either have ellipsis
// statements, or a file scoped declaration! That's no good. To play
// around the previous grammar, we simply allow what came before to also
// occur before a file scoped namespace declaration.
file_scoped_namespace_declaration: ($, previous) => {
return seq(
seq(repeat($.global_statement), repeat($._namespace_member_declaration)),
previous,
)
},

enum_member_declaration: ($, previous) => choice(
previous,
$.ellipsis,
Expand Down Expand Up @@ -129,12 +113,12 @@ module.exports = grammar(standard_grammar, {

// use syntax similar to a cast_expression, but with metavar
//TODO: use PREC.CAST from original grammar instead of 17 below
typed_metavariable: $ => prec.right(17, seq(
typed_metavariable: $ => prec(17, prec.dynamic(1, seq(
'(',
field('type', $.type),
field('metavar', $._semgrep_metavariable),
')',
)),
))),

deep_ellipsis: $ => seq(
'<...', $.expression, '...>'
Expand Down
106 changes: 106 additions & 0 deletions lang/semgrep-grammars/src/semgrep-c-sharp-pro/test/corpus/semgrep.txt
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,52 @@ __SEMGREP_EXPRESSION
(identifier))
(argument_list))))

===============================================================================
Typed metavariable 2
===============================================================================

__SEMGREP_EXPRESSION (T $X)

---

(compilation_unit
(semgrep_expression
(typed_metavariable
(identifier))))

===============================================================================
Typed metavariable 3
===============================================================================

__SEMGREP_EXPRESSION (List<T> $X)

---

(compilation_unit
(semgrep_expression
(typed_metavariable
(generic_name
(identifier)
(type_argument_list
(identifier))))))

===============================================================================
Cast expression
===============================================================================

__SEMGREP_EXPRESSION (List<T>) x

---

(compilation_unit
(semgrep_expression
(cast_expression
(generic_name
(identifier)
(type_argument_list
(identifier)))
(identifier))))

================================================================================
Ellipses + metavariables as args
================================================================================
Expand Down Expand Up @@ -376,3 +422,63 @@ namespace $N;
(global_statement
(expression_statement
(ellipsis))))

================================================================================
Preprocessor conditional with single statement
================================================================================

void F()
{
#if DEBUG
a();
#endif
}

---

(compilation_unit
(global_statement
(local_function_statement
(predefined_type)
(identifier)
(parameter_list)
(block
(preproc_if
(identifier)
(expression_statement
(invocation_expression
(identifier)
(argument_list))))))))

===============================================================================
Preprocessor conditional with multiple statements
===============================================================================

void F()
{
#if DEBUG
a();
b();
#endif
}

---

(compilation_unit
(global_statement
(local_function_statement
(predefined_type)
(identifier)
(parameter_list)
(block
(preproc_if
(identifier)
(expression_statement
(invocation_expression
(identifier)
(argument_list)))
(expression_statement
(invocation_expression
(identifier)
(argument_list))))))))

29 changes: 29 additions & 0 deletions lang/semgrep-grammars/src/semgrep-c-sharp/test/corpus/semgrep.txt
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,35 @@ __SEMGREP_EXPRESSION
(identifier))
(argument_list))))

===============================================================================
Typed metavariable 2
===============================================================================

__SEMGREP_EXPRESSION (T $X)

---

(compilation_unit
(semgrep_expression
(typed_metavariable
(identifier))))

===============================================================================
Typed metavariable 3
===============================================================================

__SEMGREP_EXPRESSION (List<T> $X)

---

(compilation_unit
(semgrep_expression
(typed_metavariable
(generic_name
(identifier)
(type_argument_list
(identifier))))))

================================================================================
Ellipses + metavariables as args
================================================================================
Expand Down

0 comments on commit a55e4a6

Please sign in to comment.