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

Richard Trieu rtrieu at google.com
Fri Apr 10 14:58:50 PDT 2015


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

================
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
+
----------------
rsmith wrote:
> 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.
Removed handling for CXXMemberCallExpr.  CallExpr below will handle the generic case.  Will try to incorporate the overloaded member detection in a future change.

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

================
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) *'}}
 }
----------------
rsmith wrote:
> Please remove the path here :-)
Path removed.

http://reviews.llvm.org/D4479

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






More information about the cfe-commits mailing list