aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2008-11-04 13:41:56 +0000
committerDouglas Gregor <dgregor@apple.com>2008-11-04 13:41:56 +0000
commit3996f23ac20de411e0b5931a451bd05142f0b712 (patch)
treee411f6acf1ef0c3279242b866cebd3dd25ad0934
parent6215dee86c0e715b9f2b0d401ab2a5fcf629f1af (diff)
Diagnose use of 'this' in a C++ default argument. Thanks to Eli for correcting my bogus assertion about it already being handled
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@58691 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/Basic/DiagnosticKinds.def2
-rw-r--r--lib/Sema/SemaDeclCXX.cpp21
-rw-r--r--test/SemaCXX/default2.cpp4
3 files changed, 20 insertions, 7 deletions
diff --git a/include/clang/Basic/DiagnosticKinds.def b/include/clang/Basic/DiagnosticKinds.def
index 3ce3d8d9a2..4b93539e4b 100644
--- a/include/clang/Basic/DiagnosticKinds.def
+++ b/include/clang/Basic/DiagnosticKinds.def
@@ -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")
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index 4a2f0b43f6..bd3da49030 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -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;
}
}
diff --git a/test/SemaCXX/default2.cpp b/test/SemaCXX/default2.cpp
index 89eb4671d9..d55045df78 100644
--- a/test/SemaCXX/default2.cpp
+++ b/test/SemaCXX/default2.cpp
@@ -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'}}
+ }
};