Is it possible to suppress Xcode 4 static analyzer warnings?

前端 未结 4 740
广开言路
广开言路 2020-12-04 15:57

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

4条回答
  •  日久生厌
    2020-12-04 16:08

    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 = ;
    
            [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).

提交回复
热议问题