Skip to content

Commit

Permalink
Strict mode: warning messages are treated as fatal errors
Browse files Browse the repository at this point in the history
Also error messages are always treated as fatal errors.

Closes #594
  • Loading branch information
stanislaw committed Mar 9, 2020
1 parent 22c7d85 commit 7f27bd5
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/generated/CLIOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

--debug Enables Debug Mode: more logs are printed

--strict Enables Strict Mode: all warning messages are treated as fatal errors

--compdb-path filename Path to a compilation database (compile_commands.json) for junk detection

--compilation-flags string Extra compilation flags for junk detection
Expand Down
2 changes: 2 additions & 0 deletions include/mull/Diagnostics/Diagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Diagnostics {
~Diagnostics();

void enableDebugMode();
void enableStrictMode();

void info(const std::string& message);
void warning(const std::string& message);
Expand All @@ -27,6 +28,7 @@ class Diagnostics {
std::mutex mutex;
bool seenProgress;
bool debugModeEnabled;
bool strictModeEnabled;
};

}
14 changes: 13 additions & 1 deletion lib/Diagnostics/Diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class mull::DiagnosticsImpl {
};

Diagnostics::Diagnostics()
: impl(new DiagnosticsImpl()), seenProgress(false), debugModeEnabled(false) {}
: impl(new DiagnosticsImpl()), seenProgress(false), debugModeEnabled(false), strictModeEnabled(false) {}

Diagnostics::~Diagnostics() {
delete impl;
Expand All @@ -35,6 +35,11 @@ void Diagnostics::enableDebugMode() {
impl->enableDebugMode();
}

void Diagnostics::enableStrictMode() {
std::lock_guard<std::mutex> guard(mutex);
strictModeEnabled = true;
}

void Diagnostics::info(const std::string &message) {
std::lock_guard<std::mutex> guard(mutex);
prepare();
Expand All @@ -45,12 +50,19 @@ void Diagnostics::warning(const std::string &message) {
std::lock_guard<std::mutex> guard(mutex);
prepare();
impl->log().warn(message);
if (strictModeEnabled) {
impl->log().warn(
"Strict Mode enabled: warning messages are treated as fatal errors. Exiting now.");
exit(1);
}
}

void Diagnostics::error(const std::string &message) {
std::lock_guard<std::mutex> guard(mutex);
prepare();
impl->log().error(message);
impl->log().error("Error messages are treated as fatal errors. Exiting now.");
exit(1);
}

void Diagnostics::progress(const std::string &message) {
Expand Down
2 changes: 1 addition & 1 deletion tests-lit/tests/options/-debug/01-debug-option/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ int main() {
; WITHOUT-DEBUG-NOT:{{^.*\[debug\].*$}}
; WITHOUT-DEBUG:{{^.*}}sample.cpp:2:12: warning: Killed: Replaced + with - [cxx_add_to_sub]{{$}}
; WITH-DEBUG:[debug] Diagnostics: debug mode enabled
; WITH-DEBUG:[debug] Diagnostics: Debug Mode enabled. Debug-level messages will be printed.
; WITH-DEBUG:{{^.*}}sample.cpp:2:12: warning: Killed: Replaced + with - [cxx_add_to_sub]{{$}}
**/
18 changes: 18 additions & 0 deletions tests-lit/tests/options/-strict/01-strict-mode/sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
int main() {
return 0;
}

/**
; RUN: cd / && %CLANG_EXEC %s -o %s.exe
; RUN: cd %CURRENT_DIR
; RUN: (unset TERM; %MULL_EXEC -test-framework CustomTest %s.exe 2>&1; test $? = 0) | %FILECHECK_EXEC %s --strict-whitespace --match-full-lines --check-prefix=WITHOUT-OPTION
; RUN: (unset TERM; %MULL_EXEC -strict -test-framework CustomTest %s.exe 2>&1; test $? = 1) | %FILECHECK_EXEC %s --strict-whitespace --match-full-lines --check-prefix=WITH-OPTION
; WITHOUT-OPTION:[warning] No bitcode: x86_64
; WITHOUT-OPTION:[info] No mutants found. Mutation score: infinitely high
; WITH-OPTION:[info] Diagnostics: Strict Mode enabled. Warning messages will be treated as fatal errors.
; WITH-OPTION:[warning] No bitcode: x86_64
; WITH-OPTION:[warning] Strict Mode enabled: warning messages are treated as fatal errors. Exiting now.
; WITH-OPTION-EMPTY:
**/
8 changes: 8 additions & 0 deletions tools/driver-cxx/CLIOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ opt<bool> tool::DebugEnabled(
init(false),
cat(MullCXXCategory));

opt<bool> tool::StrictModeEnabled(
"strict",
desc("Enables Strict Mode: all warning messages are treated as fatal errors"),
Optional,
init(false),
cat(MullCXXCategory));

opt<bool> tool::DumpCLIInterface(
"dump-cli",
desc("Prints CLI options in the Sphinx/RST friendly format"),
Expand Down Expand Up @@ -309,6 +316,7 @@ void tool::dumpCLIInterface(Diagnostics &diagnostics) {
reporters,
&IDEReporterShowKilled,
&DebugEnabled,
&StrictModeEnabled,

&CompilationDatabasePath,
&CompilationFlags,
Expand Down
1 change: 1 addition & 0 deletions tools/driver-cxx/CLIOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extern list<ReporterKind> ReportersOption;
extern opt<bool> IDEReporterShowKilled;

extern opt<bool> DebugEnabled;
extern opt<bool> StrictModeEnabled;

extern opt<SandboxKind> SandboxOption;

Expand Down
7 changes: 6 additions & 1 deletion tools/driver-cxx/driver-cxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ int main(int argc, char **argv) {

if (tool::DebugEnabled) {
diagnostics.enableDebugMode();
diagnostics.debug("Diagnostics: debug mode enabled");
diagnostics.debug("Diagnostics: Debug Mode enabled. Debug-level messages will be printed.");
}

if (tool::StrictModeEnabled) {
diagnostics.enableStrictMode();
diagnostics.info("Diagnostics: Strict Mode enabled. Warning messages will be treated as fatal errors.");
}

validateInputFile();
Expand Down

0 comments on commit 7f27bd5

Please sign in to comment.