xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/kernels/shim/status_macros.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
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     http://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 #ifndef TENSORFLOW_LITE_KERNELS_SHIM_STATUS_MACROS_H_
16 #define TENSORFLOW_LITE_KERNELS_SHIM_STATUS_MACROS_H_
17 
18 #include "absl/status/status.h"
19 
20 // A few macros to help with propagating status.
21 // They are mainly adapted from the ones in  tensorflow/core/platform/errors.h
22 // and tensorflow/core/platform/statusor.h but these files come with many
23 // transitive deps which can be too much for TFLite use cases. If these type of
24 // macros end up in `absl` we can replace them with those.
25 
26 // The macros are prefixed with SH_ to avoid name collision.
27 
28 namespace tflite {
29 namespace shim {
30 template <typename... Args>
AppendToMessage(::absl::Status * status,Args...args)31 void AppendToMessage(::absl::Status* status, Args... args) {
32   *status = ::absl::Status(status->code(),
33                            ::absl::StrCat(status->message(), "\n\t", args...));
34 }
35 }  // namespace shim
36 }  // namespace tflite
37 
38 // Propagates error up the stack and appends to the error message.
39 #define SH_RETURN_WITH_CONTEXT_IF_ERROR(expr, ...)            \
40   do {                                                        \
41     ::absl::Status _status = (expr);                          \
42     if (!_status.ok()) {                                      \
43       ::tflite::shim::AppendToMessage(&_status, __VA_ARGS__); \
44       return _status;                                         \
45     }                                                         \
46   } while (0)
47 
48 // Propages the error up the stack.
49 // This can't be merged with the SH_RETURN_WITH_CONTEXT_IF_ERROR unless some
50 // overly clever/unreadable macro magic is used.
51 #define SH_RETURN_IF_ERROR(...)             \
52   do {                                      \
53     ::absl::Status _status = (__VA_ARGS__); \
54     if (!_status.ok()) return _status;      \
55   } while (0)
56 
57 // Internal helper for concatenating macro values.
58 #define SH_STATUS_MACROS_CONCAT_NAME_INNER(x, y) x##y
59 #define SH_STATUS_MACROS_CONCAT_NAME(x, y) \
60   SH_STATUS_MACROS_CONCAT_NAME_INNER(x, y)
61 
62 // Assigns an expression to lhs or propagates the error up.
63 #define SH_ASSIGN_OR_RETURN(lhs, rexpr) \
64   SH_ASSIGN_OR_RETURN_IMPL(             \
65       SH_STATUS_MACROS_CONCAT_NAME(statusor, __COUNTER__), lhs, rexpr)
66 
67 #define SH_ASSIGN_OR_RETURN_IMPL(statusor, lhs, rexpr) \
68   auto statusor = (rexpr);                             \
69   if (!statusor.ok()) return statusor.status();        \
70   lhs = std::move(statusor.value())
71 
72 #endif  // TENSORFLOW_LITE_KERNELS_SHIM_STATUS_MACROS_H_
73