Skip to content

Commit

Permalink
generalize sarif loc code (#979)
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna authored Jan 27, 2025
1 parent 8d769d4 commit c36cb17
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
4 changes: 4 additions & 0 deletions src/codemodder/codeql.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def detect(cls, run_data: dict) -> bool:


class CodeQLLocation(SarifLocation):
@staticmethod
def get_snippet(sarif_location) -> str:
return ""

@classmethod
def from_sarif(cls, sarif_location) -> Self:
artifact_location = sarif_location["physicalLocation"]["artifactLocation"]
Expand Down
21 changes: 19 additions & 2 deletions src/codemodder/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,28 @@ class Location(ABCDataclass):

@dataclass(frozen=True)
class SarifLocation(Location):
@classmethod
@staticmethod
@abstractmethod
def from_sarif(cls, sarif_location) -> Self:
def get_snippet(sarif_location) -> str:
pass

@classmethod
def from_sarif(cls, sarif_location) -> Self:
artifact_location = sarif_location["physicalLocation"]["artifactLocation"]
file = Path(artifact_location["uri"])
snippet = cls.get_snippet(sarif_location)
start = LineInfo(
line=sarif_location["physicalLocation"]["region"]["startLine"],
column=sarif_location["physicalLocation"]["region"]["startColumn"],
snippet=snippet,
)
end = LineInfo(
line=sarif_location["physicalLocation"]["region"]["endLine"],
column=sarif_location["physicalLocation"]["region"]["endColumn"],
snippet=snippet,
)
return cls(file=file, start=start, end=end)


@dataclass(frozen=True)
class LocationWithMessage:
Expand Down
21 changes: 4 additions & 17 deletions src/codemodder/semgrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from codemodder.context import CodemodExecutionContext
from codemodder.logging import logger
from codemodder.result import LineInfo, Result, ResultSet, SarifLocation, SarifResult
from codemodder.result import Result, ResultSet, SarifLocation, SarifResult
from codemodder.sarifs import AbstractSarifToolDetector


Expand All @@ -23,24 +23,11 @@ def detect(cls, run_data: dict) -> bool:


class SemgrepLocation(SarifLocation):
@classmethod
def from_sarif(cls, sarif_location) -> Self:
artifact_location = sarif_location["physicalLocation"]["artifactLocation"]
file = Path(artifact_location["uri"])
snippet = (
@staticmethod
def get_snippet(sarif_location) -> str:
return (
sarif_location["physicalLocation"]["region"].get("snippet", {}).get("text")
)
start = LineInfo(
line=sarif_location["physicalLocation"]["region"]["startLine"],
column=sarif_location["physicalLocation"]["region"]["startColumn"],
snippet=snippet,
)
end = LineInfo(
line=sarif_location["physicalLocation"]["region"]["endLine"],
column=sarif_location["physicalLocation"]["region"]["endColumn"],
snippet=snippet,
)
return cls(file=file, start=start, end=end)


class SemgrepResult(SarifResult):
Expand Down

0 comments on commit c36cb17

Please sign in to comment.