Skip to content

Commit

Permalink
improved preprocessor error location for macro argument missmatch
Browse files Browse the repository at this point in the history
  • Loading branch information
X39 committed Sep 11, 2023
1 parent 2293b89 commit 0d5b1f2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/parser/preprocessor/default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ std::string sqf::parser::preprocessor::impl_default::instance::replace(
const std::vector<const ::sqf::runtime::parser::macro *> &macro_stack) {
if (m.args().size() != params.size()) {
m_errflag = true;
log(err::ArgCountMissmatch(m.diag_info()));
log(err::ArgCountMissmatch(original_fileinfo.to_diag_info(), m.diag_info(), m.args().size(), params.size(), std::string(m.name().begin(), m.name().end())));
return "";
}
context local_fileinfo(m.diag_info());
Expand Down
38 changes: 34 additions & 4 deletions src/runtime/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,40 @@ namespace logmessage::preprocessor {

std::string ArgCountMissmatch::formatMessage() const {
auto output = m_location.format();
auto const message = "Arg Count Missmatch."sv;

output.reserve(output.length() + message.length());
output.append(message);
auto macro_line = std::to_string(m_macro_location.line);
auto macro_column = std::to_string(m_macro_location.col);
auto macro_path = m_macro_location.path;
auto expected_arg_count = std::to_string(m_expected_arg_count);
auto actual_arg_count = std::to_string(m_actual_arg_count);
output.reserve(
output.length()
+ "The macro '"sv.length()
+ m_macro_name.length()
+ "' at line '"sv.length()
+ macro_line.length()
+ "' column '"sv.length()
+ macro_column.length()
+ "' in file '"sv.length()
+ macro_path.length()
+ "' expects '"sv.length()
+ expected_arg_count.length()
+ "' arguments but got '"sv.length()
+ actual_arg_count.length()
+ "'."sv.length()
);
output.append("The macro '"sv);
output.append(m_macro_name);
output.append("' at line '"sv);
output.append(macro_line);
output.append("' column '"sv);
output.append(macro_column);
output.append("' in file '"sv);
output.append(macro_path);
output.append("' expects '"sv);
output.append(expected_arg_count);
output.append("' arguments but got '"sv);
output.append(actual_arg_count);
output.append("'."sv);
return output;
}

Expand Down
18 changes: 16 additions & 2 deletions src/runtime/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,22 @@ namespace logmessage {
class ArgCountMissmatch : public PreprocBase {
static const loglevel level = loglevel::error;
static const size_t errorCode = 10001;
public:
ArgCountMissmatch(LogLocationInfo loc) : PreprocBase(level, errorCode, std::move(loc)) {}
LogLocationInfo m_macro_location;
size_t m_expected_arg_count;
size_t m_actual_arg_count;
std::string m_macro_name;
public:
ArgCountMissmatch(
LogLocationInfo loc,
LogLocationInfo macro_location,
size_t expected_arg_count,
size_t actual_arg_count,
std::string macro_name) :
PreprocBase(level, errorCode, std::move(loc)),
m_macro_location(macro_location),
m_expected_arg_count(expected_arg_count),
m_actual_arg_count(actual_arg_count),
m_macro_name(std::move(macro_name)) {}
[[nodiscard]] std::string formatMessage() const override;
};

Expand Down

0 comments on commit 0d5b1f2

Please sign in to comment.