[cfe-commits] r148874 - in /cfe/trunk: lib/Sema/SemaExprCXX.cpp test/Analysis/reference.cpp

Nico Weber thakis at chromium.org
Sat Jan 28 22:38:28 PST 2012


On Sat, Jan 28, 2012 at 4:50 PM, Chandler Carruth <chandlerc at google.com> wrote:
> qualifying the pointer with volatile works, and is only a touch awkward.
> We've already done this for one copy, I'll update the main open source
> version as soon as I'm able.

You mean like

template <typename T>
inline T Invalid() {
  return *static_cast<volatile typename remove_reference<T>::type*>(0);
}

? If I then pass a type with a default copy constructor (say "class A
{}; Invalid<A>();"), I get this error:

test.cc:11:10: error: no matching constructor for initialization of 'A'
  return *static_cast<volatile typename remove_reference<T>::type*>(0);
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.cc:22:3: note: in instantiation of function template
specialization 'Invalid<A>' requested here
  Invalid<A>();
  ^
test.cc:16:7: note: candidate constructor (the implicit copy
constructor) not viable: 1st argument ('volatile typename
remove_reference<A>::type' (aka 'volatile A')) would lose volatile
qualifier
class A {
      ^
test.cc:16:7: note: candidate constructor (the implicit default
constructor) not viable: requires 0 arguments, but 1 was provided
1 error generated.

>
> On Jan 28, 2012 4:21 PM, "Nico Weber" <thakis at chromium.org> wrote:
>>
>> Hi Eli,
>>
>> with this change, clang now warns about this function in gtest (which is
>> fine):
>>
>> // Invalid<T>() returns an invalid value of type T.  This is useful
>> // when a value of type T is needed for compilation, but the statement
>> // will not really be executed (or we don't care if the statement
>> // crashes).
>> template <typename T>
>> inline T Invalid() {
>>  return *static_cast<typename remove_reference<T>::type*>(NULL);
>> }
>>
>> The warning recommends to add a "volatile", but then code that calls
>> this function with a type without copy constructor fails to compile.
>> What's the recommended way to change code like this?
>>
>> Should the -Wnull-dereference diagnostic text be changed to something
>> else in an expression context? It currently says "consider using
>> __builtin_trap() or qualifying pointer with 'volatile'".
>>
>> Thanks,
>> Nico
>>
>> On Tue, Jan 24, 2012 at 2:51 PM, Eli Friedman <eli.friedman at gmail.com>
>> wrote:
>> > Author: efriedma
>> > Date: Tue Jan 24 16:51:26 2012
>> > New Revision: 148874
>> >
>> > URL: http://llvm.org/viewvc/llvm-project?rev=148874&view=rev
>> > Log:
>> > Switch PerformImplicitConversion over to use DefaultLvalueConversion for
>> > lvalue-to-rvalue conversion.
>> >
>> >
>> > Modified:
>> >    cfe/trunk/lib/Sema/SemaExprCXX.cpp
>> >    cfe/trunk/test/Analysis/reference.cpp
>> >
>> > Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
>> > URL:
>> > http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=148874&r1=148873&r2=148874&view=diff
>> >
>> > ==============================================================================
>> > --- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
>> > +++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Tue Jan 24 16:51:26 2012
>> > @@ -2324,12 +2324,14 @@
>> >     // Nothing to do.
>> >     break;
>> >
>> > -  case ICK_Lvalue_To_Rvalue:
>> > +  case ICK_Lvalue_To_Rvalue: {
>> >     assert(From->getObjectKind() != OK_ObjCProperty);
>> >     FromType = FromType.getUnqualifiedType();
>> > -    From = ImplicitCastExpr::Create(Context, FromType,
>> > CK_LValueToRValue,
>> > -                                    From, 0, VK_RValue);
>> > +    ExprResult FromRes = DefaultLvalueConversion(From);
>> > +    assert(!FromRes.isInvalid() && "Can't perform deduced
>> > conversion?!");
>> > +    From = FromRes.take();
>> >     break;
>> > +  }
>> >
>> >   case ICK_Array_To_Pointer:
>> >     FromType = Context.getArrayDecayedType(FromType);
>> >
>> > Modified: cfe/trunk/test/Analysis/reference.cpp
>> > URL:
>> > http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/reference.cpp?rev=148874&r1=148873&r2=148874&view=diff
>> >
>> > ==============================================================================
>> > --- cfe/trunk/test/Analysis/reference.cpp (original)
>> > +++ cfe/trunk/test/Analysis/reference.cpp Tue Jan 24 16:51:26 2012
>> > @@ -1,4 +1,4 @@
>> > -// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.core
>> > -analyzer-store=region -analyzer-constraints=range -verify %s
>> > +// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.core
>> > -analyzer-store=region -analyzer-constraints=range -verify
>> > -Wno-null-dereference %s
>> >  // XFAIL
>> >
>> >  typedef typeof(sizeof(int)) size_t;
>> >
>> >
>> > _______________________________________________
>> > cfe-commits mailing list
>> > cfe-commits at cs.uiuc.edu
>> > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>>
>> _______________________________________________
>> cfe-commits mailing list
>> cfe-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits




More information about the cfe-commits mailing list