xref: /aosp_15_r20/external/cronet/base/notreached.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_NOTREACHED_H_
6 #define BASE_NOTREACHED_H_
7 
8 #include "base/base_export.h"
9 #include "base/check.h"
10 #include "base/dcheck_is_on.h"
11 #include "base/logging_buildflags.h"
12 
13 // TODO(crbug.com/1520664): Remove once NOTIMPLEMENTED() call sites include
14 // base/notimplemented.h.
15 #include "base/notimplemented.h"
16 
17 namespace logging {
18 
19 // NOTREACHED() annotates should-be unreachable code. When a base::NotFatalUntil
20 // milestone is provided the instance is non-fatal (dumps without crashing)
21 // until that milestone is hit. That is: `NOTREACHED(base::NotFatalUntil::M120)`
22 // starts crashing in M120. See base/check.h.
23 //
24 // Under the kNotReachedIsFatal experiment all NOTREACHED() without a milestone
25 // argument are fatal. As of 2024-03-19 this experiment is 50/50 enabled on M124
26 // Canary and Dev with intent to roll out to stable in M124 absent any blocking
27 // issues that come up.
28 //
29 // TODO(crbug.com/851128): After kNotReachedIsFatal is universally rolled out
30 // then move callers without a non-fatal milestone argument to
31 // NOTREACHED_NORETURN(). Then rename the [[noreturn]] version back to
32 // NOTREACHED().
33 #if CHECK_WILL_STREAM() || BUILDFLAG(ENABLE_LOG_ERROR_NOT_REACHED)
34 #define NOTREACHED(...)        \
35   LOGGING_CHECK_FUNCTION_IMPL( \
36       ::logging::NotReachedError::NotReached(__VA_ARGS__), false)
37 #else
38 #define BASE_HAS_VA_ARGS(...) 1
39 #define NOTREACHED(...)                                            \
40   BASE_IF(BASE_IS_EMPTY(__VA_ARGS__),                              \
41           (true) ? ::logging::NotReachedError::TriggerNotReached() \
42                  : EAT_CHECK_STREAM_PARAMS(),                      \
43           LOGGING_CHECK_FUNCTION_IMPL(                             \
44               ::logging::NotReachedError::NotReached(__VA_ARGS__), false))
45 #endif
46 
47 // NOTREACHED_NORETURN() annotates paths that are supposed to be unreachable.
48 // They crash if they are ever hit.
49 // TODO(crbug.com/851128): Rename back to NOTREACHED() once there are no callers
50 // of the old non-CHECK-fatal macro.
51 #if CHECK_WILL_STREAM()
52 #define NOTREACHED_NORETURN() ::logging::NotReachedNoreturnError()
53 #else
54 // This function is used to be able to detect NOTREACHED() failures in stack
55 // traces where this symbol is preserved (even if inlined). Its implementation
56 // matches logging::CheckFailure() but intentionally uses a different signature.
57 [[noreturn]] IMMEDIATE_CRASH_ALWAYS_INLINE void NotReachedFailure() {
58   base::ImmediateCrash();
59 }
60 
61 #define NOTREACHED_NORETURN() \
62   (true) ? ::logging::NotReachedFailure() : EAT_CHECK_STREAM_PARAMS()
63 #endif
64 
65 // The DUMP_WILL_BE_NOTREACHED_NORETURN() macro provides a convenient way to
66 // non-fatally dump in official builds if ever hit. See DUMP_WILL_BE_CHECK for
67 // suggested usage.
68 #define DUMP_WILL_BE_NOTREACHED_NORETURN() \
69   ::logging::CheckError::DumpWillBeNotReachedNoreturn()
70 
71 }  // namespace logging
72 
73 #endif  // BASE_NOTREACHED_H_
74