[cfe-commits] r58691 - in /cfe/trunk: include/clang/Basic/DiagnosticKinds.def lib/Sema/SemaDeclCXX.cpp test/SemaCXX/default2.cpp

Douglas Gregor doug.gregor at gmail.com
Tue Nov 4 05:41:57 PST 2008


Author: dgregor
Date: Tue Nov  4 07:41:56 2008
New Revision: 58691

URL: http://llvm.org/viewvc/llvm-project?rev=58691&view=rev
Log:
Diagnose use of 'this' in a C++ default argument. Thanks to Eli for correcting my bogus assertion about it already being handled

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/test/SemaCXX/default2.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=58691&r1=58690&r2=58691&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Tue Nov  4 07:41:56 2008
@@ -812,6 +812,8 @@
      "default argument references parameter '%0'")
 DIAG(err_param_default_argument_references_local, ERROR,
      "default argument references local variable '%0' of enclosing function")
+DIAG(err_param_default_argument_references_this, ERROR,
+     "default argument references 'this'")
 DIAG(err_param_default_argument_nonfunc, ERROR,
      "default arguments can only be specified for parameters in a function"
      " declaration")

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Tue Nov  4 07:41:56 2008
@@ -46,6 +46,7 @@
 
     bool VisitExpr(Expr *Node);
     bool VisitDeclRefExpr(DeclRefExpr *DRE);
+    bool VisitPredefinedExpr(PredefinedExpr *PE);
   };
 
   /// VisitExpr - Visit all of the children of this expression.
@@ -84,14 +85,20 @@
                        VDecl->getName(), DefaultArg->getSourceRange());
     }
 
-    // C++ [dcl.fct.default]p8:
-    //   The keyword this shall not be used in a default argument of a
-    //   member function.
-    // Note: this requirement is already diagnosed by
-    // Sema::ActOnCXXThis, because the use of "this" inside a default
-    // argument doesn't occur inside the body of a non-static member
-    // function.
+    return false;
+  }
 
+  /// VisitPredefinedExpr - Visit a predefined expression, which could
+  /// refer to "this".
+  bool CheckDefaultArgumentVisitor::VisitPredefinedExpr(PredefinedExpr *PE) {
+    if (PE->getIdentType() == PredefinedExpr::CXXThis) {
+      // C++ [dcl.fct.default]p8:
+      //   The keyword this shall not be used in a default argument of a
+      //   member function.
+      return S->Diag(PE->getSourceRange().getBegin(),
+                     diag::err_param_default_argument_references_this,
+                     PE->getSourceRange());
+    }
     return false;
   }
 }

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

==============================================================================
--- cfe/trunk/test/SemaCXX/default2.cpp (original)
+++ cfe/trunk/test/SemaCXX/default2.cpp Tue Nov  4 07:41:56 2008
@@ -39,4 +39,8 @@
 
 class X {
   void f(X* x = this); // expected-error{{invalid use of 'this' outside of a nonstatic member function}}
+
+  void g() { 
+    int f(X* x = this); // expected-error{{default argument references 'this'}}
+  }
 };





More information about the cfe-commits mailing list