]> granicus.if.org Git - clang/commitdiff
Diagnose use of 'this' in a C++ default argument. Thanks to Eli for correcting my...
authorDouglas Gregor <dgregor@apple.com>
Tue, 4 Nov 2008 13:41:56 +0000 (13:41 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 4 Nov 2008 13:41:56 +0000 (13:41 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@58691 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticKinds.def
lib/Sema/SemaDeclCXX.cpp
test/SemaCXX/default2.cpp

index 3ce3d8d9a257da58926971f786f26ee275c0ef80..4b93539e4b300e3b6e50af857fb0789d792c2ab0 100644 (file)
@@ -812,6 +812,8 @@ DIAG(err_param_default_argument_references_param, ERROR,
      "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")
index 4a2f0b43f63db6d37a7035bb664f34292015850f..bd3da49030e63052d04a48a9c8cf583087bd7ad0 100644 (file)
@@ -46,6 +46,7 @@ namespace {
 
     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 @@ namespace {
                        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;
   }
 }
index 89eb4671d9326acb65d1a72f8be653bba7e830b3..d55045df78b63d2e8422c41c4e08b9d924b007d8 100644 (file)
@@ -39,4 +39,8 @@ void nondecl(int (*f)(int x = 5)) // {expected-error {{default arguments can onl
 
 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'}}
+  }
 };