Coverage Report

Created: 2026-06-22 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sentencepiece/third_party/absl/log/internal/conditions.h
Line
Count
Source
1
// Copyright 2022 The Abseil Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
// -----------------------------------------------------------------------------
16
// File: log/internal/conditions.h
17
// -----------------------------------------------------------------------------
18
//
19
// This file contains implementation of conditional log statements, like LOG_IF
20
// including all the ABSL_LOG_INTERNAL_..._CONDITION_... macros and
21
// various condition classes like LogEveryNState.
22
23
#ifndef ABSL_LOG_INTERNAL_CONDITIONS_H_
24
#define ABSL_LOG_INTERNAL_CONDITIONS_H_
25
26
#if defined(_WIN32) || defined(__hexagon__)
27
#include <cstdlib>
28
#else
29
#include <unistd.h>
30
#endif
31
#include <stdlib.h>
32
33
#include <atomic>
34
#include <cstdint>
35
36
#include "absl/base/attributes.h"
37
#include "absl/base/config.h"
38
#include "absl/log/internal/voidify.h"
39
40
// `ABSL_LOG_INTERNAL_CONDITION` prefixes another macro that expands to a
41
// temporary `LogMessage` instantiation followed by zero or more streamed
42
// expressions.  This definition is tricky to read correctly.  It evaluates to
43
// either
44
//
45
//   (void)0;
46
//
47
// or
48
//
49
//   ::absl::log_internal::Voidify() &&
50
//       ::absl::log_internal::LogMessage(...) << "the user's message";
51
//
52
// If the condition is evaluable at compile time, as is often the case, it
53
// compiles away to just one side or the other.
54
//
55
// Although this is not used anywhere a statement (e.g. `if`) could not go,
56
// the ternary expression does a better job avoiding spurious diagnostics
57
// (dangling else, missing switch case) and preserving noreturn semantics (e.g.
58
// on `LOG(FATAL)`) without requiring braces.
59
//
60
// The `switch` ensures that this expansion is the beginning of a statement (as
61
// opposed to an expression) and prevents shenanigans like
62
// `AFunction(LOG(INFO))` and `decltype(LOG(INFO))`.  The apparently-redundant
63
// `default` case makes the condition more amenable to Clang dataflow analysis.
64
#define ABSL_LOG_INTERNAL_STATELESS_CONDITION(condition) \
65
11.1M
  switch (0)                                             \
66
11.1M
  case 0:                                                \
67
5.55M
  default:                                               \
68
9.89M
    !(condition) ? (void)0 : ::absl::log_internal::Voidify() &&
69
70
// `ABSL_LOG_INTERNAL_STATEFUL_CONDITION` applies a condition like
71
// `ABSL_LOG_INTERNAL_STATELESS_CONDITION` but adds to that a series of variable
72
// declarations, including a local static object which stores the state needed
73
// to implement the stateful macros like `LOG_EVERY_N`.
74
//
75
// `for`-loops are used to declare scoped variables without braces (to permit
76
// streaming into the macro's expansion) and without the dangling-`else`
77
// problems/diagnostics that come with `if`.
78
//
79
// Two more variables are declared in separate `for`-loops:
80
//
81
// * `COUNTER` implements a streamable token whose value when streamed is the
82
//   number of times execution has passed through the macro.
83
// * A boolean flag is used to prevent any of the `for`-loops from ever actually
84
//   looping.
85
#define ABSL_LOG_INTERNAL_STATEFUL_CONDITION(condition)             \
86
  for (bool absl_log_internal_stateful_condition_do_log(condition); \
87
       absl_log_internal_stateful_condition_do_log;                 \
88
       absl_log_internal_stateful_condition_do_log = false)         \
89
  ABSL_LOG_INTERNAL_STATEFUL_CONDITION_IMPL
90
#define ABSL_LOG_INTERNAL_STATEFUL_CONDITION_IMPL(kind, ...)              \
91
  for (static ::absl::log_internal::Log##kind##State                      \
92
           absl_log_internal_stateful_condition_state;                    \
93
       absl_log_internal_stateful_condition_do_log &&                     \
94
       absl_log_internal_stateful_condition_state.ShouldLog(__VA_ARGS__); \
95
       absl_log_internal_stateful_condition_do_log = false)               \
96
    for (const uint32_t COUNTER ABSL_ATTRIBUTE_UNUSED =                   \
97
             absl_log_internal_stateful_condition_state.counter();        \
98
         absl_log_internal_stateful_condition_do_log;                     \
99
         absl_log_internal_stateful_condition_do_log = false)             \
100
  ::absl::log_internal::Voidify() &&
