[cfe-commits] r158177 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/SemaCXX/function-redecl.cpp test/SemaCXX/nested-name-spec.cpp
Kaelyn Uhrain
rikka at google.com
Thu Jun 7 16:57:09 PDT 2012
Author: rikka
Date: Thu Jun 7 18:57:08 2012
New Revision: 158177
URL: http://llvm.org/viewvc/llvm-project?rev=158177&view=rev
Log:
Ignore corrections to functions with bodies when deciding which
correction to use for an invalid function redeclaration.
Modified:
cfe/trunk/lib/Sema/SemaDecl.cpp
cfe/trunk/test/SemaCXX/function-redecl.cpp
cfe/trunk/test/SemaCXX/nested-name-spec.cpp
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=158177&r1=158176&r2=158177&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Jun 7 18:57:08 2012
@@ -4604,22 +4604,39 @@
// Also only accept corrections that have the same parent decl.
class DifferentNameValidatorCCC : public CorrectionCandidateCallback {
public:
- DifferentNameValidatorCCC(CXXRecordDecl *Parent)
- : ExpectedParent(Parent ? Parent->getCanonicalDecl() : 0) {}
+ DifferentNameValidatorCCC(ASTContext &Context, FunctionDecl *TypoFD,
+ CXXRecordDecl *Parent)
+ : Context(Context), OriginalFD(TypoFD),
+ ExpectedParent(Parent ? Parent->getCanonicalDecl() : 0) {}
virtual bool ValidateCandidate(const TypoCorrection &candidate) {
if (candidate.getEditDistance() == 0)
return false;
- if (CXXMethodDecl *MD = candidate.getCorrectionDeclAs<CXXMethodDecl>()) {
- CXXRecordDecl *Parent = MD->getParent();
- return Parent && Parent->getCanonicalDecl() == ExpectedParent;
+ llvm::SmallVector<unsigned, 1> MismatchedParams;
+ for (TypoCorrection::const_decl_iterator CDecl = candidate.begin(),
+ CDeclEnd = candidate.end();
+ CDecl != CDeclEnd; ++CDecl) {
+ FunctionDecl *FD = dyn_cast<FunctionDecl>(*CDecl);
+
+ if (FD && !FD->hasBody() &&
+ hasSimilarParameters(Context, FD, OriginalFD, MismatchedParams)) {
+ if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
+ CXXRecordDecl *Parent = MD->getParent();
+ if (Parent && Parent->getCanonicalDecl() == ExpectedParent)
+ return true;
+ } else if (!ExpectedParent) {
+ return true;
+ }
+ }
}
- return !ExpectedParent;
+ return false;
}
private:
+ ASTContext &Context;
+ FunctionDecl *OriginalFD;
CXXRecordDecl *ExpectedParent;
};
@@ -4655,7 +4672,8 @@
assert(!Prev.isAmbiguous() &&
"Cannot have an ambiguity in previous-declaration lookup");
CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD);
- DifferentNameValidatorCCC Validator(MD ? MD->getParent() : 0);
+ DifferentNameValidatorCCC Validator(SemaRef.Context, NewFD,
+ MD ? MD->getParent() : 0);
if (!Prev.empty()) {
for (LookupResult::iterator Func = Prev.begin(), FuncEnd = Prev.end();
Func != FuncEnd; ++Func) {
@@ -4685,8 +4703,8 @@
CDeclEnd = Correction.end();
CDecl != CDeclEnd; ++CDecl) {
FunctionDecl *FD = dyn_cast<FunctionDecl>(*CDecl);
- if (FD && hasSimilarParameters(SemaRef.Context, FD, NewFD,
- MismatchedParams)) {
+ if (FD && !FD->hasBody() &&
+ hasSimilarParameters(SemaRef.Context, FD, NewFD, MismatchedParams)) {
Previous.addDecl(FD);
}
}
Modified: cfe/trunk/test/SemaCXX/function-redecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/function-redecl.cpp?rev=158177&r1=158176&r2=158177&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/function-redecl.cpp (original)
+++ cfe/trunk/test/SemaCXX/function-redecl.cpp Thu Jun 7 18:57:08 2012
@@ -76,12 +76,9 @@
void GetCart(int count) const;
};
// This out-of-line definition was fine...
-void Crash::cart(int count) const {} // expected-error {{out-of-line definition of 'cart' does not match any declaration in 'Crash'}} \
- // expected-note {{'cart' declared here}} \
- // expected-note {{previous definition is here}}
+void Crash::cart(int count) const {} // expected-error {{out-of-line definition of 'cart' does not match any declaration in 'Crash'}}
// ...while this one crashed clang
-void Crash::chart(int count) const {} // expected-error {{out-of-line definition of 'chart' does not match any declaration in 'Crash'; did you mean 'cart'?}} \
- // expected-error {{redefinition of 'cart'}}
+void Crash::chart(int count) const {} // expected-error {{out-of-line definition of 'chart' does not match any declaration in 'Crash'}}
class TestConst {
public:
@@ -98,3 +95,24 @@
struct J { int typo() const; };
int J::typo_() { return 3; } // expected-error {{out-of-line definition of 'typo_' does not match any declaration in 'J'}}
+
+// Ensure we correct the redecl of Foo::isGood to Bar::Foo::isGood and not
+// Foo::IsGood even though Foo::IsGood is technically a closer match since it
+// already has a body. Also make sure Foo::beEvil is corrected to Foo::BeEvil
+// since it is a closer match than Bar::Foo::beEvil and neither have a body.
+namespace redecl_typo {
+namespace Foo {
+ bool IsGood() { return false; }
+ void BeEvil(); // expected-note {{'BeEvil' declared here}}
+}
+namespace Bar {
+ namespace Foo {
+ bool isGood(); // expected-note {{'Bar::Foo::isGood' declared here}}
+ void beEvil();
+ }
+}
+bool Foo::isGood() { // expected-error {{out-of-line definition of 'isGood' does not match any declaration in namespace 'redecl_typo::Foo'; did you mean 'Bar::Foo::isGood'?}}
+ return true;
+}
+void Foo::beEvil() {} // expected-error {{out-of-line definition of 'beEvil' does not match any declaration in namespace 'redecl_typo::Foo'; did you mean 'BeEvil'?}}
+}
Modified: cfe/trunk/test/SemaCXX/nested-name-spec.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/nested-name-spec.cpp?rev=158177&r1=158176&r2=158177&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/nested-name-spec.cpp (original)
+++ cfe/trunk/test/SemaCXX/nested-name-spec.cpp Thu Jun 7 18:57:08 2012
@@ -113,8 +113,7 @@
X = 0
};
- void f() { // expected-note{{'E::Nested::f' declared here}} \
- // expected-note{{previous definition is here}}
+ void f() {
return E::X; // expected-error{{expected a class or namespace}}
}
}
@@ -144,10 +143,7 @@
void g(int&); // expected-note{{type of 1st parameter of member declaration does not match definition ('int &' vs 'const int &')}}
}
-// TODO: Suppress the typo correction for an invalid redeclaration if the chosen
-// correction is a function that already has a body.
-void A::f() {} // expected-error{{out-of-line definition of 'f' does not match any declaration in namespace 'A'; did you mean 'E::Nested::f'?}} \
- // expected-error{{redefinition of 'f'}}
+void A::f() {} // expected-error-re{{out-of-line definition of 'f' does not match any declaration in namespace 'A'$}}
void A::g(const int&) { } // expected-error{{out-of-line definition of 'g' does not match any declaration in namespace 'A'}}
More information about the cfe-commits
mailing list