CBMC
c_defines.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: C Defines
4 
5 Author: Daniel Kroening, dkr@amazon.com
6 
7 \*******************************************************************/
8 
11 
12 #include "c_defines.h"
13 
14 #include "cscanner.h"
15 
16 #include <util/prefix.h>
17 #include <util/string_utils.h>
18 
19 #include <sstream>
20 
21 void c_definest::parse(const std::string &src)
22 {
23  const auto lines = split_string(src, '\n');
24  for(const auto &line : lines)
25  {
26  // #define __x86_64__ 1
27  // #define getc_unlocked(fp) __sgetc(fp)
28  if(!has_prefix(line, "#define "))
29  continue;
30 
31  auto space_pos = line.find(' ', 8);
32  if(space_pos == std::string::npos)
33  continue;
34 
35  auto id = line.substr(8, space_pos - 8);
36  auto value = line.substr(space_pos + 1, std::string::npos);
37  map[id].value = value;
38  }
39 }
40 
41 std::string c_definest::operator()(const std::string &src) const
42 {
43  // tokenize
44  std::istringstream in(src);
45  cscannert cscanner(in);
46  const auto tokens = cscanner.get_tokens();
47 
48  // output
49  std::ostringstream out;
50  for(auto &t : tokens)
51  {
52  if(is_identifier(t))
53  {
54  auto m_it = map.find(t.text);
55  if(m_it != map.end())
56  {
57  out << m_it->second.value;
58  }
59  else
60  out << t.text;
61  }
62  else
63  out << t.text;
64  out << " ";
65  }
66 
67  auto result = out.str();
68  result.pop_back();
69 
70  return result;
71 }
c_defines
mapt map
Definition: c_defines.h:32
std::string operator()(const std::string &) const
Definition: c_defines.cpp:41
void parse(const std::string &)
Definition: c_defines.cpp:21
std::vector< ctokent > get_tokens()
Definition: cscanner.cpp:41
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
cscanner
static bool is_identifier(int token)
Definition: parse.cpp:420
void split_string(const std::string &s, char delim, std::vector< std::string > &result, bool strip, bool remove_empty)