如何在CDT(C代码)中逐行警告关闭静态代码分析警告?

时间:2021-08-15 07:24:10

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.

我们在Eclipse中有一个使用CDT的项目。这是我们刚刚导入Eclipse的旧项目,我想确保我们开始使用静态代码分析来查找任何奇怪的东西。

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.

问题是,有很多行触发了我们想要忽略的警告,其中主要是在switch语句中的漏洞。

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?

我知道如何为lint做这个,但CDT呢?是否有单行注释我可以放在线上?

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;

7 个解决方案

#1


22  

You should try

你应该试试

//no break

before the next case.

在下一个案例之前。

#2


9  

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:

这些设置位于Window - > Preferences - > C / C ++ - > Code Analysis下。您可以自定义设置。例如,如果在案例结束时选择“无中断”,则可以定义抑制警告的注释。默认情况下,它是“不休息”。因此,巧合地将警告消息复制/粘贴到您案例中的评论中:

如何在CDT(C代码)中逐行警告关闭静态代码分析警告?

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:

请参考有关未使用变量的后续问题:在文件范围中自定义未使用的变量时,可以定义应忽略的变量名称:

如何在CDT(C代码)中逐行警告关闭静态代码分析警告? 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.

有两个神秘的预定义异常“@(#)”和“$ Id”。不幸的是我找不到任何官方文档,所以我去查看源代码。看起来检查器只是测试变量名是否包含()任何指定的异常。如果是,则警告被抑制。

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.

在Eclipse CDT之外,有流行的虚空铸造技巧。如果编译器警告未使用的变量,则将其强制转换为void。此操作无效,因此它是安全的,但从编译器的角度来看,现在使用变量。我通常将它包装在一个宏中,以便清楚地表明我正在做的事情,例如

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

void foobar()
{
    int b;     // not used. 
    UNUSED(b); // now it's used
}

#3


3  

Solved it.

I just added the text from the warning that I wanted to ignore to immediately above where the break would be.

我刚刚将警告中的文字添加到了我想要忽略的位置,直到上面的中断位置。

Like this:

      case enChC:
        ++nChannel;
        //No break at the end of case
      case enChD:
        ++nChannel;

#4


2  

As is has been said, in this specific case, it can be solved adding the comment:

正如已经说过的那样,在这种特殊情况下,可以通过添加注释来解决:

//no break

or:

//no break at the end of case

What really matters is the (no break).

真正重要的是(没有休息)。

But also, it is required that you don't have more comments between the end of this case and the next one or it won't work. For example the next case will still result in a warning:

但是,要求您在本案例结尾与下一个案例之间没有更多评论,否则它将无效。例如,下一个案例仍然会出现警告:

case enChC:
    ++nChannel;
    //No break
    //This second case decrease the value
  case enChD:
    ++nChannel;

#5


0  

You have to upgrade to Eclipse Oxygen.3 (or.2).

您必须升级到Eclipse Oxygen.3(或2)。

Beginning with these versions warnings/markers can be suppressed by simply using "Quick Fix".

从这些版本开始,只需使用“快速修复”即可抑制警告/标记。

#6


-2  

I have encountered this question,and I just want to eliminate them. I tried to add /* no break */,you should make sure it is added before the next "case". This is the question I have encountered

我遇到过这个问题,我只想消除它们。我试图添加/ *没有中断* /,你应该确保它在下一个“案例”之前添加。这是我遇到的问题

如何在CDT(C代码)中逐行警告关闭静态代码分析警告?

This is the solution I use:

这是我使用的解决方案:

如何在CDT(C代码)中逐行警告关闭静态代码分析警告?

#7


-3  

To disable the annoying notifications, you need to go to Preferences->C/C++->Editor, and then uncheck the options that say "Highlight inactive code", "Display problems as you type", etc. Hope this helps.

要禁用恼人的通知,您需要转到首选项 - > C / C ++ - >编辑器,然后取消选中“突出显示非活动代码”,“键入时显示问题”等选项。希望这会有所帮助。

#1


22  

You should try

你应该试试

//no break

before the next case.

在下一个案例之前。

#2


9  

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:

这些设置位于Window - > Preferences - > C / C ++ - > Code Analysis下。您可以自定义设置。例如,如果在案例结束时选择“无中断”,则可以定义抑制警告的注释。默认情况下,它是“不休息”。因此,巧合地将警告消息复制/粘贴到您案例中的评论中:

如何在CDT(C代码)中逐行警告关闭静态代码分析警告?

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:

请参考有关未使用变量的后续问题:在文件范围中自定义未使用的变量时,可以定义应忽略的变量名称:

如何在CDT(C代码)中逐行警告关闭静态代码分析警告? 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.

有两个神秘的预定义异常“@(#)”和“$ Id”。不幸的是我找不到任何官方文档,所以我去查看源代码。看起来检查器只是测试变量名是否包含()任何指定的异常。如果是,则警告被抑制。

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.

在Eclipse CDT之外,有流行的虚空铸造技巧。如果编译器警告未使用的变量,则将其强制转换为void。此操作无效,因此它是安全的,但从编译器的角度来看,现在使用变量。我通常将它包装在一个宏中,以便清楚地表明我正在做的事情,例如

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

void foobar()
{
    int b;     // not used. 
    UNUSED(b); // now it's used
}

#3


3  

Solved it.

I just added the text from the warning that I wanted to ignore to immediately above where the break would be.

我刚刚将警告中的文字添加到了我想要忽略的位置,直到上面的中断位置。

Like this:

      case enChC:
        ++nChannel;
        //No break at the end of case
      case enChD:
        ++nChannel;

#4


2  

As is has been said, in this specific case, it can be solved adding the comment:

正如已经说过的那样,在这种特殊情况下,可以通过添加注释来解决:

//no break

or:

//no break at the end of case

What really matters is the (no break).

真正重要的是(没有休息)。

But also, it is required that you don't have more comments between the end of this case and the next one or it won't work. For example the next case will still result in a warning:

但是,要求您在本案例结尾与下一个案例之间没有更多评论,否则它将无效。例如,下一个案例仍然会出现警告:

case enChC:
    ++nChannel;
    //No break
    //This second case decrease the value
  case enChD:
    ++nChannel;

#5


0  

You have to upgrade to Eclipse Oxygen.3 (or.2).

您必须升级到Eclipse Oxygen.3(或2)。

Beginning with these versions warnings/markers can be suppressed by simply using "Quick Fix".

从这些版本开始,只需使用“快速修复”即可抑制警告/标记。

#6


-2  

I have encountered this question,and I just want to eliminate them. I tried to add /* no break */,you should make sure it is added before the next "case". This is the question I have encountered

我遇到过这个问题,我只想消除它们。我试图添加/ *没有中断* /,你应该确保它在下一个“案例”之前添加。这是我遇到的问题

如何在CDT(C代码)中逐行警告关闭静态代码分析警告?

This is the solution I use:

这是我使用的解决方案:

如何在CDT(C代码)中逐行警告关闭静态代码分析警告?

#7


-3  

To disable the annoying notifications, you need to go to Preferences->C/C++->Editor, and then uncheck the options that say "Highlight inactive code", "Display problems as you type", etc. Hope this helps.

要禁用恼人的通知,您需要转到首选项 - > C / C ++ - >编辑器,然后取消选中“突出显示非活动代码”,“键入时显示问题”等选项。希望这会有所帮助。