[PATCH] D15924: [analyzer] Utility to extract the variable name from a memory region.

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 6 06:35:15 PST 2016


xazax.hun created this revision.
xazax.hun added reviewers: zaks.anna, dcoughlin, Alexander_Droste.
xazax.hun added subscribers: cfe-commits, dkrupp.

This patch adds a small utility to extract variable name from a memory region.
This patch does not contain any test or user yet.
There are however two differential revisions that needs similar functionality. It would be great to decide what implementation to use and make it available as a general utility.

The two potential users: http://reviews.llvm.org/D12761 and http://reviews.llvm.org/D15227

http://reviews.llvm.org/D15924

Files:
  include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h
  lib/StaticAnalyzer/Core/MemRegion.cpp

Index: lib/StaticAnalyzer/Core/MemRegion.cpp
===================================================================
--- lib/StaticAnalyzer/Core/MemRegion.cpp
+++ lib/StaticAnalyzer/Core/MemRegion.cpp
@@ -573,6 +573,19 @@
   return false;
 }
 
+StringRef MemRegion::getVariableName() const {
+  const MemRegion *reg = this;
+  while (const auto *ereg = dyn_cast<ElementRegion>(reg))
+    reg = ereg->getSuperRegion();
+  const auto *vreg = dyn_cast<DeclRegion>(reg);
+  if (!vreg)
+    return "";
+  const auto *nd = dyn_cast<NamedDecl>(vreg->getDecl());
+  if (!nd)
+    return "";
+  return nd->getName();
+}
+
 void MemRegion::printPretty(raw_ostream &os) const {
   assert(canPrintPretty() && "This region cannot be printed pretty.");
   os << "'";
Index: include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h
===================================================================
--- include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h
+++ include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h
@@ -176,6 +176,11 @@
   /// as part of a larger expression.
   virtual bool canPrintPrettyAsExpr() const;
 
+  /// \brief In case this region is a region of a variable or an element region,
+  /// this method will return the name of the variable. Otherwise it will return
+  /// an empty StringRef.
+  StringRef getVariableName() const;
+
   /// \brief Print the region as expression.
   ///
   /// When this region represents a subexpression, the method is for printing


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15924.44116.patch
Type: text/x-patch
Size: 1483 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160106/3f2aa33d/attachment.bin>


More information about the cfe-commits mailing list