101
102
// `ABSL_LOG_INTERNAL_CONDITION_*` serve to combine any conditions from the
103
// macro (e.g. `LOG_IF` or `VLOG`) with inherent conditions (e.g.
104
// `ABSL_MIN_LOG_LEVEL`) into a single boolean expression.  We could chain
105
// ternary operators instead, however some versions of Clang sometimes issue
106
// spurious diagnostics after such expressions due to a control flow analysis
107
// bug.
108
#ifdef ABSL_MIN_LOG_LEVEL
109
#define ABSL_LOG_INTERNAL_CONDITION_INFO(type, condition) \
110
  ABSL_LOG_INTERNAL_##type##_CONDITION(                   \
111
      (condition) &&                                      \
112
      ::absl::LogSeverity::kInfo >=                       \
113
          static_cast<::absl::LogSeverityAtLeast>(ABSL_MIN_LOG_LEVEL))
114
#define ABSL_LOG_INTERNAL_CONDITION_WARNING(type, condition) \
115
  ABSL_LOG_INTERNAL_##type##_CONDITION(                      \
116
      (condition) &&                                         \
117
      ::absl::LogSeverity::kWarning >=                       \
118
          static_cast<::absl::LogSeverityAtLeast>(ABSL_MIN_LOG_LEVEL))
119
#define ABSL_LOG_INTERNAL_CONDITION_ERROR(type, condition) \
120
  ABSL_LOG_INTERNAL_##type##_CONDITION(                    \
121
      (condition) &&                                       \
122
      ::absl::LogSeverity::kError >=                       \
123
          static_cast<::absl::LogSeverityAtLeast>(ABSL_MIN_LOG_LEVEL))
124
#define ABSL_LOG_INTERNAL_CONDITION_DO_NOT_SUBMIT(type, condition) \
125
  ABSL_LOG_INTERNAL_CONDITION_ERROR(type, condition)
126
// NOTE: Use ternary operators instead of short-circuiting to mitigate
127
// https://bugs.llvm.org/show_bug.cgi?id=51928.
128
#define ABSL_LOG_INTERNAL_CONDITION_FATAL(type, condition)                 \
129
  ABSL_LOG_INTERNAL_##type##_CONDITION(                                    \
130
      ((condition) ? (::absl::LogSeverity::kFatal >=                       \
131
                              static_cast<::absl::LogSeverityAtLeast>(     \
132
                                  ABSL_MIN_LOG_LEVEL)                      \
133
                          ? true                                           \
134
                          : (::absl::log_internal::AbortQuietly(), false)) \
135
                   : false))
136
// NOTE: Use ternary operators instead of short-circuiting to mitigate
137
// https://bugs.llvm.org/show_bug.cgi?id=51928.
138
#define ABSL_LOG_INTERNAL_CONDITION_QFATAL(type, condition)               \
139
  ABSL_LOG_INTERNAL_##type##_CONDITION(                                   \
140
      ((condition) ? (::absl::LogSeverity::kFatal >=                      \
141
                              static_cast<::absl::LogSeverityAtLeast>(    \
142
                                  ABSL_MIN_LOG_LEVEL)                     \
143
                          ? true                                          \
144
                          : (::absl::log_internal::ExitQuietly(), false)) \
145
                   : false))
146
#define ABSL_LOG_INTERNAL_CONDITION_DFATAL(type, condition)                    \
147
  ABSL_LOG_INTERNAL_##type##_CONDITION(                                        \
148
      (ABSL_ASSUME(absl::kLogDebugFatal == absl::LogSeverity::kError ||        \
149
                   absl::kLogDebugFatal == absl::LogSeverity::kFatal),         \
150
       (condition) &&                                                          \
151
           (::absl::kLogDebugFatal >=                                          \
152
                static_cast<::absl::LogSeverityAtLeast>(ABSL_MIN_LOG_LEVEL) || \
153
            (::absl::kLogDebugFatal == ::absl::LogSeverity::kFatal &&          \
154
             (::absl::log_internal::AbortQuietly(), false)))))
155
156
#define ABSL_LOG_INTERNAL_CONDITION_LEVEL(severity)                            \
157
  for (int absl_log_internal_severity_loop = 1;                                \
158
       absl_log_internal_severity_loop; absl_log_internal_severity_loop = 0)   \
159
    for (const absl::LogSeverity absl_log_internal_severity =                  \
160
             ::absl::NormalizeLogSeverity(severity);                           \
161
         absl_log_internal_severity_loop; absl_log_internal_severity_loop = 0) \
162
  ABSL_LOG_INTERNAL_CONDITION_LEVEL_IMPL
