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

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

我发现了一个解决scheme:假阳性(如苹果单身devise模式)可以避免:

#ifndef __clang_analyzer__ // Code not to be analyzed #endif 

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

看看这个页面,该页面显示了如何使用多个#define注释objective-c方法和参数,以帮助静态分析器(clang)做正确的事情

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

从该页面:

Clang前端支持多种GCC样式属性和编译指示的源代码级注释,可以使Clang静态分析器更有用。 这些注释既可以帮助抑制误报,也可以提高分析器查找错误的能力。

在这里看到我的答案。 您可以添加一个编译标志的文件和静态分析器将忽略它们。 这可能对您不关心的第三方代码更好,也不适用于您正在编写的第一方代码。

大多数情况下,使用诸如CF_RETURNS_RETAINED之类的东西,并遵循“创build”规则,但我遇到了一个我无法压制的情况。 最后find了一种通过查看llvm源代码来压制分析器的方法:

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

“当我们将指针存储到全局variables时,testing是否可以抑制错误。”

 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; } 

出于某种原因,分配给一个静态数组并没有压制这个警告,而是分配给一个普通的静态的'SuppressStaticAnalyzer'。 顺便说一下,上面的方法,使用CGLayerRef是我发现重绘caching图像(OpenGL除外)的最快方法。