tomzhang / googletest

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

clang and private "operator T*()" cause gtest based tests to fail to compile #442

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago

What steps will reproduce the problem?
1. Download clang-failure.cpp

2. Compile with gcc command, everything works.
---------------------------------------------------------------------
$ g++ clang-failure.cpp libgtest.a -lpthread
$
---------------------------------------------------------------------

3. Compile with clang++
---------------------------------------------------------------------
$ clang++ -lpthread clang-failure.cpp libgtest.a -lpthread
clang-failure.cpp:21:13: error: 'operator type-parameter-0-0 *' is a private 
member of 'A'
  EXPECT_EQ(A(), A());
  ~~~~~~~~~~^~~~~~~~~
/usr/include/gtest/gtest.h:1848:55: note: expanded from:
                      EqHelper<GTEST_IS_NULL_LITERAL_(expected)>::Compare, \
                                                      ^
/usr/include/gtest/internal/gtest-internal.h:162:54: note: expanded from:
    (sizeof(::testing::internal::IsNullLiteralHelper(x)) == 1)
                                                     ^
/usr/include/gtest/gtest_pred_impl.h:162:23: note: expanded from:
  GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_NONFATAL_FAILURE_)
                      ^
/usr/include/gtest/gtest_pred_impl.h:147:17: note: expanded from:
  GTEST_ASSERT_(pred_format(#v1, #v2, v1, v2),\
                ^
/usr/include/gtest/gtest_pred_impl.h:77:52: note: expanded from:
  if (const ::testing::AssertionResult gtest_ar = (expression)) \
                                                   ^~~~~~~~~~
clang-failure.cpp:7:26: note: declared private here
    template<typename T> operator T*(); // compile-time guard
                         ^
1 error generated.
$
---------------------------------------------------------------------

Problem appears to be caused by clang trying to convert the class to go via the 
varargs (which only accept POD).

A work around for this is to define;
---------------------------------------------------------------------
namespace testing {
namespace internal {

char (&IsNullLiteralHelper(const A&))[2];

}
}
---------------------------------------------------------------------

More information at;
https://code.google.com/p/googletest/source/browse/trunk/include/gtest/internal/
gtest-internal.h

---------------------------------------------------------------------
// 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

// A compile-time bool constant that is true if and only if x is a
// null pointer literal (i.e. NULL or any 0-valued compile-time
// integral constant).
#ifdef GTEST_ELLIPSIS_NEEDS_POD_
// We lose support for NULL detection where the compiler doesn't like
// passing non-POD classes through ellipsis (...).
# define GTEST_IS_NULL_LITERAL_(x) false
#else
# define GTEST_IS_NULL_LITERAL_(x) \
    (sizeof(::testing::internal::IsNullLiteralHelper(x)) == 1)
#endif  // GTEST_ELLIPSIS_NEEDS_POD_

---------------------------------------------------------------------

What version of Google Test are you using? On what operating system?
gtest 1.6.0-1ubuntu4, Linux Ubuntu Precise.

ii  clang                                                   3.0-6ubuntu3        
                                Low-Level Virtual Machine (LLVM), C language 
family frontend
ii  clang-format                                            1.191733.53539707   
                                clang-format
ii  clang-tools                                             3.2-1               
                                Clang tools
ii  libclang-common-dev                                     3.0-6ubuntu3        
                                clang library - Common development package
ii  libllvm3.0                                              3.0-4ubuntu1        
                                Low-Level Virtual Machine (LLVM), runtime 
library
ii  libllvm3.0:i386                                         3.0-4ubuntu1        
                                Low-Level Virtual Machine (LLVM), runtime 
library
ii  llvm-3.0                                                3.0-4ubuntu1        
                                Low-Level Virtual Machine (LLVM)
ii  llvm-3.0-dev                                            3.0-4ubuntu1        
                                Low-Level Virtual Machine (LLVM), libraries and 
headers
ii  llvm-3.0-runtime                                        3.0-4ubuntu1        
                                Low-Level Virtual Machine (LLVM), bytecode 
interpreter
ii  gcc-4.6                                                 4.6.3-1ubuntu5      
                                GNU C compiler
ii  gcc-4.6-base                                            4.6.3-1ubuntu5      
                                GCC, the GNU Compiler Collection (base package)
ii  gcc-4.6-base:i386                                       4.6.3-1ubuntu5      
                                GCC, the GNU Compiler Collection (base package)
ii  gcc-4.6-multilib                                        4.6.3-1ubuntu5      
                                GNU C compiler (multilib files)

$ gcc --version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3

$ clang --version
Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0)
Target: x86_64-pc-linux-gnu
Thread model: posix

Original issue reported on code.google.com by mit...@mithis.com on 10 Oct 2013 at 7:07

Attachments: