[cfe-commits] r74937 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExprCXX.cpp test/SemaCXX/type-traits-incomplete.cpp

AlisdairM(public) public at alisdairm.net
Tue Jul 7 12:30:03 PDT 2009


We might want to take further comments over to the dev list.

I've been down the route of type traits intrinsics in the past, so a couple of general observations:

i/ some type traits that are computed with metaprogramming can be more efficiently implemented as traits in the compiler.  This pays off in code trafficking heavily in type traits (i.e. you will see good performance from Boost tests, less spectacular in 'typical' code base)

Good candidates here are awkward traits like is_function and is_member_function.  Typically, any trait diagnosed by a long list of things it isn't!

Other good candidates are things like is_const or is_lvalue_reference where a tight implementation is simply checking a single bit of the IdentifierInfo.  Again, a single primary template leads to neat code and low overheads.

ii/ some traits that don't *require* compiler support are better served by it.

A good example here is is_integral.  If we plan to support extended integral types in the future, it is easier if the compiler trait is updated as the type goes in, than co-ordinating with library issues.

Note that many of the traits in category (i) and (ii) do not need complete types. Any incomplete type automatically yields a false value as they do not support class types, void or arrays of unknown size (which are the only possible incomplete types)

iii/ Further traits require compiler support but this is not widely understood yet.

A good example for this is_convertible, where degenerate use cases will stumble over private or deleted conversion operations which yield a in instant error rather than a SFINAE condition.

You will not the "complete type" requirements of is_convertible are probably the most subtle of them all.

AlisdairM

> -----Original Message-----
> From: cfe-commits-bounces at cs.uiuc.edu [mailto:cfe-commits-
> bounces at cs.uiuc.edu] On Behalf Of Anders Carlsson
> Sent: 07 July 2009 20:06
> To: cfe-commits at cs.uiuc.edu
> Subject: [cfe-commits] r74937 - in /cfe/trunk:
> include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExprCXX.cpp
> test/SemaCXX/type-traits-incomplete.cpp
> 
> Author: andersca
> Date: Tue Jul  7 14:06:02 2009
> New Revision: 74937
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=74937&view=rev
> Log:
> Some (most) type trait expressions require that the argument passed in
> is a complete type.
> 
> Added:
>     cfe/trunk/test/SemaCXX/type-traits-incomplete.cpp
> Modified:
>     cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
>     cfe/trunk/lib/Sema/SemaExprCXX.cpp
> 
> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> URL: http://llvm.org/viewvc/llvm-
> project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=74937&
> r1=74936&r2=74937&view=diff
> 
> =======================================================================
> =======
> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Jul  7
> 14:06:02 2009
> @@ -1549,6 +1549,9 @@
>  def warn_unused_expr : Warning<"expression result unused">,
>    InGroup<UnusedValue>;
> 
> +def err_incomplete_type_used_in_type_trait_expr : Error<
> +  "incomplete type %0 used in type trait expression">;
> +
>  // inline asm.
>  def err_asm_wide_character : Error<"wide string is invalid in 'asm'">;
>  def err_asm_invalid_lvalue_in_output : Error<"invalid lvalue in asm
> output">;
> 
> Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
> URL: http://llvm.org/viewvc/llvm-
> project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=74937&r1=74936&r2=74937&
> view=diff
> 
> =======================================================================
> =======
> --- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Tue Jul  7 14:06:02 2009
> @@ -1023,17 +1023,23 @@
>                                                   SourceLocation
> LParen,
>                                                   TypeTy *Ty,
>                                                   SourceLocation
> RParen) {
> -  // FIXME: Some of the type traits have requirements. Interestingly,
> only the
> -  // __is_base_of requirement is explicitly stated to be diagnosed.
> Indeed, G++
> -  // accepts __is_pod(Incomplete) without complaints, and claims that
> the type
> -  // is indeed a POD.
> +  QualType T = QualType::getFromOpaquePtr(Ty);
> +
> +  // According to http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html
> +  // all traits except __is_class, __is_enum and __is_union require a
> the type
> +  // to be complete.
> +  if (OTT != UTT_IsClass && OTT != UTT_IsEnum && OTT != UTT_IsUnion) {
> +    if (RequireCompleteType(KWLoc, T,
> +
> diag::err_incomplete_type_used_in_type_trait_expr,
> +                            SourceRange(), SourceRange(), T))
> +      return ExprError();
> +  }
> 
>    // There is no point in eagerly computing the value. The traits are
> designed
>    // to be used from type trait templates, so Ty will be a template
> parameter
>    // 99% of the time.
> -  return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, OTT,
> -                                      QualType::getFromOpaquePtr(Ty),
> -                                      RParen, Context.BoolTy));
> +  return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, OTT, T,
> +                                                RParen,
> Context.BoolTy));
>  }
> 
>  QualType Sema::CheckPointerToMemberOperands(
> 
> Added: cfe/trunk/test/SemaCXX/type-traits-incomplete.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/type-
> traits-incomplete.cpp?rev=74937&view=auto
> 
> =======================================================================
> =======
> --- cfe/trunk/test/SemaCXX/type-traits-incomplete.cpp (added)
> +++ cfe/trunk/test/SemaCXX/type-traits-incomplete.cpp Tue Jul  7
> 14:06:02 2009
> @@ -0,0 +1,7 @@
> +// RUN: clang-cc -fsyntax-only -verify %s
> +
> +struct S; // expected-note{{forward declaration of 'struct S'}}
> +
> +void f() {
> +  __is_pod(S); // expected-error{{incomplete type 'struct S' used in
> type trait expression}}
> +}
> 
> 
> _______________________________________________
> 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