Alexander_Droste updated this revision to Diff 48953. Alexander_Droste added a comment.
- remove string idx variable -> remove string copy assignments - use Twine to reduce temporary string objects, built during concatenation Is `ArrayIndices = llvm::Twine(ArrayIndices + "]" + intValAsString.str() + "[").str();` preferable to: `ArrayIndices += llvm::Twine("]" + intValAsString.str() + "[").str();` with respect to efficiency? If not, I would prefer the second version, as it is a bit more concise. http://reviews.llvm.org/D16044 Files: include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h lib/StaticAnalyzer/Core/MemRegion.cpp Index: include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h =================================================================== --- include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h +++ include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h @@ -150,6 +150,14 @@ template<typename RegionTy> const RegionTy* getAs() const; virtual bool isBoundable() const { return false; } + + /// Get variable name for memory region. The name is obtained from + /// the variable/field declaration retrieved from the memory region. + /// Regions that point to an element of an array are returned as: "arr[0]". + /// Regions that point to a struct are returned as: "st.var". + /// + /// \returns variable name for memory region + std::string getVariableName() const; }; /// MemSpaceRegion - A memory region that represents a "memory space"; Index: lib/StaticAnalyzer/Core/MemRegion.cpp =================================================================== --- lib/StaticAnalyzer/Core/MemRegion.cpp +++ lib/StaticAnalyzer/Core/MemRegion.cpp @@ -632,6 +632,49 @@ superRegion->printPrettyAsExpr(os); } +std::string MemRegion::getVariableName() const { + std::string VariableName; + std::string ArrayIndices; + const MemRegion *R = this; + SmallString<50> buf; + llvm::raw_svector_ostream os(buf); + + // Obtain array indices to add them to the variable name. + const ElementRegion *ER = nullptr; + while ((ER = R->getAs<ElementRegion>())) { + // Index is a ConcreteInt. + if (auto CI = ER->getIndex().getAs<nonloc::ConcreteInt>()) { + llvm::SmallString<2> intValAsString; + CI->getValue().toString(intValAsString); + ArrayIndices = + llvm::Twine(ArrayIndices + "]" + intValAsString.str() + "[").str(); + } + // If not a ConcreteInt, try to obtain the variable + // name by calling 'getVariableName()' recursively. + else { + std::string idx = ER->getVariableName(); + if (!idx.empty()) { + // Substring to exclude single quotes surrounding the index var name. + ArrayIndices = llvm::Twine(ArrayIndices + "]" + + idx.substr(1, idx.size() - 2) + "[") + .str(); + } + } + R = ER->getSuperRegion(); + } + std::reverse(ArrayIndices.begin(), ArrayIndices.end()); + + // Get variable name. + if (R && R->canPrintPretty()) { + R->printPretty(os); + VariableName = os.str(); + // Combine variable name with indices. + VariableName.insert(VariableName.size() - 1, ArrayIndices); + } + + return VariableName; +} + //===----------------------------------------------------------------------===// // MemRegionManager methods. //===----------------------------------------------------------------------===//
Index: include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h =================================================================== --- include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h +++ include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h @@ -150,6 +150,14 @@ template<typename RegionTy> const RegionTy* getAs() const; virtual bool isBoundable() const { return false; } + + /// Get variable name for memory region. The name is obtained from + /// the variable/field declaration retrieved from the memory region. + /// Regions that point to an element of an array are returned as: "arr[0]". + /// Regions that point to a struct are returned as: "st.var". + /// + /// \returns variable name for memory region + std::string getVariableName() const; }; /// MemSpaceRegion - A memory region that represents a "memory space"; Index: lib/StaticAnalyzer/Core/MemRegion.cpp =================================================================== --- lib/StaticAnalyzer/Core/MemRegion.cpp +++ lib/StaticAnalyzer/Core/MemRegion.cpp @@ -632,6 +632,49 @@ superRegion->printPrettyAsExpr(os); } +std::string MemRegion::getVariableName() const { + std::string VariableName; + std::string ArrayIndices; + const MemRegion *R = this; + SmallString<50> buf; + llvm::raw_svector_ostream os(buf); + + // Obtain array indices to add them to the variable name. + const ElementRegion *ER = nullptr; + while ((ER = R->getAs<ElementRegion>())) { + // Index is a ConcreteInt. + if (auto CI = ER->getIndex().getAs<nonloc::ConcreteInt>()) { + llvm::SmallString<2> intValAsString; + CI->getValue().toString(intValAsString); + ArrayIndices = + llvm::Twine(ArrayIndices + "]" + intValAsString.str() + "[").str(); + } + // If not a ConcreteInt, try to obtain the variable + // name by calling 'getVariableName()' recursively. + else { + std::string idx = ER->getVariableName(); + if (!idx.empty()) { + // Substring to exclude single quotes surrounding the index var name. + ArrayIndices = llvm::Twine(ArrayIndices + "]" + + idx.substr(1, idx.size() - 2) + "[") + .str(); + } + } + R = ER->getSuperRegion(); + } + std::reverse(ArrayIndices.begin(), ArrayIndices.end()); + + // Get variable name. + if (R && R->canPrintPretty()) { + R->printPretty(os); + VariableName = os.str(); + // Combine variable name with indices. + VariableName.insert(VariableName.size() - 1, ArrayIndices); + } + + return VariableName; +} + //===----------------------------------------------------------------------===// // MemRegionManager methods. //===----------------------------------------------------------------------===//
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits