Re:Semgrep: Semantic grep for code
by thomasahle on Thursday April 22, 2021 @01:26PM
(#26905366)
The cli is here: https://semgrep.dev/docs/getting-started/
You can write stuff like
# Check for Python == where the left and right hand sides are the same (often a bug)
$ semgrep -e '$X == $X' --lang=py path/to/srcCool! But this example is a bit simplistic since it can be done just as easily by regular grep:
grep -E '(.+) = \1' *.py
I have trouble looking at the examples in the project website (many things inside iframes are adblocked). Do you have any example of a search that would be difficult or impossible with grep?If you're that capable with grep, surely you've run into it's shortcomings and can appreciate attempts to improve the status quo. Like trying to find usages of a variable that is a substring of a word. Like trying to fix code where somebody else has used the variable name i. Sure, you can add matching on word boundaries, but then it starts to become a distraction from the original task at hand.
Your example matches the _text_, semgrep matches _the language_
given:
if 1 \
== \
1:
pass
then $ semgrep -e '$X == $X' -l python
1:if 1 \
2: == \
3: 1:
ran 1 rules on 1 files: 1 findings
to follow myself up, one can also check for expressions like what I used as an example and say "don't do that", but without regard to what is inside the if semgrep -e $'if ...:\n pass\n' -l python
Re:Semgrep: Semantic grep for code
by SavantIdiot on Thursday April 22, 2021 @01:40PM
(#26905537)
It can infer (x==y) if x=1 and y=1, which is grep cannot do.
This must surely fail... isn't it equivalent to solving the halting problem? Or does it run the whole program like a debugger?
It fails pretty fast.
import random
y = 0
def f(x):
print(x)
if random.randint(0,1) == 2:
y = 1
f(y)
This not only fails but crashes the program.If false negatives are permitted the halting problem isn't an issue.
There’s a demo on the site, I assume that’s it?
No, it's just the landing page. Apparently it does not allow to see it without running some javascript.
Re:Semgrep: Semantic grep for code
by sahkopoyta on Thursday April 22, 2021 @04:35PM
(#26907855)
Well the demo is there on the landing page
Wait, is this a web app? I was expecting a command line tool to navigate my code locally.