From fa0b939c9b595e2ca716597981748b011a9a5998 Mon Sep 17 00:00:00 2001 From: "Alastair F. Donaldson" Date: Tue, 28 Jan 2025 16:34:32 +0000 Subject: [PATCH 1/2] Clear diagnostics between processing of source files Fixes #353. --- .../src/new_mutate_frontend_action_factory.cc | 10 +++++++- test/bespoke/one_bad_file_one_good_file/bad.c | 3 +++ .../bespoke/one_bad_file_one_good_file/good.c | 3 +++ .../one_bad_file_one_good_file/test.py | 23 +++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 test/bespoke/one_bad_file_one_good_file/bad.c create mode 100644 test/bespoke/one_bad_file_one_good_file/good.c create mode 100644 test/bespoke/one_bad_file_one_good_file/test.py diff --git a/src/libdredd/src/new_mutate_frontend_action_factory.cc b/src/libdredd/src/new_mutate_frontend_action_factory.cc index 43828e8e..eaa6661f 100644 --- a/src/libdredd/src/new_mutate_frontend_action_factory.cc +++ b/src/libdredd/src/new_mutate_frontend_action_factory.cc @@ -45,15 +45,23 @@ class MutateFrontendAction : public clang::ASTFrontendAction { llvm::StringRef file) override; bool BeginInvocation(clang::CompilerInstance& compiler_instance) override { - (void)compiler_instance; // Unused. + // Clear any diagnostics generated when operating on previous source files. + compiler_instance.getDiagnostics().getClient()->clear(); + + // Sanity check to confirm that there is a current input file. const bool input_exists = !getCurrentInput().isEmpty(); (void)input_exists; // Keep release-mode compilers happy. assert(input_exists && "No current file."); + + // Check whether this file has already been processed. if (processed_files_->contains(getCurrentFile().str())) { llvm::errs() << "Warning: already processed " << getCurrentFile() << "; skipping repeat occurrence.\n"; return false; } + + // Record that this file has been processed so that duplicate processing + // will be detected in the future. processed_files_->insert(getCurrentFile().str()); return true; } diff --git a/test/bespoke/one_bad_file_one_good_file/bad.c b/test/bespoke/one_bad_file_one_good_file/bad.c new file mode 100644 index 00000000..773cd8d6 --- /dev/null +++ b/test/bespoke/one_bad_file_one_good_file/bad.c @@ -0,0 +1,3 @@ +void foo() error { + +} diff --git a/test/bespoke/one_bad_file_one_good_file/good.c b/test/bespoke/one_bad_file_one_good_file/good.c new file mode 100644 index 00000000..18979492 --- /dev/null +++ b/test/bespoke/one_bad_file_one_good_file/good.c @@ -0,0 +1,3 @@ +void bar() { + +} diff --git a/test/bespoke/one_bad_file_one_good_file/test.py b/test/bespoke/one_bad_file_one_good_file/test.py new file mode 100644 index 00000000..5f728bc5 --- /dev/null +++ b/test/bespoke/one_bad_file_one_good_file/test.py @@ -0,0 +1,23 @@ +import os +import re +import shutil +import subprocess +import sys +from pathlib import Path + +DREDD_REPO_ROOT = os.environ['DREDD_REPO_ROOT'] +DREDD_INSTALLED_EXECUTABLE = Path(DREDD_REPO_ROOT, 'third_party', 'clang+llvm', 'bin', 'dredd') + + +def main(): + # Run Dredd + cmd = [DREDD_INSTALLED_EXECUTABLE, 'bad.c', 'good.c', '--'] + result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + assert result.returncode != 0 + error_output = result.stderr.decode('utf-8') + pattern = r"Error while processing .*good\.c" + assert re.search(pattern, error_output) is None + + +if __name__ == '__main__': + sys.exit(main()) From 3a0ef9e7a33766f11a331aa502f242a59752e5f1 Mon Sep 17 00:00:00 2001 From: "Alastair F. Donaldson" Date: Tue, 28 Jan 2025 18:13:37 +0000 Subject: [PATCH 2/2] Address iwyu error. --- src/libdredd/src/new_mutate_frontend_action_factory.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libdredd/src/new_mutate_frontend_action_factory.cc b/src/libdredd/src/new_mutate_frontend_action_factory.cc index eaa6661f..dee7212a 100644 --- a/src/libdredd/src/new_mutate_frontend_action_factory.cc +++ b/src/libdredd/src/new_mutate_frontend_action_factory.cc @@ -19,6 +19,7 @@ #include #include "clang/AST/ASTConsumer.h" +#include "clang/Basic/Diagnostic.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/FrontendAction.h" #include "clang/Frontend/FrontendOptions.h"