How to use the command scan-build (with examples)
The scan-build
command-line utility is used to run a static analyzer over a codebase as part of performing a regular build. It is part of the Clang Static Analyzer, which is a tool that can help detect bugs and issues in source code.
Use case 1: Build and analyze the project in the current directory
Code:
scan-build make
Motivation:
Running the scan-build make
command allows you to perform a regular build of your project while also leveraging the static analyzer to identify any potential issues in your code. This can help catch bugs early, before they cause problems in the running application.
Explanation:
scan-build
: Invokes thescan-build
command-line utility.make
: The build tool command to build the project. This will compile the source code and create the executable.
Example output:
scan-build: Using '/usr/bin/clang' for static analysis
scan-build: Building '/path/to/project' using 'scan-build --use-analyzer=/usr/bin/clang make'
scan-build: 'scan-view /path/to/project/scan-view' directory created
scan-build: Run '/path/to/project/scan-view/index.html' to examine bug reports.
scan-build: Analysis done.
Use case 2: Run a command and pass all subsequent options to it
Code:
scan-build command command_arguments
Motivation:
The scan-build
command allows you to run any command and pass all subsequent options to it. This is useful when you want to analyze a specific command or tool using the static analyzer.
Explanation:
scan-build
: Invokes thescan-build
command-line utility.command
: The command to be executed and analyzed by the static analyzer.command_arguments
: Any arguments or options required by the command.
Example output:
scan-build: Using '/usr/bin/clang' for static analysis
scan-build: Building '/path/to/command' using 'scan-build --use-analyzer=/usr/bin/clang command command_arguments'
scan-build: 'scan-view /path/to/command/scan-view' directory created
scan-build: Run '/path/to/command/scan-view/index.html' to examine bug reports.
scan-build: Analysis done.
Use case 3: Display help
Code:
scan-build
Motivation:
When you are unsure of how to use the scan-build
command-line utility or need to quickly reference the available options, running scan-build
without any arguments will display the help message.
Explanation:
scan-build
: Invokes thescan-build
command-line utility with no additional arguments.
Example output:
Usage: scan-build [options] [command]
Options:
-h, --help Show help message and exit.
-o OUTPUT, --output=OUTPUT
Store analyzer reports in the specified directory.
-plist-absolute-paths
Make generated paths absolute.
-plist-html Generate HTML version of the plist.
-plist-files=<GLOB> Include only files that match the given glob pattern.
-plist Save analyzer reports as plist files.
--keep-empty Keep empty analyzer reports.
-v, --verbose Print verbose diagnostics.
--status-bugs Treat non-zero analyzer status as bugs.
--status-features Treat non-zero analyzer status as features.
--status-todo Treat non-zero analyzer status as to-dos.
--mode=MODE Specify a build mode ('debug' or 'release').
--use-analyzer=ANALYZER
Use the specified analyzer instead of the one in
$PATH.
...
Conclusion:
The scan-build
command-line utility is a powerful tool for running a static analyzer over a codebase during a regular build. By using the examples provided, you can easily incorporate static analysis into your development process, catching potential issues and improving the overall quality of your code.