Skip to content

Split parse_list_types into two functions #3293

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

Merged
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
33 changes: 28 additions & 5 deletions jbmc/src/java_bytecode/java_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,40 @@ std::vector<typet> parse_list_types(
const std::string class_name_prefix,
const char opening_bracket,
const char closing_bracket)
{
// Loop over the types in the given string, parsing each one in turn
// and adding to the type_list
std::vector<typet> type_list;
Copy link
Contributor

Choose a reason for hiding this comment

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

⛏️

const std::vector<std::string> type_strings = parse_raw_list_types(src, opening_bracket, closing_bracket); 
std::vector<typet> type_list;
std::transform(
  type_strings.begin(), 
  type_strings.end(), 
  std::back_inserter(type_list), 
  [&](const std::string &raw_type) {
    const typet new_type = java_type_from_string(raw_type, class_name_prefix);
    INVARIANT(new_type != nil_typet(), "Failed to parse type");
    return new_type;
});
return type_list;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does this actually give any real benefit when you're simply looping through an entire list with no bells or whistles?

Copy link
Contributor

Choose a reason for hiding this comment

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

That's exactly when you should use it, so the reader can tell that you are looping through an entire list with no bells and whistles! It wouldn't work if you had some whistles.

FYI the ⛏️ here means a non-blocking comment that you could fix if you're in the area but mostly there as something to try and do in future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cheers, was mostly just curious about the recommendation (wasn't meant as a refutal).

for(const std::string &raw_type :
parse_raw_list_types(src, opening_bracket, closing_bracket))
{
const typet new_type = java_type_from_string(raw_type, class_name_prefix);
INVARIANT(new_type != nil_typet(), "Failed to parse type");
type_list.push_back(new_type);
}
return type_list;
}

/// Given a substring of a descriptor or signature that contains one or more
/// types parse out the individual type strings.
/// \param src: The input string that is wrapped in either ( ) or < >
/// \param opening_bracket: For checking string is passed in as expected, the
/// opening bracket (i.e. '(' or '<').
/// \param closing_bracket: For checking string is passed in as expected, the
/// closing bracket (i.e. ')' or '>').
/// \return A vector of strings that represent the bytecode type.
std::vector<std::string> parse_raw_list_types(
const std::string src,
const char opening_bracket,
const char closing_bracket)
{
PRECONDITION(src.size() >= 2);
PRECONDITION(src[0] == opening_bracket);
PRECONDITION(src[src.size() - 1] == closing_bracket);

// Loop over the types in the given string, parsing each one in turn
// and adding to the type_list
std::vector<typet> type_list;
std::vector<std::string> type_list;
for(std::size_t i = 1; i < src.size() - 1; i++)
{
size_t start = i;
Expand Down Expand Up @@ -369,10 +395,7 @@ std::vector<typet> parse_list_types(
}

std::string sub_str = src.substr(start, i - start + 1);
const typet &new_type = java_type_from_string(sub_str, class_name_prefix);
INVARIANT(new_type != nil_typet(), "Failed to parse type");

type_list.push_back(new_type);
type_list.emplace_back(sub_str);
}
return type_list;
}
Expand Down
4 changes: 4 additions & 0 deletions jbmc/src/java_bytecode/java_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ size_t find_closing_semi_colon_for_reference_type(
const std::string src,
size_t starting_point = 0);

std::vector<std::string> parse_raw_list_types(
std::string src,
char opening_bracket,
char closing_bracket);

bool is_java_array_tag(const irep_idt &tag);
bool is_valid_java_array(const struct_typet &);
Expand Down