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

Anders Carlsson andersca at mac.com
Tue Jul 7 12:06:26 PDT 2009


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}}
+}





More information about the cfe-commits mailing list