Skip to content

Commit 0320f51

Browse files
derekmaurocopybara-github
authored andcommitted
Work around a maybe-uninitialized warning under GCC 12
Some Mock constructors insert the pointer to the Mock itself into a global registry. Since GCC cannot see how the pointer is used (only as an identifier), it cannot tell that the object doesn't need to be initialized at that point at all. Work around this by using uintptr_t instead. PiperOrigin-RevId: 452380347 Change-Id: Ia5a493057ed90719de1d0efab71de9a8a08ddf8b
1 parent 548b13d commit 0320f51

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

googlemock/include/gmock/gmock-nice-strict.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
6464
#define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
6565

66+
#include <cstdint>
6667
#include <type_traits>
6768

6869
#include "gmock/gmock-spec-builders.h"
@@ -109,25 +110,37 @@ constexpr bool HasStrictnessModifier() {
109110
template <typename Base>
110111
class NiceMockImpl {
111112
public:
112-
NiceMockImpl() { ::testing::Mock::AllowUninterestingCalls(this); }
113+
NiceMockImpl() {
114+
::testing::Mock::AllowUninterestingCalls(reinterpret_cast<uintptr_t>(this));
115+
}
113116

114-
~NiceMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
117+
~NiceMockImpl() {
118+
::testing::Mock::UnregisterCallReaction(reinterpret_cast<uintptr_t>(this));
119+
}
115120
};
116121

117122
template <typename Base>
118123
class NaggyMockImpl {
119124
public:
120-
NaggyMockImpl() { ::testing::Mock::WarnUninterestingCalls(this); }
125+
NaggyMockImpl() {
126+
::testing::Mock::WarnUninterestingCalls(reinterpret_cast<uintptr_t>(this));
127+
}
121128

122-
~NaggyMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
129+
~NaggyMockImpl() {
130+
::testing::Mock::UnregisterCallReaction(reinterpret_cast<uintptr_t>(this));
131+
}
123132
};
124133

125134
template <typename Base>
126135
class StrictMockImpl {
127136
public:
128-
StrictMockImpl() { ::testing::Mock::FailUninterestingCalls(this); }
137+
StrictMockImpl() {
138+
::testing::Mock::FailUninterestingCalls(reinterpret_cast<uintptr_t>(this));
139+
}
129140

130-
~StrictMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
141+
~StrictMockImpl() {
142+
::testing::Mock::UnregisterCallReaction(reinterpret_cast<uintptr_t>(this));
143+
}
131144
};
132145

133146
} // namespace internal

googlemock/include/gmock/gmock-spec-builders.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
6262
#define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
6363

64+
#include <cstdint>
6465
#include <functional>
6566
#include <map>
6667
#include <memory>
@@ -404,22 +405,22 @@ class GTEST_API_ Mock {
404405

405406
// Tells Google Mock to allow uninteresting calls on the given mock
406407
// object.
407-
static void AllowUninterestingCalls(const void* mock_obj)
408+
static void AllowUninterestingCalls(uintptr_t mock_obj)
408409
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
409410

410411
// Tells Google Mock to warn the user about uninteresting calls on
411412
// the given mock object.
412-
static void WarnUninterestingCalls(const void* mock_obj)
413+
static void WarnUninterestingCalls(uintptr_t mock_obj)
413414
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
414415

415416
// Tells Google Mock to fail uninteresting calls on the given mock
416417
// object.
417-
static void FailUninterestingCalls(const void* mock_obj)
418+
static void FailUninterestingCalls(uintptr_t mock_obj)
418419
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
419420

420421
// Tells Google Mock the given mock object is being destroyed and
421422
// its entry in the call-reaction table should be removed.
422-
static void UnregisterCallReaction(const void* mock_obj)
423+
static void UnregisterCallReaction(uintptr_t mock_obj)
423424
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex);
424425

425426
// Returns the reaction Google Mock will have on uninteresting calls

