[cfe-commits] [Patch] Extend the self-reference on initialization warning to check constructor arguments as well.

Douglas Gregor dgregor at apple.com
Mon Aug 29 17:57:26 PDT 2011

On Aug 19, 2011, at 11:45 AM, Richard Trieu wrote:

> Extend the self-reference warning to catch when a constructor references itself upon initialization, such as using itself within its own copy constructor.
> struct S {};
> S s(s);

     void VisitExpr(Expr *E) {
       if (isa<ObjCMessageExpr>(*E)) return;
+      if (isRecordType) {
+        Expr *expr = E;
+        if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
+          ValueDecl *VD = ME->getMemberDecl();
+          if (isa<EnumConstantDecl>(VD) || isa<VarDecl>(VD)) return;
+          expr = ME->getBase();
+        }
+        if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(expr))
+          HandleDeclRefExpr(DRE);
+      }

Did you mean to have a "return" after the call to HandleDeclRefExpr? Otherwise, the call to Inherited::VisitExpr(E) will just end up recursing on the base anyway, without the benefit of the extra checking we're doing in this function. The same issue crops up in several other places… or am I missing something?

Otherwise, patch looks fine, but could you format

+        if (DeclRefExpr *DRE =
+            dyn_cast<DeclRefExpr>(E->getBase()->IgnoreParenImpCasts()))

with some better spacing on the second line, e.g.,

	if (DeclRefExpr *DRE
	      = dyn_cast<DeclRefExpr>(E->getBase()->IgnoreParenImpCasts()))

	- Doug

More information about the cfe-commits mailing list