]> granicus.if.org Git - clang/commitdiff
Improve diagnostic for calling non-const method on const object. Fixes rdar://7743000
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Tue, 16 Nov 2010 08:04:45 +0000 (08:04 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Tue, 16 Nov 2010 08:04:45 +0000 (08:04 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@119336 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaOverload.cpp
test/SemaCXX/copy-initialization.cpp

index 560120b51e79c60a326296dbc507642990a8e82f..ae5537ab7c51c9cd6bf45f0e8d9e7b71809e2c9c 100644 (file)
@@ -771,6 +771,10 @@ def err_reference_bind_init_list : Error<
 def err_init_list_bad_dest_type : Error<
   "%select{|non-aggregate }0type %1 cannot be initialized with an initializer "
   "list">;
+def err_member_function_call_bad_cvr : Error<"member function %0 not viable: "
+    "'this' argument has type %1, but function is not marked "
+    "%select{const|restrict|const or restrict|volatile|const or volatile|"
+    "volatile or restrict|const, volatile, or restrict}2">;
 
 def err_reference_init_drops_quals : Error<
   "initialization of reference to type %0 with a %select{value|temporary}1 of type %2 drops "
index 0c8d78025b435be19ea332914f3f90bc5390631c..65d6bc8f7a02a9d80365d781c5a2e0c477c5fa0b 100644 (file)
@@ -3187,10 +3187,26 @@ Sema::PerformObjectArgumentInitialization(Expr *&From,
   ImplicitConversionSequence ICS
     = TryObjectArgumentInitialization(*this, From->getType(), Method,
                                       Method->getParent());
-  if (ICS.isBad())
+  if (ICS.isBad()) {
+    if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) {
+      Qualifiers FromQs = FromRecordType.getQualifiers();
+      Qualifiers ToQs = DestType.getQualifiers();
+      unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers();
+      if (CVR) {
+        Diag(From->getSourceRange().getBegin(),
+             diag::err_member_function_call_bad_cvr)
+          << Method->getDeclName() << FromRecordType << (CVR - 1)
+          << From->getSourceRange();
+        Diag(Method->getLocation(), diag::note_previous_decl)
+          << Method->getDeclName();
+        return true;
+      }
+    }
+
     return Diag(From->getSourceRange().getBegin(),
                 diag::err_implicit_object_parameter_init)
        << ImplicitParamRecordType << FromRecordType << From->getSourceRange();
+  }
 
   if (ICS.Standard.Second == ICK_Derived_To_Base)
     return PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method);
index a28a1941ece4a1d6ebc9757a8abde1516d306655..fb83dcfbd01761b5227dfdd43b5ddd22e339a1e0 100644 (file)
@@ -19,11 +19,11 @@ void f(Y y, int *ip, float *fp) {
 }
 
 struct foo {
- void bar();
+ void bar(); // expected-note{{declared here}}
 };
 
 // PR3600
-void test(const foo *P) { P->bar(); } // expected-error{{cannot initialize object parameter of type 'foo' with an expression of type 'const foo'}}
+void test(const foo *P) { P->bar(); } // expected-error{{'bar' not viable: 'this' argument has type 'const foo', but function is not marked const}}
 
 namespace PR6757 {
   struct Foo {