[cfe-commits] PATCH: Make DiagnoseInvalidRedeclaration state when a member decl does not match due to "const" mismatch (issue 5167048)
rikka at google.com
rikka at google.com
Fri Sep 30 16:12:38 PDT 2011
Reviewers: chandlerc,
Please review this at http://codereview.appspot.com/5167048/
Affected files:
M include/clang/Basic/DiagnosticSemaKinds.td
M lib/Sema/SemaDecl.cpp
M test/SemaCXX/function-redecl.cpp
M test/SemaCXX/nested-name-spec.cpp
-------------- next part --------------
Index: include/clang/Basic/DiagnosticSemaKinds.td
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 1b974b0c0240dd702f395faa450e44e59bef0298..d8a0eb689dc76976755ec62157daa96ac24041b2 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3164,6 +3164,8 @@ def warn_member_extra_qualification : Warning<
def err_member_qualification : Error<
"non-friend class member %0 cannot have a qualified name">;
def note_member_def_close_match : Note<"member declaration nearly matches">;
+def note_member_def_close_const_match : Note<
+ "member declaration %select{has|lacks}0 const keyword">;
def note_member_def_close_param_match : Note<
"type of %ordinal0 parameter of member declaration does not match "
"definition (%1 vs %2)">;
Index: lib/Sema/SemaDecl.cpp
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 983ed3eb995734284100fdb2cba15d7683c10de9..62777515c62a3fa4b137d3c74605cd31f1808ec7 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -4357,10 +4357,17 @@ static void DiagnoseInvalidRedeclaration(Sema &S, FunctionDecl *NewFD,
else
S.Diag(NewFD->getLocation(), DiagMsg) << Name << DC << NewFD->getLocation();
+ bool NewFDisConst = false;
+ if (CXXMethodDecl *NewMD = dyn_cast<CXXMethodDecl>(NewFD))
+ NewFDisConst = NewMD->getTypeQualifiers() & Qualifiers::Const;
+
for (llvm::SmallVector<std::pair<FunctionDecl*, unsigned>, 1>::iterator
NearMatch = NearMatches.begin(), NearMatchEnd = NearMatches.end();
NearMatch != NearMatchEnd; ++NearMatch) {
FunctionDecl *FD = NearMatch->first;
+ bool FDisConst = false;
+ if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
+ FDisConst = MD->getTypeQualifiers() & Qualifiers::Const;
if (unsigned Idx = NearMatch->second) {
ParmVarDecl *FDParam = FD->getParamDecl(Idx-1);
@@ -4369,7 +4376,10 @@ static void DiagnoseInvalidRedeclaration(Sema &S, FunctionDecl *NewFD,
<< Idx << FDParam->getType() << NewFD->getParamDecl(Idx-1)->getType();
} else if (Correction) {
S.Diag(FD->getLocation(), diag::note_previous_decl)
- << Correction.getQuoted(S.getLangOptions());
+ << Correction.getQuoted(S.getLangOptions());
+ } else if (FDisConst != NewFDisConst) {
+ S.Diag(FD->getLocation(), diag::note_member_def_close_const_match)
+ << NewFDisConst << FD->getSourceRange().getEnd();
} else
S.Diag(FD->getLocation(), diag::note_member_def_close_match);
}
Index: test/SemaCXX/function-redecl.cpp
diff --git a/test/SemaCXX/function-redecl.cpp b/test/SemaCXX/function-redecl.cpp
index 2bc04a6d077c99aef2bfe6c56af4b4f50718461d..bc25e146f1c83d7643f6e8c650744c37b9246173 100644
--- a/test/SemaCXX/function-redecl.cpp
+++ b/test/SemaCXX/function-redecl.cpp
@@ -62,7 +62,7 @@ struct Foo {
}
class Bar {
- void f(test1::Foo::Inner foo) const; // expected-note {{member declaration nearly matches}}
+ void f(test1::Foo::Inner foo) const; // expected-note {{member declaration has const keyword}}
};
using test1::Foo;
@@ -79,3 +79,16 @@ class Crash {
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'}}
+
+class TestConst {
+ public:
+ int getit() const; // expected-note {{member declaration has const keyword}}
+ void setit(int); // expected-note {{member declaration lacks const keyword}}
+};
+
+int TestConst::getit() { // expected-error {{out-of-line definition of 'getit' does not match any declaration in 'TestConst'}}
+ return 1;
+}
+
+void TestConst::setit(int) const { // expected-error {{out-of-line definition of 'setit' does not match any declaration in 'TestConst'}}
+}
Index: test/SemaCXX/nested-name-spec.cpp
diff --git a/test/SemaCXX/nested-name-spec.cpp b/test/SemaCXX/nested-name-spec.cpp
index 8e73c3e2ed32b2dbe6e2581c24d597e813dd2ff2..c34200b78d88846433f87fceabc2e7e09e8eb2e6 100644
--- a/test/SemaCXX/nested-name-spec.cpp
+++ b/test/SemaCXX/nested-name-spec.cpp
@@ -27,10 +27,10 @@ int A::C::cx = 17;
static int A::C::cx2 = 17; // expected-error{{'static' can}}
class C2 {
- void m(); // expected-note{{member declaration nearly matches}}
+ void m(); // expected-note{{member declaration lacks const keyword}}
void f(const int& parm); // expected-note{{type of 1st parameter of member declaration does not match definition ('const int &' vs 'int')}}
- void f(int) const; // expected-note{{member declaration nearly matches}}
+ void f(int) const; // expected-note{{member declaration has const keyword}}
void f(float);
int x;
@@ -121,7 +121,7 @@ namespace E {
class Operators {
- Operators operator+(const Operators&) const; // expected-note{{member declaration nearly matches}}
+ Operators operator+(const Operators&) const; // expected-note{{member declaration has const keyword}}
operator bool();
};
More information about the cfe-commits
mailing list