Skip to content

Commit 045c6e2

Browse files
committed
cpp_parsert: construct with message handler
This both avoids an object of static lifetime as well as it fixes the (transitive) use of the deprecated messaget() constructor.
1 parent bc5efc9 commit 045c6e2

6 files changed

+40
-43
lines changed

src/cpp/cpp_language.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,9 @@ bool cpp_languaget::parse(
102102
std::istringstream i_preprocessed(o_preprocessed.str());
103103

104104
// parsing
105-
106-
cpp_parser.clear();
105+
cpp_parsert cpp_parser{message_handler};
107106
cpp_parser.set_file(path);
108107
cpp_parser.in=&i_preprocessed;
109-
cpp_parser.log.set_message_handler(message_handler);
110108
cpp_parser.mode=config.ansi_c.mode;
111109

112110
bool result=cpp_parser.parse();
@@ -245,11 +243,9 @@ bool cpp_languaget::to_expr(
245243
std::istringstream i_preprocessed(code);
246244

247245
// parsing
248-
249-
cpp_parser.clear();
246+
cpp_parsert cpp_parser{message_handler};
250247
cpp_parser.set_file(irep_idt());
251248
cpp_parser.in=&i_preprocessed;
252-
cpp_parser.log.set_message_handler(message_handler);
253249

254250
bool result=cpp_parser.parse();
255251

src/cpp/cpp_parser.cpp

+5-18
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,12 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "cpp_parser.h"
1313

14-
#include <util/config.h>
15-
16-
cpp_parsert cpp_parser;
17-
18-
bool cpp_parse();
14+
bool cpp_parse(cpp_parsert &, message_handlert &);
1915

2016
bool cpp_parsert::parse()
2117
{
22-
// We use the ANSI-C scanner
23-
ansi_c_parser.cpp98=true;
24-
ansi_c_parser.cpp11 =
25-
config.cpp.cpp_standard == configt::cppt::cpp_standardt::CPP11 ||
26-
config.cpp.cpp_standard == configt::cppt::cpp_standardt::CPP14 ||
27-
config.cpp.cpp_standard == configt::cppt::cpp_standardt::CPP17;
28-
ansi_c_parser.ts_18661_3_Floatn_types=false;
29-
ansi_c_parser.in=in;
30-
ansi_c_parser.mode=mode;
31-
ansi_c_parser.set_file(get_file());
32-
ansi_c_parser.log.set_message_handler(log.get_message_handler());
33-
34-
return cpp_parse();
18+
token_buffer.ansi_c_parser.in = in;
19+
token_buffer.ansi_c_parser.mode = mode;
20+
token_buffer.ansi_c_parser.set_file(get_file());
21+
return cpp_parse(*this, log.get_message_handler());
3522
}

src/cpp/cpp_parser.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ class cpp_parsert:public parsert
3434
asm_block_following=false;
3535
}
3636

37-
cpp_parsert():
38-
mode(configt::ansi_ct::flavourt::ANSI),
39-
recognize_wchar_t(true),
40-
asm_block_following(false)
37+
explicit cpp_parsert(message_handlert &message_handler)
38+
: parsert(message_handler),
39+
mode(configt::ansi_ct::flavourt::ANSI),
40+
recognize_wchar_t(true),
41+
token_buffer(message_handler),
42+
asm_block_following(false)
4143
{
4244
}
4345

@@ -67,6 +69,4 @@ class cpp_parsert:public parsert
6769
bool asm_block_following;
6870
};
6971

70-
extern cpp_parsert cpp_parser;
71-
7272
#endif // CPROVER_CPP_CPP_PARSER_H

src/cpp/cpp_token_buffer.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "cpp_token_buffer.h"
1313

