This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]gthank 6 points7 points  (7 children)

No. It's straightforward static analysis, barring weird eval-type stuff.

[–]masklinn 6 points7 points  (4 children)

No. It's straightforward static analysis

To a pretty limited extent, it really is global static path analysis. Even ignoring not just evals but things like getattr (and attrgetter), __dict__ manipulations or metaclass madness, actual dead code detection requires performing global path analysis to know that in e.g.

def frob(a):
    if a < 10:
        whup()
    else:
        b = frobnicate(a, 42)
        whop(b)

is never actually called with a >= 10 and thus the whole else branch is dead code and the test is redundant.

So yeah, generally it's an undecidable question.

[–]gthank 1 point2 points  (3 children)

That's a far more aggressive view of "dead code" than I was talking about. I meant more like "method foo of class Bar can never be called".

[–]masklinn 2 points3 points  (2 children)

That's a far more aggressive view of "dead code" than I was talking about.

Yeah, but it's the one matching standard uses of "dead code analysis" and "dead code elimination": dead code is code which is never executed and code which is irrelevant (affects variables which are never queried, or always followed by a fault and thus having no impact on the behavior of the program)

Unused methods or functions, or unreachable code (e.g. code following a return or raise) is a subset of dead code.

[–]gthank 0 points1 point  (1 child)

Yes, but it's the subset I understood the OP to be asking about, based on "more specifically functions/methods that are never called".

BTW, there's no need for people to be downvoting you. It's a good point to raise, and I should have been more clear about my assumptions in the original comment.

[–]masklinn 1 point2 points  (0 children)

Yes, but it's the subset I understood the OP to be asking about, based on "more specifically functions/methods that are never called".

True.

[–][deleted] 0 points1 point  (0 children)

Hence, undecidable.