Static Value-Flow Analysis
Functions | Variables
CppUtil.cpp File Reference
#include "SVF-LLVM/CppUtil.h"
#include "SVF-LLVM/BasicTypes.h"
#include "SVF-LLVM/LLVMUtil.h"
#include "Util/Casting.h"
#include "Util/SVFUtil.h"
#include "SVF-LLVM/LLVMModule.h"
#include "SVF-LLVM/ObjTypeInference.h"
#include <cxxabi.h>

Go to the source code of this file.

Functions

static bool isOperOverload (const std::string &name)
 
static std::string getBeforeParenthesis (const std::string &name)
 
static void handleThunkFunction (cppUtil::DemangledName &dname)
 
void stripBracketsAndNamespace (cppUtil::DemangledName &dname)
 
std::vector< std::stringfindInnermostBrackets (const std::string &input)
 
std::string stripWhitespaces (const std::string &str)
 
std::vector< std::stringsplitAndStrip (const std::string &input, char delimiter)
 

Variables

const std::string vtblLabelAfterDemangle = "vtable for "
 
const std::string NVThunkFunLabel = "non-virtual thunk to "
 
const std::string VThunkFuncLabel = "virtual thunk to "
 
const std::string vtblLabelBeforeDemangle = "_ZTV"
 
const std::string vfunPreLabel = "_Z"
 
const std::string clsName = "class."
 
const std::string structName = "struct."
 
const std::string vtableType = "(...)**"
 
const std::string znwm = "_Znwm"
 
const std::string zn1Label = "_ZN1"
 
const std::string znstLabel = "_ZNSt"
 
const std::string znst5Label = "_ZNSt5"
 
const std::string znst12Label = "_ZNSt12"
 
const std::string znst6Label = "_ZNSt6"
 
const std::string znst7Label = "_ZNSt7"
 
const std::string znst14Label = "_ZNSt14"
 
const std::string znkstLabel = "_ZNKSt"
 
const std::string znkst5Label = "_ZNKSt15_"
 
const std::string znkst20Label = "_ZNKSt20_"
 
const std::string znkst23Label = "_ZNKSt23_"
 
const std::string znkLabel = "_ZNK"
 
const std::string znk9Label = "_ZNK9"
 
const std::string ztilabel = "_ZTI"
 
const std::string ztiprefix = "typeinfo for "
 
const std::string dyncast = "__dynamic_cast"
 

Function Documentation

◆ findInnermostBrackets()

std::vector<std::string> findInnermostBrackets ( const std::string input)

find the innermost brackets, e.g., return "int const, A" for "__gnu_cxx::__aligned_membuf<std::pair<int const, A> >::_M_ptr() const"

Parameters
input
Returns

Definition at line 735 of file CppUtil.cpp.

736 {
737  typedef std::pair<u32_t, u32_t> StEdIdxPair;
738  std::stack<int> stack;
739  std::vector<StEdIdxPair> innerMostPairs;
740  std::vector<bool> used(input.length(), false);
741 
742  for (u32_t i = 0; i < input.length(); ++i)
743  {
744  if (input[i] == '<')
745  {
746  stack.push(i);
747  }
748  else if (input[i] == '>' && i > 0 && input[i - 1] != '-')
749  {
750  if (!stack.empty())
751  {
752  int openIndex = stack.top();
753  stack.pop();
754 
755  // Check if this pair is innermost
756  bool isInnermost = true;
757  for (u32_t j = openIndex + 1; j < i && isInnermost; ++j)
758  {
759  if (used[j])
760  {
761  isInnermost = false;
762  }
763  }
764 
765  if (isInnermost)
766  {
767  innerMostPairs.emplace_back(openIndex, i);
768  used[openIndex] = used[i] = true; // Mark these indices as used
769  }
770  }
771  }
772  }
773  std::vector<std::string> ans(innerMostPairs.size());
774  std::transform(innerMostPairs.begin(), innerMostPairs.end(), ans.begin(), [&input](StEdIdxPair &p) -> std::string
775  {
776  return input.substr(p.first + 1, p.second - p.first - 1);
777  });
778  return ans;
779 }
unsigned u32_t
Definition: CommandLine.h:18
cJSON * p
Definition: cJSON.cpp:2559
const char *const string
Definition: cJSON.h:172

