Clang Static Analyzer: The absolute best thing you can do for yourself and your code.

Discussion in 'iOS Development' started by SkylarEC, May 15, 2009.

  1. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    I was spurred to use the clang static analyzer by francis who saw it in some iPhone video. Basically, clang is another compiler separate from gcc, created by the folks at http://llvm.org/ [See: http://clang.llvm.org/] One of the tools created as a part of the clang project is the static analyzer.

    In simple terms, the static analyzer goes through your code and finds not only bugs that Xcode misses, but it will find any memory leaks and alert you. Saving you the trouble of tracking down leaks in Instruments. The analyzer will even yell at you if you don't follow naming conventions.

    When finished going through your build, the static analyzer will launch a web server that displays any bugs it may find in easy to read page that mimics the appearance of Xcode.

    See the following image for an example. It tells you the leak and the path taken to arrive at the leak. Specifically, it's the release line I commented out.

    Please Register or Log in to view images


    Full sized image: http://static-cache.ifans.com/imgcache2/39175.png


    clang static analysis is now included in Xcode versions 3.2 and higher. If you are running Xcode 3.2 or higher, then please ignore the rest of this post. To perform an analysis with Xcode 3.2 or higher, you must first perform a "Clean all." Next, click on the "Build" menu in Xcode and click "Build And Analysize."



    Installation:
    1. Navigate to http://clang.llvm.org/StaticAnalysis.html
    2. Download the linked checker tarbell (it says tar.bz2, but it's really tar.bz2.tar).
    3. Extract that and copy that to a directory on your device. I chose ~/Developer/clang
    4. Open terminal and type
      • sudo nano /etc/paths
    5. Enter the directory in which you keep your clang stuffs. Press 'Ctrl + X' to Exit, and press 'Y' to save.

    You're now done with installation. Quit and restart terminal.

    To use this,
    1. First make sure you go into Xcode and "Clean All" before you do anything.
    2. When that's all set, open terminal and navigate to the directory of the app you want to build.
    3. Enter the following command. Make sure to replace the name od the sdk with the one you currently want to build with.
      • scan-build -k -V xcodebuild -configuration Debug -sdk iphonesimulator3.0



    A couple of things I noticed:
    • If you want to use this on 3.0, you will need to open the correct UIKit.h file and comment out [READCTED] due to an Apple failiure.
    • If you want to retest a file and scan-build isn't testing it, and "Clean All" isn't getting the job done, you will have to open that file and make a small change (like entering a space, or newline--feel free to delete it immediately afterward), and resaving the object.



    Linux Instructions from page three of this thread: http://www.ifans.com/forums/showpost.php?p=1574771&postcount=21


    Again, I STRONGLY recommend all devs use this.
  2. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Looks really useful!
  3. bddckr

    bddckr Active Member

    Joined:
    Dec 2, 2007
    Messages:
    1,434
    Likes Received:
    18
    Device:
    iPhone 4 (Black)
    Thanks Skylar, will be THE tool to use.
  4. masaj

    masaj New Member

    Joined:
    Apr 27, 2009
    Messages:
    13
    Likes Received:
    0
    Is there a similar tool for developers on linux?
  5. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Yeah, I've been using this for a bit now, I saw it on the Stanford CS 193P iPhone Development podcasts on iTunes U. This + Instruments = AMAZING.

    I remember something wrong on 3.0 though, some header error. Maybe it was UILoca... you know the rest, but I don't think I can say it here.

    EDIT: I see that you wrote that above, never mind.
  6. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
  7. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    I see what you did there, and I deserve that. To be fair, are you going to trust a completely third party checker like Lint, which you said couldn't detect the difference between #import and #include, or a tool that was developed in conjunction with its own compiler, Clang (from the LLVM cats). PCC, notwithstanding.

    I still stand by what I said in that will not trust Lint.

    I do, however, not want this thread to get cluttered up over our disagreements, so if you'd like to discuss this further, you can either PM me directly, or start a new thread.
  8. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    I was just kidding anyway, no worries. I don't even use any Lint programs on a day-to-day basis, only when I run into problems.

    Back to the thread.
  9. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    This looks very useful, I installed it and it looks to have worked. I ran it on one of my apps and it said it didn't find any errors, which is a good sign.
  10. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    bamhm183, you need to Clean All from Xcode before you run it. Also, it may not present you with any errors, so you might need to scroll up and read through the output. Alternatively, as Clean All doesn't always work (because Clang doesn't use the same files, you will need t change and resave each file, then run clang again.

Share This Page