UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MicrosoftPlatformCodeAnalysis.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5
6// Code analysis features
7#if defined( _PREFAST_ ) || defined( PVS_STUDIO ) || defined(__clang_analyzer__)
8 #define USING_CODE_ANALYSIS 1
9#else
10 #define USING_CODE_ANALYSIS 0
11#endif
12
13//
14// NOTE: To suppress a single occurrence of a code analysis warning:
15//
16// CA_SUPPRESS( <WarningNumber> )
17// ...code that triggers warning...
18//
19
20//
21// NOTE: To disable all code analysis warnings for a section of code (such as include statements
22// for a third party library), you can use the following:
23//
24// #if USING_CODE_ANALYSIS
25// MSVC_PRAGMA( warning( push ) )
26// MSVC_PRAGMA( warning( disable : ALL_CODE_ANALYSIS_WARNINGS ) )
27// #endif // USING_CODE_ANALYSIS
28//
29// <code with warnings>
30//
31// #if USING_CODE_ANALYSIS
32// MSVC_PRAGMA( warning( pop ) )
33// #endif // USING_CODE_ANALYSIS
34//
35
36#if USING_CODE_ANALYSIS
37
38// If enabled, add in settings for PVS-Studio Analysis
40
41#if !defined(__clang_analyzer__)
42
43 // Input argument
44 // Example: void SetValue( CA_IN bool bReadable );
45 #define CA_IN __in
46
47 // Output argument
48 // Example: void FillValue( CA_OUT bool& bWriteable );
49 #define CA_OUT __out
50
51 // Specifies that a function parameter may only be read from, never written.
52 // NOTE: CA_READ_ONLY is inferred automatically if your parameter is has a const qualifier.
53 // Example: void SetValue( CA_READ_ONLY bool bReadable );
54 #define CA_READ_ONLY [Pre(Access=Read)]
55
56 // Specifies that a function parameter may only be written to, never read.
57 // Example: void FillValue( CA_WRITE_ONLY bool& bWriteable );
58 #define CA_WRITE_ONLY [Pre(Access=Write)]
59
60 // Incoming pointer parameter must not be NULL and must point to a valid location in memory.
61 // Place before a function parameter's type name.
62 // Example: void SetPointer( CA_VALID_POINTER void* Pointer );
63 #define CA_VALID_POINTER [Pre(Null=No,Valid=Yes)]
64
65 // Caller must check the return value. Place before the return value in a function declaration.
66 // Example: CA_CHECK_RETVAL int32 GetNumber();
67 #define CA_CHECK_RETVAL [returnvalue:Post(MustCheck=Yes)]
68
69 // Function is expected to never return
70 #define CA_NO_RETURN __declspec(noreturn)
71
72 // Suppresses a warning for a single occurrence. Should be used only for code analysis warnings on Windows platform!
73 #define CA_SUPPRESS( WarningNumber ) __pragma( warning( suppress: WarningNumber ) )
74
75 // Tells the code analysis engine to assume the statement to be true. Useful for suppressing false positive warnings.
76#if defined( PVS_STUDIO )
78 #define CA_ASSUME( Expr ) (__builtin_expect(!bool(Expr), 0) ? CA_AssumeNoReturn() : (void)0)
79#else
80 // NOTE: We use a double operator not here to avoid issues with passing certain class objects directly into __analysis_assume (which may cause a bogus compiler warning)
81 #define CA_ASSUME( Expr ) __analysis_assume( !!( Expr ) )
82#endif
83
84 // Does a simple 'if (Condition)', but disables warnings about using constants in the condition. Helps with some macro expansions.
85 #define CA_CONSTANT_IF(Condition) __pragma(warning(push)) __pragma(warning(disable:6326)) if (Condition) __pragma(warning(pop))
86
87
88 //
89 // Disable some code analysis warnings that we are NEVER interested in
90 //
91
92 // NOTE: Please be conservative about adding new suppressions here! If you add a suppression, please
93 // add a comment that explains the rationale.
94
95 // We don't use exceptions or care to gracefully handle _alloca() failure. Also, we wrap _alloca in an
96 // appAlloca macro (not inline methods) and don't want to suppress at all call sites.
97 #pragma warning(disable : 6255) // warning C6255: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead.
98
99 // This a very common false positive warning (but some cases are not false positives!). Disabling for now so that we
100 // can more quickly see the benefits of static analysis on new code.
101 #pragma warning(disable : 6102) // warning C6102: Using 'variable' from failed function call at line 'line'.
102
103 // We use this exception handler in Windows code, it may be worth a closer look, but disabling for now
104 // so we can get the benefits of analysis on cross-platform code sooner.
105 #pragma warning(disable : 6320) // warning C6320: Exception-filter expression is the constant EXCEPTION_EXECUTE_HANDLER. This might mask exceptions that were not intended to be handled.
106
107 // Branching on constants allows us to ensure code paths compile even if they are inactive (eg. if(PLATFORM_FOO){ ... }), and is good practice that should not be discouraged.
108 #pragma warning(disable : 6326) // warning C6326 : Potential comparison of a constant with another constant.
109
110 // Likewise, expressions involving constants can also be valuable to check code paths compile.
111 #pragma warning(disable : 6240) // warning C6240 : (<expression> && <non-zero constant>) always evaluates to the result of <expression>. Did you intend to use the bitwise-and operator?
112
113 // This is a warning that a static array is exactly 365 members long. We get false positives because our code gen can make arrays exactly this size.
114 // EX: 365 properties in a UObject will auto gen a table exactly this long.
115 #pragma warning(disable : 6393) // warning C6393: A lookup table of size 365 is not sufficient to handle leap years.
116
117#else // defined(__clang_analyzer__)
118
119// A fake function marked with noreturn that acts as a marker for CA_ASSUME to ensure the
120// static analyzer doesn't take an analysis path that is assumed not to be navigable.
122
123// Input argument
124// Example: void SetValue( CA_IN bool bReadable );
125#define CA_IN
126
127// Output argument
128// Example: void FillValue( CA_OUT bool& bWriteable );
129#define CA_OUT
130
131// Specifies that a function parameter may only be read from, never written.
132// NOTE: CA_READ_ONLY is inferred automatically if your parameter is has a const qualifier.
133// Example: void SetValue( CA_READ_ONLY bool bReadable );
134#define CA_READ_ONLY
135
136// Specifies that a function parameter may only be written to, never read.
137// Example: void FillValue( CA_WRITE_ONLY bool& bWriteable );
138#define CA_WRITE_ONLY
139
140// Incoming pointer parameter must not be NULL and must point to a valid location in memory.
141// Place before a function parameter's type name.
142// Example: void SetPointer( CA_VALID_POINTER void* Pointer );
143#define CA_VALID_POINTER
144
145// Caller must check the return value. Place before the return value in a function declaration.
146// Example: CA_CHECK_RETVAL int32 GetNumber();
147#define CA_CHECK_RETVAL
148
149// Function is expected to never return
150#define CA_NO_RETURN __declspec(noreturn)
151
152// Suppresses a warning for a single occurrence. Should be used only for code analysis warnings on Windows platform!
153#define CA_SUPPRESS( WarningNumber )
154
155// Tells the code analysis engine to assume the statement to be true. Useful for suppressing false positive warnings.
156#define CA_ASSUME( Expr ) (__builtin_expect(!bool(Expr), 0) ? CA_AssumeNoReturn() : (void)0)
157
158// Does a simple 'if (Condition)', but disables warnings about using constants in the condition. Helps with some macro expansions.
159#define CA_CONSTANT_IF(Condition) if (Condition)
160
161
162#endif
163
164
165#endif
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
__declspec(dllimport) int __stdcall IsDebuggerPresent()