googlemock/src/gmock-spec-builders.cc

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <memory>
4242
#include <set>
4343
#include <string>
44+
#include <unordered_map>
4445
#include <vector>
4546

4647
#include "gmock/gmock.h"
@@ -534,57 +535,63 @@ MockObjectRegistry g_mock_object_registry;
534535

535536
// Maps a mock object to the reaction Google Mock should have when an
536537
// uninteresting method is called. Protected by g_gmock_mutex.
537-
std::map<const void*, internal::CallReaction> g_uninteresting_call_reaction;
538+
std::unordered_map<uintptr_t, internal::CallReaction>&
539+
UninterestingCallReactionMap() {
540+
static auto* map = new std::unordered_map<uintptr_t, internal::CallReaction>;
541+
return *map;
542+
}
538543

539544
// Sets the reaction Google Mock should have when an uninteresting
540545
// method of the given mock object is called.
541-
void SetReactionOnUninterestingCalls(const void* mock_obj,
546+
void SetReactionOnUninterestingCalls(uintptr_t mock_obj,
542547
internal::CallReaction reaction)
543548
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
544549
internal::MutexLock l(&internal::g_gmock_mutex);
545-
g_uninteresting_call_reaction[mock_obj] = reaction;
550+
UninterestingCallReactionMap()[mock_obj] = reaction;
546551
}
547552

548553
} // namespace
549554

550555
// Tells Google Mock to allow uninteresting calls on the given mock
551556
// object.
552-
void Mock::AllowUninterestingCalls(const void* mock_obj)
557+
void Mock::AllowUninterestingCalls(uintptr_t mock_obj)
553558
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
554559
SetReactionOnUninterestingCalls(mock_obj, internal::kAllow);
555560
}
556561

557562
// Tells Google Mock to warn the user about uninteresting calls on the
558563
// given mock object.
559-
void Mock::WarnUninterestingCalls(const void* mock_obj)
564+
void Mock::WarnUninterestingCalls(uintptr_t mock_obj)
560565
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
561566
SetReactionOnUninterestingCalls(mock_obj, internal::kWarn);
562567
}
563568

564569
// Tells Google Mock to fail uninteresting calls on the given mock
565570
// object.
566-
void Mock::FailUninterestingCalls(const void* mock_obj)
571+
void Mock::FailUninterestingCalls(uintptr_t mock_obj)
567572
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
568573
SetReactionOnUninterestingCalls(mock_obj, internal::kFail);
569574
}
570575

571576
// Tells Google Mock the given mock object is being destroyed and its
572577
// entry in the call-reaction table should be removed.
573-
void Mock::UnregisterCallReaction(const void* mock_obj)
578+
void Mock::UnregisterCallReaction(uintptr_t mock_obj)
574579
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
575580
internal::MutexLock l(&internal::g_gmock_mutex);
576-
g_uninteresting_call_reaction.erase(mock_obj);
581+
UninterestingCallReactionMap().erase(static_cast<uintptr_t>(mock_obj));
577582
}
578583

579584
// Returns the reaction Google Mock will have on uninteresting calls
580585
// made on the given mock object.
581586
internal::CallReaction Mock::GetReactionOnUninterestingCalls(
582587
const void* mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
583588
internal::MutexLock l(&internal::g_gmock_mutex);
584-
return (g_uninteresting_call_reaction.count(mock_obj) == 0)
589+
return (UninterestingCallReactionMap().count(
590+
reinterpret_cast<uintptr_t>(mock_obj)) == 0)
585591
? internal::intToCallReaction(
586592
GMOCK_FLAG_GET(default_mock_behavior))
587-
: g_uninteresting_call_reaction[mock_obj];
593+
: UninterestingCallReactionMap()[reinterpret_cast<uintptr_t>(
594+
mock_obj)];
588595
}
589596

590597
// Tells Google Mock to ignore mock_obj when checking for leaked mock

0 commit comments

Comments
 (0)