[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:52:42 PST 2019


xbolva00 updated this revision to Diff 189073.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D58882/new/

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 terminate (void) { }
+}
+
+namespace mystd {
+    void terminate (void) { }
+}
+
+auto warn1(void) {
+    return &std::terminate; // expected-warning{{taking the address of the standard library function 'std::terminate' is not allowed}}
+}
+
+auto warn2(void) {
+    return &(std::terminate); // expected-warning{{taking the address of the standard library function 'std::terminate' is not allowed}}
+}
+
+auto nowarn1(void) {
+    return &mystd::terminate;
+}
+
+auto nowarn2(void) {
+    return &(mystd::terminate);
+}
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -12048,8 +12048,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.189073.patch
Type: text/x-patch
Size: 2847 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190303/5dd44ad8/attachment-0001.bin>


More information about the cfe-commits mailing list