[cfe-commits] r92345 - in /cfe/trunk: lib/Sema/Sema.h lib/Sema/SemaExpr.cpp lib/Sema/SemaLookup.cpp test/FixIt/typo.cpp
Douglas Gregor
dgregor at apple.com
Wed Dec 30 23:42:17 PST 2009
Author: dgregor
Date: Thu Dec 31 01:42:17 2009
New Revision: 92345
URL: http://llvm.org/viewvc/llvm-project?rev=92345&view=rev
Log:
Typo correction for member access into classes/structs/unions, e.g.,
s.fnd("hello")
Modified:
cfe/trunk/lib/Sema/Sema.h
cfe/trunk/lib/Sema/SemaExpr.cpp
cfe/trunk/lib/Sema/SemaLookup.cpp
cfe/trunk/test/FixIt/typo.cpp
Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=92345&r1=92344&r2=92345&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Thu Dec 31 01:42:17 2009
@@ -1209,6 +1209,7 @@
VisibleDeclConsumer &Consumer);
bool CorrectTypo(LookupResult &R, Scope *S, const CXXScopeSpec *SS,
+ DeclContext *MemberContext = 0,
bool EnteringContext = false);
void FindAssociatedClassesAndNamespaces(Expr **Args, unsigned NumArgs,
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=92345&r1=92344&r2=92345&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Dec 31 01:42:17 2009
@@ -932,7 +932,7 @@
// We didn't find anything, so try to correct for a typo.
if (S && CorrectTypo(R, S, &SS) &&
- (isa<ValueDecl>(*R.begin()) || isa<TemplateDecl>(*R.begin()))) {
+ (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) {
if (SS.isEmpty())
Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName()
<< CodeModificationHint::CreateReplacement(R.getNameLoc(),
@@ -2346,6 +2346,23 @@
// The record definition is complete, now look up the member.
SemaRef.LookupQualifiedName(R, DC);
+ if (!R.empty())
+ return false;
+
+ // We didn't find anything with the given name, so try to correct
+ // for typos.
+ DeclarationName Name = R.getLookupName();
+ if (SemaRef.CorrectTypo(R, 0, &SS, DC) &&
+ (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) {
+ SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest)
+ << Name << DC << R.getLookupName() << SS.getRange()
+ << CodeModificationHint::CreateReplacement(R.getNameLoc(),
+ R.getLookupName().getAsString());
+ return false;
+ } else {
+ R.clear();
+ }
+
return false;
}
Modified: cfe/trunk/lib/Sema/SemaLookup.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaLookup.cpp?rev=92345&r1=92344&r2=92345&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaLookup.cpp (original)
+++ cfe/trunk/lib/Sema/SemaLookup.cpp Thu Dec 31 01:42:17 2009
@@ -2134,6 +2134,9 @@
/// \param SS the nested-name-specifier that precedes the name we're
/// looking for, if present.
///
+/// \param MemberContext if non-NULL, the context in which to look for
+/// a member access expression.
+///
/// \param EnteringContext whether we're entering the context described by
/// the nested-name-specifier SS.
///
@@ -2141,7 +2144,7 @@
/// structure will contain the results of name lookup for the
/// corrected name. Otherwise, returns false.
bool Sema::CorrectTypo(LookupResult &Res, Scope *S, const CXXScopeSpec *SS,
- bool EnteringContext) {
+ DeclContext *MemberContext, bool EnteringContext) {
// We only attempt to correct typos for identifiers.
IdentifierInfo *Typo = Res.getLookupName().getAsIdentifierInfo();
if (!Typo)
@@ -2158,7 +2161,9 @@
return false;
TypoCorrectionConsumer Consumer(Typo);
- if (SS && SS->isSet()) {
+ if (MemberContext)
+ LookupVisibleDecls(MemberContext, Res.getLookupKind(), Consumer);
+ else if (SS && SS->isSet()) {
DeclContext *DC = computeDeclContext(*SS, EnteringContext);
if (!DC)
return false;
@@ -2193,7 +2198,11 @@
// success if we found something that was not ambiguous.
Res.clear();
Res.setLookupName(BestName);
- LookupParsedName(Res, S, SS, /*AllowBuiltinCreation=*/false, EnteringContext);
+ if (MemberContext)
+ LookupQualifiedName(Res, MemberContext);
+ else
+ LookupParsedName(Res, S, SS, /*AllowBuiltinCreation=*/false,
+ EnteringContext);
if (Res.isAmbiguous()) {
Res.suppressDiagnostics();
Modified: cfe/trunk/test/FixIt/typo.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/FixIt/typo.cpp?rev=92345&r1=92344&r2=92345&view=diff
==============================================================================
--- cfe/trunk/test/FixIt/typo.cpp (original)
+++ cfe/trunk/test/FixIt/typo.cpp Thu Dec 31 01:42:17 2009
@@ -23,5 +23,6 @@
}
bool test_string(std::string s) {
- return s.find("hello") == std::string::pos; // expected-error{{no member named 'pos' in 'class std::basic_string<char>'; did you mean 'npos'?}}
+ 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'?}}
}
More information about the cfe-commits
mailing list