Typo correction for template names, e.g.,

typo.cpp:27:8: error: no template named 'basic_sting' in namespace 'std'; 
    did you mean 'basic_string'?
  std::basic_sting<char> b2;
  ~~~~~^~~~~~~~~~~
       basic_string

llvm-svn: 92348
This commit is contained in:
Douglas Gregor 2009-12-31 08:11:17 +00:00
parent 5f8a005d38
commit ff18cc1141
3 changed files with 32 additions and 1 deletions

View File

@ -2557,6 +2557,9 @@ def err_undeclared_use_suggest : Error<
"use of undeclared %0; did you mean %1?">;
def err_undeclared_var_use_suggest : Error<
"use of undeclared identifier %0; did you mean %1?">;
def err_no_template_suggest : Error<"no template named %0; did you mean %1?">;
def err_no_member_template_suggest : Error<
"no template named %0 in %1; did you mean %2?">;
}

View File

@ -102,7 +102,8 @@ TemplateNameKind Sema::isTemplateName(Scope *S,
QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr);
LookupResult R(*this, TName, SourceLocation(), LookupOrdinaryName);
LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
LookupOrdinaryName);
R.suppressDiagnostics();
LookupTemplateName(R, S, SS, ObjectType, EnteringContext);
if (R.empty())
@ -202,6 +203,29 @@ void Sema::LookupTemplateName(LookupResult &Found,
assert(!Found.isAmbiguous() &&
"Cannot handle template name-lookup ambiguities");
if (Found.empty()) {
// If we did not find any names, attempt to correct any typos.
DeclarationName Name = Found.getLookupName();
if (CorrectTypo(Found, S, &SS, LookupCtx)) {
FilterAcceptableTemplateNames(Context, Found);
if (!Found.empty() && isa<TemplateDecl>(*Found.begin())) {
if (LookupCtx)
Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
<< Name << LookupCtx << Found.getLookupName() << SS.getRange()
<< CodeModificationHint::CreateReplacement(Found.getNameLoc(),
Found.getLookupName().getAsString());
else
Diag(Found.getNameLoc(), diag::err_no_template_suggest)
<< Name << Found.getLookupName()
<< CodeModificationHint::CreateReplacement(Found.getNameLoc(),
Found.getLookupName().getAsString());
} else
Found.clear();
} else {
Found.clear();
}
}
FilterAcceptableTemplateNames(Context, Found);
if (Found.empty())
return;

View File

@ -23,6 +23,10 @@ float area(float radius, float pi) {
}
bool test_string(std::string s) {
basc_string<char> b1; // expected-error{{no template named 'basc_string'; did you mean 'basic_string'?}}
std::basic_sting<char> b2; // expected-error{{no template named 'basic_sting' in namespace 'std'; did you mean 'basic_string'?}}
(void)b1;
(void)b2;
return s.fnd("hello") // expected-error{{no member named 'fnd' in 'class std::basic_string<char>'; did you mean 'find'?}}
== std::string::pos; // expected-error{{no member named 'pos' in 'class std::basic_string<char>'; did you mean 'npos'?}}
}