From: Anders Carlsson Date: Fri, 9 Oct 2009 23:51:55 +0000 (+0000) Subject: Add CheckCallReturnType and start using it for regular call expressions. This will... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8c8d91917c307dc3ba4f60661377c745f2a6bef2;p=clang Add CheckCallReturnType and start using it for regular call expressions. This will improve error messages. For struct B; B f(); void g() { f(); } We now get t.cpp:6:3: error: calling 'f' with incomplete return type 'struct B' f(); ^~~ t.cpp:3:3: note: 'f' declared here B f(); ^ t.cpp:1:8: note: forward declaration of 'struct B' struct B; ^ git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@83692 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index ae2af6a0c5..ee1155ff20 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1770,7 +1770,11 @@ def err_block_decl_ref_not_modifiable_lvalue : Error< def err_typecheck_call_not_function : Error< "called object type %0 is not a function or function pointer">; def err_call_incomplete_return : Error< - "return type of called function (%0) is incomplete">; + "calling function with incomplete return type %0">; +def err_call_function_incomplete_return : Error< + "calling %0 with incomplete return type %1">; +def note_function_with_incomplete_return_type_declared_here : Note< + "%0 declared here">; def err_call_incomplete_argument : Error< "argument type %0 is incomplete">; def err_typecheck_call_too_few_args : Error< diff --git a/include/clang/Basic/PartialDiagnostic.h b/include/clang/Basic/PartialDiagnostic.h index d893dd3aef..e8cc564c8a 100644 --- a/include/clang/Basic/PartialDiagnostic.h +++ b/include/clang/Basic/PartialDiagnostic.h @@ -140,7 +140,7 @@ public: DeclarationName N); }; -inline PartialDiagnostic PDiag(unsigned DiagID) { +inline PartialDiagnostic PDiag(unsigned DiagID = 0) { return PartialDiagnostic(DiagID); } diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 33d7468787..6c9466f863 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -493,7 +493,10 @@ public: virtual TypeResult ActOnTypeName(Scope *S, Declarator &D); bool RequireCompleteType(SourceLocation Loc, QualType T, - const PartialDiagnostic &PD); + const PartialDiagnostic &PD, + std::pair Note = + std::make_pair(SourceLocation(), PDiag())); QualType getQualifiedNameType(const CXXScopeSpec &SS, QualType T); @@ -959,6 +962,12 @@ public: OwningExprResult BuildOverloadedArrowExpr(Scope *S, ExprArg Base, SourceLocation OpLoc); + /// CheckCallReturnType - Checks that a call expression's return type is + /// complete. Returns true on failure. The location passed in is the location + /// that best represents the call. + bool CheckCallReturnType(QualType ReturnType, SourceLocation Loc, + CallExpr *CE, FunctionDecl *FD); + /// Helpers for dealing with blocks and functions. void CheckFallThroughForFunctionDef(Decl *D, Stmt *Body); void CheckFallThroughForBlock(QualType BlockTy, Stmt *Body); diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index b4384b11fd..9831fba319 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -3013,11 +3013,9 @@ Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, << Fn->getType() << Fn->getSourceRange()); // Check for a valid return type - if (!FuncT->getResultType()->isVoidType() && - RequireCompleteType(Fn->getSourceRange().getBegin(), - FuncT->getResultType(), - PDiag(diag::err_call_incomplete_return) - << TheCall->getSourceRange())) + if (CheckCallReturnType(FuncT->getResultType(), + Fn->getSourceRange().getBegin(), TheCall.get(), + FDecl)) return ExprError(); // We know the result type of the call, set it. @@ -6223,3 +6221,26 @@ void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { return; } } + +bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, + CallExpr *CE, FunctionDecl *FD) { + if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) + return false; + + PartialDiagnostic Note = + FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) + << FD->getDeclName() : PDiag(); + SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); + + if (RequireCompleteType(Loc, ReturnType, + FD ? + PDiag(diag::err_call_function_incomplete_return) + << CE->getSourceRange() << FD->getDeclName() : + PDiag(diag::err_call_incomplete_return) + << CE->getSourceRange(), + std::make_pair(NoteLoc, Note))) + return true; + + return false; +} + diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index 04568a5d3a..9615a3cf57 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -2759,7 +2759,7 @@ void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, // empty. if (const RecordType *T1Rec = T1->getAs()) { // Complete the type if it can be completed. Otherwise, we're done. - if (RequireCompleteType(OpLoc, T1, PartialDiagnostic(0))) + if (RequireCompleteType(OpLoc, T1, PDiag())) return; LookupResult Operators; @@ -4219,10 +4219,10 @@ Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, Matches.end()); return getMostSpecialized(TemplateMatches.data(), TemplateMatches.size(), TPOC_Other, From->getLocStart(), - PartialDiagnostic(0), - PartialDiagnostic(diag::err_addr_ovl_ambiguous) + PDiag(), + PDiag(diag::err_addr_ovl_ambiguous) << TemplateMatches[0]->getDeclName(), - PartialDiagnostic(diag::err_ovl_template_candidate)); + PDiag(diag::err_ovl_template_candidate)); } // [...] any function template specializations in the set are diff --git a/lib/Sema/SemaType.cpp b/lib/Sema/SemaType.cpp index da72197f0c..6767712085 100644 --- a/lib/Sema/SemaType.cpp +++ b/lib/Sema/SemaType.cpp @@ -1820,7 +1820,9 @@ void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) { /// @returns @c true if @p T is incomplete and a diagnostic was emitted, /// @c false otherwise. bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, - const PartialDiagnostic &PD) { + const PartialDiagnostic &PD, + std::pair Note) { unsigned diag = PD.getDiagID(); // FIXME: Add this assertion to help us flush out problems with @@ -1864,6 +1866,10 @@ bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, // We have an incomplete type. Produce a diagnostic. Diag(Loc, PD) << T; + // If we have a note, produce it. + if (!Note.first.isInvalid()) + Diag(Note.first, Note.second); + // If the type was a forward declaration of a class/struct/union // type, produce const TagType *Tag = 0; diff --git a/test/CXX/dcl.dcl/dcl.spec/dcl.stc/p9.cpp b/test/CXX/dcl.dcl/dcl.spec/dcl.stc/p9.cpp index 5d9f9e7a51..907a91a86e 100644 --- a/test/CXX/dcl.dcl/dcl.spec/dcl.stc/p9.cpp +++ b/test/CXX/dcl.dcl/dcl.spec/dcl.stc/p9.cpp @@ -2,11 +2,11 @@ struct S; // expected-note {{forward declaration of 'struct S'}} extern S a; -extern S f(); +extern S f(); // expected-note {{'f' declared here}} extern void g(S a); // expected-note {{candidate function}} void h() { // FIXME: This diagnostic could be better. g(a); // expected-error {{no matching function for call to 'g'}} - f(); // expected-error {{return type of called function ('struct S') is incomplete}} + f(); // expected-error {{calling 'f' with incomplete return type 'struct S'}} } diff --git a/test/Sema/incomplete-call.c b/test/Sema/incomplete-call.c index aedfe50bbf..15d97683c5 100644 --- a/test/Sema/incomplete-call.c +++ b/test/Sema/incomplete-call.c @@ -2,12 +2,12 @@ struct foo; // expected-note 3 {{forward declaration of 'struct foo'}} -struct foo a(); +struct foo a(); // expected-note {{'a' declared here}} void b(struct foo); void c(); void func() { - a(); // expected-error{{return type of called function ('struct foo') is incomplete}} + a(); // expected-error{{calling 'a' with incomplete return type 'struct foo'}} b(*(struct foo*)0); // expected-error{{argument type 'struct foo' is incomplete}} c(*(struct foo*)0); // expected-error{{argument type 'struct foo' is incomplete}} } diff --git a/test/SemaCXX/enum.cpp b/test/SemaCXX/enum.cpp index 431f457259..db256812ab 100644 --- a/test/SemaCXX/enum.cpp +++ b/test/SemaCXX/enum.cpp @@ -31,7 +31,7 @@ struct s1 { enum e1 { YES, NO }; static enum e1 badfunc(struct s1 *q) { - return q->bar(); // expected-error{{return type of called function ('enum s1::e1') is incomplete}} + return q->bar(); // expected-error{{calling function with incomplete return type 'enum s1::e1'}} } enum e2; // expected-error{{ISO C++ forbids forward references to 'enum' types}}