Improve error message with ambiguous function overloads.

Issue #316.
This commit is contained in:
Matt Pharr
2012-07-06 11:25:57 -07:00
parent 2d8026625b
commit 78cb45fb25

View File

@@ -7656,25 +7656,27 @@ FunctionSymbolExpr::GetConstant(const Type *type) const {
} }
static void static std::string
lPrintOverloadCandidates(SourcePos pos, const std::vector<Symbol *> &funcs, lGetOverloadCandidateMessage(const std::vector<Symbol *> &funcs,
const std::vector<const Type *> &argTypes, const std::vector<const Type *> &argTypes,
const std::vector<bool> *argCouldBeNULL) { const std::vector<bool> *argCouldBeNULL) {
for (unsigned int i = 0; i < funcs.size(); ++i) { std::string message = "Passed types: (";
const FunctionType *ft = CastType<FunctionType>(funcs[i]->type);
AssertPos(pos, ft != NULL);
Error(funcs[i]->pos, "Candidate function: %s.", ft->GetString().c_str());
}
std::string passedTypes = "Passed types: (";
for (unsigned int i = 0; i < argTypes.size(); ++i) { for (unsigned int i = 0; i < argTypes.size(); ++i) {
if (argTypes[i] != NULL) if (argTypes[i] != NULL)
passedTypes += argTypes[i]->GetString(); message += argTypes[i]->GetString();
else else
passedTypes += "(unknown type)"; message += "(unknown type)";
passedTypes += (i < argTypes.size()-1) ? ", " : ")\n\n"; message += (i < argTypes.size()-1) ? ", " : ")\n";
} }
Error(pos, "%s", passedTypes.c_str());
for (unsigned int i = 0; i < funcs.size(); ++i) {
const FunctionType *ft = CastType<FunctionType>(funcs[i]->type);
Assert(ft != NULL);
message += "Candidate: ";
message += ft->GetString();
message += "\n";
}
return message;
} }
@@ -7947,20 +7949,23 @@ FunctionSymbolExpr::ResolveOverloads(SourcePos argPos,
} }
else if (matches.size() > 1) { else if (matches.size() > 1) {
// Multiple matches: ambiguous // Multiple matches: ambiguous
std::string candidateMessage =
lGetOverloadCandidateMessage(matches, argTypes, argCouldBeNULL);
Error(pos, "Multiple overloaded functions matched call to function " Error(pos, "Multiple overloaded functions matched call to function "
"\"%s\"%s.", funName, "\"%s\"%s.\n%s", funName,
exactMatchOnly ? " only considering exact matches" : ""); exactMatchOnly ? " only considering exact matches" : "",
lPrintOverloadCandidates(argPos, matches, argTypes, argCouldBeNULL); candidateMessage.c_str());
return false; return false;
} }
else { else {
// No matches at all // No matches at all
failure: failure:
std::string candidateMessage =
lGetOverloadCandidateMessage(matches, argTypes, argCouldBeNULL);
Error(pos, "Unable to find any matching overload for call to function " Error(pos, "Unable to find any matching overload for call to function "
"\"%s\"%s.", funName, "\"%s\"%s.\n%s", funName,
exactMatchOnly ? " only considering exact matches" : ""); exactMatchOnly ? " only considering exact matches" : "",
lPrintOverloadCandidates(argPos, candidateFunctions, argTypes, candidateMessage.c_str());
argCouldBeNULL);
return false; return false;
} }
} }