[PATCH] D91828: [Sema/Attribute] Ignore noderef attribute in unevaluated context

Jann Horn via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 19 15:53:55 PST 2020


thejh created this revision.
thejh added reviewers: leonardchan, rsmith, aaron.ballman, mcgrathr.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
thejh requested review of this revision.

The noderef attribute is for catching code that accesses pointers in
a different address space. Unevaluated code is always safe in that regard.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D91828

Files:
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaExprMember.cpp
  clang/test/Frontend/noderef.c


Index: clang/test/Frontend/noderef.c
===================================================================
--- clang/test/Frontend/noderef.c
+++ clang/test/Frontend/noderef.c
@@ -63,6 +63,12 @@
   p = &s->a;
   p = &(*s).b;
 
+  // Nothing in sizeof() actually accesses memory
+  x = sizeof(s->a);          // ok
+  x = sizeof(*s);            // ok
+  x = sizeof(s[0]);          // ok
+  x = sizeof(s->a + (s->b)); // ok
+
   // Nested struct access
   struct S2 NODEREF *s2_noderef;    // expected-note 5 {{s2_noderef declared here}}
   p = s2_noderef->a;  // ok since result is an array in a struct
Index: clang/lib/Sema/SemaExprMember.cpp
===================================================================
--- clang/lib/Sema/SemaExprMember.cpp
+++ clang/lib/Sema/SemaExprMember.cpp
@@ -1734,6 +1734,9 @@
 }
 
 void Sema::CheckMemberAccessOfNoDeref(const MemberExpr *E) {
+  if (isUnevaluatedContext())
+    return;
+
   QualType ResultTy = E->getType();
 
   // Do not warn on member accesses to arrays since this returns an array
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -4751,6 +4751,9 @@
 }
 
 void Sema::CheckSubscriptAccessOfNoDeref(const ArraySubscriptExpr *E) {
+  if (isUnevaluatedContext())
+    return;
+
   QualType ResultTy = E->getType();
   ExpressionEvaluationContextRecord &LastRecord = ExprEvalContexts.back();
 
@@ -14666,7 +14669,8 @@
                             OpLoc, CanOverflow, CurFPFeatureOverrides());
 
   if (Opc == UO_Deref && UO->getType()->hasAttr(attr::NoDeref) &&
-      !isa<ArrayType>(UO->getType().getDesugaredType(Context)))
+      !isa<ArrayType>(UO->getType().getDesugaredType(Context)) &&
+      !isUnevaluatedContext())
     ExprEvalContexts.back().PossibleDerefs.insert(UO);
 
   // Convert the result back to a half vector.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91828.306549.patch
Type: text/x-patch
Size: 1899 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201119/27e5bb2b/attachment.bin>


More information about the cfe-commits mailing list