Bug 310285 - [checker] Checker for unreachable code
Summary: [checker] Checker for unreachable code
Status: NEW
Alias: None
Product: CDT
Classification: Tools
Component: cdt-codan (show other bugs)
Version: 7.0   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: CDT Codan Inbox CLA
QA Contact: Elena Laskavaia CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-04-23 09:58 EDT by Jens Elmenthaler CLA
Modified: 2019-12-30 18:54 EST (History)
6 users (show)

See Also:


Attachments
Checker to detect dead code (6.50 KB, patch)
2011-11-13 14:29 EST, Mathias De Maré CLA
mathias.demare: review?
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jens Elmenthaler CLA 2010-04-23 09:58:23 EDT
Build Identifier: 

just run into that, and my compiler didn't tell me:

void function() {
  //...
  return;
  otherFunction(); // Code not reachable, my compiler didn't complain:-(
}

Reproducible: Always
Comment 1 Elena Laskavaia CLA 2010-04-23 10:06:28 EDT
fixed version to 7.0
Comment 2 Elena Laskavaia CLA 2010-04-23 10:07:56 EDT
I almost can do it now because I added Control Flow Graph - but it would be very buggy. Maybe can add very dumb one that checks for code after "return" statement only.
Comment 3 Jens Elmenthaler CLA 2010-04-23 10:18:56 EDT
(In reply to comment #2)
> I almost can do it now because I added Control Flow Graph - but it would be
> very buggy. Maybe can add very dumb one that checks for code after "return"
> statement only.
No hurry necessary. I just think that the codan framework has all the potential to bring the CDT where the JDT already is: kind of have an intelligent compiler that provides the feedback very quick, and sometimes even provide quick fixes for it.

Because of that, whenever I stumble over such useful error I will dump them to bugzilla. This way, whenever someone has the time to provide new checkers, having ideas about useful checkers shouldn't be a problem;-)
Comment 4 Mathias De Maré CLA 2011-11-13 14:29:02 EST
Created attachment 206903 [details]
Checker to detect dead code

I had a look at the Control Flow Graph Alena mentioned, and it looked quite useful.

So I made a simple checkers that shows a warning if dead nodes are encountered in the graph.

Currently, that's only after a return statement or the throwing of an exception. However, it looks like the ControlFlowGraphBuilder could just as well support more complicated dead code detection.
Comment 5 Marc-André Laperle CLA 2012-03-31 22:19:33 EDT
Hello Mathias. Have you tested the checker on big files? How is the performance?
Comment 6 Sergey Prigogin CLA 2012-04-02 21:00:44 EDT
Please make sure that the new checker is disabled by default. We can enable it
later when we determine that it doesn't produce false positives.
Comment 7 Elena Laskavaia CLA 2012-04-02 22:02:52 EDT
Can you make multi-project eclipse patch? I cannot apply this one...
Comment 8 Marco Stornelli CLA 2019-03-24 04:14:57 EDT
A dead code checker would be really welcome, but I think the control flow graph lacks a basic feature in order to implement something like that: evaluation of constant expression. AFAIK the control flow grraph creates decision nodes with outgoing edges regardless the condition. Example:

int i = 0;
if (i < 0)
   return 1; <--dead code
return 2;

OR

int i = 0;
while(1) {
    i++;
}
return 2; <----dead code

Maybe we should open a new bug report to add a checker for "always true/false" conditions and set this as "blocked by". Without this feature the current patch is quite limited and I don't know if it's ok to add it.
Comment 9 Nathan Ridge CLA 2019-03-24 18:12:39 EDT
(In reply to Marco Stornelli from comment #8)
> A dead code checker would be really welcome, but I think the control flow
> graph lacks a basic feature in order to implement something like that:
> evaluation of constant expression.

The control flow graph should already be able to do this: see ControlFlowGraphBuilder.isConstant().
Comment 10 Marco Stornelli CLA 2019-03-25 01:56:24 EDT
(In reply to Nathan Ridge from comment #9)
> (In reply to Marco Stornelli from comment #8)
> > A dead code checker would be really welcome, but I think the control flow
> > graph lacks a basic feature in order to implement something like that:
> > evaluation of constant expression.
> 
> The control flow graph should already be able to do this: see
> ControlFlowGraphBuilder.isConstant().

Not exactly, it just checks for constant values instead of constant expression and only for if/else. It doesn't work with the examples I did.
Comment 11 Nathan Ridge CLA 2019-03-25 02:32:37 EDT
(In reply to Marco Stornelli from comment #10)
> Not exactly, it just checks for constant values instead of constant
> expression 

The function used in the implementation of isConstant(), ValueFactory.getConstantNumericalValue(IASTExpression), should be able to take an arbitrary expression and, if it's a constant expression, give you its value.

> and only for if/else.

Sure, it may require additional hookup to use this facility for other types of control flow constructs.

> It doesn't work with the examples I did.

Note that in your first example, "i" is not a constant. For your first example to work as written (without adding "const" or "constexpr" to "i"), we would also need data flow analysis to determine that the value of "i" does not change between its declaration and its use in the if condition. That's a lot of more complexity in the general case, so I wouldn't bother with that for now.
Comment 12 Marco Stornelli CLA 2019-03-25 04:21:07 EDT
(In reply to Nathan Ridge from comment #11)
> (In reply to Marco Stornelli from comment #10)
> > Not exactly, it just checks for constant values instead of constant
> > expression 
> 
> The function used in the implementation of isConstant(),
> ValueFactory.getConstantNumericalValue(IASTExpression), should be able to
> take an arbitrary expression and, if it's a constant expression, give you
> its value.
> 
> > and only for if/else.
> 
> Sure, it may require additional hookup to use this facility for other types
> of control flow constructs.
> 
> > It doesn't work with the examples I did.
> 
> Note that in your first example, "i" is not a constant. For your first
> example to work as written (without adding "const" or "constexpr" to "i"),
> we would also need data flow analysis to determine that the value of "i"
> does not change between its declaration and its use in the if condition.
> That's a lot of more complexity in the general case, so I wouldn't bother
> with that for now.

Sorry for the confusion but it's what I meant with "constant expression". An expression with a value which don't change between declaration and condition as you said.

So if I understood you will be ok with the current patch about dead code (maybe with the addition of current constant check for while/do-while and so on). I can take a look.
Comment 13 Jonah Graham CLA 2019-12-30 18:54:33 EST
This bug was assigned and targeted at a now released milestone (or Future or Next that isn't being used by CDT). As that milestone has now passed, the milestone field has been cleared. If this bug has been fixed, please set the milestone to the version it was fixed in and mark the bug as resolved.