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 <iomanip>
14 #include <sstream>
15 
17 operator()(const symbolt &src, const std::string &extra_info)
18 {
19  std::string basename = get_base_name(src.location.get_file().c_str(), false);
20 
21  std::stringstream ss;
22  ss << FILE_LOCAL_PREFIX;
23  ss << std::regex_replace(
24  std::regex_replace(basename, forbidden, "_"), multi_under, "_")
25  << "_";
26 
27  if(extra_info != "")
28  ss << extra_info << "_";
29  ss << src.name;
30  return irep_idt(ss.str());
31 }
32 
34 operator()(const symbolt &src, const std::string &extra_info)
35 {
36  char const *str = src.location.get_working_directory().c_str();
37  unsigned long hash = 5381;
38  int c;
39  while((c = *str++))
40  hash = ((hash << 5) + hash) + c;
41 
42  uint32_t eight_nibble_hash = (uint32_t)hash;
43 
44  std::stringstream ss;
45  ss << FILE_LOCAL_PREFIX << std::setfill('0') << std::setw(8) << std::hex
46  << eight_nibble_hash << "_" << extra_info << "_" << src.name;
47  return irep_idt(ss.str());
48 }
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
get_base_name
std::string get_base_name(const std::string &in, bool strip_suffix)
cleans a filename from path and extension
Definition: get_base_name.cpp:16
dstringt::c_str
const char * c_str() const
Definition: dstring.h:115
file_name_manglert::forbidden
const std::regex forbidden
Definition: name_mangler.h:154
file_name_manglert::multi_under
const std::regex multi_under
Definition: name_mangler.h:155
djb_manglert::operator()
irep_idt operator()(const symbolt &, const std::string &)
Definition: name_mangler.cpp:34
file_name_manglert::operator()
irep_idt operator()(const symbolt &, const std::string &)
Definition: name_mangler.cpp:17
FILE_LOCAL_PREFIX
#define FILE_LOCAL_PREFIX
Definition: name_mangler.h:16
irep_idt
dstringt irep_idt
Definition: irep.h:37
get_base_name.h
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
symbolt
Symbol table entry.
Definition: symbol.h:27
source_locationt::get_file
const irep_idt & get_file() const
Definition: source_location.h:35
source_locationt::get_working_directory
const irep_idt & get_working_directory() const
Definition: source_location.h:40
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
name_mangler.h
Mangle names of file-local functions to make them unique.