cprover
ansi_c_parser.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_ANSI_C_ANSI_C_PARSER_H
11 #define CPROVER_ANSI_C_ANSI_C_PARSER_H
12 
13 #include <cassert>
14 #include <set>
15 
16 #include <util/parser.h>
17 #include <util/expr.h>
18 #include <util/string_hash.h>
19 #include <util/mp_arith.h>
20 #include <util/config.h>
21 
22 #include "ansi_c_parse_tree.h"
23 #include "ansi_c_scope.h"
24 
26 
27 class ansi_c_parsert:public parsert
28 {
29 public:
31 
33  : tag_following(false),
34  asm_block_following(false),
36  mode(modet::NONE),
37  cpp98(false),
38  cpp11(false),
39  for_has_scope(false),
41  {
42  }
43 
44  virtual bool parse() override
45  {
46  return yyansi_cparse()!=0;
47  }
48 
49  virtual void clear() override
50  {
52  parse_tree.clear();
53 
54  // scanner state
55  tag_following=false;
56  asm_block_following=false;
58  string_literal.clear();
59  pragma_pack.clear();
60  pragma_cprover.clear();
61 
62  // set up global scope
63  scopes.clear();
64  scopes.push_back(scopet());
65  }
66 
67  // internal state of the scanner
71  std::string string_literal;
72  std::list<exprt> pragma_pack;
73  std::list<std::set<irep_idt>> pragma_cprover;
74 
77 
78  // recognize C++98 and C++11 keywords
79  bool cpp98, cpp11;
80 
81  // in C99 and upwards, for(;;) has a scope
83 
84  // ISO/IEC TS 18661-3:2015
86 
89 
90  typedef std::list<scopet> scopest;
92 
94  {
95  return scopes.front();
96  }
97 
98  const scopet &root_scope() const
99  {
100  return scopes.front();
101  }
102 
103  void pop_scope()
104  {
105  scopes.pop_back();
106  }
107 
109  {
110  assert(!scopes.empty());
111  return scopes.back();
112  }
113 
114  enum class decl_typet { TAG, MEMBER, PARAMETER, OTHER };
115 
116  // convert a declarator and then add it to existing an declaration
117  void add_declarator(exprt &declaration, irept &declarator);
118 
119  // adds a tag to the current scope
120  void add_tag_with_body(irept &tag);
121 
122  void copy_item(const ansi_c_declarationt &declaration)
123  {
124  assert(declaration.id()==ID_declaration);
125  parse_tree.items.push_back(declaration);
126  }
127 
128  void new_scope(const std::string &prefix)
129  {
130  const scopet &current=current_scope();
131  scopes.push_back(scopet());
132  scopes.back().prefix=current.prefix+prefix;
133  }
134 
136  const irep_idt &base_name, // in
137  irep_idt &identifier, // out
138  bool tag,
139  bool label);
140 
141  static ansi_c_id_classt get_class(const typet &type);
142 
143  irep_idt lookup_label(const irep_idt base_name)
144  {
145  irep_idt identifier;
146  lookup(base_name, identifier, false, true);
147  return identifier;
148  }
149 
151  {
152  source_location.remove(ID_pragma);
153  for(const auto &pragma_set : pragma_cprover)
154  {
155  for(const auto &pragma : pragma_set)
156  source_location.add_pragma(pragma);
157  }
158  }
159 };
160 
162 
163 int yyansi_cerror(const std::string &error);
165 
166 #endif // CPROVER_ANSI_C_ANSI_C_PARSER_H
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
complexity_violationt::NONE
@ NONE
parsert::source_location
source_locationt source_location
Definition: parser.h:135
ansi_c_parsert::add_declarator
void add_declarator(exprt &declaration, irept &declarator)
Definition: ansi_c_parser.cpp:84
ansi_c_parsert::scopet
ansi_c_scopet scopet
Definition: ansi_c_parser.h:88
mp_arith.h
yyansi_cparse
int yyansi_cparse()
typet
The type of an expression, extends irept.
Definition: type.h:29
ansi_c_parse_tree.h
ansi_c_parsert::cpp98
bool cpp98
Definition: ansi_c_parser.h:79
ansi_c_id_classt
ansi_c_id_classt
Definition: ansi_c_scope.h:18
ansi_c_parsert::decl_typet
decl_typet
Definition: ansi_c_parser.h:114
ansi_c_parse_treet::items
itemst items
Definition: ansi_c_parse_tree.h:22
source_locationt::add_pragma
void add_pragma(const irep_idt &pragma)
Definition: source_location.h:195
parsert::clear
virtual void clear()
Definition: parser.h:32
exprt
Base class for all expressions.
Definition: expr.h:53
ansi_c_parsert::modet
configt::ansi_ct::flavourt modet
Definition: ansi_c_parser.h:75
ansi_c_scopet::prefix
std::string prefix
Definition: ansi_c_scope.h:47
ansi_c_parsert::parenthesis_counter
unsigned parenthesis_counter
Definition: ansi_c_parser.h:70
ansi_c_parsert::copy_item
void copy_item(const ansi_c_declarationt &declaration)
Definition: ansi_c_parser.h:122
ansi_c_parsert::new_scope
void new_scope(const std::string &prefix)
Definition: ansi_c_parser.h:128
ansi_c_parsert::asm_block_following
bool asm_block_following
Definition: ansi_c_parser.h:69
ansi_c_parsert::lookup
ansi_c_id_classt lookup(const irep_idt &base_name, irep_idt &identifier, bool tag, bool label)
Definition: ansi_c_parser.cpp:15
expr.h
yyansi_cerror
int yyansi_cerror(const std::string &error)
Definition: ansi_c_parser.cpp:78
ansi_c_parsert::pragma_pack
std::list< exprt > pragma_pack
Definition: ansi_c_parser.h:72
ansi_c_parsert::ts_18661_3_Floatn_types
bool ts_18661_3_Floatn_types
Definition: ansi_c_parser.h:85
ansi_c_parsert::lookup_label
irep_idt lookup_label(const irep_idt base_name)
Definition: ansi_c_parser.h:143
ansi_c_parsert::decl_typet::OTHER
@ OTHER
ansi_c_parsert::pop_scope
void pop_scope()
Definition: ansi_c_parser.h:103
ansi_c_parsert::set_pragma_cprover
void set_pragma_cprover()
Definition: ansi_c_parser.h:150
ansi_c_parsert::get_class
static ansi_c_id_classt get_class(const typet &type)
Definition: ansi_c_parser.cpp:157
ansi_c_parsert::pragma_cprover
std::list< std::set< irep_idt > > pragma_cprover
Definition: ansi_c_parser.h:73
ansi_c_scopet
Definition: ansi_c_scope.h:40
ansi_c_parsert::scopest
std::list< scopet > scopest
Definition: ansi_c_parser.h:90
ansi_c_parsert::decl_typet::PARAMETER
@ PARAMETER
ansi_c_parsert::ansi_c_parsert
ansi_c_parsert()
Definition: ansi_c_parser.h:32
ansi_c_parsert::clear
virtual void clear() override
Definition: ansi_c_parser.h:49
ansi_c_parser
ansi_c_parsert ansi_c_parser
Definition: ansi_c_parser.cpp:13
ansi_c_parsert
Definition: ansi_c_parser.h:28
string_hash.h
string hashing
ansi_c_scanner_init
void ansi_c_scanner_init()
ansi_c_parsert::string_literal
std::string string_literal
Definition: ansi_c_parser.h:71
ansi_c_parse_treet
Definition: ansi_c_parse_tree.h:18
irept::id
const irep_idt & id() const
Definition: irep.h:418
ansi_c_parsert::decl_typet::MEMBER
@ MEMBER
irept::remove
void remove(const irep_namet &name)
Definition: irep.cpp:93
ansi_c_identifiert
Definition: ansi_c_scope.h:29
parsert
Definition: parser.h:24
ansi_c_parsert::tag_following
bool tag_following
Definition: ansi_c_parser.h:68
ansi_c_parsert::cpp11
bool cpp11
Definition: ansi_c_parser.h:79
ansi_c_parsert::mode
modet mode
Definition: ansi_c_parser.h:76
ansi_c_parsert::scopes
scopest scopes
Definition: ansi_c_parser.h:91
ansi_c_parsert::add_tag_with_body
void add_tag_with_body(irept &tag)
Definition: ansi_c_parser.cpp:58
ansi_c_declarationt
Definition: ansi_c_declaration.h:73
ansi_c_parsert::parse_tree
ansi_c_parse_treet parse_tree
Definition: ansi_c_parser.h:30
ansi_c_parsert::root_scope
const scopet & root_scope() const
Definition: ansi_c_parser.h:98
parser.h
Parser utilities.
ansi_c_parsert::current_scope
scopet & current_scope()
Definition: ansi_c_parser.h:108
ansi_c_parsert::parse
virtual bool parse() override
Definition: ansi_c_parser.h:44
ansi_c_parsert::decl_typet::TAG
@ TAG
config.h
ansi_c_scope.h
irept
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:394
ansi_c_parsert::identifiert
ansi_c_identifiert identifiert
Definition: ansi_c_parser.h:87
configt::ansi_ct::flavourt
flavourt
Definition: config.h:107
ansi_c_parsert::root_scope
scopet & root_scope()
Definition: ansi_c_parser.h:93
ansi_c_parsert::for_has_scope
bool for_has_scope
Definition: ansi_c_parser.h:82
ansi_c_parse_treet::clear
void clear()
Definition: ansi_c_parse_tree.cpp:18