[PATCH] Add more information when displaying a "read-only variable is not assignable" error

Richard Smith richard at metafoo.co.uk
Fri Mar 13 13:38:10 PDT 2015


================
Comment at: lib/Sema/SemaExpr.cpp:8780
@@ -8779,1 +8779,3 @@
 
+static bool IsTypeReadable(QualType Ty, bool IsDereference) {
+  Ty = Ty.getNonReferenceType();
----------------
Should this be called something more like `isTypeModifiable`?

================
Comment at: lib/Sema/SemaExpr.cpp:8860-8882
@@ +8859,25 @@
+
+    // Member calls
+    if (const CXXMemberCallExpr *Call = dyn_cast<CXXMemberCallExpr>(E)) {
+      if (const MemberExpr *ME = dyn_cast<MemberExpr>(Call->getCallee())) {
+        if (const CXXMethodDecl *MD =
+                dyn_cast<CXXMethodDecl>(ME->getMemberDecl())) {
+          QualType ReturnType = MD->getReturnType();
+          if (!IsTypeReadable(ReturnType, IsDereference)) {
+            if (!DiagnosticEmitted) {
+              S.Diag(Loc, diag::err_typecheck_assign_const)
+                  << ExprRange << ConstFunction << MD;
+              DiagnosticEmitted = true;
+            }
+            S.Diag(MD->getReturnTypeSourceRange().getBegin(),
+                   diag::note_typecheck_assign_const)
+                << ConstFunction << MD << ReturnType
+                << MD->getReturnTypeSourceRange();
+          }
+          E = Call->getCallee();
+          continue;
+        }
+      }
+      break;
+    } // End CXXMemberCallExpr
+
----------------
Can you combine this with the general `CallExpr` check below? The only difference seems to be that you keep going after hitting a `CXXMemberCallExpr`, in case it's a `MemberExpr` with a `const` base object.

================
Comment at: lib/Sema/SemaExpr.cpp:8877-8878
@@ +8876,4 @@
+          }
+          E = Call->getCallee();
+          continue;
+        }
----------------
It'd be nice to condition this on the existence of another overload of the callee that is not `const` and has a non-`const`-qualified return type.

================
Comment at: lib/Sema/SemaExpr.cpp:8887
@@ +8886,3 @@
+      const FunctionDecl *FD = CE->getDirectCallee();
+      if (FD->getReturnType().getNonReferenceType().isConstQualified()) {
+        if (!DiagnosticEmitted) {
----------------
Should you be using `IsTypeReadable` here?

================
Comment at: test/Sema/assign.c:9
@@ -7,1 +8,3 @@
+  x->a = 10;
+  // expected-error at -1 {{cannot assign to variable 'x' with const-qualified type 'const struct (anonymous struct at /usr/local/google/home/rtrieu/clang/noop/llvm/tools/clang/test/Sema/assign.c:5:19) *'}}
 }
----------------
Please remove the path here :-)

http://reviews.llvm.org/D4479

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/






More information about the cfe-commits mailing list