Skip to content

Commit

Permalink
refactor: Remove unneeded catch-all blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
slavek-kucera authored Mar 19, 2024
1 parent 7d929f5 commit ea812ce
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 125 deletions.
66 changes: 31 additions & 35 deletions parser_library/src/checking/asm_instr_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <array>
#include <regex>

#include "context/common_types.h"
#include "checker_helper.h"
#include "diagnostic_collector.h"
#include "lexing/tools.h"
#include "utils/string_operations.h"
Expand Down Expand Up @@ -1150,67 +1150,63 @@ bool alias::check(const std::vector<const asm_operand*>& to_check,
{
if (!operands_size_corresponding(to_check, stmt_range, add_diagnostic))
return false;
auto first = get_simple_operand(to_check[0]);
if (first == nullptr || first->operand_identifier.size() < 3)
const auto& r = to_check.front()->operand_range;
const auto first = get_simple_operand(to_check[0]);
if (!first)
{
add_diagnostic(diagnostic_op::error_A151_ALIAS_op_format(to_check[0]->operand_range));
add_diagnostic(diagnostic_op::error_A151_ALIAS_op_format(r));
return false;
}
if (first->operand_identifier[1] == '\'' && first->operand_identifier[first->operand_identifier.size() - 1] == '\'')
std::string_view value = first->operand_identifier;
if (value.size() < 3 || value[1] != '\'' || value.back() != '\'')
{
if (first->operand_identifier[0] == 'C')
{
add_diagnostic(diagnostic_op::error_A151_ALIAS_op_format(r));
return false;
}
const auto type = value.front();
value.remove_prefix(2);
value.remove_suffix(1);
switch (type)
{
case 'c':
case 'C':
// TO DO - no support for four characters in EBCDIC (¢, ¬, ±, ¦) - we throw an error although it should
// not be
static const std::regex regex(R"([\.<¢\(\+\|&!\$\*\);¬\-\/¦,%_>\?`,:#@\=\"~±\[\]\{\}\^\\a-zA-Z0-9]*)");
std::string substr = first->operand_identifier.substr(2, first->operand_identifier.size() - 3);
if (!std::regex_match(substr, regex))
if (static const std::regex regex(R"([\.<¢\(\+\|&!\$\*\);¬\-\/¦,%_>\?`,:#@\=\"~±\[\]\{\}\^\\a-zA-Z0-9]*)");
!std::regex_match(value.cbegin(), value.cend(), regex))
{
add_diagnostic(diagnostic_op::error_A152_ALIAS_C_format(first->operand_range));
return false;
}
return true;
}
else if (first->operand_identifier[0] == 'X')
{
if ((first->operand_identifier.size() - 3) % 2 == 1)

case 'x':
case 'X':
if (value.size() % 2 == 1)
{
add_diagnostic(diagnostic_op::error_A154_ALIAS_X_format_no_of_chars(first->operand_range));
return false;
}
int max_value = ALIAS_max_val;
int min_value = ALIAS_min_val;
for (size_t i = 2; i < first->operand_identifier.size() - 1; i += 2)
for (size_t i = 0; i < value.size(); i += 2)
{
std::string tocomp = "";
tocomp.push_back(first->operand_identifier[i]);
tocomp.push_back(first->operand_identifier[i + 1]);
if (tocomp == "0x" || tocomp == "0X")
{
add_diagnostic(diagnostic_op::error_A153_ALIAS_X_format(first->operand_range));
return false;
}
int comparing = 0;
try
{
comparing = std::stoul(tocomp, nullptr, 16);
}
catch (...)
const auto val = as_int(value.substr(i, 2), 16);
if (!val)
{
add_diagnostic(diagnostic_op::error_A153_ALIAS_X_format(first->operand_range));
return false;
}
if (comparing < min_value || comparing > max_value)
if (*val < ALIAS_min_val || *val > ALIAS_max_val)
{
add_diagnostic(diagnostic_op::error_A155_ALIAS_X_format_range(first->operand_range));
return false;
}
}
return true;
}

default:
add_diagnostic(diagnostic_op::error_A151_ALIAS_op_format(r));
return false;
}
add_diagnostic(diagnostic_op::error_A151_ALIAS_op_format(first->operand_range));
return false;
}

ainsert::ainsert(const std::vector<label_types>& allowed_types, std::string_view name_of_instruction)
Expand Down
78 changes: 30 additions & 48 deletions parser_library/src/checking/asm_instr_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
#include "asm_instr_class.h"

#include <array>
#include <charconv>
#include <optional>
#include <string_view>

#include "checker_helper.h"
#include "diagnostic_collector.h"

using namespace hlasm_plugin::parser_library;
Expand Down Expand Up @@ -204,6 +208,9 @@ bool assembler_instruction::check_typecheck_operands(const std::vector<std::uniq
bool assembler_instruction::check_codepage_parameter(
const one_operand& input, const diagnostic_collector& add_diagnostic) const
{
static constexpr const int min_value = 0x0474;
static constexpr const int max_value = 0x047C;

// hexa value
const std::string& input_str = input.operand_identifier;
if (input_str.front() == 'X')
Expand All @@ -214,30 +221,18 @@ bool assembler_instruction::check_codepage_parameter(
return false;
}
// get value
std::string value = "";
for (size_t i = 2; i < input_str.size() - 1; i++)
{
if (!isxdigit((unsigned char)input_str[i]))
{
add_diagnostic(diagnostic_op::error_A215_CODEPAGE_format(name_of_instruction, input.operand_range));
return false;
}
value += input_str[i];
}
try
const char* const b = input_str.data() + 2;
const char* const e = input_str.data() + input_str.size() - 1;
if (std::any_of(b, e, [](unsigned char c) { return !isxdigit(c); }))
{
int val_hexa = std::stoul(value, nullptr, 16);
int min_value = 0x0474;
int max_value = 0x047C;
if (val_hexa < min_value || val_hexa > max_value)
{
add_diagnostic(diagnostic_op::error_A216_CODEPAGE_value(name_of_instruction, input.operand_range));
return false;
}
add_diagnostic(diagnostic_op::error_A215_CODEPAGE_format(name_of_instruction, input.operand_range));
return false;
}
catch (...)
const auto val = as_int(std::string_view(b, e), 16);
if (!val || *val < min_value || *val > max_value)
{
assert(false);
add_diagnostic(diagnostic_op::error_A216_CODEPAGE_value(name_of_instruction, input.operand_range));
return false;
}
}
// decimal value
Expand All @@ -248,8 +243,8 @@ bool assembler_instruction::check_codepage_parameter(
add_diagnostic(diagnostic_op::error_A215_CODEPAGE_format(name_of_instruction, input.operand_range));
return false;
}
auto val = std::stoi(input_str);
if (val < 1140 || val > 1148)
const auto val = as_int(input_str);
if (!val || *val < min_value || *val > max_value)
{
add_diagnostic(diagnostic_op::error_A216_CODEPAGE_value(name_of_instruction, input.operand_range));
return false;
Expand Down Expand Up @@ -296,12 +291,8 @@ bool assembler_instruction::check_fail_parameters(const std::vector<std::unique_
return false;
}
auto ident = get_simple_operand(current_operand->operand_parameters[0].get());
int ident_val = 0;
try
{
ident_val = std::stoi(ident->operand_identifier);
}
catch (...)
const auto ident_val = as_int(ident->operand_identifier);
if (!ident_val)
{
add_diagnostic(diagnostic_op::error_A235_FAIL_param_number_format(instr_name,
current_operand->operand_identifier,
Expand All @@ -310,7 +301,7 @@ bool assembler_instruction::check_fail_parameters(const std::vector<std::unique_
}
if (current_operand->operand_identifier == "MNOTE" || current_operand->operand_identifier == "MSG")
{
if (ident_val < 0 || ident_val > 7)
if (*ident_val < 0 || *ident_val > 7)
{
add_diagnostic(diagnostic_op::error_A237_FAIL_severity_message(
instr_name, current_operand->operand_identifier, ident->operand_range));
Expand All @@ -320,7 +311,7 @@ bool assembler_instruction::check_fail_parameters(const std::vector<std::unique_
else if (current_operand->operand_identifier == "MAXERRS"
|| current_operand->operand_identifier == "NOMAXERRS")
{
if (ident_val < 32 || ident_val > 65535)
if (*ident_val < 32 || *ident_val > 65535)
{
add_diagnostic(diagnostic_op::error_A236_FAIL_MAXXERS_value(
instr_name, current_operand->operand_identifier, ident->operand_range));
Expand Down Expand Up @@ -441,19 +432,15 @@ bool assembler_instruction::check_using_parameters(const std::vector<std::unique
auto param = get_simple_operand(current_operand->operand_parameters[0].get());
if (current_operand->operand_identifier == "WARN")
{
int number = 0;
try
{
number = std::stoi(param->operand_identifier);
}
catch (...)
const auto number = as_int(param->operand_identifier);
if (!number)
{
add_diagnostic(diagnostic_op::error_A228_USING_complex_param_no(instr_name,
current_operand->operand_identifier,
current_operand->operand_parameters[0]->operand_range));
return false;
}
if (!is_byte_value(number))
if (!is_byte_value(*number))
{
add_diagnostic(diagnostic_op::error_A229_USING_WARN_format(
instr_name, current_operand->operand_parameters[0]->operand_range));
Expand All @@ -462,12 +449,12 @@ bool assembler_instruction::check_using_parameters(const std::vector<std::unique
}
else if (current_operand->operand_identifier == "LIMIT")
{
auto ident = param->operand_identifier;
const std::string_view ident = param->operand_identifier;
if (ident.size() >= 3 && ident.front() == 'X' && ident[1] == '\'' && ident.back() == '\''
&& is_value_hexa(ident.substr(2, ident.size() - 3)))
{
// the value is specified in hexa
if (std::stoul(ident.substr(2, ident.size() - 3), nullptr, 16) > 0xFFF)
if (auto val = as_int(ident.substr(2, ident.size() - 3), 16); !val || *val > 0xFFF)
{
add_diagnostic(
diagnostic_op::error_A232_USING_LIMIT_hexa(instr_name, current_operand->operand_range));
Expand All @@ -476,19 +463,14 @@ bool assembler_instruction::check_using_parameters(const std::vector<std::unique
}
else
{
int number = 0;
try
{
number = std::stoi(ident);
}
catch (...)
if (auto number = as_int(ident); !number)
{
// the value is in incorrect format
add_diagnostic(
diagnostic_op::error_A230_USING_LIMIT_format(instr_name, current_operand->operand_range));
return false;
};
if (number > 4095)
}
else if (*number > 4095)
{
// the number is specified in decimal but is not lower than the allowed value
add_diagnostic(
Expand Down
5 changes: 0 additions & 5 deletions parser_library/src/checking/asm_instr_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@
#ifndef HLASMPLUGIN_PARSERLIBRARY_INSTR_CLASS_H
#define HLASMPLUGIN_PARSERLIBRARY_INSTR_CLASS_H

#include <algorithm>
#include <functional>
#include <iomanip>
#include <string>
#include <vector>

#include "checker_helper.h"
#include "diagnosable.h"
#include "instr_operand.h"

namespace hlasm_plugin::parser_library {
Expand Down
Loading

0 comments on commit ea812ce

Please sign in to comment.