[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Julia] Macro calls are parsed the same as function calls? #10495

Open
1 of 3 tasks
iuliadmtru opened this issue Sep 2, 2024 · 0 comments
Open
1 of 3 tasks

[Julia] Macro calls are parsed the same as function calls? #10495

iuliadmtru opened this issue Sep 2, 2024 · 0 comments

Comments

@iuliadmtru
Copy link

Describe the bug
In the Julia syntax, both calls and macrocalls exist. However, Semgrep generates ASTs with the same node (Call) for both of them. For example, for basic calls:

f(x):

   ExprStmt(
     Call(
       N(
         Id(("f", ()),
           {id_info_id=8; id_flags=Ref(0); id_resolved=Ref(None);
            id_type=Ref(None); id_svalue=Ref(None); })),
       [Arg(
          N(
            Id(("x", ()),
              {id_info_id=9; id_flags=Ref(0); id_resolved=Ref(None);
               id_type=Ref(None); id_svalue=Ref(None); })))]), ())

@m(x):

   ExprStmt(
     Call(OtherExpr(("@", ()), [I(("m", ()))]),
       [Arg(
          N(
            Id(("x", ()),
              {id_info_id=7; id_flags=Ref(0); id_resolved=Ref(None);
               id_type=Ref(None); id_svalue=Ref(None); })))]), ())

This can be problematic when writing rules. For example, a struct wrapped around Base.@kwdef gets matched as a function call: https://semgrep.dev/playground/s/WAyDy. This can be avoided like this, but it seems a bit frail and I think they should be treated differently anyway.

The tree sitter seems to differentiate as well between function calls and macro calls, so I believe the problem is with the Semgrep generated AST.

As a note, macro definitions and function definitions are treated differently:

f(x) = x:

   DefStmt(
     ({
       name=EN(
              Id(("f", ()),
                {id_info_id=2; id_flags=Ref(0); id_resolved=Ref(None);
                 id_type=Ref(None); id_svalue=Ref(None); }));
       attrs=[]; tparams=None; },
      FuncDef(
        {fkind=(Function, ());
         fparams=[Param(
                    {pname=Some(("x", ())); pdefault=None; ptype=None;
                     pattrs=[];
                     pinfo={id_info_id=3; id_flags=Ref(0);
                            id_resolved=Ref(Some((Parameter, -1)));
                            id_type=Ref(None); id_svalue=Ref(None); };
                     })];
         frettype=None;
         fbody=FBExpr(
                 N(
                   Id(("x", ()),
                     {id_info_id=1; id_flags=Ref(0); id_resolved=Ref(
                      None); id_type=Ref(None); id_svalue=Ref(None); })));
         })))

macro m(x) :(x) end:

   DefStmt(
     ({
       name=EN(
              Id(("m", ()),
                {id_info_id=4; id_flags=Ref(0); id_resolved=Ref(None);
                 id_type=Ref(None); id_svalue=Ref(None); }));
       attrs=[]; tparams=None; },
      MacroDef(
        {macroparams=[("x", ())];
         macrobody=[S(
                      ExprStmt(
                        OtherExpr((":", ()),
                          [E(
                             N(
                               Id(("x", ()),
                                 {id_info_id=6; id_flags=Ref(0);
                                  id_resolved=Ref(None); id_type=Ref(
                                  None); id_svalue=Ref(None); })))]), ()))];
         })))

To Reproduce
The playground link above.

Expected behavior
Macro calls should be treated differently. Maybe they should not be matched with $X(...), but with @$X(...)?

What is the priority of the bug to you?

  • P0: blocking your adoption of Semgrep or workflow
  • P1: important to fix or quite annoying
  • P2: regular bug that should get fixed

Use case
This would make the generated AST more correct/complete.

@aryx aryx added the lang:julia label Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants