Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ansi_c_parsert: construct with message handler #8141

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions regression/cbmc/fault_localization-all_properties2/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
FUTURE
main.c
--localize-faults
^EXIT=10$
Expand All @@ -8,4 +8,6 @@ line 9 function main$
--
--
CBMC wrongly reports line 7 as the faulty statement when instead it should be
line 9.
line 9. The test is marked as "FUTURE" as it will sometimes pass, depending on
what model the solver produces. It would, therefore, break our checking of
"KNOWNBUG" tests.
21 changes: 6 additions & 15 deletions src/ansi-c/ansi_c_language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ bool ansi_c_languaget::parse(
ansi_c_internal_additions(code, config.ansi_c.float16_type);
std::istringstream codestr(code);

ansi_c_parser.clear();
ansi_c_parsert ansi_c_parser{message_handler};
ansi_c_parser.set_file(ID_built_in);
ansi_c_parser.in=&codestr;
ansi_c_parser.log.set_message_handler(message_handler);
ansi_c_parser.for_has_scope=config.ansi_c.for_has_scope;
ansi_c_parser.ts_18661_3_Floatn_types=config.ansi_c.ts_18661_3_Floatn_types;
ansi_c_parser.float16_type = config.ansi_c.float16_type;
Expand All @@ -83,25 +82,19 @@ bool ansi_c_languaget::parse(
ansi_c_parser.cpp11=false; // it's not C++
ansi_c_parser.mode=config.ansi_c.mode;

ansi_c_scanner_init();

bool result=ansi_c_parser.parse();

if(!result)
{
ansi_c_parser.set_line_no(0);
ansi_c_parser.set_file(path);
ansi_c_parser.in=&i_preprocessed;
ansi_c_scanner_init();
result=ansi_c_parser.parse();
}

// save result
parse_tree.swap(ansi_c_parser.parse_tree);

// save some memory
ansi_c_parser.clear();

return result;
}

Expand Down Expand Up @@ -199,15 +192,16 @@ bool ansi_c_languaget::to_expr(

// parsing

ansi_c_parser.clear();
ansi_c_parsert ansi_c_parser{message_handler};
ansi_c_parser.set_file(irep_idt());
ansi_c_parser.in=&i_preprocessed;
ansi_c_parser.log.set_message_handler(message_handler);
ansi_c_parser.mode=config.ansi_c.mode;
ansi_c_parser.for_has_scope = config.ansi_c.for_has_scope;
ansi_c_parser.ts_18661_3_Floatn_types=config.ansi_c.ts_18661_3_Floatn_types;
ansi_c_parser.float16_type = config.ansi_c.float16_type;
ansi_c_parser.bf16_type = config.ansi_c.bf16_type;
ansi_c_scanner_init();
ansi_c_parser.cpp98 = false; // it's not C++
ansi_c_parser.cpp11 = false; // it's not C++
ansi_c_parser.mode = config.ansi_c.mode;

bool result=ansi_c_parser.parse();

Expand All @@ -221,9 +215,6 @@ bool ansi_c_languaget::to_expr(
result = ansi_c_typecheck(expr, message_handler, ns);
}

// save some memory
ansi_c_parser.clear();

// now remove that (void) cast
if(expr.id()==ID_typecast &&
expr.type().id()==ID_empty &&
Expand Down
25 changes: 16 additions & 9 deletions src/ansi-c/ansi_c_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,22 @@ Author: Daniel Kroening, [email protected]

#include "c_storage_spec.h"

ansi_c_parsert ansi_c_parser;
int yyansi_clex_init_extra(ansi_c_parsert *, void **);
int yyansi_clex_destroy(void *);
int yyansi_cparse(ansi_c_parsert &, void *);
void yyansi_cset_debug(int, void *);

bool ansi_c_parsert::parse()
{
void *scanner;
yyansi_clex_init_extra(this, &scanner);
#ifdef ANSI_C_DEBUG
yyansi_cset_debug(1, scanner);
#endif
bool parse_fail = yyansi_cparse(*this, scanner) != 0;
yyansi_clex_destroy(scanner);
return parse_fail;
}

ansi_c_id_classt ansi_c_parsert::lookup(
const irep_idt &base_name,
Expand Down Expand Up @@ -73,14 +88,6 @@ void ansi_c_parsert::add_tag_with_body(irept &tag)
}
}

extern char *yyansi_ctext;

int yyansi_cerror(const std::string &error)
{
ansi_c_parser.parse_error(error, yyansi_ctext);
return 0;
}

void ansi_c_parsert::add_declarator(
exprt &declaration,
irept &declarator)
Expand Down
22 changes: 8 additions & 14 deletions src/ansi-c/ansi_c_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
#include "ansi_c_parse_tree.h"
#include "ansi_c_scope.h"

int yyansi_cparse();

class ansi_c_parsert:public parsert
{
public:
ansi_c_parse_treet parse_tree;

ansi_c_parsert()
: tag_following(false),
explicit ansi_c_parsert(message_handlert &message_handler)
: parsert(message_handler),
tag_following(false),
asm_block_following(false),
parenthesis_counter(0),
mode(modet::NONE),
Expand All @@ -37,14 +36,14 @@
float16_type(false),
bf16_type(false)
{
// set up global scope
scopes.clear();
scopes.push_back(scopet());
}

virtual bool parse() override
{
return yyansi_cparse()!=0;
}
bool parse() override;

virtual void clear() override
void clear() override

Check warning on line 46 in src/ansi-c/ansi_c_parser.h

View check run for this annotation

Codecov / codecov/patch

src/ansi-c/ansi_c_parser.h#L46

Added line #L46 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Is this function still used anywhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will clean this up across the type hierarchy (removing just this override seems dangerous, as any use of .clear() would just invoke parsert::clear(), which would end up in incomplete cleanup) in a follow-up PR.

{
parsert::clear();
parse_tree.clear();
Expand Down Expand Up @@ -170,9 +169,4 @@
std::list<std::map<const irep_idt, bool>> pragma_cprover_stack;
};

extern ansi_c_parsert ansi_c_parser;

int yyansi_cerror(const std::string &error);
void ansi_c_scanner_init();

#endif // CPROVER_ANSI_C_ANSI_C_PARSER_H
8 changes: 4 additions & 4 deletions src/ansi-c/builtin_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ static bool convert(
{
std::istringstream in(s.str());

ansi_c_parser.clear();
ansi_c_parsert ansi_c_parser{message_handler};
ansi_c_parser.set_file(ID_built_in);
ansi_c_parser.in=&in;
ansi_c_parser.log.set_message_handler(message_handler);
ansi_c_parser.for_has_scope=config.ansi_c.for_has_scope;
ansi_c_parser.ts_18661_3_Floatn_types = config.ansi_c.ts_18661_3_Floatn_types;
ansi_c_parser.float16_type = config.ansi_c.float16_type;
ansi_c_parser.bf16_type = config.ansi_c.bf16_type;
ansi_c_parser.cpp98=false; // it's not C++
ansi_c_parser.cpp11=false; // it's not C++
ansi_c_parser.mode=config.ansi_c.mode;

ansi_c_scanner_init();

if(ansi_c_parser.parse())
return true;

Expand Down
Loading
Loading