[cfe-commits] r92894 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaOverload.cpp test/SemaCXX/overloaded-operator.cpp test/SemaTemplate/instantiate-subscript.cpp
John McCall
rjmccall at apple.com
Wed Jan 6 18:04:15 PST 2010
Author: rjmccall
Date: Wed Jan 6 20:04:15 2010
New Revision: 92894
URL: http://llvm.org/viewvc/llvm-project?rev=92894&view=rev
Log:
Improve the lead diagnostic for C++ object subscript expressions with
no viable overloads. Use a different message when the class provides
no operator[] overloads at all; use it for operator(), too.
Partially addresses PR 5900.
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaOverload.cpp
cfe/trunk/test/SemaCXX/overloaded-operator.cpp
cfe/trunk/test/SemaTemplate/instantiate-subscript.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=92894&r1=92893&r2=92894&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Jan 6 20:04:15 2010
@@ -928,6 +928,10 @@
def err_ovl_no_viable_oper : Error<"no viable overloaded '%0'">;
def err_ovl_deleted_oper : Error<
"overload resolution selected %select{unavailable|deleted}0 operator '%1'">;
+def err_ovl_no_viable_subscript :
+ Error<"no viable overloaded operator[] for type %0">;
+def err_ovl_no_oper :
+ Error<"type %0 does not provide a %select{subscript|call}1 operator">;
def err_ovl_no_viable_object_call : Error<
"no matching function for call to object of type %0">;
Modified: cfe/trunk/lib/Sema/SemaOverload.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaOverload.cpp?rev=92894&r1=92893&r2=92894&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaOverload.cpp (original)
+++ cfe/trunk/lib/Sema/SemaOverload.cpp Wed Jan 6 20:04:15 2010
@@ -5449,16 +5449,17 @@
}
case OR_No_Viable_Function: {
- // No viable function; try to create a built-in operation, which will
- // produce an error. Then, show the non-viable candidates.
- OwningExprResult Result =
- CreateBuiltinArraySubscriptExpr(move(Base), LLoc, move(Idx), RLoc);
- assert(Result.isInvalid() &&
- "C++ subscript operator overloading is missing candidates!");
- if (Result.isInvalid())
- PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false,
- "[]", LLoc);
- return move(Result);
+ if (CandidateSet.empty())
+ Diag(LLoc, diag::err_ovl_no_oper)
+ << Args[0]->getType() << /*subscript*/ 0
+ << Args[0]->getSourceRange() << Args[1]->getSourceRange();
+ else
+ Diag(LLoc, diag::err_ovl_no_viable_subscript)
+ << Args[0]->getType()
+ << Args[0]->getSourceRange() << Args[1]->getSourceRange();
+ PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false,
+ "[]", LLoc);
+ return ExprError();
}
case OR_Ambiguous:
@@ -5712,9 +5713,14 @@
break;
case OR_No_Viable_Function:
- Diag(Object->getSourceRange().getBegin(),
- diag::err_ovl_no_viable_object_call)
- << Object->getType() << Object->getSourceRange();
+ if (CandidateSet.empty())
+ Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper)
+ << Object->getType() << /*call*/ 1
+ << Object->getSourceRange();
+ else
+ Diag(Object->getSourceRange().getBegin(),
+ diag::err_ovl_no_viable_object_call)
+ << Object->getType() << Object->getSourceRange();
PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false);
break;
Modified: cfe/trunk/test/SemaCXX/overloaded-operator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/overloaded-operator.cpp?rev=92894&r1=92893&r2=92894&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/overloaded-operator.cpp (original)
+++ cfe/trunk/test/SemaCXX/overloaded-operator.cpp Wed Jan 6 20:04:15 2010
@@ -324,3 +324,27 @@
a += x;
}
}
+
+// PR5900
+namespace pr5900 {
+ struct NotAnArray {};
+ void test0() {
+ NotAnArray x;
+ x[0] = 0; // expected-error {{does not provide a subscript operator}}
+ }
+
+ struct NonConstArray {
+ int operator[](unsigned); // expected-note {{candidate}}
+ };
+ int test1() {
+ const NonConstArray x;
+ return x[0]; // expected-error {{no viable overloaded operator[] for type}}
+ }
+
+ // Not really part of this PR, but implemented at the same time.
+ struct NotAFunction {};
+ void test2() {
+ NotAFunction x;
+ x(); // expected-error {{does not provide a call operator}}
+ }
+}
Modified: cfe/trunk/test/SemaTemplate/instantiate-subscript.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiate-subscript.cpp?rev=92894&r1=92893&r2=92894&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiate-subscript.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiate-subscript.cpp Wed Jan 6 20:04:15 2010
@@ -16,7 +16,7 @@
template<typename T, typename U, typename Result>
struct Subscript0 {
void test(T t, U u) {
- Result &result = t[u]; // expected-error{{subscripted value is not}}
+ Result &result = t[u]; // expected-error{{no viable overloaded operator[] for type}}
}
};
More information about the cfe-commits
mailing list