1*093aaca2SEndre FülöpRunning the analyzer within Xcode 2*093aaca2SEndre Fülöp================================= 3*093aaca2SEndre Fülöp 4*093aaca2SEndre Fülöp.. contents:: 5*093aaca2SEndre Fülöp :local: 6*093aaca2SEndre Fülöp 7*093aaca2SEndre FülöpSince Xcode 3.2, users have been able to run the static analyzer `directly within Xcode <https://developer.apple.com/library/ios/recipes/xcode_help-source_editor/chapters/Analyze.html#//apple_ref/doc/uid/TP40009975-CH4-SW1>`_. 8*093aaca2SEndre Fülöp 9*093aaca2SEndre FülöpIt integrates directly with the Xcode build system and presents analysis results directly within Xcode's editor. 10*093aaca2SEndre Fülöp 11*093aaca2SEndre FülöpCan I use the open source analyzer builds with Xcode? 12*093aaca2SEndre Fülöp----------------------------------------------------- 13*093aaca2SEndre Fülöp 14*093aaca2SEndre Fülöp**Yes**. Instructions are included below. 15*093aaca2SEndre Fülöp 16*093aaca2SEndre Fülöp.. image:: ../images/analyzer_xcode.png 17*093aaca2SEndre Fülöp 18*093aaca2SEndre Fülöp**Viewing static analyzer results in Xcode** 19*093aaca2SEndre Fülöp 20*093aaca2SEndre FülöpKey features: 21*093aaca2SEndre Fülöp------------- 22*093aaca2SEndre Fülöp 23*093aaca2SEndre Fülöp- **Integrated workflow:** Results are integrated within Xcode. There is no experience of using a separate tool, and activating the analyzer requires a single keystroke or mouse click. 24*093aaca2SEndre Fülöp- **Transparency:** Works effortlessly with Xcode projects (including iPhone projects). 25*093aaca2SEndre Fülöp- **Cons:** Doesn't work well with non-Xcode projects. For those, consider :doc:`CommandLineUsage`. 26*093aaca2SEndre Fülöp 27*093aaca2SEndre FülöpGetting Started 28*093aaca2SEndre Fülöp--------------- 29*093aaca2SEndre Fülöp 30*093aaca2SEndre FülöpXcode is available as a free download from Apple on the `Mac App Store <https://itunes.apple.com/us/app/xcode/id497799835?mt=12>`_, with `instructions available <https://developer.apple.com/library/ios/recipes/xcode_help-source_editor/chapters/Analyze.html#//apple_ref/doc/uid/TP40009975-CH4-SW1>`_ for using the analyzer. 31*093aaca2SEndre Fülöp 32*093aaca2SEndre FülöpUsing open source analyzer builds with Xcode 33*093aaca2SEndre Fülöp-------------------------------------------- 34*093aaca2SEndre Fülöp 35*093aaca2SEndre FülöpBy default, Xcode uses the version of ``clang`` that came bundled with it to analyze your code. It is possible to change Xcode's behavior to use an alternate version of ``clang`` for this purpose while continuing to use the ``clang`` that came with Xcode for compiling projects. 36*093aaca2SEndre Fülöp 37*093aaca2SEndre FülöpWhy try open source builds? 38*093aaca2SEndre Fülöp---------------------------- 39*093aaca2SEndre Fülöp 40*093aaca2SEndre FülöpThe advantage of using open source analyzer builds (provided on this website) is that they are often newer than the analyzer provided with Xcode, and thus can contain bug fixes, new checks, or simply better analysis. 41*093aaca2SEndre Fülöp 42*093aaca2SEndre FülöpOn the other hand, new checks can be experimental, with results of variable quality. Users are encouraged to file bug reports (for any version of the analyzer) where they encounter false positives or other issues here: :doc:`FilingBugs`. 43*093aaca2SEndre Fülöp 44*093aaca2SEndre Fülöpset-xcode-analyzer 45*093aaca2SEndre Fülöp------------------ 46*093aaca2SEndre Fülöp 47*093aaca2SEndre FülöpStarting with analyzer build checker-234, analyzer builds contain a command line utility called ``set-xcode-analyzer`` that allows users to change what copy of ``clang`` that Xcode uses for analysis:: 48*093aaca2SEndre Fülöp 49*093aaca2SEndre Fülöp $ set-xcode-analyzer -h 50*093aaca2SEndre Fülöp Usage: set-xcode-analyzer [options] 51*093aaca2SEndre Fülöp 52*093aaca2SEndre Fülöp Options: 53*093aaca2SEndre Fülöp -h, --help show this help message and exit 54*093aaca2SEndre Fülöp --use-checker-build=PATH 55*093aaca2SEndre Fülöp Use the Clang located at the provided absolute path, 56*093aaca2SEndre Fülöp e.g. /Users/foo/checker-1 57*093aaca2SEndre Fülöp --use-xcode-clang Use the Clang bundled with Xcode 58*093aaca2SEndre Fülöp 59*093aaca2SEndre FülöpOperationally, **set-xcode-analyzer** edits Xcode's configuration files to point it to use the version of ``clang`` you specify for static analysis. Within this model it provides you two basic modes: 60*093aaca2SEndre Fülöp 61*093aaca2SEndre Fülöp- **--use-xcode-clang:** Switch Xcode (back) to using the ``clang`` that came bundled with it for static analysis. 62*093aaca2SEndre Fülöp- **--use-checker-build:** Switch Xcode to using the ``clang`` provided by the specified analyzer build. 63*093aaca2SEndre Fülöp 64*093aaca2SEndre FülöpThings to keep in mind 65*093aaca2SEndre Fülöp---------------------- 66*093aaca2SEndre Fülöp 67*093aaca2SEndre Fülöp- You should quit Xcode prior to running ``set-xcode-analyzer``. 68*093aaca2SEndre Fülöp- You will need to run ``set-xcode-analyzer`` under **``sudo``** in order to have write privileges to modify the Xcode configuration files. 69*093aaca2SEndre Fülöp 70*093aaca2SEndre FülöpExamples 71*093aaca2SEndre Fülöp-------- 72*093aaca2SEndre Fülöp 73*093aaca2SEndre Fülöp**Example 1**: Telling Xcode to use checker-235:: 74*093aaca2SEndre Fülöp 75*093aaca2SEndre Fülöp $ pwd 76*093aaca2SEndre Fülöp /tmp 77*093aaca2SEndre Fülöp $ tar xjf checker-235.tar.bz2 78*093aaca2SEndre Fülöp $ sudo checker-235/set-xcode-analyzer --use-checker-build=/tmp/checker-235 79*093aaca2SEndre Fülöp 80*093aaca2SEndre FülöpNote that you typically won't install an analyzer build in ``/tmp``, but the point of this example is that ``set-xcode-analyzer`` just wants a full path to an untarred analyzer build. 81*093aaca2SEndre Fülöp 82*093aaca2SEndre Fülöp**Example 2**: Telling Xcode to use a very specific version of ``clang``:: 83*093aaca2SEndre Fülöp 84*093aaca2SEndre Fülöp $ sudo set-xcode-analyzer --use-checker-build=~/mycrazyclangbuild/bin/clang 85*093aaca2SEndre Fülöp 86*093aaca2SEndre Fülöp**Example 3**: Resetting Xcode to its default behavior:: 87*093aaca2SEndre Fülöp 88*093aaca2SEndre Fülöp $ sudo set-xcode-analyzer --use-xcode-clang 89