[PATCH] D58882: [Diagnostics] Address of a standard library function

Dávid Bolvanský via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 2 23:33:49 PST 2019


xbolva00 created this revision.
xbolva00 added a reviewer: rsmith.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

"Taking the address of a standard library function is not allowed" - https://usingstdcpp.org/2018/03/18/jacksonville18-iso-cpp-report/


Repository:
  rC Clang

https://reviews.llvm.org/D58882

Files:
  include/clang/Basic/DiagnosticGroups.td
  include/clang/Basic/DiagnosticSemaKinds.td
  lib/Sema/SemaExpr.cpp
  test/SemaCXX/warn_address_standard_library_function.cpp


Index: test/SemaCXX/warn_address_standard_library_function.cpp
===================================================================
--- test/SemaCXX/warn_address_standard_library_function.cpp
+++ test/SemaCXX/warn_address_standard_library_function.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Waddress -std=c++14 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Waddress -std=c++17 %s
+
+namespace std {
+    void foo (void) { }
+}
+
+namespace mystd {
+    void foo (void) { }
+}
+
+auto warn1(void) {
+    return &std::foo; // expected-warning{{taking the address of the standard library function 'std::foo' is not allowed}}
+}
+
+auto warn2(void) {
+    return &(std::foo); // expected-warning{{taking the address of the standard library function 'std::foo' is not allowed}}
+}
+
+auto nowarn1(void) {
+    return &mystd::foo;
+}
+
+auto nowarn2(void) {
+    return &(mystd::foo);
+}
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -11934,7 +11934,6 @@
 
   Expr::LValueClassification lval = op->ClassifyLValue(Context);
   unsigned AddressOfError = AO_No_Error;
-
   if (lval == Expr::LV_ClassTemporary || lval == Expr::LV_ArrayTemporary) {
     bool sfinae = (bool)isSFINAEContext();
     Diag(OpLoc, isSFINAEContext() ? diag::err_typecheck_addrof_temporary
@@ -12048,8 +12047,11 @@
           return MPTy;
         }
       }
-    } else if (!isa<FunctionDecl>(dcl) && !isa<NonTypeTemplateParmDecl>(dcl) &&
-               !isa<BindingDecl>(dcl))
+    } else if (isa<FunctionDecl>(dcl)) {
+      StringRef QualName =  cast<FunctionDecl>(dcl)->getQualifiedNameAsString();
+      if (QualName.startswith("std::"))
+        Diag(OpLoc, diag::warn_taking_address_standard_library_function) << QualName;
+    } else if (!isa<NonTypeTemplateParmDecl>(dcl) && !isa<BindingDecl>(dcl))
       llvm_unreachable("Unknown/unexpected decl type");
   }
 
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -6730,6 +6730,9 @@
 
 def err_cannot_form_pointer_to_member_of_reference_type : Error<
   "cannot form a pointer-to-member to member %0 of reference type %1">;
+def warn_taking_address_standard_library_function : Warning<
+  "taking the address of the standard library function '%0' is not allowed">
+  , InGroup<Address>;
 def err_incomplete_object_call : Error<
   "incomplete type in call to object of type %0">;
 
Index: include/clang/Basic/DiagnosticGroups.td
===================================================================
--- include/clang/Basic/DiagnosticGroups.td
+++ include/clang/Basic/DiagnosticGroups.td
@@ -687,7 +687,7 @@
 // Aggregation warning settings.
 
 // Populate -Waddress with warnings from other groups.
-def : DiagGroup<"address", [PointerBoolConversion,
+def Address : DiagGroup<"address", [PointerBoolConversion,
                             StringCompare,
                             TautologicalPointerCompare]>;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58882.189070.patch
Type: text/x-patch
Size: 3131 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190303/b8ad8767/attachment.bin>


More information about the cfe-commits mailing list