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


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
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to