[PATCH] D109467: [analyzer] check for std::__addressof for inner pointer checker

Balázs Benics via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 26 00:51:42 PDT 2022


steakhal updated this revision to Diff 425139.
steakhal edited the summary of this revision.
steakhal added a comment.

- rebased (now using `matchesAny(...)`
- added a test


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D109467

Files:
  clang/lib/StaticAnalyzer/Checkers/InnerPointerChecker.cpp
  clang/test/Analysis/inner-pointer.cpp


Index: clang/test/Analysis/inner-pointer.cpp
===================================================================
--- clang/test/Analysis/inner-pointer.cpp
+++ clang/test/Analysis/inner-pointer.cpp
@@ -20,6 +20,9 @@
 template <class T>
 T *addressof(T &arg);
 
+template <class T>
+T *__addressof(T &arg);
+
 char *data(std::string &c);
 
 } // end namespace std
@@ -383,6 +386,14 @@
   consume(c); // no-warning
 }
 
+void func_AddressofFn_() {
+  const char *c;
+  std::string s;
+  c = s.c_str();
+  (void)std::__addressof(s);
+  consume(c); // no-warning
+}
+
 void func_std_data() {
   const char *c;
   std::string s;
Index: clang/lib/StaticAnalyzer/Checkers/InnerPointerChecker.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Checkers/InnerPointerChecker.cpp
+++ clang/lib/StaticAnalyzer/Checkers/InnerPointerChecker.cpp
@@ -35,9 +35,9 @@
 class InnerPointerChecker
     : public Checker<check::DeadSymbols, check::PostCall> {
 
-  CallDescription AppendFn, AssignFn, AddressofFn, ClearFn, CStrFn, DataFn,
-      DataMemberFn, EraseFn, InsertFn, PopBackFn, PushBackFn, ReplaceFn,
-      ReserveFn, ResizeFn, ShrinkToFitFn, SwapFn;
+  CallDescription AppendFn, AssignFn, AddressofFn, AddressofFn_, ClearFn,
+      CStrFn, DataFn, DataMemberFn, EraseFn, InsertFn, PopBackFn, PushBackFn,
+      ReplaceFn, ReserveFn, ResizeFn, ShrinkToFitFn, SwapFn;
 
 public:
   class InnerPointerBRVisitor : public BugReporterVisitor {
@@ -74,7 +74,7 @@
   InnerPointerChecker()
       : AppendFn({"std", "basic_string", "append"}),
         AssignFn({"std", "basic_string", "assign"}),
-        AddressofFn({"std", "addressof"}),
+        AddressofFn({"std", "addressof"}), AddressofFn_({"std", "__addressof"}),
         ClearFn({"std", "basic_string", "clear"}),
         CStrFn({"std", "basic_string", "c_str"}), DataFn({"std", "data"}, 1),
         DataMemberFn({"std", "basic_string", "data"}),
@@ -179,9 +179,9 @@
       if (!ArgRegion)
         continue;
 
-      // std::addressof function accepts a non-const reference as an argument,
+      // std::addressof functions accepts a non-const reference as an argument,
       // but doesn't modify it.
-      if (AddressofFn.matches(Call))
+      if (matchesAny(Call, AddressofFn, AddressofFn_))
         continue;
 
       markPtrSymbolsReleased(Call, State, ArgRegion, C);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109467.425139.patch
Type: text/x-patch
Size: 2373 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220426/7217b778/attachment-0001.bin>


More information about the cfe-commits mailing list