mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Python: CG trace: Handle BUILD_LIST
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
def foo():
|
||||
print("foo")
|
||||
|
||||
|
||||
def bar():
|
||||
print("bar")
|
||||
|
||||
|
||||
[foo, bar][0]()
|
||||
@@ -67,6 +67,19 @@ class BytecodeTuple(BytecodeExpr):
|
||||
return f"({elements_formatted})"
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True, eq=True, order=True)
|
||||
class BytecodeList(BytecodeExpr):
|
||||
elements: List[BytecodeExpr]
|
||||
|
||||
def __str__(self):
|
||||
elements_formatted = (
|
||||
", ".join(str(e) for e in self.elements)
|
||||
if len(self.elements) > 1
|
||||
else f"{self.elements[0]},"
|
||||
)
|
||||
return f"[{elements_formatted}]"
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True, eq=True, order=True)
|
||||
class BytecodeCall(BytecodeExpr):
|
||||
function: BytecodeExpr
|
||||
@@ -174,13 +187,14 @@ def expr_from_instruction(instructions: List[Instruction], index: int) -> Byteco
|
||||
obj_expr = expr_that_added_elem_to_stack(instructions, index - 1, 1)
|
||||
return BytecodeSubscript(key=key_expr, object=obj_expr)
|
||||
|
||||
elif inst.opname in ["BUILD_TUPLE"]:
|
||||
elif inst.opname in ["BUILD_TUPLE", "BUILD_LIST"]:
|
||||
elements = []
|
||||
for i in range(inst.arg):
|
||||
element_expr = expr_that_added_elem_to_stack(instructions, index - 1, i)
|
||||
elements.append(element_expr)
|
||||
elements.reverse()
|
||||
return BytecodeTuple(elements=elements)
|
||||
klass = {"BUILD_TUPLE": BytecodeTuple, "BUILD_LIST": BytecodeList}[inst.opname]
|
||||
return klass(elements=elements)
|
||||
|
||||
# https://docs.python.org/3/library/dis.html#opcode-CALL_FUNCTION
|
||||
elif inst.opname in [
|
||||
|
||||
Reference in New Issue
Block a user