diff options
author | Kaelyn Uhrain <rikka@google.com> | 2011-08-18 18:19:12 +0000 |
---|---|---|
committer | Kaelyn Uhrain <rikka@google.com> | 2011-08-18 18:19:12 +0000 |
commit | 516116308007280f0dd6661cdd94112468c3c5fc (patch) | |
tree | 3dc3a4ff5e81ca36a9258a57836923c98b5d8412 /lib/Sema/SemaDecl.cpp | |
parent | a676379b26edc959193f9f919ba9c6d296a57824 (diff) |
Rework DiagnoseInvalidRedeclaration to add the ability to correct typos when
diagnosing invalid function redeclarations.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@137966 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaDecl.cpp')
-rw-r--r-- | lib/Sema/SemaDecl.cpp | 92 |
1 files changed, 69 insertions, 23 deletions
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 67493c7716..898fca4203 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -4204,28 +4204,79 @@ bool Sema::AddOverriddenMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { return AddedAny; } -static void DiagnoseInvalidRedeclaration(Sema &S, FunctionDecl *NewFD) { - LookupResult Prev(S, NewFD->getDeclName(), NewFD->getLocation(), +static void DiagnoseInvalidRedeclaration(Sema &S, FunctionDecl *NewFD, + bool isFriendDecl) { + DeclarationName Name = NewFD->getDeclName(); + DeclContext *DC = NewFD->getDeclContext(); + LookupResult Prev(S, Name, NewFD->getLocation(), Sema::LookupOrdinaryName, Sema::ForRedeclaration); llvm::SmallVector<unsigned, 1> MismatchedParams; - S.LookupQualifiedName(Prev, NewFD->getDeclContext()); + llvm::SmallVector<std::pair<FunctionDecl*, unsigned>, 1> NearMatches; + TypoCorrection Correction; + unsigned DiagMsg = isFriendDecl ? diag::err_no_matching_local_friend + : diag::err_member_def_does_not_match; + + NewFD->setInvalidDecl(); + S.LookupQualifiedName(Prev, DC); assert(!Prev.isAmbiguous() && "Cannot have an ambiguity in previous-declaration lookup"); - for (LookupResult::iterator Func = Prev.begin(), FuncEnd = Prev.end(); - Func != FuncEnd; ++Func) { - FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func); - if (FD && isNearlyMatchingFunction(S.Context, FD, NewFD, - MismatchedParams)) { - if (MismatchedParams.size() > 0) { - unsigned Idx = MismatchedParams.front(); - ParmVarDecl *FDParam = FD->getParamDecl(Idx); - S.Diag(FDParam->getTypeSpecStartLoc(), - diag::note_member_def_close_param_match) - << Idx+1 << FDParam->getType() << NewFD->getParamDecl(Idx)->getType(); - } else - S.Diag(FD->getLocation(), diag::note_member_def_close_match); + if (!Prev.empty()) { + for (LookupResult::iterator Func = Prev.begin(), FuncEnd = Prev.end(); + Func != FuncEnd; ++Func) { + FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func); + if (FD && isNearlyMatchingFunction(S.Context, FD, NewFD, + MismatchedParams)) { + // Add 1 to the index so that 0 can mean the mismatch didn't + // involve a parameter + unsigned ParamNum = + MismatchedParams.empty() ? 0 : MismatchedParams.front() + 1; + NearMatches.push_back(std::make_pair(FD, ParamNum)); + } + } + // If the qualified name lookup yielded nothing, try typo correction + } else if ((Correction = S.CorrectTypo(Prev.getLookupNameInfo(), + Prev.getLookupKind(), 0, 0, DC))) { + DiagMsg = isFriendDecl ? diag::err_no_matching_local_friend_suggest + : diag::err_member_def_does_not_match_suggest; + for (TypoCorrection::decl_iterator CDecl = Correction.begin(), + CDeclEnd = Correction.end(); + CDecl != CDeclEnd; ++CDecl) { + FunctionDecl *FD = dyn_cast<FunctionDecl>(*CDecl); + if (FD && isNearlyMatchingFunction(S.Context, FD, NewFD, + MismatchedParams)) { + // Add 1 to the index so that 0 can mean the mismatch didn't + // involve a parameter + unsigned ParamNum = + MismatchedParams.empty() ? 0 : MismatchedParams.front() + 1; + NearMatches.push_back(std::make_pair(FD, ParamNum)); + } } } + + if (Correction) + S.Diag(NewFD->getLocation(), DiagMsg) + << Name << DC << Correction.getQuoted(S.getLangOptions()) + << FixItHint::CreateReplacement( + NewFD->getLocation(), Correction.getAsString(S.getLangOptions())); + else + S.Diag(NewFD->getLocation(), DiagMsg) << Name << DC << NewFD->getLocation(); + + for (llvm::SmallVector<std::pair<FunctionDecl*, unsigned>, 1>::iterator + NearMatch = NearMatches.begin(), NearMatchEnd = NearMatches.end(); + NearMatch != NearMatchEnd; ++NearMatch) { + FunctionDecl *FD = NearMatch->first; + + if (unsigned Idx = NearMatch->second) { + ParmVarDecl *FDParam = FD->getParamDecl(Idx-1); + S.Diag(FDParam->getTypeSpecStartLoc(), + diag::note_member_def_close_param_match) + << Idx << FDParam->getType() << NewFD->getParamDecl(Idx-1)->getType(); + } else if (Correction) { + S.Diag(FD->getLocation(), diag::note_previous_decl) + << Correction.getQuoted(S.getLangOptions()); + } else + S.Diag(FD->getLocation(), diag::note_member_def_close_match); + } } NamedDecl* @@ -4939,19 +4990,14 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, // Complain about this problem, and attempt to suggest close // matches (e.g., those that differ only in cv-qualifiers and // whether the parameter types are references). - Diag(D.getIdentifierLoc(), diag::err_member_def_does_not_match) - << Name << DC << D.getCXXScopeSpec().getRange(); - NewFD->setInvalidDecl(); - DiagnoseInvalidRedeclaration(*this, NewFD); + DiagnoseInvalidRedeclaration(*this, NewFD, false); } // Unqualified local friend declarations are required to resolve // to something. } else if (isFriend && cast<CXXRecordDecl>(CurContext)->isLocalClass()) { - Diag(D.getIdentifierLoc(), diag::err_no_matching_local_friend); - NewFD->setInvalidDecl(); - DiagnoseInvalidRedeclaration(*this, NewFD); + DiagnoseInvalidRedeclaration(*this, NewFD, true); } } else if (!IsFunctionDefinition && D.getCXXScopeSpec().isSet() && |