[cfe-commits] r80802 - in /cfe/trunk: lib/Sema/SemaDeclCXX.cpp test/CodeGenCXX/trivial-constructor-init.cpp

Douglas Gregor dgregor at apple.com
Thu Sep 3 07:55:46 PDT 2009


On Sep 2, 2009, at 10:10 AM, Fariborz Jahanian wrote:

> Author: fjahanian
> Date: Wed Sep  2 12:10:17 2009
> New Revision: 80802
>
> URL: http://llvm.org/viewvc/llvm-project?rev=80802&view=rev
> Log:
> Allow null initialization of scalara data members
> in constructors's initializer list. pr4854
>
>
> Added:
>    cfe/trunk/test/CodeGenCXX/trivial-constructor-init.cpp
> Modified:
>    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
>
> Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=80802&r1=80801&r2=80802&view=diff
>
> =
> =
> =
> =
> =
> =
> =
> =
> ======================================================================
> --- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Wed Sep  2 12:10:17 2009
> @@ -786,11 +786,17 @@
>       C = PerformInitializationByConstructor(
>             FieldType, (Expr **)Args, NumArgs, IdLoc,
>             SourceRange(IdLoc, RParenLoc), Member->getDeclName(),  
> IK_Direct);
> -  } else if (NumArgs != 1) {
> +  } else if (NumArgs != 1 && NumArgs != 0) {
>     return Diag(IdLoc, diag::err_mem_initializer_mismatch)
>                 << Member->getDeclName() << SourceRange(IdLoc,  
> RParenLoc);
>   } else if (!HasDependentArg) {
> -    Expr *NewExp = (Expr*)Args[0];
> +    Expr *NewExp;
> +    if (NumArgs == 0) {
> +      NewExp = new (Context) CXXZeroInitValueExpr(FieldType, IdLoc,  
> RParenLoc);
> +      NumArgs = 1;

We need to check that we aren't trying to zero-initialize a reference  
type. For example, this code should be rejected (it currently causes  
an assert):

   struct X {
     X() : value() { }

     int &value;
   };

We have a similar issue with the implicitly-generated base  
initializer, where we silently accept this ill-fomed code:

   struct X {
     X() { }

     int &value;
   };

We do, however, get a truly beautiful diagnostic for the case where we  
implicitly define the default constructor:

   struct X {
     int &value;
   };

   X x;

/Users/dgregor/foo.cpp:5:3: error: cannot define the implicit default
       constructor for 'struct X', because reference member 'value'  
cannot be
       default-initialized
X x;
   ^
/Users/dgregor/foo.cpp:2:8: note: declared at
   int &value;
        ^
2 diagnostics generated.

:)

	- Doug



More information about the cfe-commits mailing list