blob: 7b6df9863ba2da864fb2a4d52bf9c1ea0531d2dc [file] [log] [blame]
Yavor Goulishev9c08e842020-04-29 14:03:33 -07001// Copyright 2014 The Crashpad 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 CRASHPAD_UTIL_MISC_INITIALIZATION_INITIALIZATION_STATE_DCHECK_H_
16#define CRASHPAD_UTIL_MISC_INITIALIZATION_INITIALIZATION_STATE_DCHECK_H_
17
18//! \file
19
20#include "base/compiler_specific.h"
21#include "base/logging.h"
22#include "base/macros.h"
23#include "build/build_config.h"
24#include "util/misc/initialization_state.h"
25
26namespace crashpad {
27
28#if DCHECK_IS_ON() || DOXYGEN
29
30//! \brief Tracks whether data are initialized, triggering a DCHECK assertion
31//! on an invalid data access.
32//!
33//! Put an InitializationStateDcheck member into a class to help DCHECK that
34//! it’s in the right states at the right times. This is useful for classes with
35//! Initialize() methods. The chief advantage of InitializationStateDcheck over
36//! having a member variable to track state is that when the only use of the
37//! variable is to DCHECK, it wastes space (in memory and executable code) in
38//! non-DCHECK builds unless the code is also peppered with ugly `#%ifdef`s.
39//!
40//! This implementation concentrates the ugly `#%ifdef`s in one location.
41//!
42//! Usage:
43//!
44//! \code
45//! class Class {
46//! public:
47//! Class() : initialized_() {}
48//!
49//! void Initialize() {
50//! INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
51//! // Perform initialization.
52//! INITIALIZATION_STATE_SET_VALID(initialized_);
53//! }
54//!
55//! void DoSomething() {
56//! INITIALIZATION_STATE_DCHECK_VALID(initialized_);
57//! // Do something.
58//! }
59//!
60//! private:
61//! InitializationStateDcheck initialized_;
62//! };
63//! \endcode
64class InitializationStateDcheck : public InitializationState {
65 public:
66 InitializationStateDcheck() : InitializationState() {}
67
68 //! \brief Returns the object’s state.
69 //!
70 //! Consumers of this class should not call this method. Use the
71 //! INITIALIZATION_STATE_SET_INITIALIZING(), INITIALIZATION_STATE_SET_VALID(),
72 //! and INITIALIZATION_STATE_DCHECK_VALID() macros instead.
73 //
74 // The superclass’ state() accessor is protected, but it needs to be exposed
75 // to consumers of this class for the macros below to work properly. The
76 // macros prefer access to the unerlying state value over a simple boolean
77 // because with access to the state value, DCHECK_EQ can be used, which, when
78 // tripped, prints both the expected and observed values. This can aid
79 // troubleshooting.
80 State state() const { return InitializationState::state(); }
81
82 //! \brief Marks an uninitialized object as initializing.
83 //!
84 //! If the object is in the #kStateUninitialized state, changes its state to
85 //! #kStateInvalid (initializing) and returns the previous
86 //! (#kStateUninitialized) state. Otherwise, returns the object’s current
87 //! state.
88 //!
89 //! Consumers of this class should not call this method. Use the
90 //! INITIALIZATION_STATE_SET_INITIALIZING() macro instead.
91 State SetInitializing();
92
93 //! \brief Marks an initializing object as valid.
94 //!
95 //! If the object is in the #kStateInvalid (initializing) state, changes its
96 //! state to #kStateValid and returns the previous (#kStateInvalid) state.
97 //! Otherwise, returns the object’s current state.
98 //!
99 //! Consumers of this class should not call this method. Use the
100 //! INITIALIZATION_STATE_SET_VALID() macro instead.
101 State SetValid();
102
103 private:
104 DISALLOW_COPY_AND_ASSIGN(InitializationStateDcheck);
105};
106
107// Using macros enables the non-DCHECK no-op implementation below to be more
108// compact and less intrusive. These are macros instead of methods that call
109// DCHECK to enable the DCHECK failure message to point to the correct file and
110// line number, and to allow additional messages to be streamed on failure with
111// the << operator.
112
113//! \brief Checks that a crashpad::InitializationStateDcheck object is in the
114//! crashpad::InitializationState::kStateUninitialized state, and changes
115//! its state to initializing
116//! (crashpad::InitializationState::kStateInvalid).
117//!
118//! If the object is not in the correct state, a DCHECK assertion is triggered
119//! and the object’s state remains unchanged.
120//!
121//! \param[in] initialization_state_dcheck A crashpad::InitializationStateDcheck
122//! object.
123//!
124//! \sa crashpad::InitializationStateDcheck
125#define INITIALIZATION_STATE_SET_INITIALIZING(initialization_state_dcheck) \
126 DCHECK_EQ((initialization_state_dcheck).SetInitializing(), \
127 (initialization_state_dcheck).kStateUninitialized)
128
129//! \brief Checks that a crashpad::InitializationStateDcheck object is in the
130//! initializing (crashpad::InitializationState::kStateInvalid) state, and
131//! changes its state to crashpad::InitializationState::kStateValid.
132//!
133//! If the object is not in the correct state, a DCHECK assertion is triggered
134//! and the object’s state remains unchanged.
135//!
136//! \param[in] initialization_state_dcheck A crashpad::InitializationStateDcheck
137//! object.
138//!
139//! \sa crashpad::InitializationStateDcheck
140#define INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck) \
141 DCHECK_EQ((initialization_state_dcheck).SetValid(), \
142 (initialization_state_dcheck).kStateInvalid)
143
144//! \brief Checks that a crashpad::InitializationStateDcheck object is in the
145//! crashpad::InitializationState::kStateValid state.
146//!
147//! If the object is not in the correct state, a DCHECK assertion is triggered.
148//!
149//! \param[in] initialization_state_dcheck A crashpad::InitializationStateDcheck
150//! object.
151//!
152//! \sa crashpad::InitializationStateDcheck
153#define INITIALIZATION_STATE_DCHECK_VALID(initialization_state_dcheck) \
154 DCHECK_EQ((initialization_state_dcheck).state(), \
155 (initialization_state_dcheck).kStateValid)
156
157#else
158
159#if defined(COMPILER_MSVC)
160// bool[0] (below) is not accepted by MSVC.
161struct InitializationStateDcheck {
162};
163#else
164// Since this is to be used as a DCHECK (for debugging), it should be
165// non-intrusive in non-DCHECK (non-debug, release) builds. An empty struct
166// would still have a nonzero size (rationale:
167// http://www.stroustrup.com/bs_faq2.html#sizeof-empty). Zero-length arrays are
168// technically invalid according to the standard, but clang and g++ accept them
169// without complaint even with warnings turned up. They take up no space at all,
170// and they can be “initialized” with the same () syntax used to initialize
171// objects of the DCHECK_IS_ON() InitializationStateDcheck class above.
172using InitializationStateDcheck = bool[0];
173#endif // COMPILER_MSVC
174
175// Avoid triggering warnings by repurposing these macros when DCHECKs are
176// disabled.
177#define INITIALIZATION_STATE_SET_INITIALIZING(initialization_state_dcheck) \
178 ALLOW_UNUSED_LOCAL(initialization_state_dcheck)
179#define INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck) \
180 ALLOW_UNUSED_LOCAL(initialization_state_dcheck)
181#define INITIALIZATION_STATE_DCHECK_VALID(initialization_state_dcheck) \
182 ALLOW_UNUSED_LOCAL(initialization_state_dcheck)
183
184#endif
185
186} // namespace crashpad
187
188#endif // CRASHPAD_UTIL_MISC_INITIALIZATION_INITIALIZATION_STATE_DCHECK_H_