[cfe-commits] r83133 - in /cfe/trunk: lib/Sema/SemaExprCXX.cpp test/SemaCXX/overloaded-operator.cpp

John McCall rjmccall at apple.com
Tue Sep 29 18:01:30 PDT 2009


Author: rjmccall
Date: Tue Sep 29 20:01:30 2009
New Revision: 83133

URL: http://llvm.org/viewvc/llvm-project?rev=83133&view=rev
Log:
Detect operator-> chains of arbitrary length.  Use a terrible data structure
to strike fear into the hearts of CPUs everywhere.


Modified:
    cfe/trunk/lib/Sema/SemaExprCXX.cpp
    cfe/trunk/test/SemaCXX/overloaded-operator.cpp

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Tue Sep 29 20:01:30 2009
@@ -1977,17 +1977,23 @@
   //   [...] When operator->returns, the operator-> is applied  to the value
   //   returned, with the original second operand.
   if (OpKind == tok::arrow) {
+    // The set of types we've considered so far.
+    llvm::SmallVector<CanQualType,8> CTypes;
+    CTypes.push_back(Context.getCanonicalType(BaseType));
+    
     while (BaseType->isRecordType()) {
       Base = BuildOverloadedArrowExpr(S, move(Base), BaseExpr->getExprLoc());
       BaseExpr = (Expr*)Base.get();
       if (BaseExpr == NULL)
         return ExprError();
-      if (Context.getCanonicalType(BaseExpr->getType()) == 
-          Context.getCanonicalType(BaseType)) {
+      BaseType = BaseExpr->getType();
+      CanQualType CBaseType = Context.getCanonicalType(BaseType);
+      if (std::find(CTypes.begin(), CTypes.end(), CBaseType) != CTypes.end()) {
+        // TODO: note the chain of conversions
         Diag(OpLoc, diag::err_operator_arrow_circular);
         return ExprError();
       }
-      BaseType = BaseExpr->getType();
+      CTypes.push_back(CBaseType);
     }
   }
 

Modified: cfe/trunk/test/SemaCXX/overloaded-operator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/overloaded-operator.cpp?rev=83133&r1=83132&r2=83133&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/overloaded-operator.cpp (original)
+++ cfe/trunk/test/SemaCXX/overloaded-operator.cpp Tue Sep 29 20:01:30 2009
@@ -224,3 +224,19 @@
   AX a; 
   a->b = 0; // expected-error {{circular pointer delegation detected}}
 }
+
+struct CircA {
+  struct CircB& operator->();
+  int val;
+};
+struct CircB {
+  struct CircC& operator->();
+};
+struct CircC {
+  struct CircA& operator->();
+};
+
+void circ() {
+  CircA a;
+  a->val = 0; // expected-error {{circular pointer delegation detected}}
+}





More information about the cfe-commits mailing list