UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ClangPlatformCodeAnalysis.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2// IWYU pragma: begin_exports
3
4#pragma once
5
6// HEADER_UNIT_UNSUPPORTED - Clang not supporting header units
7
8// Guard to prevent non-unity incremental checks using MSVC to try to compile this header.
9#if defined(__clang__)
10
11// Code analysis features
12#if defined( __clang_analyzer__ ) || defined( PVS_STUDIO )
13 #define USING_CODE_ANALYSIS 1
14#else
15 #define USING_CODE_ANALYSIS 0
16#endif
17
18//
19// NOTE: To suppress a single occurrence of a code analysis warning:
20//
21// CA_SUPPRESS( <WarningNumber> )
22// ...code that triggers warning...
23//
24
25//
26// NOTE: To disable all code analysis warnings for a section of code (such as include statements
27// for a third party library), you can use the following:
28//
29// #if USING_CODE_ANALYSIS
30// MSVC_PRAGMA( warning( push ) )
31// MSVC_PRAGMA( warning( disable : ALL_CODE_ANALYSIS_WARNINGS ) )
32// #endif // USING_CODE_ANALYSIS
33//
34// <code with warnings>
35//
36// #if USING_CODE_ANALYSIS
37// MSVC_PRAGMA( warning( pop ) )
38// #endif // USING_CODE_ANALYSIS
39//
40
41#if USING_CODE_ANALYSIS
42
43 // If enabled, add in settings for PVS-Studio Analysis
45
46 // A fake function marked with noreturn that acts as a marker for CA_ASSUME to ensure the
47 // static analyzer doesn't take an analysis path that is assumed not to be navigable.
49
50 // Input argument
51 // Example: void SetValue( CA_IN bool bReadable );
52 #define CA_IN
53
54 // Output argument
55 // Example: void FillValue( CA_OUT bool& bWriteable );
56 #define CA_OUT
57
58 // Specifies that a function parameter may only be read from, never written.
59 // NOTE: CA_READ_ONLY is inferred automatically if your parameter is has a const qualifier.
60 // Example: void SetValue( CA_READ_ONLY bool bReadable );
61 #define CA_READ_ONLY
62
63 // Specifies that a function parameter may only be written to, never read.
64 // Example: void FillValue( CA_WRITE_ONLY bool& bWriteable );
65 #define CA_WRITE_ONLY
66
67 // Incoming pointer parameter must not be NULL and must point to a valid location in memory.
68 // Place before a function parameter's type name.
69 // Example: void SetPointer( CA_VALID_POINTER void* Pointer );
70 #define CA_VALID_POINTER
71
72 // Caller must check the return value. Place before the return value in a function declaration.
73 // Example: CA_CHECK_RETVAL int32 GetNumber();
74 #define CA_CHECK_RETVAL
75
76 // Function is expected to never return
77 #define CA_NO_RETURN __attribute__((analyzer_noreturn))
78
79 // Suppresses a warning for a single occurrence. Should be used only for code analysis warnings on Windows platform!
80 #define CA_SUPPRESS( WarningNumber )
81
82 // Tells the code analysis engine to assume the statement to be true. Useful for suppressing false positive warnings.
83 #define CA_ASSUME( Expr ) (__builtin_expect(!bool(Expr), 0) ? CA_AssumeNoReturn() : (void)0)
84
85 // Does a simple 'if (Condition)', but disables warnings about using constants in the condition. Helps with some macro expansions.
86 #define CA_CONSTANT_IF(Condition) if (Condition)
87
88
89 //
90 // Disable some code analysis warnings that we are NEVER interested in
91 //
92
93 // NOTE: Please be conservative about adding new suppressions here! If you add a suppression, please
94 // add a comment that explains the rationale.
95
96#endif
97
98#if defined(__has_feature) && __has_feature(address_sanitizer)
99 #define USING_ADDRESS_SANITISER 1
100#else
101 #define USING_ADDRESS_SANITISER 0
102#endif
103
104#if defined(__has_feature) && __has_feature(hwaddress_sanitizer)
105 #define USING_HW_ADDRESS_SANITISER 1
106#else
107 #define USING_HW_ADDRESS_SANITISER 0
108#endif
109
110#if defined(__has_feature) && __has_feature(thread_sanitizer)
111 #define USING_THREAD_SANITISER 1
112#else
113 #define USING_THREAD_SANITISER 0
114#endif
115
116// Clang does not expose __has_feature(undefined_behavior_sanitizer) so this define comes directly from UBT.
117#ifndef USING_UNDEFINED_BEHAVIOR_SANITISER
118 #define USING_UNDEFINED_BEHAVIOR_SANITISER 0
119#endif
120
121#ifndef USING_INSTRUMENTATION
122 #define USING_INSTRUMENTATION 0
123#endif
124
125#if USING_INSTRUMENTATION
126 // For instrumentation purpose, it is important that we do not skip any memory ops
127 // even if marked as TSAN_SAFE.
128 #define TSAN_SAFE
129#endif
130
131// We do want all the additional annotations for the instrumentation too.
132#if USING_THREAD_SANITISER || USING_INSTRUMENTATION
133
134 // This might already have been defined in the instrumentation block above
135#ifndef TSAN_SAFE
136 // Function attribute to disable thread sanitiser validation on specific functions that assume non-atomic load/stores are implicitly atomic
137 // This is only safe for int32/uint32/int64/uint64/uintptr_t/intptr_t/void* types on x86/x64 strongly-consistent memory systems.
138 #define TSAN_SAFE __attribute__((no_sanitize("thread")))
139#endif
140
141 // Thread-sanitiser annotation functions.
142 #ifdef __cplusplus
143 extern "C" {
144 #endif
145 void AnnotateHappensBefore(const char *f, int l, void *addr);
146 void AnnotateHappensAfter(const char *f, int l, void *addr);
147 #ifdef __cplusplus
148 }
149 #endif
150
151 // Annotate that previous load/stores occur before addr
152 #define TSAN_BEFORE(addr) AnnotateHappensBefore(__FILE__, __LINE__, (void*)(addr))
153
154 // Annotate that previous load/stores occur after addr
155 #define TSAN_AFTER(addr) AnnotateHappensAfter(__FILE__, __LINE__, (void*)(addr))
156
157 // Because annotating the global bools is tiresome...
158 #ifdef __cplusplus
159 #include <atomic>
160
161 // We need something that is defaulting to relaxed to avoid introducing additional barrier
162 // that cause issues to go unnoticed.
163 template <typename T>
164 class TTSANSafeValue
165 {
166 std::atomic<T> Value;
167
168 public:
171 __attribute__((always_inline)) operator T() const { return Value.load(std::memory_order_relaxed); }
172 __attribute__((always_inline)) void operator=(T InValue) { Value.store(InValue, std::memory_order_relaxed); }
173 __attribute__((always_inline)) T operator++() { return Value.fetch_add(1, std::memory_order_relaxed) + 1; }
174 __attribute__((always_inline)) T operator++(int) { return Value.fetch_add(1, std::memory_order_relaxed); }
175 __attribute__((always_inline)) T operator--() { return Value.fetch_sub(1, std::memory_order_relaxed) - 1; }
176 __attribute__((always_inline)) T operator--(int) { return Value.fetch_sub(1, std::memory_order_relaxed); }
177
178 __attribute__((always_inline)) T operator->() { return Value.load(std::memory_order_relaxed); }
179
181 {
182 Value.store((T)Other, std::memory_order_relaxed);
183 }
184
186 {
187 Value.store((T)Other, std::memory_order_relaxed);
188 return *this;
189 }
190
191 template <typename OtherType>
192 __attribute__((always_inline)) T operator+=(OtherType InValue) { return Value.fetch_add(InValue, std::memory_order_relaxed) + InValue; }
193
194 template <typename OtherType>
195 __attribute__((always_inline)) T operator-=(OtherType InValue) { return Value.fetch_sub(InValue, std::memory_order_relaxed) - InValue; }
196 };
197
198 #define TSAN_ATOMIC(Type) TTSANSafeValue<Type>
199 #endif
200
201#endif
202
203#endif // __clang__
204
205// IWYU pragma: end_exports
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
float swift_float2 __attribute__((__ext_vector_type__(2)))
Definition MarketplaceKitWrapper.h:67
FStringBuilderBase & operator+=(FStringBuilderBase &Builder, ANSICHAR Char)
Definition StringBuilder.h:582