[cfe-commits] r83692 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td include/clang/Basic/PartialDiagnostic.h lib/Sema/Sema.h lib/Sema/SemaExpr.cpp lib/Sema/SemaOverload.cpp lib/Sema/SemaType.cpp test/CXX/dcl.dcl/dcl.spec/dcl.stc/p9.cpp test/Sema/incomplete-call.c test/SemaCXX/enum.cpp
Anders Carlsson
andersca at mac.com
Fri Oct 9 16:51:56 PDT 2009
Author: andersca
Date: Fri Oct 9 18:51:55 2009
New Revision: 83692
URL: http://llvm.org/viewvc/llvm-project?rev=83692&view=rev
Log:
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;
^
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/include/clang/Basic/PartialDiagnostic.h
cfe/trunk/lib/Sema/Sema.h
cfe/trunk/lib/Sema/SemaExpr.cpp
cfe/trunk/lib/Sema/SemaOverload.cpp
cfe/trunk/lib/Sema/SemaType.cpp
cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.stc/p9.cpp
cfe/trunk/test/Sema/incomplete-call.c
cfe/trunk/test/SemaCXX/enum.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=83692&r1=83691&r2=83692&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Oct 9 18:51:55 2009
@@ -1770,7 +1770,11 @@
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<
Modified: cfe/trunk/include/clang/Basic/PartialDiagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/PartialDiagnostic.h?rev=83692&r1=83691&r2=83692&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/PartialDiagnostic.h (original)
+++ cfe/trunk/include/clang/Basic/PartialDiagnostic.h Fri Oct 9 18:51:55 2009
@@ -140,7 +140,7 @@
DeclarationName N);
};
-inline PartialDiagnostic PDiag(unsigned DiagID) {
+inline PartialDiagnostic PDiag(unsigned DiagID = 0) {
return PartialDiagnostic(DiagID);
}
Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=83692&r1=83691&r2=83692&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Fri Oct 9 18:51:55 2009
@@ -493,7 +493,10 @@
virtual TypeResult ActOnTypeName(Scope *S, Declarator &D);
bool RequireCompleteType(SourceLocation Loc, QualType T,
- const PartialDiagnostic &PD);
+ const PartialDiagnostic &PD,
+ std::pair<SourceLocation,
+ PartialDiagnostic> Note =
+ std::make_pair(SourceLocation(), PDiag()));
QualType getQualifiedNameType(const CXXScopeSpec &SS, QualType T);
@@ -959,6 +962,12 @@
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);
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=83692&r1=83691&r2=83692&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Oct 9 18:51:55 2009
@@ -3013,11 +3013,9 @@
<< 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 @@
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;
+}
+
Modified: cfe/trunk/lib/Sema/SemaOverload.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaOverload.cpp?rev=83692&r1=83691&r2=83692&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaOverload.cpp (original)
+++ cfe/trunk/lib/Sema/SemaOverload.cpp Fri Oct 9 18:51:55 2009
@@ -2759,7 +2759,7 @@
// empty.
if (const RecordType *T1Rec = T1->getAs<RecordType>()) {
// 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 @@
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
Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=83692&r1=83691&r2=83692&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Fri Oct 9 18:51:55 2009
@@ -1820,7 +1820,9 @@
/// @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<SourceLocation,
+ PartialDiagnostic> Note) {
unsigned diag = PD.getDiagID();
// FIXME: Add this assertion to help us flush out problems with
@@ -1864,6 +1866,10 @@
// 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;
Modified: cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.stc/p9.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.stc/p9.cpp?rev=83692&r1=83691&r2=83692&view=diff
==============================================================================
--- cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.stc/p9.cpp (original)
+++ cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.stc/p9.cpp Fri Oct 9 18:51:55 2009
@@ -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'}}
}
Modified: cfe/trunk/test/Sema/incomplete-call.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/incomplete-call.c?rev=83692&r1=83691&r2=83692&view=diff
==============================================================================
--- cfe/trunk/test/Sema/incomplete-call.c (original)
+++ cfe/trunk/test/Sema/incomplete-call.c Fri Oct 9 18:51:55 2009
@@ -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}}
}
Modified: cfe/trunk/test/SemaCXX/enum.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/enum.cpp?rev=83692&r1=83691&r2=83692&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/enum.cpp (original)
+++ cfe/trunk/test/SemaCXX/enum.cpp Fri Oct 9 18:51:55 2009
@@ -31,7 +31,7 @@
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}}
More information about the cfe-commits
mailing list