diff options
| author | Anders Carlsson <andersca@mac.com> | 2011-05-06 14:25:31 +0000 |
|---|---|---|
| committer | Anders Carlsson <andersca@mac.com> | 2011-05-06 14:25:31 +0000 |
| commit | 47061ee5bc64f7621838ad954e6e887cd95d0721 (patch) | |
| tree | d336b48e430a8aa136841316cda7553913f7844b /clang | |
| parent | 5fe4a7dc9619249a34388f7d25d2a4ac4dbcf5d9 (diff) | |
| download | bcm5719-llvm-47061ee5bc64f7621838ad954e6e887cd95d0721.tar.gz bcm5719-llvm-47061ee5bc64f7621838ad954e6e887cd95d0721.zip | |
Warn when trying to call a pure virtual member function in a class from the class constructor/destructor. Fixes PR7966.
llvm-svn: 130982
Diffstat (limited to 'clang')
| -rw-r--r-- | clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 | ||||
| -rw-r--r-- | clang/lib/Sema/SemaOverload.cpp | 13 | ||||
| -rw-r--r-- | clang/test/SemaCXX/warn-pure-virtual-call-from-ctor-dtor.cpp | 7 |
3 files changed, 24 insertions, 0 deletions
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 8b73f901265..6d90ba17c61 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -728,6 +728,10 @@ def err_implicit_object_parameter_init : Error< def err_qualified_member_of_unrelated : Error< "%q0 is not a member of class %1">; +def warn_call_to_pure_virtual_member_function_from_ctor_dtor : Warning< + "call to pure virtual member function %0; overrides of %0 in subclasses are " + "not available in the %select{constructor|destructor}1 of %2">; + def note_field_decl : Note<"member is declared here">; def note_ivar_decl : Note<"ivar is declared here">; def note_bitfield_decl : Note<"bit-field is declared here">; diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 79caecc5a83..64f04de87aa 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -8781,6 +8781,19 @@ Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, if (CheckFunctionCall(Method, TheCall)) return ExprError(); + if ((isa<CXXConstructorDecl>(CurContext) || + isa<CXXDestructorDecl>(CurContext)) && + TheCall->getMethodDecl()->isPure()) { + const CXXMethodDecl *MD = TheCall->getMethodDecl(); + + if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) + Diag(MemExpr->getLocStart(), + diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) + << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) + << MD->getParent()->getDeclName(); + + Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); + } return MaybeBindToTemporary(TheCall); } diff --git a/clang/test/SemaCXX/warn-pure-virtual-call-from-ctor-dtor.cpp b/clang/test/SemaCXX/warn-pure-virtual-call-from-ctor-dtor.cpp new file mode 100644 index 00000000000..698eccd1d2f --- /dev/null +++ b/clang/test/SemaCXX/warn-pure-virtual-call-from-ctor-dtor.cpp @@ -0,0 +1,7 @@ +// RUN: %clang_cc1 %s -fsyntax-only -verify +struct A { + A() { f(); } // expected-warning {{call to pure virtual member function 'f'; overrides of 'f' in subclasses are not available in the constructor of 'A'}} + ~A() { f(); } // expected-warning {{call to pure virtual member function 'f'; overrides of 'f' in subclasses are not available in the destructor of 'A'}} + + virtual void f() = 0; // expected-note 2 {{'f' declared here}} +}; |