◆ getBeforeParenthesis()

static std::string getBeforeParenthesis ( const std::string name)
static

Definition at line 107 of file CppUtil.cpp.

108 {
109  size_t lastRightParen = name.rfind(')');
110  assert(lastRightParen > 0);
111 
112  s32_t paren_num = 1, pos;
113  for (pos = lastRightParen - 1; pos >= 0; pos--)
114  {
115  if (name[pos] == ')')
116  paren_num++;
117  if (name[pos] == '(')
118  paren_num--;
119  if (paren_num == 0)
120  break;
121  }
122  return name.substr(0, pos);
123 }
const char *const name
Definition: cJSON.h:264
signed s32_t
Definition: GeneralType.h:47

◆ handleThunkFunction()

static void handleThunkFunction ( cppUtil::DemangledName dname)
static

Definition at line 146 of file CppUtil.cpp.

147 {
148  // when handling multi-inheritance,
149  // the compiler may generate thunk functions
150  // to perform `this` pointer adjustment
151  // they are indicated with `virtual thunk to `
152  // and `nun-virtual thunk to`.
153  // if the classname starts with part of a
154  // demangled name starts with
155  // these prefixes, we need to remove the prefix
156  // to get the real class name
157 
158  static std::vector<std::string> thunkPrefixes = {VThunkFuncLabel,
160  };
161  for (unsigned i = 0; i < thunkPrefixes.size(); i++)
162  {
163  auto prefix = thunkPrefixes[i];
164  if (dname.className.size() > prefix.size() &&
165  dname.className.compare(0, prefix.size(), prefix) == 0)
166  {
167  dname.className = dname.className.substr(prefix.size());
168  dname.isThunkFunc = true;
169  return;
170  }
171  }
172 }
const std::string VThunkFuncLabel
Definition: CppUtil.cpp:47
const std::string NVThunkFunLabel
Definition: CppUtil.cpp:46

◆ isOperOverload()

static bool isOperOverload ( const std::string name)
static

Definition at line 84 of file CppUtil.cpp.

85 {
86  u32_t leftnum = 0, rightnum = 0;
87  std::string subname = name;
88  size_t leftpos, rightpos;
89  leftpos = subname.find('<');
90  while (leftpos != std::string::npos)
91  {
92  subname = subname.substr(leftpos + 1);
93  leftpos = subname.find('<');
94  leftnum++;
95  }
96  subname = name;
97  rightpos = subname.find('>');
98  while (rightpos != std::string::npos)
99  {
100  subname = subname.substr(rightpos + 1);
101  rightpos = subname.find('>');
102  rightnum++;
103  }
104  return leftnum != rightnum;
105 }

◆ splitAndStrip()

std::vector<std::string> splitAndStrip ( const std::string input,
char  delimiter 
)

Definition at line 800 of file CppUtil.cpp.

801 {
802  std::vector<std::string> tokens;
803  size_t start = 0, end = 0;
804 
805  while ((end = input.find(delimiter, start)) != std::string::npos)
806  {
807  tokens.push_back(stripWhitespaces(input.substr(start, end - start)));
808  start = end + 1;
809  }
810 
811  tokens.push_back(stripWhitespaces(input.substr(start)));
812 
813  return tokens;
814 }
std::string stripWhitespaces(const std::string &str)
Definition: CppUtil.cpp:786

◆ stripBracketsAndNamespace()

void stripBracketsAndNamespace ( cppUtil::DemangledName dname)

strip off brackets and namespace from classname e.g., for ‘namespace::A<...::...>::f’, we get ‘A’ by stripping off namespace and <>

Definition at line 472 of file CppUtil.cpp.

473 {
476  size_t colon = dname.className.rfind("::");
477  if (colon == std::string::npos)
478  {
480  }
481  else
482  {
483  // strip off namespace
484  dname.className =
485  cppUtil::getBeforeBrackets(dname.className.substr(colon + 2));
486  }
487 }
std::string getBeforeBrackets(const std::string &name)
Definition: CppUtil.cpp:127

◆ stripWhitespaces()

std::string stripWhitespaces ( const std::string str)

strip off the whitespaces from the beginning and ending of str

Parameters
str
Returns

Definition at line 786 of file CppUtil.cpp.

787 {
788  auto start = std::find_if(str.begin(), str.end(), [](unsigned char ch)
789  {
790  return !std::isspace(ch);
791  });
792  auto end = std::find_if(str.rbegin(), str.rend(), [](unsigned char ch)
793  {
794  return !std::isspace(ch);
795  }).base();
796 
797  return (start < end) ? std::string(start, end) : std::string();
798 }

Variable Documentation

◆ clsName

const std::string clsName = "class."

Definition at line 55 of file CppUtil.cpp.

◆ dyncast

const std::string dyncast = "__dynamic_cast"

Definition at line 81 of file CppUtil.cpp.

◆ NVThunkFunLabel

const std::string NVThunkFunLabel = "non-virtual thunk to "

Definition at line 46 of file CppUtil.cpp.

◆ structName

const std::string structName = "struct."

Definition at line 56 of file CppUtil.cpp.

◆ vfunPreLabel

const std::string vfunPreLabel = "_Z"

Definition at line 53 of file CppUtil.cpp.

◆ vtableType

const std::string vtableType = "(...)**"

Definition at line 57 of file CppUtil.cpp.

◆ vtblLabelAfterDemangle

const std::string vtblLabelAfterDemangle = "vtable for "

Definition at line 43 of file CppUtil.cpp.

◆ vtblLabelBeforeDemangle

const std::string vtblLabelBeforeDemangle = "_ZTV"

Definition at line 50 of file CppUtil.cpp.

◆ VThunkFuncLabel

const std::string VThunkFuncLabel = "virtual thunk to "

Definition at line 47 of file CppUtil.cpp.

◆ zn1Label

const std::string zn1Label = "_ZN1"

Definition at line 60 of file CppUtil.cpp.

◆ znk9Label

const std::string znk9Label = "_ZNK9"

Definition at line 77 of file CppUtil.cpp.

◆ znkLabel

const std::string znkLabel = "_ZNK"

Definition at line 76 of file CppUtil.cpp.

◆ znkst20Label

const std::string znkst20Label = "_ZNKSt20_"

Definition at line 71 of file CppUtil.cpp.

◆ znkst23Label

const std::string znkst23Label = "_ZNKSt23_"

Definition at line 73 of file CppUtil.cpp.

◆ znkst5Label

const std::string znkst5Label = "_ZNKSt15_"

Definition at line 70 of file CppUtil.cpp.

◆ znkstLabel

const std::string znkstLabel = "_ZNKSt"

Definition at line 69 of file CppUtil.cpp.

◆ znst12Label

const std::string znst12Label = "_ZNSt12"

Definition at line 63 of file CppUtil.cpp.

◆ znst14Label

const std::string znst14Label = "_ZNSt14"

Definition at line 66 of file CppUtil.cpp.

◆ znst5Label

const std::string znst5Label = "_ZNSt5"

Definition at line 62 of file CppUtil.cpp.

◆ znst6Label

const std::string znst6Label = "_ZNSt6"

Definition at line 64 of file CppUtil.cpp.

◆ znst7Label

const std::string znst7Label = "_ZNSt7"

Definition at line 65 of file CppUtil.cpp.

◆ znstLabel

const std::string znstLabel = "_ZNSt"

Definition at line 61 of file CppUtil.cpp.

◆ znwm

const std::string znwm = "_Znwm"

Definition at line 59 of file CppUtil.cpp.

◆ ztilabel

const std::string ztilabel = "_ZTI"

Definition at line 79 of file CppUtil.cpp.

◆ ztiprefix

const std::string ztiprefix = "typeinfo for "

Definition at line 80 of file CppUtil.cpp.