[PATCH] D84202: [clang][noderef] Enable -Wnoderef warning for CXX C-style casts

Leonard Chan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 20 21:16:31 PDT 2020


leonardchan created this revision.
leonardchan added reviewers: aaron.ballman, rsmith.
leonardchan added a project: clang.

We find it more valuable to warn on all instances we're removing the `noderef` attribute from a pointer. This patch removes the type holes where we initially didn't warn on C-style casts. For instances where removing `noderef` is intended, users can still disable the warning on line granularity with pragmas.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84202

Files:
  clang/lib/Sema/SemaCast.cpp
  clang/test/Frontend/noderef.cpp


Index: clang/test/Frontend/noderef.cpp
===================================================================
--- clang/test/Frontend/noderef.cpp
+++ clang/test/Frontend/noderef.cpp
@@ -120,20 +120,18 @@
 void cast_from_void_ptr(NODEREF void *x) {
   int *a = static_cast<int *>(x);      // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
 
-  // Allow regular C-style casts and C-style through reinterpret_casts to be holes
-  int *b = reinterpret_cast<int *>(x);
-  int *c = (int *)x;
+  int *b = reinterpret_cast<int *>(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
+  int *c = (int *)x; // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
 }
 
 void conversion_sequences() {
   NODEREF int *x;
   int *x2 = x;                     // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
   int *x3 = static_cast<int *>(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
-  int *x4 = reinterpret_cast<int *>(x);
 
   // Functional cast - This is exactly equivalent to a C-style cast.
   typedef int *INT_PTR;
-  int *x5 = INT_PTR(x);
+  int *x5 = INT_PTR(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
 
   NODEREF Child *child;
   Child *child2 = dynamic_cast<Child *>(child); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
Index: clang/lib/Sema/SemaCast.cpp
===================================================================
--- clang/lib/Sema/SemaCast.cpp
+++ clang/lib/Sema/SemaCast.cpp
@@ -164,9 +164,9 @@
 
   void CheckNoDeref(Sema &S, const QualType FromType, const QualType ToType,
                     SourceLocation OpLoc) {
-    if (const auto *PtrType = dyn_cast<PointerType>(FromType)) {
+    if (const auto *PtrType = FromType->getAs<PointerType>()) {
       if (PtrType->getPointeeType()->hasAttr(attr::NoDeref)) {
-        if (const auto *DestType = dyn_cast<PointerType>(ToType)) {
+        if (const auto *DestType = ToType->getAs<PointerType>()) {
           if (!DestType->getPointeeType()->hasAttr(attr::NoDeref)) {
             S.Diag(OpLoc, diag::warn_noderef_to_dereferenceable_pointer);
           }
@@ -1032,6 +1032,8 @@
 /// like this:
 /// char *bytes = reinterpret_cast\<char*\>(int_ptr);
 void CastOperation::CheckReinterpretCast() {
+  CheckNoDerefRAII NoderefCheck(*this);
+
   if (ValueKind == VK_RValue && !isPlaceholder(BuiltinType::Overload))
     SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
   else
@@ -2489,6 +2491,7 @@
 void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle,
                                        bool ListInitialization) {
   assert(Self.getLangOpts().CPlusPlus);
+  CheckNoDerefRAII NoderefCheck(*this);
 
   // Handle placeholders.
   if (isPlaceholder()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84202.279339.patch
Type: text/x-patch
Size: 2913 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200721/12c40423/attachment-0001.bin>


More information about the cfe-commits mailing list