是否可以抑制 Xcode 4 静态分析器警告?

发布于 2024-11-03 19:03:03 字数 47 浏览 3 评论 0原文

Xcode 4 静态分析器在我的代码中报告了一些误报。有什么办法可以压制他们吗?

The Xcode 4 static analyzer reports in my code some false positives. Is there any way to suppress them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘寄 2024-11-10 19:03:03

我找到了一个解决方案:可以通过以下方式避免误报(如 Apple 单例设计模式):

#ifndef __clang_analyzer__

// Code not to be analyzed

#endif

分析器不会分析这些预处理器指令之间的代码。

I found a solution: false positives (like the Apple singleton design pattern) can be avoided with:

#ifndef __clang_analyzer__

// Code not to be analyzed

#endif

Analyzer will not analyze the code between those preprocessor directives.

厌倦 2024-11-10 19:03:03

看一下这个页面,它展示了如何使用多个 #defines 来注释 Objective-C 方法和参数,以帮助静态分析器 (clang) 做正确的事情

http://clang-analyzer.llvm.org/annotations.html

从该页面:

Clang 前端支持多个源代码级注释
GCC 风格的属性和编译指示的形式可以帮助使用
Clang 静态分析器更有用。这些注释都可以帮助
抑制误报并增强分析仪的能力
查找错误。

Take a look at this page which shows how to use several #defines to annotate objective-c methods and parameters to help the static analyzer (clang) do the right thing

http://clang-analyzer.llvm.org/annotations.html

From that page:

The Clang frontend supports several source-level annotations in the
form of GCC-style attributes and pragmas that can help make using the
Clang Static Analyzer more useful. These annotations can both help
suppress false positives as well as enhance the analyzer's ability to
find bugs.

饮惑 2024-11-10 19:03:03

查看我的答案 这里。您可以向文件添加编译标志,静态分析器将忽略它们。这对于您不关心的第 3 方代码可能更好,而不是您正在编写的第一方代码。

See my answer here. You can add a compile flag to the files and static analyzer will ignore them. This is probably better for 3rd party code you aren't concerned about, and not for first party code you are writing.

硪扪都還晓 2024-11-10 19:03:03

大多数时候,使用 CF_RETURNS_RETAINED 之类的东西并遵循“创建”规则对我来说是有效的,但我遇到了一个我无法抑制的情况。
最后通过查看llvm源代码找到了抑制分析器的方法:

https://llvm.org/svn/llvm-project/cfe/trunk/test/ARCMT/objcmt-arc-cf-annotations.m.result

“测试一下当我们将指针存储到一个
全球。”

static CGLayerRef sSuppressStaticAnalyzer;
static CGLayerRef sDmxImg[2][2][1000]; // a cache of quartz drawings.
CGLayerRef CachedDmxImg(...) // which lives for lifetime of app!
{
    ...

    CGLayerRef img = sDmxImg[isDefault][leadingZeroes][dmxVal];
    if ( !img )
    {
        NSRect imgRect = <some cool rectangle>;

        [NSGraphicsContext saveGraphicsState];
        CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
        CGLayerRef cgLayerRef = CGLayerCreateWithContext(ctx, imgRect.size, NULL);
        CGContextRef layerCtx = CGLayerGetContext(cgLayerRef);
        [NSGraphicsContext setCurrentContext: [NSGraphicsContext graphicsContextWithGraphicsPort:layerCtx flipped:YES]];

        ... draw some gorgeous expensive Quartz stuff ...

        img = cgLayerRef;
        sDmxImg[isDefault][leadingZeroes][dmxVal] = cgLayerRef;
        sSuppressStaticAnalyzer = cgLayerRef; // suppress static analyzer warning!
        [NSGraphicsContext restoreGraphicsState];
   }
   return img;
}

分配给静态数组不会抑制警告,但分配给普通的旧静态'sSuppressStaticAnalyzer'
顺便说一句,上述方法中,使用 CGLayerRef 是我发现重绘缓存图像的最快方法(除了 OpenGL 之外)。

most of the time, using things like CF_RETURNS_RETAINED and following the 'create' rule works for me, but I ran into a case I could NOT suppress.
Finally found a way to suppress the analyzer by looking at llvm source code:

https://llvm.org/svn/llvm-project/cfe/trunk/test/ARCMT/objcmt-arc-cf-annotations.m.result

"Test to see if we suppress an error when we store the pointer to a
global."

static CGLayerRef sSuppressStaticAnalyzer;
static CGLayerRef sDmxImg[2][2][1000]; // a cache of quartz drawings.
CGLayerRef CachedDmxImg(...) // which lives for lifetime of app!
{
    ...

    CGLayerRef img = sDmxImg[isDefault][leadingZeroes][dmxVal];
    if ( !img )
    {
        NSRect imgRect = <some cool rectangle>;

        [NSGraphicsContext saveGraphicsState];
        CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
        CGLayerRef cgLayerRef = CGLayerCreateWithContext(ctx, imgRect.size, NULL);
        CGContextRef layerCtx = CGLayerGetContext(cgLayerRef);
        [NSGraphicsContext setCurrentContext: [NSGraphicsContext graphicsContextWithGraphicsPort:layerCtx flipped:YES]];

        ... draw some gorgeous expensive Quartz stuff ...

        img = cgLayerRef;
        sDmxImg[isDefault][leadingZeroes][dmxVal] = cgLayerRef;
        sSuppressStaticAnalyzer = cgLayerRef; // suppress static analyzer warning!
        [NSGraphicsContext restoreGraphicsState];
   }
   return img;
}

For some reason, assigning to a static array didn't suppress the warning, but assigning to a plain old static 'sSuppressStaticAnalyzer' does.
By the way the above method, using CGLayerRef is the fastest way I've found to redraw cached images (besides OpenGL).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文