Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I turn off a static code analysis warning on a line by line warning in CDT (C code)?

We have a project using CDT in Eclipse. It's an old project that we just imported into Eclipse, and I want to ensure we start using static code analysis to find any weirdnesses.

The thing is, there are a bunch of lines that trigger warnings that we want to just ignore, with the main ones being fallthroughs within switch statements.

I know how to do this for lint, but what about for CDT? Is there a single-line comment that I can put right above the line?

Example: ("No break at the end of case")

  case enChA:  
    nChannel++;
    // I want to ignore this fallthrough       
  case enChB:      
    nChannel++;
    // And this one...
  case enChC:
    nChannel++;
    // And this one...
  case enChD:
    nChannel++;
    // do some more stuff...
    break;
like image 926
c.fogelklou Avatar asked Jun 05 '13 09:06

c.fogelklou


2 Answers

You should try

//no break

before the next case.

like image 103
gkovacs90 Avatar answered Oct 27 '22 08:10

gkovacs90


These settings are located under Window -> Preferences -> C/C++ -> Code Analysis. You can customize the settings. For example if you pick No break at the end of case, you can define the comment that suppresses the warning. By default it's "no break". So coincidentally copy/pasting the warning message into the comment worked in your case:

CDT static code analysis

As you can see the text doesn't have to be an exact match and it doesn't seem to be case sensitive either.

Referring to your follow-up question about unused variables: When you customize Unused variable in file scope you can define variable names that should be ignored:

enter image description here There are two cryptic predefined exceptions "@(#)" and "$Id". Unfortunately I couldn't find any official documentation so I went looking into the source. It looks like the checker simply tests if a variable name contains() any of the specified exceptions. If it does, the warning is suppressed.

Outside of Eclipse CDT, there's the popular void-casting trick. If the compiler warns about an unused variable, cast it to void. This operation has no effect, so it's safe, but from the perspective of the compiler that variable is now used. I usually wrap it in a macro to make abundantly clear what I'm doing, e.g.

#define UNUSED(var) (void)(var)

void foobar()
{
    int b;     // not used. 
    UNUSED(b); // now it's used
}
like image 31
djf Avatar answered Oct 27 '22 09:10

djf