Skip to content

Commit

Permalink
cpp_parsert: construct with message handler
Browse files Browse the repository at this point in the history
This both avoids an object of static lifetime as well as it fixes the
(transitive) use of the deprecated messaget() constructor.
  • Loading branch information
tautschnig committed Dec 20, 2023
1 parent bc5efc9 commit 045c6e2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 43 deletions.
8 changes: 2 additions & 6 deletions src/cpp/cpp_language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,9 @@ bool cpp_languaget::parse(
std::istringstream i_preprocessed(o_preprocessed.str());

// parsing

cpp_parser.clear();
cpp_parsert cpp_parser{message_handler};
cpp_parser.set_file(path);
cpp_parser.in=&i_preprocessed;
cpp_parser.log.set_message_handler(message_handler);
cpp_parser.mode=config.ansi_c.mode;

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

// parsing

cpp_parser.clear();
cpp_parsert cpp_parser{message_handler};
cpp_parser.set_file(irep_idt());
cpp_parser.in=&i_preprocessed;
cpp_parser.log.set_message_handler(message_handler);

bool result=cpp_parser.parse();

Expand Down
23 changes: 5 additions & 18 deletions src/cpp/cpp_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,12 @@ Author: Daniel Kroening, [email protected]

#include "cpp_parser.h"

#include <util/config.h>

cpp_parsert cpp_parser;

bool cpp_parse();
bool cpp_parse(cpp_parsert &, message_handlert &);

bool cpp_parsert::parse()
{
// We use the ANSI-C scanner
ansi_c_parser.cpp98=true;
ansi_c_parser.cpp11 =
config.cpp.cpp_standard == configt::cppt::cpp_standardt::CPP11 ||
config.cpp.cpp_standard == configt::cppt::cpp_standardt::CPP14 ||
config.cpp.cpp_standard == configt::cppt::cpp_standardt::CPP17;
ansi_c_parser.ts_18661_3_Floatn_types=false;
ansi_c_parser.in=in;
ansi_c_parser.mode=mode;
ansi_c_parser.set_file(get_file());
ansi_c_parser.log.set_message_handler(log.get_message_handler());

return cpp_parse();
token_buffer.ansi_c_parser.in = in;
token_buffer.ansi_c_parser.mode = mode;
token_buffer.ansi_c_parser.set_file(get_file());
return cpp_parse(*this, log.get_message_handler());
}
12 changes: 6 additions & 6 deletions src/cpp/cpp_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ class cpp_parsert:public parsert
asm_block_following=false;
}

cpp_parsert():
mode(configt::ansi_ct::flavourt::ANSI),
recognize_wchar_t(true),
asm_block_following(false)
explicit cpp_parsert(message_handlert &message_handler)
: parsert(message_handler),
mode(configt::ansi_ct::flavourt::ANSI),
recognize_wchar_t(true),
token_buffer(message_handler),
asm_block_following(false)
{
}

Expand Down Expand Up @@ -67,6 +69,4 @@ class cpp_parsert:public parsert
bool asm_block_following;
};

extern cpp_parsert cpp_parser;

#endif // CPROVER_CPP_CPP_PARSER_H
2 changes: 0 additions & 2 deletions src/cpp/cpp_token_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ Author: Daniel Kroening, [email protected]

#include "cpp_token_buffer.h"

#include <ansi-c/ansi_c_parser.h>

int cpp_token_buffert::LookAhead(unsigned offset)
{
PRECONDITION(current_pos <= token_vector.size());
Expand Down
19 changes: 16 additions & 3 deletions src/cpp/cpp_token_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,28 @@ Author: Daniel Kroening, [email protected]
#ifndef CPROVER_CPP_CPP_TOKEN_BUFFER_H
#define CPROVER_CPP_CPP_TOKEN_BUFFER_H

#include <util/config.h>
#include <util/invariant.h>

#include <ansi-c/ansi_c_parser.h>

#include "cpp_token.h"

#include <list>

#include <util/invariant.h>

class cpp_token_buffert
{
public:
cpp_token_buffert():current_pos(0)
explicit cpp_token_buffert(message_handlert &message_handler)
: ansi_c_parser(message_handler), current_pos(0)
{
// We use the ANSI-C scanner
ansi_c_parser.cpp98 = true;
ansi_c_parser.cpp11 =
config.cpp.cpp_standard == configt::cppt::cpp_standardt::CPP11 ||
config.cpp.cpp_standard == configt::cppt::cpp_standardt::CPP14 ||
config.cpp.cpp_standard == configt::cppt::cpp_standardt::CPP17;
ansi_c_parser.ts_18661_3_Floatn_types = false;
}

typedef unsigned int post;
Expand Down Expand Up @@ -51,6 +62,8 @@ class cpp_token_buffert
return tokens.back();
}

ansi_c_parsert ansi_c_parser;

protected:
typedef std::list<cpp_tokent> tokenst;
tokenst tokens;
Expand Down
19 changes: 11 additions & 8 deletions src/cpp/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,10 @@ void new_scopet::print_rec(std::ostream &out, unsigned indent) const
class Parser // NOLINT(readability/identifiers)
{
public:
explicit Parser(cpp_parsert &_cpp_parser)
Parser(cpp_parsert &_cpp_parser, message_handlert &message_handler)
: lex(_cpp_parser.token_buffer),
parser(_cpp_parser),
parse_tree(_cpp_parser.parse_tree),
message_handler(message_handler),
max_errors(10),
cpp11(
config.cpp.cpp_standard == configt::cppt::cpp_standardt::CPP11 ||
Expand All @@ -212,7 +213,8 @@ class Parser // NOLINT(readability/identifiers)

protected:
cpp_token_buffert &lex;
cpp_parsert &parser;
cpp_parse_treet &parse_tree;
message_handlert &message_handler;

// scopes
new_scopet root_scope;
Expand Down Expand Up @@ -517,8 +519,9 @@ bool Parser::SyntaxError()

message+="'";

parser.log.error().source_location = source_location;
parser.log.error() << message << messaget::eom;
messaget log{message_handler};
log.error().source_location = source_location;
log.error() << message << messaget::eom;
}

return ++number_of_errors < max_errors;
Expand Down Expand Up @@ -8381,7 +8384,7 @@ bool Parser::operator()()

while(rProgram(item))
{
parser.parse_tree.items.push_back(item);
parse_tree.items.push_back(item);
item.clear();
}

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

bool cpp_parse()
bool cpp_parse(cpp_parsert &cpp_parser, message_handlert &message_handler)
{
Parser parser(cpp_parser);
Parser parser(cpp_parser, message_handler);
return parser();
}

0 comments on commit 045c6e2

Please sign in to comment.