[clang] [Clang] add support for handling assume attributes (PR #101063)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 29 11:51:35 PDT 2024


https://github.com/vortex73 created https://github.com/llvm/llvm-project/pull/101063

None

>From 97de745341dfc17e7ad9c595d239346a9114a4c6 Mon Sep 17 00:00:00 2001
From: Narayan Sreekumar <nsreekumar6 at gmail.com>
Date: Tue, 30 Jul 2024 00:18:46 +0530
Subject: [PATCH] [Clang] add support for handling assume attributes

---
 .../Checkers/BuiltinFunctionChecker.cpp       | 25 +++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp
index b198b1c2ff4d1..1a4eb88fb6ab2 100644
--- a/clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp
@@ -22,6 +22,11 @@
 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
+#include <clang/AST/Stmt.h>
+#include <clang/Basic/LLVM.h>
+#include <clang/StaticAnalyzer/Core/PathSensitive/SVals.h>
+#include <cstdio>
+#include <llvm/Support/raw_ostream.h>
 
 using namespace clang;
 using namespace ento;
@@ -77,6 +82,26 @@ bool BuiltinFunctionChecker::evalCall(const CallEvent &Call,
   const LocationContext *LCtx = C.getLocationContext();
   const Expr *CE = Call.getOriginExpr();
 
+  if (const auto *AttrStmt = dyn_cast<AttributedStmt>(CE)) {
+      for (const Attr *I : AttrStmt->getAttrs()) {
+          if (const auto *AssumeAttr = dyn_cast<CXXAssumeAttr>(I)) {
+              const Expr *AssumeExpr = AssumeAttr->getAssumption();
+              SVal Arg = C.getSVal(AssumeExpr);
+              if (Arg.isUndef())
+                  return true;
+
+              state = state->assume(Arg.castAs<DefinedOrUnknownSVal>(), true);
+              if (!state) {
+                  C.generateSink(C.getState(), C.getPredecessor());
+                  return true;
+              }
+
+              C.addTransition(state);
+              return true;
+          } 
+      }
+  }
+
   if (isBuiltinLikeFunction(Call)) {
     C.addTransition(state->BindExpr(CE, LCtx, Call.getArgSVal(0)));
     return true;



More information about the cfe-commits mailing list