Skip to content

Commit db5b814

Browse files
jckingkyessenov
authored andcommitted
Introduce CEL_RETURN_IF_ERROR and CEL_ASSIGN_OR_RETURN
PiperOrigin-RevId: 401062623
1 parent e540ae2 commit db5b814

56 files changed

Lines changed: 564 additions & 311 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

base/BUILD

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,6 @@ licenses(["notice"]) # Apache v2.0
22

33
package(default_visibility = ["//visibility:public"])
44

5-
cc_library(
6-
name = "testing",
7-
testonly = True,
8-
srcs = [
9-
"testing.cc",
10-
],
11-
hdrs = [
12-
"testing.h",
13-
],
14-
deps = [
15-
"//internal:testing",
16-
"@com_google_absl//absl/status",
17-
"@com_google_absl//absl/status:statusor",
18-
],
19-
)
20-
21-
cc_library(
22-
name = "status_macros",
23-
hdrs = [
24-
"status_macros.h",
25-
],
26-
)
27-
285
cc_library(
296
name = "unilib",
307
srcs = [

base/status_macros.h

Lines changed: 0 additions & 81 deletions
This file was deleted.

common/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ cc_library(
5151
srcs = ["overflow.cc"],
5252
hdrs = ["overflow.h"],
5353
deps = [
54-
"//util/task:status",
54+
"//internal:status_macros",
5555
"@com_google_absl//absl/status",
5656
"@com_google_absl//absl/status:statusor",
5757
"@com_google_absl//absl/time",

common/overflow.cc

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "absl/status/status.h"
77
#include "absl/status/statusor.h"
88
#include "absl/time/time.h"
9-
#include "util/task/status_macros.h"
9+
#include "internal/status_macros.h"
1010

1111
namespace google::api::expr::common {
1212
namespace {
@@ -72,8 +72,8 @@ StatusOr<int64_t> CheckedAdd(int64_t x, int64_t y) {
7272
}
7373
return absl::OutOfRangeError("integer overflow");
7474
#else
75-
RETURN_IF_ERROR(CheckRange(y > 0 ? x <= kInt64Max - y : x >= kInt64Min - y,
76-
"integer overflow"));
75+
CEL_RETURN_IF_ERROR(CheckRange(
76+
y > 0 ? x <= kInt64Max - y : x >= kInt64Min - y, "integer overflow"));
7777
return x + y;
7878
#endif
7979
}
@@ -86,8 +86,8 @@ StatusOr<int64_t> CheckedSub(int64_t x, int64_t y) {
8686
}
8787
return absl::OutOfRangeError("integer overflow");
8888
#else
89-
RETURN_IF_ERROR(CheckRange(y < 0 ? x <= kInt64Max + y : x >= kInt64Min + y,
90-
"integer overflow"));
89+
CEL_RETURN_IF_ERROR(CheckRange(
90+
y < 0 ? x <= kInt64Max + y : x >= kInt64Min + y, "integer overflow"));
9191
return x - y;
9292
#endif
9393
}
@@ -100,7 +100,7 @@ StatusOr<int64_t> CheckedNegation(int64_t v) {
100100
}
101101
return absl::OutOfRangeError("integer overflow");
102102
#else
103-
RETURN_IF_ERROR(CheckRange(v != kInt64Min, "integer overflow"));
103+
CEL_RETURN_IF_ERROR(CheckRange(v != kInt64Min, "integer overflow"));
104104
return -v;
105105
#endif
106106
}
@@ -113,7 +113,7 @@ StatusOr<int64_t> CheckedMul(int64_t x, int64_t y) {
113113
}
114114
return absl::OutOfRangeError("integer overflow");
115115
#else
116-
RETURN_IF_ERROR(
116+
CEL_RETURN_IF_ERROR(
117117
CheckRange(!((x == -1 && y == kInt64Min) || (y == -1 && x == kInt64Min) ||
118118
(x > 0 && y > 0 && x > kInt64Max / y) ||
119119
(x < 0 && y < 0 && x < kInt64Max / y) ||
@@ -127,14 +127,16 @@ StatusOr<int64_t> CheckedMul(int64_t x, int64_t y) {
127127
}
128128

129129
StatusOr<int64_t> CheckedDiv(int64_t x, int64_t y) {
130-
RETURN_IF_ERROR(CheckRange(x != kInt64Min || y != -1, "integer overflow"));
131-
RETURN_IF_ERROR(CheckArgument(y != 0, "divide by zero"));
130+
CEL_RETURN_IF_ERROR(
131+
CheckRange(x != kInt64Min || y != -1, "integer overflow"));
132+
CEL_RETURN_IF_ERROR(CheckArgument(y != 0, "divide by zero"));
132133
return x / y;
133134
}
134135

135136
StatusOr<int64_t> CheckedMod(int64_t x, int64_t y) {
136-
RETURN_IF_ERROR(CheckRange(x != kInt64Min || y != -1, "integer overflow"));
137-
RETURN_IF_ERROR(CheckArgument(y != 0, "modulus by zero"));
137+
CEL_RETURN_IF_ERROR(
138+
CheckRange(x != kInt64Min || y != -1, "integer overflow"));
139+
CEL_RETURN_IF_ERROR(CheckArgument(y != 0, "modulus by zero"));
138140
return x % y;
139141
}
140142

@@ -146,7 +148,8 @@ StatusOr<uint64_t> CheckedAdd(uint64_t x, uint64_t y) {
146148
}
147149
return absl::OutOfRangeError("unsigned integer overflow");
148150
#else
149-
RETURN_IF_ERROR(CheckRange(x <= kUint64Max - y, "unsigned integer overflow"));
151+
CEL_RETURN_IF_ERROR(
152+
CheckRange(x <= kUint64Max - y, "unsigned integer overflow"));
150153
return x + y;
151154
#endif
152155
}
@@ -159,7 +162,7 @@ StatusOr<uint64_t> CheckedSub(uint64_t x, uint64_t y) {
159162
}
160163
return absl::OutOfRangeError("unsigned integer overflow");
161164
#else
162-
RETURN_IF_ERROR(CheckRange(y <= x, "unsigned integer overflow"));
165+
CEL_RETURN_IF_ERROR(CheckRange(y <= x, "unsigned integer overflow"));
163166
return x - y;
164167
#endif
165168
}
@@ -172,24 +175,25 @@ StatusOr<uint64_t> CheckedMul(uint64_t x, uint64_t y) {
172175
}
173176
return absl::OutOfRangeError("unsigned integer overflow");
174177
#else
175-
RETURN_IF_ERROR(
178+
CEL_RETURN_IF_ERROR(
176179
CheckRange(y == 0 || x <= kUint64Max / y, "unsigned integer overflow"));
177180
return x * y;
178181
#endif
179182
}
180183

181184
StatusOr<uint64_t> CheckedDiv(uint64_t x, uint64_t y) {
182-
RETURN_IF_ERROR(CheckArgument(y != 0, "divide by zero"));
185+
CEL_RETURN_IF_ERROR(CheckArgument(y != 0, "divide by zero"));
183186
return x / y;
184187
}
185188

186189
StatusOr<uint64_t> CheckedMod(uint64_t x, uint64_t y) {
187-
RETURN_IF_ERROR(CheckArgument(y != 0, "modulus by zero"));
190+
CEL_RETURN_IF_ERROR(CheckArgument(y != 0, "modulus by zero"));
188191
return x % y;
189192
}
190193

191194
StatusOr<absl::Duration> CheckedAdd(absl::Duration x, absl::Duration y) {
192-
RETURN_IF_ERROR(CheckRange(IsFinite(x) && IsFinite(y), "integer overflow"));
195+
CEL_RETURN_IF_ERROR(
196+
CheckRange(IsFinite(x) && IsFinite(y), "integer overflow"));
193197
// absl::Duration can handle +- infinite durations, but the Go time.Duration
194198
// implementation caps the durations to those expressible within a single
195199
// int64_t rather than (seconds int64_t, nanos int32_t).
@@ -201,26 +205,29 @@ StatusOr<absl::Duration> CheckedAdd(absl::Duration x, absl::Duration y) {
201205
// Since Go is the more conservative of the implementations and 290 year
202206
// durations seem quite reasonable, this code mirrors the conservative
203207
// overflow behavior which would be observed in Go.
204-
ASSIGN_OR_RETURN(int64_t nanos, CheckedAdd(absl::ToInt64Nanoseconds(x),
205-
absl::ToInt64Nanoseconds(y)));
208+
CEL_ASSIGN_OR_RETURN(int64_t nanos, CheckedAdd(absl::ToInt64Nanoseconds(x),
209+
absl::ToInt64Nanoseconds(y)));
206210
return absl::Nanoseconds(nanos);
207211
}
208212

209213
StatusOr<absl::Duration> CheckedSub(absl::Duration x, absl::Duration y) {
210-
RETURN_IF_ERROR(CheckRange(IsFinite(x) && IsFinite(y), "integer overflow"));
211-
ASSIGN_OR_RETURN(int64_t nanos, CheckedSub(absl::ToInt64Nanoseconds(x),
212-
absl::ToInt64Nanoseconds(y)));
214+
CEL_RETURN_IF_ERROR(
215+
CheckRange(IsFinite(x) && IsFinite(y), "integer overflow"));
216+
CEL_ASSIGN_OR_RETURN(int64_t nanos, CheckedSub(absl::ToInt64Nanoseconds(x),
217+
absl::ToInt64Nanoseconds(y)));
213218
return absl::Nanoseconds(nanos);
214219
}
215220

216221
StatusOr<absl::Duration> CheckedNegation(absl::Duration v) {
217-
RETURN_IF_ERROR(CheckRange(IsFinite(v), "integer overflow"));
218-
ASSIGN_OR_RETURN(int64_t nanos, CheckedNegation(absl::ToInt64Nanoseconds(v)));
222+
CEL_RETURN_IF_ERROR(CheckRange(IsFinite(v), "integer overflow"));
223+
CEL_ASSIGN_OR_RETURN(int64_t nanos,
224+
CheckedNegation(absl::ToInt64Nanoseconds(v)));
219225
return absl::Nanoseconds(nanos);
220226
}
221227

222228
StatusOr<absl::Time> CheckedAdd(absl::Time t, absl::Duration d) {
223-
RETURN_IF_ERROR(CheckRange(IsFinite(t) && IsFinite(d), "timestamp overflow"));
229+
CEL_RETURN_IF_ERROR(
230+
CheckRange(IsFinite(t) && IsFinite(d), "timestamp overflow"));
224231
// First we break time into its components by truncating and subtracting.
225232
const int64_t s1 = absl::ToUnixSeconds(t);
226233
const int64_t ns1 = (t - absl::FromUnixSeconds(s1)) / absl::Nanoseconds(1);
@@ -232,87 +239,91 @@ StatusOr<absl::Time> CheckedAdd(absl::Time t, absl::Duration d) {
232239
const int64_t ns2 = absl::ToInt64Nanoseconds(d % kOneSecondDuration);
233240

234241
// Add seconds first, detecting any overflow.
235-
ASSIGN_OR_RETURN(int64_t s, CheckedAdd(s1, s2));
242+
CEL_ASSIGN_OR_RETURN(int64_t s, CheckedAdd(s1, s2));
236243
// Nanoseconds cannot overflow as nanos are normalized to [0, 999999999].
237244
absl::Duration ns = absl::Nanoseconds(ns2 + ns1);
238245

239246
// Normalize nanoseconds to be positive and carry extra nanos to seconds.
240247
if (ns < absl::ZeroDuration() || ns >= kOneSecondDuration) {
241248
// Add seconds, or no-op if nanseconds negative (ns never < -999_999_999ns)
242-
ASSIGN_OR_RETURN(s, CheckedAdd(s, ns / kOneSecondDuration));
249+
CEL_ASSIGN_OR_RETURN(s, CheckedAdd(s, ns / kOneSecondDuration));
243250
ns -= (ns / kOneSecondDuration) * kOneSecondDuration;
244251
// Subtract a second to make the nanos positive.
245252
if (ns < absl::ZeroDuration()) {
246-
ASSIGN_OR_RETURN(s, CheckedAdd(s, -1));
253+
CEL_ASSIGN_OR_RETURN(s, CheckedAdd(s, -1));
247254
ns += kOneSecondDuration;
248255
}
249256
}
250257
// Check if the the number of seconds from Unix epoch is within our acceptable
251258
// range.
252-
RETURN_IF_ERROR(
259+
CEL_RETURN_IF_ERROR(
253260
CheckRange(s >= kMinUnixTime && s <= kMaxUnixTime, "timestamp overflow"));
254261

255262
// Return resulting time.
256263
return absl::FromUnixSeconds(s) + ns;
257264
}
258265

259266
StatusOr<absl::Time> CheckedSub(absl::Time t, absl::Duration d) {
260-
ASSIGN_OR_RETURN(auto neg_duration, CheckedNegation(d));
267+
CEL_ASSIGN_OR_RETURN(auto neg_duration, CheckedNegation(d));
261268
return CheckedAdd(t, neg_duration);
262269
}
263270

264271
StatusOr<absl::Duration> CheckedSub(absl::Time t1, absl::Time t2) {
265-
RETURN_IF_ERROR(CheckRange(IsFinite(t1) && IsFinite(t2), "integer overflow"));
272+
CEL_RETURN_IF_ERROR(
273+
CheckRange(IsFinite(t1) && IsFinite(t2), "integer overflow"));
266274
// First we break time into its components by truncating and subtracting.
267275
const int64_t s1 = absl::ToUnixSeconds(t1);
268276
const int64_t ns1 = (t1 - absl::FromUnixSeconds(s1)) / absl::Nanoseconds(1);
269277
const int64_t s2 = absl::ToUnixSeconds(t2);
270278
const int64_t ns2 = (t2 - absl::FromUnixSeconds(s2)) / absl::Nanoseconds(1);
271279

272280
// Subtract seconds first, detecting any overflow.
273-
ASSIGN_OR_RETURN(int64_t s, CheckedSub(s1, s2));
281+
CEL_ASSIGN_OR_RETURN(int64_t s, CheckedSub(s1, s2));
274282
// Nanoseconds cannot overflow as nanos are normalized to [0, 999999999].
275283
absl::Duration ns = absl::Nanoseconds(ns1 - ns2);
276284

277285
// Scale the seconds result to nanos.
278-
ASSIGN_OR_RETURN(const int64_t t, CheckedMul(s, kOneSecondNanos));
286+
CEL_ASSIGN_OR_RETURN(const int64_t t, CheckedMul(s, kOneSecondNanos));
279287
// Add the seconds (scaled to nanos) to the nanosecond value.
280-
ASSIGN_OR_RETURN(const int64_t v,
281-
CheckedAdd(t, absl::ToInt64Nanoseconds(ns)));
288+
CEL_ASSIGN_OR_RETURN(const int64_t v,
289+
CheckedAdd(t, absl::ToInt64Nanoseconds(ns)));
282290
return absl::Nanoseconds(v);
283291
}
284292

285293
StatusOr<int64_t> CheckedDoubleToInt64(double v) {
286-
RETURN_IF_ERROR(
294+
CEL_RETURN_IF_ERROR(
287295
CheckRange(std::isfinite(v) && v < kDoubleToIntMax && v > kDoubleToIntMin,
288296
"double out of int64_t range"));
289297
return static_cast<int64_t>(v);
290298
}
291299

292300
StatusOr<uint64_t> CheckedDoubleToUint64(double v) {
293-
RETURN_IF_ERROR(CheckRange(std::isfinite(v) && v >= 0 && v < kDoubleTwoTo64,
294-
"double out of uint64_t range"));
301+
CEL_RETURN_IF_ERROR(
302+
CheckRange(std::isfinite(v) && v >= 0 && v < kDoubleTwoTo64,
303+
"double out of uint64_t range"));
295304
return static_cast<uint64_t>(v);
296305
}
297306

298307
StatusOr<uint64_t> CheckedInt64ToUint64(int64_t v) {
299-
RETURN_IF_ERROR(CheckRange(v >= 0, "int64 out of uint64_t range"));
308+
CEL_RETURN_IF_ERROR(CheckRange(v >= 0, "int64 out of uint64_t range"));
300309
return static_cast<uint64_t>(v);
301310
}
302311

303312
StatusOr<int32_t> CheckedInt64ToInt32(int64_t v) {
304-
RETURN_IF_ERROR(
313+
CEL_RETURN_IF_ERROR(
305314
CheckRange(v >= kInt32Min && v <= kInt32Max, "int64 out of int32_t range"));
306315
return static_cast<int32_t>(v);
307316
}
308317

309318
StatusOr<int64_t> CheckedUint64ToInt64(uint64_t v) {
310-
RETURN_IF_ERROR(CheckRange(v <= kUintToIntMax, "uint64 out of int64_t range"));
319+
CEL_RETURN_IF_ERROR(
320+
CheckRange(v <= kUintToIntMax, "uint64 out of int64_t range"));
311321
return static_cast<int64_t>(v);
312322
}
313323

314324
StatusOr<uint32_t> CheckedUint64ToUint32(uint64_t v) {
315-
RETURN_IF_ERROR(CheckRange(v <= kUint32Max, "uint64 out of uint32_t range"));
325+
CEL_RETURN_IF_ERROR(
326+
CheckRange(v <= kUint32Max, "uint64 out of uint32_t range"));
316327
return static_cast<uint32_t>(v);
317328
}
318329

0 commit comments

Comments
 (0)