Skip to content

Commit

Permalink
GCC and CL message handler: print warnings as errors
Browse files Browse the repository at this point in the history
We already treated warnings as errors (if requested via command-line
options), but we should also mimic the output presented by CL/GCC with
those command-line options set.
  • Loading branch information
tautschnig committed Apr 23, 2024
1 parent fb27888 commit 5b611b2
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/goto-cc/armcc_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ int armcc_modet::doit()
cmdline.isset("diag_warning=") ? messaget::M_WARNING : messaget::M_ERROR;

Check warning on line 49 in src/goto-cc/armcc_mode.cpp

View check run for this annotation

Codecov / codecov/patch

src/goto-cc/armcc_mode.cpp#L48-L49

Added lines #L48 - L49 were not covered by tests
const auto verbosity = messaget::eval_verbosity(
cmdline.get_value("verbosity"), default_verbosity, message_handler);
message_handler.print_warnings_as_errors(cmdline.isset("diag_error="));

Check warning on line 52 in src/goto-cc/armcc_mode.cpp

View check run for this annotation

Codecov / codecov/patch

src/goto-cc/armcc_mode.cpp#L51-L52

Added lines #L51 - L52 were not covered by tests

messaget log{message_handler};
log.debug() << "ARM mode" << messaget::eom;
Expand Down
1 change: 1 addition & 0 deletions src/goto-cc/as_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ int as_modet::doit()
messaget::M_WARNING : messaget::M_ERROR;
messaget::eval_verbosity(
cmdline.get_value("verbosity"), default_verbosity, message_handler);
message_handler.print_warnings_as_errors(cmdline.isset("fatal-warnings"));

Check warning on line 113 in src/goto-cc/as_mode.cpp

View check run for this annotation

Codecov / codecov/patch

src/goto-cc/as_mode.cpp#L113

Added line #L113 was not covered by tests

if(act_as_as86)
{
Expand Down
3 changes: 3 additions & 0 deletions src/goto-cc/cl_message_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ void cl_message_handlert::print(

std::ostringstream formatted_message;

if(level == messaget::M_WARNING && warnings_are_errors)
formatted_message << "error: warning treated as error\n";

Check warning on line 29 in src/goto-cc/cl_message_handler.cpp

View check run for this annotation

Codecov / codecov/patch

src/goto-cc/cl_message_handler.cpp#L28-L29

Added lines #L28 - L29 were not covered by tests

const irep_idt file = location.get_file();
const std::string &line = id2string(location.get_line());
formatted_message << file << '(' << line << "): ";
Expand Down
10 changes: 10 additions & 0 deletions src/goto-cc/cl_message_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ class cl_message_handlert : public console_message_handlert
const source_locationt &location) override;

using console_message_handlert::print;

/// With \p yes set to \c true, prefix warnings with an error message.
/// \param yes: Whether or not to prefix warnings.
void print_warnings_as_errors(bool yes)

Check warning on line 35 in src/goto-cc/cl_message_handler.h

View check run for this annotation

Codecov / codecov/patch

src/goto-cc/cl_message_handler.h#L35

Added line #L35 was not covered by tests
{
warnings_are_errors = yes;

Check warning on line 37 in src/goto-cc/cl_message_handler.h

View check run for this annotation

Codecov / codecov/patch

src/goto-cc/cl_message_handler.h#L37

Added line #L37 was not covered by tests
}

private:
bool warnings_are_errors = false;
};

#endif // CPROVER_GOTO_CC_CL_MESSAGE_HANDLER_H
6 changes: 5 additions & 1 deletion src/goto-cc/gcc_message_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ void gcc_message_handlert::print(
else
out << column << ": ";

if(level == messaget::M_ERROR)
if(
level == messaget::M_ERROR ||
(level == messaget::M_WARNING && warnings_are_errors))
{
out << string(messaget::red) << "error: ";
}
else if(level == messaget::M_WARNING)
out << string(messaget::bright_magenta) << "warning: ";

Expand Down
10 changes: 10 additions & 0 deletions src/goto-cc/gcc_message_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ class gcc_message_handlert : public console_message_handlert
const std::string &message,
const source_locationt &location) override;

/// With \p yes set to \c true, prefix warnings with "error:" instead of
/// "warning:".
/// \param yes: Whether or not to prefix warnings with "error:".
void print_warnings_as_errors(bool yes)
{
warnings_are_errors = yes;
}

private:
bool warnings_are_errors = false;

/// feed a command into a string
std::string string(const messaget::commandt &c) const
{
Expand Down
2 changes: 2 additions & 0 deletions src/goto-cc/gcc_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ int gcc_modet::doit()
messaget::M_WARNING : messaget::M_ERROR;
messaget::eval_verbosity(
cmdline.get_value("verbosity"), default_verbosity, gcc_message_handler);
gcc_message_handler.print_warnings_as_errors(
cmdline.isset("Werror") && !cmdline.isset("Wno-error"));

bool act_as_bcc=
base_name=="bcc" ||
Expand Down
2 changes: 2 additions & 0 deletions src/goto-cc/ld_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ int ld_modet::doit()

messaget::eval_verbosity(
cmdline.get_value("verbosity"), messaget::M_ERROR, gcc_message_handler);
gcc_message_handler.print_warnings_as_errors(
cmdline.isset("fatal-warnings") && !cmdline.isset("no-fatal-warnings"));

compilet compiler(
cmdline,
Expand Down
1 change: 1 addition & 0 deletions src/goto-cc/ms_cl_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ int ms_cl_modet::doit()
: messaget::M_ERROR;

Check warning on line 61 in src/goto-cc/ms_cl_mode.cpp

View check run for this annotation

Codecov / codecov/patch

src/goto-cc/ms_cl_mode.cpp#L59-L61

Added lines #L59 - L61 were not covered by tests
const auto verbosity = messaget::eval_verbosity(
cmdline.get_value("verbosity"), default_verbosity, message_handler);
message_handler.print_warnings_as_errors(cmdline.isset("WX"));

Check warning on line 64 in src/goto-cc/ms_cl_mode.cpp

View check run for this annotation

Codecov / codecov/patch

src/goto-cc/ms_cl_mode.cpp#L63-L64

Added lines #L63 - L64 were not covered by tests

ms_cl_versiont ms_cl_version;
ms_cl_version.get("cl.exe");
Expand Down
1 change: 1 addition & 0 deletions src/goto-cc/ms_link_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ int ms_link_modet::doit()

messaget::eval_verbosity(
cmdline.get_value("verbosity"), messaget::M_ERROR, message_handler);
message_handler.print_warnings_as_errors(cmdline.isset("WX"));

Check warning on line 38 in src/goto-cc/ms_link_mode.cpp

View check run for this annotation

Codecov / codecov/patch

src/goto-cc/ms_link_mode.cpp#L38

Added line #L38 was not covered by tests

compilet compiler(cmdline, message_handler, cmdline.isset("WX"));

Check warning on line 40 in src/goto-cc/ms_link_mode.cpp

View check run for this annotation

Codecov / codecov/patch

src/goto-cc/ms_link_mode.cpp#L40

Added line #L40 was not covered by tests

Expand Down

0 comments on commit 5b611b2

Please sign in to comment.