Coverage Report

Created: 2026-08-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/status/internal/status_internal.h
Line
Count
Source
1
// Copyright 2019 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
#ifndef ABSL_STATUS_INTERNAL_STATUS_INTERNAL_H_
15
#define ABSL_STATUS_INTERNAL_STATUS_INTERNAL_H_
16
17
#include <atomic>
18
#include <cstdint>
19
#include <memory>
20
#include <optional>
21
#include <string>
22
#include <utility>
23
#include <vector>
24
25
#include "absl/base/attributes.h"
26
#include "absl/base/config.h"
27
#include "absl/base/nullability.h"
28
#include "absl/container/inlined_vector.h"
29
#include "absl/strings/cord.h"
30
#include "absl/strings/string_view.h"
31
#include "absl/types/optional.h"
32
#include "absl/types/optional_ref.h"
33
#include "absl/types/source_location.h"
34
#include "absl/types/span.h"
35
36
#ifndef SWIG
37
// Disabled for SWIG as it doesn't parse attributes correctly.
38
namespace absl {
39
ABSL_NAMESPACE_BEGIN
40
// Returned Status objects may not be ignored. Codesearch doesn't handle ifdefs
41
// as part of a class definitions (b/6995610), so we use a forward declaration.
42
//
43
// TODO(b/176172494): ABSL_MUST_USE_RESULT should expand to the more strict
44
// [[nodiscard]]. For now, just use [[nodiscard]] directly when it is available.
45
#if ABSL_HAVE_CPP_ATTRIBUTE(nodiscard)
46
class [[nodiscard]] ABSL_ATTRIBUTE_TRIVIAL_ABI
47
    Status;
48
#else
49
class ABSL_MUST_USE_RESULT ABSL_ATTRIBUTE_TRIVIAL_ABI
50
    Status;
51
#endif
52
53
ABSL_NAMESPACE_END
54
}  // namespace absl
55
#endif  // !SWIG
56
57
namespace absl {
58
ABSL_NAMESPACE_BEGIN
59
60
enum class StatusCode : int;
61
enum class StatusToStringMode : int;
62
63
namespace status_internal {
64
#ifndef SWIG
65
class StatusPrivateAccessor;
66
class StatusPrivateAccessorForStatusBuilder;
67
#endif  // !SWIG
68
69
// Container for status payloads.
70
struct Payload {
71
  std::string type_url;
72
  absl::Cord payload;
73
};
74
75
using Payloads = absl::InlinedVector<Payload, 1>;
76
77
// Reference-counted representation of Status data.
78
class StatusRep {
79
 public:
80
  StatusRep(absl::StatusCode code_arg, absl::string_view message_arg,
81
            std::unique_ptr<status_internal::Payloads> payloads_arg)
82
3.15k
      : ref_(int32_t{1}),
83
3.15k
        code_(code_arg),
84
3.15k
        message_(message_arg),
85
3.15k
        payloads_(std::move(payloads_arg)) {}
86
87
0
  absl::StatusCode code() const { return code_; }
88
0
  const std::string& message() const { return message_; }
89
90
  // Ref and unref are const to allow access through a const pointer, and are
91
  // used during copying operations.
92
0
  void Ref() const { ref_.fetch_add(1, std::memory_order_relaxed); }
93
  void Unref() const;
94
95
  // Payload methods correspond to the same methods in absl::Status.
96
  std::optional<absl::Cord> GetPayload(absl::string_view type_url) const;
97
  void SetPayload(absl::string_view type_url, absl::Cord payload);
98
  struct EraseResult {
99
    bool erased;
100
    uintptr_t new_rep;
101
  };
102
  EraseResult ErasePayload(absl::string_view type_url);
103
  void ForEachPayload(
104
      absl::FunctionRef<void(absl::string_view, const absl::Cord&)> visitor)
105
      const;
106
107
  absl::Span<const SourceLocation> GetSourceLocations() const;
108
  void AddSourceLocation(absl::SourceLocation loc);
109
110
  std::string ToString(StatusToStringMode mode) const;
111
112
  bool operator==(const StatusRep& other) const;
113
0
  bool operator!=(const StatusRep& other) const { return !(*this == other); }
114
115
  // Returns an equivalent heap allocated StatusRep with refcount 1.
116
  //
117
  // If `new_message` is provided, the message will be replaced with the new
118
  // message.
119
  StatusRep* absl_nonnull Clone(
120
      absl::optional_ref<absl::string_view> new_message, bool include_payloads,
121
      bool include_source_locations) const;
122
123
  // Same as Clone(), but also removes a reference to `this`. `this` is not safe
124
  // to be used after calling as it may have been deleted.
125
  StatusRep* absl_nonnull CloneAndUnref(
126
      absl::optional_ref<absl::string_view> new_message, bool include_payloads,
127
      bool include_source_locations) const;
128
  StatusRep* absl_nonnull CloneAndUnref() const;
129
130
 private:
131
  mutable std::atomic<int32_t> ref_;
132
  absl::StatusCode code_;
133
134
  // As an internal implementation detail, we guarantee that if status.message()
135
  // is non-empty, then the resulting string_view is null terminated.
136
  // This is required to implement 'StatusMessageAsCStr(...)'
137
  std::string message_;
138
  absl::InlinedVector<absl::SourceLocation, 1> source_locations_;
139
  std::unique_ptr<status_internal::Payloads> payloads_;
140
};
141
142
absl::StatusCode MapToLocalCode(int value);
143
144
// Returns a pointer to a newly-allocated string with the given `prefix`,
145
// suitable for output as an error message in assertion/`CHECK()` failures.
146
//
147
// This is an internal implementation detail for Abseil logging.
148
ABSL_ATTRIBUTE_PURE_FUNCTION
149
const char* absl_nonnull MakeCheckFailString(
150
    const absl::Status* absl_nonnull status, const char* absl_nonnull prefix);
151
152
}  // namespace status_internal
153
154
ABSL_NAMESPACE_END
155
}  // namespace absl
156
157
#endif  // ABSL_STATUS_INTERNAL_STATUS_INTERNAL_H_