From 832487a457ac4b3a0ffeaddb1d8d8e3d4118b039 Mon Sep 17 00:00:00 2001 From: Martin Jambon Date: Wed, 18 Sep 2024 15:06:34 -0700 Subject: [PATCH 1/4] Mark c-sharp as working with tree-sitter 0.22.6 --- lang/language-variants-0.20.6 | 1 - lang/language-variants-0.22.6 | 1 + lang/languages-0.20.6 | 1 - lang/languages-0.22.6 | 1 + 4 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lang/language-variants-0.20.6 b/lang/language-variants-0.20.6 index 391680c5..6bfe4558 100644 --- a/lang/language-variants-0.20.6 +++ b/lang/language-variants-0.20.6 @@ -1,6 +1,5 @@ apex bash -c-sharp dart elixir fsharp diff --git a/lang/language-variants-0.22.6 b/lang/language-variants-0.22.6 index 2944501e..8eb6e620 100644 --- a/lang/language-variants-0.22.6 +++ b/lang/language-variants-0.22.6 @@ -1,4 +1,5 @@ c +c-sharp cairo circom clojure diff --git a/lang/languages-0.20.6 b/lang/languages-0.20.6 index e61996bc..2418e999 100644 --- a/lang/languages-0.20.6 +++ b/lang/languages-0.20.6 @@ -1,5 +1,4 @@ bash -c-sharp dart elixir fsharp diff --git a/lang/languages-0.22.6 b/lang/languages-0.22.6 index 2944501e..8eb6e620 100644 --- a/lang/languages-0.22.6 +++ b/lang/languages-0.22.6 @@ -1,4 +1,5 @@ c +c-sharp cairo circom clojure From 0701e7086f2b12eed5f155c35a1861360937fb4c Mon Sep 17 00:00:00 2001 From: Martin Jambon Date: Wed, 18 Sep 2024 15:20:52 -0700 Subject: [PATCH 2/4] Update tree-sitter-c-sharp to the latest --- lang/semgrep-grammars/src/tree-sitter-c-sharp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/semgrep-grammars/src/tree-sitter-c-sharp b/lang/semgrep-grammars/src/tree-sitter-c-sharp index e1384e2f..fd7f7402 160000 --- a/lang/semgrep-grammars/src/tree-sitter-c-sharp +++ b/lang/semgrep-grammars/src/tree-sitter-c-sharp @@ -1 +1 @@ -Subproject commit e1384e2f132936019b43aaaae154cd780fb497ce +Subproject commit fd7f7402db6e66afd70b402fb2e367b2d71c10d6 From f8862c07df7c9b0d6e9f932854af52e6b1d89237 Mon Sep 17 00:00:00 2001 From: Martin Jambon Date: Wed, 18 Sep 2024 15:46:01 -0700 Subject: [PATCH 3/4] Update semgrep extensions for C# --- .../src/semgrep-c-sharp/grammar.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lang/semgrep-grammars/src/semgrep-c-sharp/grammar.js b/lang/semgrep-grammars/src/semgrep-c-sharp/grammar.js index d9289bf6..5c36ecad 100644 --- a/lang/semgrep-grammars/src/semgrep-c-sharp/grammar.js +++ b/lang/semgrep-grammars/src/semgrep-c-sharp/grammar.js @@ -25,7 +25,9 @@ module.exports = grammar(standard_grammar, { conflicts: ($, previous) => [ ...previous, - [$._expression, $.parameter] + [$.expression, $.parameter], + [$.file_scoped_namespace_declaration, $.preproc_if_in_top_level], + [$.file_scoped_namespace_declaration, $.preproc_else_in_top_level], ], rules: { @@ -39,7 +41,7 @@ module.exports = grammar(standard_grammar, { }, // Alternate "entry point". Allows parsing a standalone expression. - semgrep_expression: $ => seq('__SEMGREP_EXPRESSION', $._expression), + semgrep_expression: $ => seq('__SEMGREP_EXPRESSION', $.expression), // Metavariables identifier: ($, previous) => { @@ -68,7 +70,7 @@ module.exports = grammar(standard_grammar, { ); }, - _declaration: ($, previous) => { + declaration: ($, previous) => { return choice( ...previous.members, $.ellipsis @@ -77,7 +79,7 @@ module.exports = grammar(standard_grammar, { // We want ellipses to be interchangeable with namespace member declarations, so // we need to add them in to `type_declaration` here. - _type_declaration: ($, previous) => { + type_declaration: ($, previous) => { return choice( ...previous.members, $.ellipsis @@ -117,7 +119,7 @@ module.exports = grammar(standard_grammar, { }, // Expression ellipsis - _expression: ($, previous) => { + expression: ($, previous) => { return choice( ...previous.members, $.ellipsis, @@ -129,7 +131,7 @@ module.exports = grammar(standard_grammar, { // TODO: how to use PREC.DOT from original grammar instead of 18 below? member_access_ellipsis_expression : $ => prec(18, seq( - field('expression', choice($._expression, $.predefined_type, $._name)), + field('expression', choice($.expression, $.predefined_type, $._name)), choice('.', '->'), $.ellipsis )), @@ -138,13 +140,13 @@ module.exports = grammar(standard_grammar, { //TODO: use PREC.CAST from original grammar instead of 17 below typed_metavariable: $ => prec.right(17, seq( '(', - field('type', $._type), + field('type', $.type), field('metavar', $._semgrep_metavariable), ')', )), deep_ellipsis: $ => seq( - '<...', $._expression, '...>' + '<...', $.expression, '...>' ), ellipsis: $ => '...', From db3cedacd317e67335481a2f5ebced4049be3267 Mon Sep 17 00:00:00 2001 From: Martin Jambon Date: Wed, 18 Sep 2024 16:00:04 -0700 Subject: [PATCH 4/4] Update semgrep-c-sharp test expectations --- .../semgrep-c-sharp/test/corpus/semgrep.txt | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/lang/semgrep-grammars/src/semgrep-c-sharp/test/corpus/semgrep.txt b/lang/semgrep-grammars/src/semgrep-c-sharp/test/corpus/semgrep.txt index 6473ea20..66ceb46c 100644 --- a/lang/semgrep-grammars/src/semgrep-c-sharp/test/corpus/semgrep.txt +++ b/lang/semgrep-grammars/src/semgrep-c-sharp/test/corpus/semgrep.txt @@ -27,11 +27,10 @@ class $CLASS { (predefined_type) (variable_declarator (identifier) - (equals_value_clause - (conditional_expression - (identifier) - (identifier) - (identifier))))))))))) + (conditional_expression + (identifier) + (identifier) + (identifier)))))))))) ================================================================================ Ellipsis for expression @@ -59,14 +58,12 @@ class Foo { (expression_statement (assignment_expression (identifier) - (assignment_operator) (integer_literal))) (expression_statement (ellipsis)) (expression_statement (assignment_expression (identifier) - (assignment_operator) (integer_literal)))))))) ================================================================================ @@ -95,14 +92,12 @@ class Foo { (expression_statement (assignment_expression (identifier) - (assignment_operator) (integer_literal))) (expression_statement (ellipsis)) (expression_statement (assignment_expression (identifier) - (assignment_operator) (integer_literal)))))))) ================================================================================ @@ -129,7 +124,6 @@ class Foo { (expression_statement (assignment_expression (identifier) - (assignment_operator) (deep_ellipsis (integer_literal))))))))) @@ -236,7 +230,6 @@ $X = $O.foo(). ... .bar(); (expression_statement (assignment_expression (identifier) - (assignment_operator) (invocation_expression (member_access_expression (member_access_ellipsis_expression @@ -360,7 +353,9 @@ namespace $N { }; (namespace_declaration (identifier) (declaration_list)) - (ellipsis)) + (global_statement + (expression_statement + (ellipsis)))) ================================================================================ File scoped namespace declarations and ellipses @@ -373,9 +368,11 @@ namespace $N; -------------------------------------------------------------------------------- (compilation_unit + (global_statement + (expression_statement + (ellipsis))) (file_scoped_namespace_declaration - (global_statement - (expression_statement - (ellipsis))) - (identifier) - (ellipsis))) + (identifier)) + (global_statement + (expression_statement + (ellipsis))))