clm971910 / googletest

Automatically exported from code.google.com/p/googletest
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

gtest cause warnings under gcc 4.5.1 #322

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hello, I'm using gtest,

Waning:
Test.cpp.cpp:45770:139: warning: converting ‘false’ to pointer type for 
argument 1 of ‘char 
testing::internal::IsNullLiteralHelper(testing::internal::Secret*)’

source line:
EXPECT_EQ(false, bdb->ExistsAttr(NULL, "/xspace/abc/cccc", "abc"));

/// I know it can be rewritten to EXPECT_FALSE(bdb->ExistsAttr(NULL, 
"/xspace/abc/cccc", "abc"));, but it's not the point.

after proprocessing:
switch (0) case 0:
    if (const ::testing::AssertionResult gtest_ar = (::testing::internal:: EqHelper<(sizeof(::testing::internal::IsNullLiteralHelper
(false)) == 1)>::Compare("false", "bdb->ExistsAttr(__null, 
\"/xspace/abc/bbbb\", \"abc\")", false, bdb->ExistsAttr(__null, "/xspace/
abc/bbbb", "abc")))) ;
    else ::testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "TestBdb.cpp", 146, gtest_ar.failure_message
()) = ::testing::Message();
}

relatived gtest code:

// Two overloaded helpers for checking at compile time whether an
// expression is a null pointer literal (i.e. NULL or any 0-valued
// compile-time integral constant).  Their return values have
// different sizes, so we can use sizeof() to test which version is
// picked by the compiler.  These helpers have no implementations, as
// we only need their signatures.
//
// Given IsNullLiteralHelper(x), the compiler will pick the first
// version if x can be implicitly converted to Secret*, and pick the
// second version otherwise.  Since Secret is a secret and incomplete
// type, the only expression a user can write that has type Secret* is
// a null pointer literal.  Therefore, we know that x is a null
// pointer literal if and only if the first version is picked by the
// compiler.
char IsNullLiteralHelper(Secret* p);
char (&IsNullLiteralHelper(...))[2];  // NOLINT

Thanks a lot.

Original issue reported on code.google.com by chen3feng on 13 Oct 2010 at 1:57

GoogleCodeExporter commented 9 years ago
This is a GCC bug.  gtest isn't trying to actually covert false to a pointer.  
It just uses a Template Meta Programming trick that's blessed by the C++ 
standard to detect NULL literals.

Also, the canonical way to write your assertion is EXPECT_FALSE(...).  That 
will avoid the warning.

Original comment by w...@google.com on 29 Oct 2010 at 4:56

GoogleCodeExporter commented 9 years ago
Hi,

And what is the canonical way to rewrite:
EXPECT_EQ(DEBUG_MODE != 0, DLOG_IS_ON(INFO));
?
The problem prevents me compiling Chromium with gcc 4.5.2
TIA

Original comment by dvyu...@google.com on 6 Apr 2011 at 6:42

GoogleCodeExporter commented 9 years ago
Please send questions to the mailing list instead of here.  Thanks.

Original comment by w...@google.com on 6 Apr 2011 at 6:47

GoogleCodeExporter commented 9 years ago
For the record, there is a really trivial fix (to gtest) for this issue... add 
this additional overload:

char (&IsNullLiteralHelper(bool))[2];

Original comment by matthew....@kitware.com on 12 Feb 2013 at 7:19