[PATCH] SemaChecking: return type validation for member functions
Saleem Abdulrasool
compnerd at compnerd.org
Sun Jan 5 22:52:31 PST 2014
Hi rsmith,
Perform additional validation on the return type of a CXXMemberDeclExpr.
Previously, we did not verify that the returned type is concrete if it were a
RecordDecl. If we are not dealing with a decltype expression, perform an
additional check to ensure that there are no unimplemented virtual methods in
the return type.
This addresses PR18393.
http://llvm-reviews.chandlerc.com/D2514
Files:
lib/Sema/SemaExprCXX.cpp
test/Analysis/PR18393.cpp
test/SemaCXX/conditional-expr.cpp
Index: lib/Sema/SemaExprCXX.cpp
===================================================================
--- lib/Sema/SemaExprCXX.cpp
+++ lib/Sema/SemaExprCXX.cpp
@@ -4946,6 +4946,13 @@
bool IsDecltype = ExprEvalContexts.back().IsDecltype;
CXXDestructorDecl *Destructor = IsDecltype ? 0 : LookupDestructor(RD);
+ if (!IsDecltype && RD->isAbstract()) {
+ Diag(E->getExprLoc(), diag::err_abstract_type_in_decl)
+ << Sema::AbstractReturnType << RT->desugar();
+ DiagnoseAbstractType(RD);
+ return Owned(E);
+ }
+
if (Destructor) {
MarkFunctionReferenced(E->getExprLoc(), Destructor);
CheckDestructorAccess(E->getExprLoc(), Destructor,
Index: test/Analysis/PR18393.cpp
===================================================================
--- /dev/null
+++ test/Analysis/PR18393.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// PR18393
+
+struct base {
+ virtual void method() = 0;
+ // expected-note at -1 {{unimplemented pure virtual method 'method' in 'base'}}
+};
+
+struct derived : base {
+ virtual void method();
+};
+
+struct holder {
+ holder() : d_() {}
+ base get() const { return d_; }
+ const derived d_;
+};
+
+void function(const base &);
+
+void test() {
+ holder h;
+ function(h.get());
+ // expected-error at -1 {{return type 'base' is an abstract class}}
+}
+
Index: test/SemaCXX/conditional-expr.cpp
===================================================================
--- test/SemaCXX/conditional-expr.cpp
+++ test/SemaCXX/conditional-expr.cpp
@@ -219,7 +219,9 @@
// *must* create a separate temporary copy of class objects. This can only
// be properly tested at runtime, though.
- const Abstract &a = true ? static_cast<const Abstract&>(Derived1()) : Derived2(); // expected-error {{allocating an object of abstract class type 'const Abstract'}}
+ const Abstract &a = true ? static_cast<const Abstract&>(Derived1()) : Derived2();
+ // expected-error at -1 {{allocating an object of abstract class type 'const Abstract'}}
+ // expected-error at -2 {{return type 'Abstract' is an abstract class}}
true ? static_cast<const Abstract&>(Derived1()) : throw 3; // expected-error {{allocating an object of abstract class type 'const Abstract'}}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2514.1.patch
Type: text/x-patch
Size: 2223 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140105/b73ec840/attachment.bin>
More information about the cfe-commits
mailing list