Static analysis examines your C++ source code for defects — buffer overruns, null pointer dereferences, uninitialized memory, and resource leaks — at build time, before a line of code executes on a customer’s machine. Visual Studio ships a fully integrated static analysis engine that requires no external tools: enable it with a single compiler flag, annotate your APIs with SAL macros, and optionally layer on the C++ Core Guidelines checker for modern C++ safety rules.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/MicrosoftDocs/cpp-docs/llms.txt
Use this file to discover all available pages before exploring further.
The /analyze Compiler Flag
The /analyze flag activates MSVC’s built-in code analysis engine. It runs as part of compilation and emits warnings to the same Error List window as ordinary compiler diagnostics.
- Command Line
- MSBuild / Visual Studio
- CMake
/analyze:log output.sarif to emit results in SARIF format for CI/CD consumption (Visual Studio 2022 and later).Microsoft recommends enabling at least the Native Recommended rule set throughout your project’s entire development timeline — not just for release builds. The earlier a defect is caught, the cheaper it is to fix.
Common Warnings Caught by /analyze
The code analysis engine reports C6xxx and C28xxx warning codes. Some of the most impactful:
| Warning | Category | Description |
|---|---|---|
C6001 | Uninitialized memory | Using uninitialized memory |
C6011 | Null dereference | Dereferencing a potentially NULL pointer |
C6053 | Buffer overrun | strncpy / wcsncpy not null-terminated |
C6200 | Index out of range | Array index exceeds buffer size |
C6386 | Buffer overrun | Write beyond buffer end |
C6387 | Invalid parameter | Parameter could be 0 / NULL when not expected |
C28182 | Null dereference | Pointer is NULL in the same path as a dereference |
SAL Annotations
SAL (Source Annotation Language) is a set of macros that describe the preconditions and postconditions of function parameters and return values. The code analysis engine uses these annotations to find more bugs, more accurately, with fewer false positives.Core SAL Macros
Annotating a Real Function
Null-Pointer Annotations
Annotation Quick Reference
| Annotation | Meaning |
|---|---|
_In_ | Read-only parameter, must not be NULL |
_In_opt_ | Read-only parameter, may be NULL |
_In_z_ | Null-terminated string input |
_Out_ | Output parameter, caller-allocated |
_Out_opt_ | Optional output, may be NULL |
_Inout_ | Read and modified by the function |
_Out_writes_(n) | Writable buffer of n elements |
_In_reads_(n) | Readable buffer of n elements |
_Ret_maybenull_ | Return value may be NULL |
_Ret_notnull_ | Return value is guaranteed non-NULL |
_Outptr_ | Callee allocates; output via pointer-to-pointer |
C++ Core Guidelines Checker
The C++ Core Guidelines Checker is an extension of the/analyze engine that enforces the C++ Core Guidelines authored by Bjarne Stroustrup and Herb Sutter. It is installed by default in Visual Studio 2017 and later.
Enabling the Core Check Rules
Select rule sets
From the Active Rules dropdown, choose Choose multiple rule sets. In the dialog, add one or more of:
- C++ Core Check Bounds Rules
- C++ Core Check Type Rules
- C++ Core Check Lifetime Rules
- C++ Core Check Guidelines (full set)
Example: Core Check Warnings
| Code | Rule | Description |
|---|---|---|
C26400 | I.11 | Do not assign result of new to raw pointer |
C26401 | I.11 | Do not delete a raw pointer not owned by this scope |
C26409 | R.11 | Avoid explicit new / delete; use make_unique |
C26429 | F.23 | Use not_null<T> to indicate when NULL is never valid |
C26481 | Bounds.1 | Use span instead of pointer arithmetic |
C26485 | Bounds.3 | No array-to-pointer decay |
C26494 | Type.5 | Always initialize variables |
Suppressing Warnings
Use#pragma warning to suppress specific analysis warnings on a per-file or per-line basis:
Configuring Analysis with .editorconfig
Visual Studio respects .editorconfig for per-directory linting configuration. While most static analysis settings live in project properties, you can use .editorconfig to enforce style-level rules that complement the analyzer:
.ruleset file in your project and reference it from the Active Rules project property:
Integrating Analysis in CI/CD
Run/analyze in your CI pipeline to catch regressions before they merge:
/analyze Reference
Full compiler flag documentation for code analysis options and rule set configuration.
SAL Annotations Guide
Complete SAL macro reference with examples for annotating structs, locking behaviors, and return values.
C++ Core Guidelines
The upstream guidelines document maintained by the Standard C++ Foundation at isocpp.github.io.
BinSkim User Guide
Binary-level security checker that validates PE/ELF hardening flags in compiled outputs.