xref: /llvm-project/clang/docs/analyzer/user-docs/CommandLineUsage.rst (revision 5e81437f2ba03ee0ab93b26a9654da9b95dab3b0)
1093aaca2SEndre FülöpCommand Line Usage: scan-build and CodeChecker
2093aaca2SEndre Fülöp==============================================
3093aaca2SEndre Fülöp
4093aaca2SEndre FülöpThis document provides guidelines for running the static analyzer from the command line on whole projects.
5*5e81437fSBrnBlrgCodeChecker and scan-build are two CLI tools for using CSA on multiple files (translation units).
6093aaca2SEndre FülöpBoth provide a way of driving the analyzer, detecting compilation flags, and generating reports.
7093aaca2SEndre FülöpCodeChecker is more actively maintained, provides heuristics for working with multiple versions of popular compilers and it also comes with a web-based GUI for viewing, filtering, categorizing and suppressing the results.
8093aaca2SEndre FülöpTherefore CodeChecker is recommended in case you need any of the above features or just more customizability in general.
9093aaca2SEndre Fülöp
10093aaca2SEndre FülöpComparison of CodeChecker and scan-build
11093aaca2SEndre Fülöp----------------------------------------
12093aaca2SEndre Fülöp
13093aaca2SEndre FülöpThe static analyzer is by design a GUI tool originally intended to be consumed by the XCode IDE.
14093aaca2SEndre FülöpIts purpose is to find buggy execution paths in the program, and such paths are very hard to comprehend by looking at a non-interactive standard output.
15093aaca2SEndre FülöpIt is possible, however, to invoke the static analyzer from the command line in order to obtain analysis results, and then later view them interactively in a graphical interface.
16093aaca2SEndre FülöpThe following tools are used commonly to run the analyzer from the command line.
17093aaca2SEndre FülöpBoth tools are wrapper scripts to drive the analysis and the underlying invocations of the Clang compiler:
18093aaca2SEndre Fülöp
19093aaca2SEndre Fülöp1. scan-build_ is an old and simple command line tool that emits static analyzer warnings as HTML files while compiling your project. You can view the analysis results in your web browser.
20093aaca2SEndre Fülöp    - Useful for individual developers who simply want to view static analysis results at their desk, or in a very simple collaborative environment.
21093aaca2SEndre Fülöp    - Works on all major platforms (Windows, Linux, macOS) and is available as a package in many Linux distributions.
22093aaca2SEndre Fülöp    - Does not include support for cross-translation-unit analysis.
23093aaca2SEndre Fülöp
24093aaca2SEndre Fülöp2. CodeChecker_ is a driver and web server that runs the static analyzer on your projects on demand and maintains a database of issues.
25093aaca2SEndre Fülöp    - Perfect for managing large amounts of thee static analyzer warnings in a collaborative environment.
26093aaca2SEndre Fülöp    - Generally much more feature-rich than scan-build.
27093aaca2SEndre Fülöp    - Supports incremental analysis: Results can be stored in a database, subsequent analysis runs can be compared to list the newly added defects.
28093aaca2SEndre Fülöp    - :doc:`CrossTranslationUnit` is supported fully on Linux via CodeChecker.
29093aaca2SEndre Fülöp    - Can run clang-tidy checkers too.
30093aaca2SEndre Fülöp    - Open source, but out-of-tree, i.e. not part of the LLVM project.
31093aaca2SEndre Fülöp
32093aaca2SEndre Fülöpscan-build
33093aaca2SEndre Fülöp----------
34093aaca2SEndre Fülöp
35093aaca2SEndre Fülöp**scan-build** is a command line utility that enables a user to run the static analyzer over their codebase as part of performing a regular build (from the command line).
36093aaca2SEndre Fülöp
37093aaca2SEndre FülöpHow does it work?
38093aaca2SEndre Fülöp~~~~~~~~~~~~~~~~~
39093aaca2SEndre Fülöp
40093aaca2SEndre FülöpDuring a project build, as source files are compiled they are also analyzed in tandem by the static analyzer.
41093aaca2SEndre Fülöp
42093aaca2SEndre FülöpUpon completion of the build, results are then presented to the user within a web browser.
43093aaca2SEndre Fülöp
44093aaca2SEndre FülöpWill it work with any build system?
45093aaca2SEndre Fülöp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46093aaca2SEndre Fülöp
47093aaca2SEndre Fülöp**scan-build** has little or no knowledge about how you build your code. It works by overriding the ``CC`` and ``CXX`` environment variables to (hopefully) change your build to use a "fake" compiler instead of the one that would normally build your project. This fake compiler executes either ``clang`` or ``gcc`` (depending on the platform) to compile your code and then executes the static analyzer to analyze your code.
48093aaca2SEndre Fülöp
49093aaca2SEndre FülöpThis "poor man's interposition" works amazingly well in many cases and falls down in others. Please consult the information on this page on making the best use of **scan-build**, which includes getting it to work when the aforementioned hack fails to work.
50093aaca2SEndre Fülöp
51093aaca2SEndre Fülöp.. image:: ../images/scan_build_cmd.png
52093aaca2SEndre Fülöp
53093aaca2SEndre Fülöp.. image:: ../images/analyzer_html.png
54093aaca2SEndre Fülöp
55093aaca2SEndre Fülöp**Viewing static analyzer results in a web browser**
56093aaca2SEndre Fülöp
57093aaca2SEndre FülöpBasic Usage
58093aaca2SEndre Fülöp~~~~~~~~~~~
59093aaca2SEndre Fülöp
60093aaca2SEndre FülöpBasic usage of ``scan-build`` is designed to be simple: just place the word "scan-build" in front of your build command::
61093aaca2SEndre Fülöp
62093aaca2SEndre Fülöp  $ scan-build make
63093aaca2SEndre Fülöp  $ scan-build xcodebuild
64093aaca2SEndre Fülöp
65093aaca2SEndre FülöpIn the first case ``scan-build`` analyzes the code of a project built with ``make`` and in the second case ``scan-build`` analyzes a project built using ``xcodebuild``.
66093aaca2SEndre Fülöp
67093aaca2SEndre FülöpHere is the general format for invoking ``scan-build``::
68093aaca2SEndre Fülöp
69093aaca2SEndre Fülöp  $ scan-build [scan-build options] <command> [command options]
70093aaca2SEndre Fülöp
71093aaca2SEndre FülöpOperationally, ``scan-build`` literally runs <command> with all of the subsequent options passed to it. For example, one can pass ``-j4`` to ``make`` get a parallel build over 4 cores::
72093aaca2SEndre Fülöp
73093aaca2SEndre Fülöp  $ scan-build make -j4
74093aaca2SEndre Fülöp
75093aaca2SEndre FülöpIn almost all cases, ``scan-build`` makes no effort to interpret the options after the build command; it simply passes them through. In general, ``scan-build`` should support parallel builds, but **not distributed builds**.
76093aaca2SEndre Fülöp
77093aaca2SEndre FülöpIt is also possible to use ``scan-build`` to analyze specific files::
78093aaca2SEndre Fülöp
79093aaca2SEndre Fülöp  $ scan-build gcc -c t1.c t2.c
80093aaca2SEndre Fülöp
81093aaca2SEndre FülöpThis example causes the files ``t1.c`` and ``t2.c`` to be analyzed.
82093aaca2SEndre Fülöp
83093aaca2SEndre FülöpFor Windows Users
84093aaca2SEndre Fülöp~~~~~~~~~~~~~~~~~
85093aaca2SEndre Fülöp
86093aaca2SEndre FülöpWindows users must have Perl installed to use scan-build.
87093aaca2SEndre Fülöp
88093aaca2SEndre Fülöp``scan-build.bat`` script allows you to launch scan-build in the same way as it described in the Basic Usage section above. To invoke scan-build from an arbitrary location, add the path to the folder containing scan-build.bat to your PATH environment variable.
89093aaca2SEndre Fülöp
90093aaca2SEndre FülöpIf you have unexpected compilation/make problems when running scan-build with MinGW/MSYS the following information may be helpful:
91093aaca2SEndre Fülöp
92093aaca2SEndre Fülöp- If getting unexpected ``"fatal error: no input files"`` while building with MSYS make from the Windows cmd, try one of these solutions:
93093aaca2SEndre Fülöp  - Use MinGW ``mingw32-make`` instead of MSYS ``make`` and exclude the path to MSYS from PATH to prevent ``mingw32-make`` from using MSYS utils. MSYS utils are dependent on the MSYS runtime and they are not intended for being run from the Windows cmd. Specifically, makefile commands with backslashed quotes may be heavily corrupted when passed for execution.
94093aaca2SEndre Fülöp  - Run ``make`` from the sh shell::
95093aaca2SEndre Fülöp
96093aaca2SEndre Fülöp      $ scan-build [scan-build options] sh -c "make [make options]"
97093aaca2SEndre Fülöp
98093aaca2SEndre Fülöp- If getting ``"Error : *** target pattern contains no `%'"`` while using GNU Make 3.81, try to use another version of make.
99093aaca2SEndre Fülöp
100093aaca2SEndre FülöpOther Options
101093aaca2SEndre Fülöp~~~~~~~~~~~~~
102093aaca2SEndre Fülöp
103093aaca2SEndre FülöpAs mentioned above, extra options can be passed to ``scan-build``. These options prefix the build command. For example::
104093aaca2SEndre Fülöp
105093aaca2SEndre Fülöp  $ scan-build -k -V make
106093aaca2SEndre Fülöp  $ scan-build -k -V xcodebuild
107093aaca2SEndre Fülöp
108093aaca2SEndre FülöpHere is a subset of useful options:
109093aaca2SEndre Fülöp
110093aaca2SEndre Fülöp- **-o**: Target directory for HTML report files. Subdirectories will be created as needed to represent separate "runs" of the analyzer. If this option is not specified, a directory is created in ``/tmp`` to store the reports.
111093aaca2SEndre Fülöp- **-h** *(or no arguments)*: Display all ``scan-build`` options.
112093aaca2SEndre Fülöp- **-k**, **--keep-going**: Add a "keep on going" option to the specified build command. This option currently supports ``make`` and ``xcodebuild``. This is a convenience option; one can specify this behavior directly using build options.
113093aaca2SEndre Fülöp- **-v**: Verbose output from scan-build and the analyzer. **A second and third "-v" increases verbosity**, and is useful for filing bug reports against the analyzer.
114093aaca2SEndre Fülöp- **-V**: View analysis results in a web browser when the build command completes.
115093aaca2SEndre Fülöp- **--use-analyzer Xcode** *(or)* **--use-analyzer [path to clang]**: ``scan-build`` uses the 'clang' executable relative to itself for static analysis. One can override this behavior with this option by using the 'clang' packaged with Xcode (on OS X) or from the PATH.
116093aaca2SEndre Fülöp
117093aaca2SEndre FülöpA complete list of options can be obtained by running ``scan-build`` with no arguments.
118093aaca2SEndre Fülöp
119093aaca2SEndre FülöpOutput of scan-build
120093aaca2SEndre Fülöp~~~~~~~~~~~~~~~~~~~~
121093aaca2SEndre Fülöp
122093aaca2SEndre FülöpThe output of scan-build is a set of HTML files, each one which represents a separate bug report. A single ``index.html`` file is generated for surveying all of the bugs. You can then just open ``index.html`` in a web browser to view the bug reports.
123093aaca2SEndre Fülöp
124093aaca2SEndre FülöpWhere the HTML files are generated is specified with a **-o** option to ``scan-build``. If **-o** isn't specified, a directory in ``/tmp`` is created to store the files (``scan-build`` will print a message telling you where they are). If you want to view the reports immediately after the build completes, pass **-V** to ``scan-build``.
125093aaca2SEndre Fülöp
126093aaca2SEndre FülöpRecommended Usage Guidelines
127093aaca2SEndre Fülöp~~~~~~~~~~~~~~~~~~~~~~~~~~~~
128093aaca2SEndre Fülöp
129093aaca2SEndre FülöpThis section describes a few recommendations with running the analyzer.
130093aaca2SEndre Fülöp
131093aaca2SEndre FülöpAlways Analyze a Project in its "Debug" Configuration
132093aaca2SEndre Fülöp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
133093aaca2SEndre Fülöp
134093aaca2SEndre FülöpMost projects can be built in a "debug" mode that enables assertions. Assertions are picked up by the static analyzer to prune infeasible paths, which in some cases can greatly reduce the number of false positives (bogus error reports) emitted by the tool.
135093aaca2SEndre Fülöp
136093aaca2SEndre FülöpAnother option is to use ``--force-analyze-debug-code`` flag of **scan-build** tool which would enable assertions automatically.
137093aaca2SEndre Fülöp
138093aaca2SEndre FülöpUse Verbose Output when Debugging scan-build
139093aaca2SEndre Fülöp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
140093aaca2SEndre Fülöp
141093aaca2SEndre Fülöp``scan-build`` takes a **-v** option to emit verbose output about what it's doing; two **-v** options emit more information. Redirecting the output of ``scan-build`` to a text file (make sure to redirect standard error) is useful for filing bug reports against ``scan-build`` or the analyzer, as we can see the exact options (and files) passed to the analyzer. For more comprehensible logs, don't perform a parallel build.
142093aaca2SEndre Fülöp
143093aaca2SEndre FülöpRun './configure' through scan-build
144093aaca2SEndre Fülöp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
145093aaca2SEndre Fülöp
146093aaca2SEndre FülöpIf an analyzed project uses an autoconf generated ``configure`` script, you will probably need to run ``configure`` script through ``scan-build`` in order to analyze the project.
147093aaca2SEndre Fülöp
148093aaca2SEndre Fülöp**Example**::
149093aaca2SEndre Fülöp
150093aaca2SEndre Fülöp  $ scan-build ./configure
151093aaca2SEndre Fülöp  $ scan-build --keep-cc make
152093aaca2SEndre Fülöp
153093aaca2SEndre FülöpThe reason ``configure`` also needs to be run through ``scan-build`` is because ``scan-build`` scans your source files by *interposing* on the compiler. This interposition is currently done by ``scan-build`` temporarily setting the environment variable ``CC`` to ``ccc-analyzer``. The program ``ccc-analyzer`` acts like a fake compiler, forwarding its command line arguments over to the compiler to perform regular compilation and ``clang`` to perform static analysis.
154093aaca2SEndre Fülöp
155093aaca2SEndre FülöpRunning ``configure`` typically generates makefiles that have hardwired paths to the compiler, and by running ``configure`` through ``scan-build`` that path is set to ``ccc-analyzer``.
156093aaca2SEndre Fülöp
157093aaca2SEndre FülöpAnalyzing iPhone Projects
158093aaca2SEndre Fülöp~~~~~~~~~~~~~~~~~~~~~~~~~
159093aaca2SEndre Fülöp
160093aaca2SEndre FülöpConceptually Xcode projects for iPhone applications are nearly the same as their cousins for desktop applications. **scan-build** can analyze these projects as well, but users often encounter problems with just building their iPhone projects from the command line because there are a few extra preparative steps they need to take (e.g., setup code signing).
161093aaca2SEndre Fülöp
162093aaca2SEndre FülöpRecommendation: use "Build and Analyze"
163093aaca2SEndre Fülöp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
164093aaca2SEndre Fülöp
165093aaca2SEndre FülöpThe absolute easiest way to analyze iPhone projects is to use the `Analyze feature in Xcode <https://developer.apple.com/library/ios/recipes/xcode_help-source_editor/chapters/Analyze.html#//apple_ref/doc/uid/TP40009975-CH4-SW1>`_ (which is based on the static analyzer). There a user can analyze their project right from a menu without most of the setup described later.
166093aaca2SEndre Fülöp
167093aaca2SEndre Fülöp`Instructions are available <../xcode.html>`_ on this website on how to use open source builds of the analyzer as a replacement for the one bundled with Xcode.
168093aaca2SEndre Fülöp
169093aaca2SEndre FülöpUsing scan-build directly
170093aaca2SEndre Fülöp~~~~~~~~~~~~~~~~~~~~~~~~~
171093aaca2SEndre Fülöp
172093aaca2SEndre FülöpIf you wish to use **scan-build** with your iPhone project, keep the following things in mind:
173093aaca2SEndre Fülöp
174093aaca2SEndre Fülöp- Analyze your project in the ``Debug`` configuration, either by setting this as your configuration with Xcode or by passing ``-configuration Debug`` to ``xcodebuild``.
175093aaca2SEndre Fülöp- Analyze your project using the ``Simulator`` as your base SDK. It is possible to analyze your code when targeting the device, but this is much easier to do when using Xcode's *Build and Analyze* feature.
176093aaca2SEndre Fülöp- Check that your code signing SDK is set to the simulator SDK as well, and make sure this option is set to ``Don't Code Sign``.
177093aaca2SEndre Fülöp
178093aaca2SEndre FülöpNote that you can most of this without actually modifying your project. For example, if your application targets iPhoneOS 2.2, you could run **scan-build** in the following manner from the command line::
179093aaca2SEndre Fülöp
180093aaca2SEndre Fülöp  $ scan-build xcodebuild -configuration Debug -sdk iphonesimulator2.2
181093aaca2SEndre Fülöp
182093aaca2SEndre FülöpAlternatively, if your application targets iPhoneOS 3.0::
183093aaca2SEndre Fülöp
184093aaca2SEndre Fülöp  $ scan-build xcodebuild -configuration Debug -sdk iphonesimulator3.0
185093aaca2SEndre Fülöp
186093aaca2SEndre FülöpGotcha: using the right compiler
187093aaca2SEndre Fülöp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
188093aaca2SEndre Fülöp
189093aaca2SEndre FülöpRecall that **scan-build** analyzes your project by using a compiler to compile the project and ``clang`` to analyze your project. The script uses simple heuristics to determine which compiler should be used (it defaults to ``clang`` on Darwin and ``gcc`` on other platforms). When analyzing iPhone projects, **scan-build** may pick the wrong compiler than the one Xcode would use to build your project. For example, this could be because multiple versions of a compiler may be installed on your system, especially if you are developing for the iPhone.
190093aaca2SEndre Fülöp
191093aaca2SEndre FülöpWhen compiling your application to run on the simulator, it is important that **scan-build** finds the correct version of ``gcc/clang``. Otherwise, you may see strange build errors that only happen when you run ``scan-build``.
192093aaca2SEndre Fülöp
193093aaca2SEndre Fülöp**scan-build** provides the ``--use-cc`` and ``--use-c++`` options to hardwire which compiler scan-build should use for building your code. Note that although you are chiefly interested in analyzing your project, keep in mind that running the analyzer is intimately tied to the build, and not being able to compile your code means it won't get fully analyzed (if at all).
194093aaca2SEndre Fülöp
195093aaca2SEndre FülöpIf you aren't certain which compiler Xcode uses to build your project, try just running ``xcodebuild`` (without **scan-build**). You should see the full path to the compiler that Xcode is using, and use that as an argument to ``--use-cc``.
196093aaca2SEndre Fülöp
197093aaca2SEndre FülöpCodeChecker
198093aaca2SEndre Fülöp-----------
199093aaca2SEndre Fülöp
200093aaca2SEndre FülöpBasic Usage
201093aaca2SEndre Fülöp~~~~~~~~~~~
202093aaca2SEndre Fülöp
203093aaca2SEndre FülöpInstall CodeChecker as described here: `CodeChecker Install Guide <https://github.com/Ericsson/codechecker/#Install-guide>`_.
204093aaca2SEndre Fülöp
205093aaca2SEndre FülöpCreate a compilation database. If you use cmake then pass the ``-DCMAKE_EXPORT_COMPILE_COMMANDS=1`` parameter to cmake. Cmake will create a ``compile_commands.json`` file.
206093aaca2SEndre FülöpIf you have a Makefile based or similar build system then you can log the build commands with the help of CodeChecker::
207093aaca2SEndre Fülöp
208093aaca2SEndre Fülöp    make clean
209093aaca2SEndre Fülöp    CodeChecker log -b "make" -o compile_commands.json
210093aaca2SEndre Fülöp
211093aaca2SEndre FülöpAnalyze your project::
212093aaca2SEndre Fülöp
213093aaca2SEndre Fülöp    CodeChecker analyze compile_commands.json -o ./reports
214093aaca2SEndre Fülöp
215093aaca2SEndre FülöpView the analysis results.
216093aaca2SEndre FülöpPrint the detailed results in the command line::
217093aaca2SEndre Fülöp
218093aaca2SEndre Fülöp    CodeChecker parse --print-steps ./reports
219093aaca2SEndre Fülöp
220093aaca2SEndre FülöpOr view the detailed results in a browser::
221093aaca2SEndre Fülöp
222093aaca2SEndre Fülöp    CodeChecker parse ./reports -e html -o ./reports_html
223093aaca2SEndre Fülöp    firefox ./reports_html/index.html
224093aaca2SEndre Fülöp
225093aaca2SEndre FülöpOptional: store the analysis results in a DB::
226093aaca2SEndre Fülöp
227093aaca2SEndre Fülöp    mkdir ./ws
228093aaca2SEndre Fülöp    CodeChecker server -w ./ws -v 8555 &
229093aaca2SEndre Fülöp    CodeChecker store ./reports --name my-project --url http://localhost:8555/Default
230093aaca2SEndre Fülöp
231093aaca2SEndre FülöpOptional: manage (categorize, suppress) the results in your web browser::
232093aaca2SEndre Fülöp
233093aaca2SEndre Fülöp    firefox http://localhost:8555/Default
234093aaca2SEndre Fülöp
235093aaca2SEndre FülöpDetailed Usage
236093aaca2SEndre Fülöp~~~~~~~~~~~~~~
237093aaca2SEndre Fülöp
238093aaca2SEndre FülöpFor extended documentation please refer to the `official site of CodeChecker <https://github.com/Ericsson/codechecker/blob/master/docs/usage.md>`_!
239093aaca2SEndre Fülöp
240