[PATCH] D152246: [clang][ThreadSafety] Analyze known function pointer values

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 6 01:41:55 PDT 2023


tbaeder created this revision.
tbaeder added reviewers: delesley, aaronpuchert, Eugene.Zelenko, aaron.ballman.
Herald added a reviewer: NoQ.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

When we're calling a function pointer, try to get the currently known value of the variable and analyze calling that instead.

I think ideally we would get all possible values at this point and analyze them all(?), but I'm not sure how to implement this.
I've not added a test case because of the above issue/uncertainty, so I'd like some advice on how to implement that //or// if it's not what should happen.

(I've added everyone with >100 lines in this file as a reviewer, except Caitlin Sadowski, who I can't find in Phabricator.)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152246

Files:
  clang/lib/Analysis/ThreadSafety.cpp


Index: clang/lib/Analysis/ThreadSafety.cpp
===================================================================
--- clang/lib/Analysis/ThreadSafety.cpp
+++ clang/lib/Analysis/ThreadSafety.cpp
@@ -2063,8 +2063,21 @@
     examineArguments(Exp->getDirectCallee(), Exp->arg_begin(), Exp->arg_end());
   }
 
-  auto *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
-  if(!D || !D->hasAttrs())
+  const auto *CD = Exp->getCalleeDecl();
+  if (!CD)
+    return;
+
+  const auto *D = dyn_cast<NamedDecl>(CD);
+  // For function pointers, try to get the currently known
+  // value of the pointer.
+  if (const auto *VD = dyn_cast<VarDecl>(CD)) {
+    if (const Expr *E = Analyzer->LocalVarMap.lookupExpr(D, LVarCtx)) {
+      if (const auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
+        D = DRE->getDecl();
+    }
+  }
+
+  if (!D->hasAttrs())
     return;
   handleCall(Exp, D);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152246.528739.patch
Type: text/x-patch
Size: 900 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230606/86924d97/attachment-0001.bin>


More information about the cfe-commits mailing list