Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
googletest
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
googletest
Commits
42bc671f
Commit
42bc671f
authored
May 23, 2017
by
Billy Donahue
Committed by
GitHub
May 23, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1091 from nico/wmicro
Fix -Wmicrosoft-cast warnings when using gtest with clang on Windows.
parents
59c795ce
b2cbbec0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
38 deletions
+51
-38
gtest-printers.h
googletest/include/gtest/gtest-printers.h
+51
-38
No files found.
googletest/include/gtest/gtest-printers.h
View file @
42bc671f
...
...
@@ -364,11 +364,18 @@ class UniversalPrinter;
template
<
typename
T
>
void
UniversalPrint
(
const
T
&
value
,
::
std
::
ostream
*
os
);
enum
DefaultPrinterType
{
kPrintContainer
,
kPrintPointer
,
kPrintFunctionPointer
,
kPrintOther
,
};
template
<
DefaultPrinterType
type
>
struct
WrapPrinterType
{};
// Used to print an STL-style container when the user doesn't define
// a PrintTo() for it.
template
<
typename
C
>
void
DefaultPrintTo
(
IsContainer
/* dummy */
,
false_type
/* is not a pointer */
,
void
DefaultPrintTo
(
WrapPrinterType
<
kPrintContainer
>
/* dummy */
,
const
C
&
container
,
::
std
::
ostream
*
os
)
{
const
size_t
kMaxCount
=
32
;
// The maximum number of elements to print.
*
os
<<
'{'
;
...
...
@@ -401,40 +408,38 @@ void DefaultPrintTo(IsContainer /* dummy */,
// implementation-defined. Therefore they will be printed as raw
// bytes.)
template
<
typename
T
>
void
DefaultPrintTo
(
IsNotContainer
/* dummy */
,
true_type
/* is a pointer */
,
void
DefaultPrintTo
(
WrapPrinterType
<
kPrintPointer
>
/* dummy */
,
T
*
p
,
::
std
::
ostream
*
os
)
{
if
(
p
==
NULL
)
{
*
os
<<
"NULL"
;
}
else
{
//
C++ doesn't allow casting from a function pointer to any object
//
pointer.
//
// IsTrue() silences warnings: "Condition is always true",
// "unreachable code".
if
(
IsTrue
(
ImplicitlyConvertible
<
T
*
,
const
void
*>::
value
))
{
// T is not a function type. We just call << to print p,
// relying on ADL to pick up user-defined << for their pointer
// types, if any.
*
os
<<
p
;
}
else
{
// T is a function type, so '*os << p' doesn't do what we want
// (it just prints p as bool). We want to print p as a cons
t
// void*. However, we cannot cast it to const void* directly,
// even using reinterpret_cast, as earlier versions of gcc
// (e.g. 3.4.5) cannot compile the cast when p is a function
// pointer. Casting to UInt64 first solves the problem.
*
os
<<
reinterpret_cast
<
const
void
*>
(
reinterpret_cast
<
internal
::
UInt64
>
(
p
));
}
//
T is not a function type. We just call << to print p,
//
relying on ADL to pick up user-defined << for their pointer
//
types, if any.
*
os
<<
p
;
}
}
template
<
typename
T
>
void
DefaultPrintTo
(
WrapPrinterType
<
kPrintFunctionPointer
>
/* dummy */
,
T
*
p
,
::
std
::
ostream
*
os
)
{
if
(
p
==
NULL
)
{
*
os
<<
"NULL"
;
}
else
{
// T is a function type, so '*os << p' doesn't do what we wan
t
// (it just prints p as bool). We want to print p as a const
// void*. However, we cannot cast it to const void* directly,
// even using reinterpret_cast, as earlier versions of gcc
// (e.g. 3.4.5) cannot compile the cast when p is a function
// pointer. Casting to UInt64 first solves the problem.
*
os
<<
reinterpret_cast
<
const
void
*>
(
reinterpret_cast
<
internal
::
UInt64
>
(
p
));
}
}
// Used to print a non-container, non-pointer value when the user
// doesn't define PrintTo() for it.
template
<
typename
T
>
void
DefaultPrintTo
(
IsNotContainer
/* dummy */
,
false_type
/* is not a pointer */
,
void
DefaultPrintTo
(
WrapPrinterType
<
kPrintOther
>
/* dummy */
,
const
T
&
value
,
::
std
::
ostream
*
os
)
{
::
testing_internal
::
DefaultPrintNonContainerTo
(
value
,
os
);
}
...
...
@@ -452,11 +457,8 @@ void DefaultPrintTo(IsNotContainer /* dummy */,
// wants).
template
<
typename
T
>
void
PrintTo
(
const
T
&
value
,
::
std
::
ostream
*
os
)
{
// DefaultPrintTo() is overloaded. The type of its first two
// arguments determine which version will be picked. If T is an
// STL-style container, the version for container will be called; if
// T is a pointer, the pointer version will be called; otherwise the
// generic version will be called.
// DefaultPrintTo() is overloaded. The type of its first argument
// determines which version will be picked.
//
// Note that we check for container types here, prior to we check
// for protocol message types in our operator<<. The rationale is:
...
...
@@ -468,13 +470,24 @@ void PrintTo(const T& value, ::std::ostream* os) {
// elements; therefore we check for container types here to ensure
// that our format is used.
//
// The second argument of DefaultPrintTo() is needed to bypass a bug
// in Symbian's C++ compiler that prevents it from picking the right
// overload between:
//
// PrintTo(const T& x, ...);
// PrintTo(T* x, ...);
DefaultPrintTo
(
IsContainerTest
<
T
>
(
0
),
is_pointer
<
T
>
(),
value
,
os
);
// Note that MSVC and clang-cl do allow an implicit conversion from
// pointer-to-function to pointer-to-object, but clang-cl warns on it.
// So don't use ImplicitlyConvertible if it can be helped since it will
// cause this warning, and use a separate overload of DefaultPrintTo for
// function pointers so that the `*os << p` in the object pointer overload
// doesn't cause that warning either.
DefaultPrintTo
(
WrapPrinterType
<
sizeof
(
IsContainerTest
<
T
>
(
0
))
==
sizeof
(
IsContainer
)
?
kPrintContainer
:
!
is_pointer
<
T
>::
value
?
kPrintOther
#if GTEST_LANG_CXX11
:
std
::
is_function
<
typename
std
::
remove_pointer
<
T
>::
type
>::
value
#else
:
!
internal
::
ImplicitlyConvertible
<
T
,
const
void
*>::
value
#endif
?
kPrintFunctionPointer
:
kPrintPointer
>
(),
value
,
os
);
}
// The following list of PrintTo() overloads tells
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment