CBMC
name_mangler.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Name mangling
4 
5 Author: Kareem Khazem <karkhaz@karkhaz.com>, 2019
6 
7 \*******************************************************************/
8 
9 #include "name_mangler.h"
10 
11 #include <util/get_base_name.h>
12 
13 #include <cstdint>
14 #include <iomanip>
15 #include <sstream>
16 
18 operator()(const symbolt &src, const std::string &extra_info)
19 {
20  std::string basename = get_base_name(src.location.get_file().c_str(), false);
21 
22  std::stringstream ss;
23  ss << FILE_LOCAL_PREFIX;
24  ss << std::regex_replace(
25  std::regex_replace(basename, forbidden, "_"), multi_under, "_")
26  << "_";
27 
28  if(extra_info != "")
29  ss << extra_info << "_";
30  ss << src.name;
31  return irep_idt(ss.str());
32 }
33 
35 operator()(const symbolt &src, const std::string &extra_info)
36 {
37  char const *str = src.location.get_working_directory().c_str();
38  unsigned long hash = 5381;
39  int c;
40  while((c = *str++))
41  hash = ((hash << 5) + hash) + c;
42 
43  uint32_t eight_nibble_hash = (uint32_t)hash;
44 
45  std::stringstream ss;
46  ss << FILE_LOCAL_PREFIX << std::setfill('0') << std::setw(8) << std::hex
47  << eight_nibble_hash << "_" << extra_info << "_" << src.name;
48  return irep_idt(ss.str());
49 }
irep_idt operator()(const symbolt &, const std::string &)
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
const char * c_str() const
Definition: dstring.h:116
irep_idt operator()(const symbolt &, const std::string &)
const std::regex multi_under
Definition: name_mangler.h:158
const std::regex forbidden
Definition: name_mangler.h:157
const irep_idt & get_working_directory() const
const irep_idt & get_file() const
Symbol table entry.
Definition: symbol.h:28
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
irep_idt name
The unique identifier.
Definition: symbol.h:40
std::string get_base_name(const std::string &in, bool strip_suffix)
cleans a filename from path and extension
Mangle names of file-local functions to make them unique.
#define FILE_LOCAL_PREFIX
Definition: name_mangler.h:16
dstringt irep_idt