163
#define ABSL_LOG_INTERNAL_CONDITION_LEVEL_IMPL(type, condition)            \
164
  ABSL_LOG_INTERNAL_##type##_CONDITION(                                    \
165
      ((condition) &&                                                      \
166
       (absl_log_internal_severity >=                                      \
167
            static_cast<::absl::LogSeverityAtLeast>(ABSL_MIN_LOG_LEVEL) || \
168
        (absl_log_internal_severity == ::absl::LogSeverity::kFatal &&      \
169
         (::absl::log_internal::AbortQuietly(), false)))))
170
#else  // ndef ABSL_MIN_LOG_LEVEL
171
#define ABSL_LOG_INTERNAL_CONDITION_INFO(type, condition) \
172
612k
  ABSL_LOG_INTERNAL_##type##_CONDITION(condition)
173
#define ABSL_LOG_INTERNAL_CONDITION_WARNING(type, condition) \
174
1.02k
  ABSL_LOG_INTERNAL_##type##_CONDITION(condition)
175
#define ABSL_LOG_INTERNAL_CONDITION_ERROR(type, condition) \
176
0
  ABSL_LOG_INTERNAL_##type##_CONDITION(condition)
177
#define ABSL_LOG_INTERNAL_CONDITION_DO_NOT_SUBMIT(type, condition) \
178
  ABSL_LOG_INTERNAL_CONDITION_ERROR(type, condition)
179
#define ABSL_LOG_INTERNAL_CONDITION_FATAL(type, condition) \
180
4.94M
  ABSL_LOG_INTERNAL_##type##_CONDITION(condition)
181
#define ABSL_LOG_INTERNAL_CONDITION_QFATAL(type, condition) \
182
  ABSL_LOG_INTERNAL_##type##_CONDITION(condition)
183
#define ABSL_LOG_INTERNAL_CONDITION_DFATAL(type, condition) \
184
  ABSL_LOG_INTERNAL_##type##_CONDITION(condition)
185
#define ABSL_LOG_INTERNAL_CONDITION_LEVEL(severity)                            \
186
  for (int absl_log_internal_severity_loop = 1;                                \
187
       absl_log_internal_severity_loop; absl_log_internal_severity_loop = 0)   \
188
    for (const absl::LogSeverity absl_log_internal_severity =                  \
189
             ::absl::NormalizeLogSeverity(severity);                           \
190
         absl_log_internal_severity_loop; absl_log_internal_severity_loop = 0) \
191
  ABSL_LOG_INTERNAL_CONDITION_LEVEL_IMPL
192
#define ABSL_LOG_INTERNAL_CONDITION_LEVEL_IMPL(type, condition) \
193
  ABSL_LOG_INTERNAL_##type##_CONDITION(condition)
194
#endif  // ndef ABSL_MIN_LOG_LEVEL
195
196
namespace absl {
197
ABSL_NAMESPACE_BEGIN
198
namespace log_internal {
199
200
// Stateful condition class name should be "Log" + name + "State".
201
class LogEveryNState final {
202
 public:
203
  bool ShouldLog(int n);
204
0
  uint32_t counter() { return counter_.load(std::memory_order_relaxed); }
205
206
 private:
207
  std::atomic<uint32_t> counter_{0};
208
};
209
210
class LogFirstNState final {
211
 public:
212
  bool ShouldLog(int n);
213
0
  uint32_t counter() { return counter_.load(std::memory_order_relaxed); }
214
215
 private:
216
  std::atomic<uint32_t> counter_{0};
217
};
218
219
class LogEveryPow2State final {
220
 public:
221
  bool ShouldLog();
222
0
  uint32_t counter() { return counter_.load(std::memory_order_relaxed); }
223
224
 private:
225
  std::atomic<uint32_t> counter_{0};
226
};
227
228
class LogEveryNSecState final {
229
 public:
230
  bool ShouldLog(double seconds);
231
0
  uint32_t counter() { return counter_.load(std::memory_order_relaxed); }
232
233
 private:
234
  std::atomic<uint32_t> counter_{0};
235
  // Cycle count according to CycleClock that we should next log at.
236
  std::atomic<int64_t> next_log_time_cycles_{0};
237
};
238
239
// Helper routines to abort the application quietly
240
241
0
[[noreturn]] inline void AbortQuietly() { abort(); }
242
0
[[noreturn]] inline void ExitQuietly() { _exit(1); }
243
}  // namespace log_internal
244
ABSL_NAMESPACE_END
245
}  // namespace absl
246
247
#endif  // ABSL_LOG_INTERNAL_CONDITIONS_H_