[cfe-commits] r92045 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExprCXX.cpp test/SemaCXX/typeid.cpp test/SemaTemplate/instantiate-expr-4.cpp

Douglas Gregor dgregor at apple.com
Wed Dec 23 13:06:06 PST 2009


Author: dgregor
Date: Wed Dec 23 15:06:06 2009
New Revision: 92045

URL: http://llvm.org/viewvc/llvm-project?rev=92045&view=rev
Log:
Diagnose the use of incomplete types in C++ typeid expressions

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExprCXX.cpp
    cfe/trunk/test/SemaCXX/typeid.cpp
    cfe/trunk/test/SemaTemplate/instantiate-expr-4.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=92045&r1=92044&r2=92045&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Dec 23 15:06:06 2009
@@ -1830,6 +1830,7 @@
 // Other C++ expressions
 def err_need_header_before_typeid : Error<
   "you need to include <typeinfo> before using the 'typeid' operator">;
+def err_incomplete_typeid : Error<"'typeid' of incomplete type %0">;
 def err_static_illegal_in_new : Error<
   "the 'static' modifier for the array size is not legal in new expressions">;
 def err_array_new_needs_size : Error<

Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=92045&r1=92044&r2=92045&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Wed Dec 23 15:06:06 2009
@@ -37,7 +37,22 @@
     //   that is the operand of typeid are always ignored.
     // FIXME: Preserve type source info.
     // FIXME: Preserve the type before we stripped the cv-qualifiers?
-    TyOrExpr =GetTypeFromParser(TyOrExpr).getUnqualifiedType().getAsOpaquePtr();
+    QualType T = GetTypeFromParser(TyOrExpr);
+    if (T.isNull())
+      return ExprError();
+    
+    // C++ [expr.typeid]p4:
+    //   If the type of the type-id is a class type or a reference to a class 
+    //   type, the class shall be completely-defined.
+    QualType CheckT = T;
+    if (const ReferenceType *RefType = CheckT->getAs<ReferenceType>())
+      CheckT = RefType->getPointeeType();
+    
+    if (CheckT->getAs<RecordType>() &&
+        RequireCompleteType(OpLoc, CheckT, diag::err_incomplete_typeid))
+      return ExprError();
+    
+    TyOrExpr = T.getUnqualifiedType().getAsOpaquePtr();
   }
 
   IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info");
@@ -66,7 +81,8 @@
           // C++ [expr.typeid]p3:
           //   [...] If the type of the expression is a class type, the class
           //   shall be completely-defined.
-          // FIXME: implement this!
+          if (RequireCompleteType(OpLoc, T, diag::err_incomplete_typeid))
+            return ExprError();
         }
       }
 

Modified: cfe/trunk/test/SemaCXX/typeid.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/typeid.cpp?rev=92045&r1=92044&r2=92045&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/typeid.cpp (original)
+++ cfe/trunk/test/SemaCXX/typeid.cpp Wed Dec 23 15:06:06 2009
@@ -14,3 +14,11 @@
 {
   (void)typeid(int);
 }
+
+struct X; // expected-note 3{{forward declaration}}
+
+void g1(X &x) {
+  (void)typeid(X); // expected-error{{'typeid' of incomplete type 'struct X'}}
+  (void)typeid(X&); // expected-error{{'typeid' of incomplete type 'struct X'}}
+  (void)typeid(x); // expected-error{{'typeid' of incomplete type 'struct X'}}
+}

Modified: cfe/trunk/test/SemaTemplate/instantiate-expr-4.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiate-expr-4.cpp?rev=92045&r1=92044&r2=92045&view=diff

==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiate-expr-4.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiate-expr-4.cpp Wed Dec 23 15:06:06 2009
@@ -133,7 +133,7 @@
   }
 };
 
-struct Incomplete; // expected-note{{forward}}
+struct Incomplete; // expected-note 2{{forward}}
 
 template struct Throw1<int>;
 template struct Throw1<int*>;
@@ -143,7 +143,6 @@
 // typeid expressions
 // ---------------------------------------------------------------------
 
-// FIXME: This should really include <typeinfo>, but we don't have that yet.
 namespace std {
   class type_info;
 }
@@ -154,7 +153,7 @@
     if (ptr)
       return typeid(ptr);
     else
-      return typeid(T);
+      return typeid(T); // expected-error{{'typeid' of incomplete type 'struct Incomplete'}}
   }
 };
 
@@ -163,7 +162,7 @@
 };
 
 template struct TypeId0<int>;
-template struct TypeId0<Incomplete>;
+template struct TypeId0<Incomplete>; // expected-note{{instantiation of member function}}
 template struct TypeId0<Abstract>;
 
 // ---------------------------------------------------------------------





More information about the cfe-commits mailing list