From 40f7a007e9d06078ff49e5bcc4587150c098fb66 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Mon, 4 Jan 2010 17:27:12 +0000 Subject: [PATCH] When declaring an Objective-C implementation without a corresponding interface, suggest correction of typos. For example, given: @interface NSString @end @implementation NSstring @end we'll warn with: t.m:4:19: warning: cannot find interface declaration for 'NSstring'; did you mean 'NSString'? @implementation NSstring ^ However, since this is just a warning, we don't provide a fix-it hint. Good idea, Ted! llvm-svn: 92488 --- .../clang/Basic/DiagnosticSemaKinds.td | 2 ++ clang/lib/Sema/SemaDeclObjC.cpp | 22 +++++++++++++++---- clang/test/SemaObjC/undef-superclass-1.m | 2 ++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index fb7a6abf1043..14271c8d9072 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -2575,6 +2575,8 @@ def err_property_not_found_suggest : Error< "property %0 not found on object of type %1; did you mean %2?">; def err_undef_interface_suggest : Error< "cannot find interface declaration for %0; did you mean %1?">; +def warn_undef_interface_suggest : Warning< + "cannot find interface declaration for %0; did you mean %1?">; def err_undef_superclass_suggest : Error< "cannot find interface declaration for %0, superclass of %1; did you mean " "%2?">; diff --git a/clang/lib/Sema/SemaDeclObjC.cpp b/clang/lib/Sema/SemaDeclObjC.cpp index 6ff898970af0..fb2cc712dce9 100644 --- a/clang/lib/Sema/SemaDeclObjC.cpp +++ b/clang/lib/Sema/SemaDeclObjC.cpp @@ -689,13 +689,27 @@ Sema::DeclPtrTy Sema::ActOnStartClassImplementation( if (PrevDecl && !isa(PrevDecl)) { Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; Diag(PrevDecl->getLocation(), diag::note_previous_definition); - } else { - // Is there an interface declaration of this class; if not, warn! - IDecl = dyn_cast_or_null(PrevDecl); - if (!IDecl || IDecl->isForwardDecl()) { + } else if ((IDecl = dyn_cast_or_null(PrevDecl))) { + // If this is a forward declaration of an interface, warn. + if (IDecl->isForwardDecl()) { Diag(ClassLoc, diag::warn_undef_interface) << ClassName; IDecl = 0; } + } else { + // We did not find anything with the name ClassName; try to correct for + // typos in the class name. + LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName); + if (CorrectTypo(R, TUScope, 0) && + (IDecl = R.getAsSingle())) { + // Suggest the (potentially) correct interface name. However, don't + // provide a code-modification hint or use the typo name for recovery, + // because this is just a warning. The program may actually be correct. + Diag(ClassLoc, diag::warn_undef_interface_suggest) + << ClassName << R.getLookupName(); + IDecl = 0; + } else { + Diag(ClassLoc, diag::warn_undef_interface) << ClassName; + } } // Check that super class name is valid class name diff --git a/clang/test/SemaObjC/undef-superclass-1.m b/clang/test/SemaObjC/undef-superclass-1.m index 7611cf3b1554..56697e23c2b5 100644 --- a/clang/test/SemaObjC/undef-superclass-1.m +++ b/clang/test/SemaObjC/undef-superclass-1.m @@ -31,3 +31,5 @@ @implementation RecursiveClass @end +@implementation iNTF3 // expected-warning{{cannot find interface declaration for 'iNTF3'; did you mean 'INTF3'?}} +@end