14-
#include <ansi-c/ansi_c_parser.h>
15-
1614
int cpp_token_buffert::LookAhead(unsigned offset)
1715
{
1816
PRECONDITION(current_pos <= token_vector.size());

src/cpp/cpp_token_buffer.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,28 @@ Author: Daniel Kroening, [email protected]
1212
#ifndef CPROVER_CPP_CPP_TOKEN_BUFFER_H
1313
#define CPROVER_CPP_CPP_TOKEN_BUFFER_H
1414

15+
#include <util/config.h>
16+
#include <util/invariant.h>
17+
18+
#include <ansi-c/ansi_c_parser.h>
19+
1520
#include "cpp_token.h"
1621

1722
#include <list>
1823

19-
#include <util/invariant.h>
20-
2124
class cpp_token_buffert
2225
{
2326
public:
24-
cpp_token_buffert():current_pos(0)
27+
explicit cpp_token_buffert(message_handlert &message_handler)
28+
: ansi_c_parser(message_handler), current_pos(0)
2529
{
30+
// We use the ANSI-C scanner
31+
ansi_c_parser.cpp98 = true;
32+
ansi_c_parser.cpp11 =
33+
config.cpp.cpp_standard == configt::cppt::cpp_standardt::CPP11 ||
34+
config.cpp.cpp_standard == configt::cppt::cpp_standardt::CPP14 ||
35+
config.cpp.cpp_standard == configt::cppt::cpp_standardt::CPP17;
36+
ansi_c_parser.ts_18661_3_Floatn_types = false;
2637
}
2738

2839
typedef unsigned int post;
@@ -51,6 +62,8 @@ class cpp_token_buffert
5162
return tokens.back();
5263
}
5364

65+
ansi_c_parsert ansi_c_parser;
66+
5467
protected:
5568
typedef std::list<cpp_tokent> tokenst;
5669
tokenst tokens;

src/cpp/parse.cpp

+11-8
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,10 @@ void new_scopet::print_rec(std::ostream &out, unsigned indent) const
195195
class Parser // NOLINT(readability/identifiers)
196196
{
197197
public:
198-
explicit Parser(cpp_parsert &_cpp_parser)
198+
Parser(cpp_parsert &_cpp_parser, message_handlert &message_handler)
199199
: lex(_cpp_parser.token_buffer),
200-
parser(_cpp_parser),
200+
parse_tree(_cpp_parser.parse_tree),
201+
message_handler(message_handler),
201202
max_errors(10),
202203
cpp11(
203204
config.cpp.cpp_standard == configt::cppt::cpp_standardt::CPP11 ||
@@ -212,7 +213,8 @@ class Parser // NOLINT(readability/identifiers)
212213

213214
protected:
214215
cpp_token_buffert &lex;
215-
cpp_parsert &parser;
216+
cpp_parse_treet &parse_tree;
217+
message_handlert &message_handler;
216218

217219
// scopes
218220
new_scopet root_scope;
@@ -517,8 +519,9 @@ bool Parser::SyntaxError()
517519

518520
message+="'";
519521

520-
parser.log.error().source_location = source_location;
521-
parser.log.error() << message << messaget::eom;
522+
messaget log{message_handler};
523+
log.error().source_location = source_location;
524+
log.error() << message << messaget::eom;
522525
}
523526

524527
return ++number_of_errors < max_errors;
@@ -8381,7 +8384,7 @@ bool Parser::operator()()
83818384

83828385
while(rProgram(item))
83838386
{
8384-
parser.parse_tree.items.push_back(item);
8387+
parse_tree.items.push_back(item);
83858388
item.clear();
83868389
}
83878390

@@ -8392,8 +8395,8 @@ bool Parser::operator()()
83928395
return number_of_errors!=0;
83938396
}
83948397

8395-
bool cpp_parse()
8398+
bool cpp_parse(cpp_parsert &cpp_parser, message_handlert &message_handler)
83968399
{
8397-
Parser parser(cpp_parser);
8400+
Parser parser(cpp_parser, message_handler);
83988401
return parser();
83998402
}

0 commit comments

Comments
 (0)