Skip to content

Commit

Permalink
Merge pull request #847 from mull-project/delete-temp-files
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexDenisov committed Mar 27, 2021
2 parents eed5960 + 6f6c1a8 commit fd5a170
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 13 deletions.
6 changes: 6 additions & 0 deletions docs/generated/CLIOptions.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
--output path output file

--workers number How many threads to use

--timeout number Timeout per test run (milliseconds)
Expand All @@ -24,6 +26,10 @@

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

--keep-object-files Keep temporary object files

--keep-executable Keep temporary executable file

--no-test-output Does not capture output from test runs

--no-mutant-output Does not capture output from mutant runs
Expand Down
3 changes: 3 additions & 0 deletions include/mull/Config/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ struct Configuration {
bool captureMutantOutput;
bool skipSanityCheckRun;
bool includeNotCovered;
bool keepObjectFiles;
bool keepExecutable;

int timeout;
unsigned linkerTimeout;
Expand All @@ -26,6 +28,7 @@ struct Configuration {
std::vector<std::string> bitcodePaths;

std::string executable;
std::string outputFile;
std::string coverageInfo;

std::string linker;
Expand Down
3 changes: 2 additions & 1 deletion lib/Config/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ unsigned MullDefaultLinkerTimeoutMilliseconds = 30000;

Configuration::Configuration()
: debugEnabled(false), dryRunEnabled(false), captureTestOutput(true), captureMutantOutput(true),
skipSanityCheckRun(false), includeNotCovered(false), timeout(MullDefaultTimeoutMilliseconds),
skipSanityCheckRun(false), includeNotCovered(false), keepObjectFiles(false),
keepExecutable(false), timeout(MullDefaultTimeoutMilliseconds),
linkerTimeout(MullDefaultLinkerTimeoutMilliseconds), diagnostics(IDEDiagnosticsKind::None),
parallelization(singleThreadParallelization()) {}

Expand Down
10 changes: 10 additions & 0 deletions lib/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ Driver::normalRunMutations(const std::vector<MutationPoint *> &mutationPoints,
singleTask.execute("Link mutated program",
[&]() { executable = toolchain.linker().linkObjectFiles(objectFiles); });

if (!config.keepObjectFiles) {
for (auto &objectFile : objectFiles) {
llvm::sys::fs::remove(objectFile);
}
}

Runner runner(diagnostics);
/// On macOS, sometimes newly compiled programs take more time to execute for the first run
/// As we take the execution time as a baseline for timeout it makes sense to have an additional
Expand All @@ -258,6 +264,10 @@ Driver::normalRunMutations(const std::vector<MutationPoint *> &mutationPoints,
diagnostics, "Running mutants", mutants, mutationResults, std::move(tasks));
mutantRunner.execute();

if (!config.keepExecutable) {
llvm::sys::fs::remove(executable);
}

return mutationResults;
}

Expand Down
28 changes: 18 additions & 10 deletions lib/Toolchain/Linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,33 @@
using namespace mull;
using namespace std::string_literals;

std::string outputFileName(const Configuration &configuration, Diagnostics &diagnostics) {
if (configuration.outputFile.empty()) {
llvm::Twine prefix("mull");
llvm::SmallString<128> resultPath;

if (std::error_code err = llvm::sys::fs::createTemporaryFile(prefix, "exe", resultPath)) {
diagnostics.error("Cannot create temporary file"s + err.message());
return std::string();
}
return resultPath.str().str();
}
return configuration.outputFile;
}

Linker::Linker(const Configuration &configuration, Diagnostics &diagnostics)
: configuration(configuration), diagnostics(diagnostics) {}

std::string Linker::linkObjectFiles(const std::vector<std::string> &objects) {
llvm::Twine prefix("mull");
llvm::SmallString<128> resultPath;

if (std::error_code err = llvm::sys::fs::createTemporaryFile(prefix, "exe", resultPath)) {
diagnostics.error("Cannot create temporary file"s + err.message());
return std::string();
}
std::string outputFile = outputFileName(configuration, diagnostics);
Runner runner(diagnostics);
std::vector<std::string> arguments;
std::copy(std::begin(configuration.linkerFlags),
std::end(configuration.linkerFlags),
std::back_inserter(arguments));
std::copy(std::begin(objects), std::end(objects), std::back_inserter(arguments));
arguments.emplace_back("-o");
arguments.push_back(resultPath.str().str());
arguments.push_back(outputFile);
ExecutionResult result = runner.runProgram(
configuration.linker, arguments, {}, configuration.linkerTimeout, true, std::nullopt);
std::stringstream commandStream;
Expand All @@ -49,6 +57,6 @@ std::string Linker::linkObjectFiles(const std::vector<std::string> &objects) {
diagnostics.error(message.str());
}
diagnostics.debug("Link command: "s + command);
diagnostics.info("Compiled executable: "s + resultPath.c_str());
return resultPath.str().str();
diagnostics.info("Compiled executable: "s + outputFile);
return outputFile;
}
3 changes: 2 additions & 1 deletion tests-lit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ set(LIT_COMMAND
${LIT_EXEC}
-vv
${CMAKE_CURRENT_SOURCE_DIR}/tests
)

)

add_custom_target(tests-lit
COMMAND cd ${CMAKE_CURRENT_SOURCE_DIR} && make clean
Expand Down
2 changes: 1 addition & 1 deletion tests-lit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ CLEAN_FIND_CMD=find \
CLEAN_FILES=$(shell $(CLEAN_FIND_CMD))
clean: ## Clean all temporary artefacts
echo $(CLEAN_FILES) | \
xargs rm -rfv
xargs rm -rf
19 changes: 19 additions & 0 deletions tests-lit/tests/keep-temps/01/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
int doSomething(int arg1, int arg2) {
return arg1 + arg2;
}

int main() {
if (doSomething(2, 3) != 5) {
return 1;
}
return 0;
}

// clang-format off

// RUN: cd / && %clang_cc %s -fembed-bitcode -g -o %s.exe
// RUN: cd %CURRENT_DIR
// RUN: unset TERM; %MULL_EXEC -linker=%clang_cc -mutators=cxx_add_to_sub -output=%s.mutated-keep.exe -keep-executable %s.exe
// RUN: test -f %s.mutated-keep.exe
// RUN: unset TERM; %MULL_EXEC -linker=%clang_cc -mutators=cxx_add_to_sub -output=%s.mutated-delete.exe %s.exe
// RUN: test ! -f %s.mutated-delete.exe
22 changes: 22 additions & 0 deletions tools/mull-cxx/CLIOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ opt<std::string> tool::InputFile(
value_desc("path"),
cat(MullCXXCategory));

opt<std::string> tool::OutputFile(
"output",
desc("output file"),
value_desc("path"),
init(""),
cat(MullCXXCategory));

opt<unsigned> tool::Workers(
"workers",
desc("How many threads to use"),
Expand Down Expand Up @@ -139,6 +146,16 @@ opt<bool> tool::EnableAST(
llvm::cl::desc("Enable \"white\" AST search (disabled by default)"),
llvm::cl::cat(MullCXXCategory), llvm::cl::init(false));

opt<bool> tool::KeepObjectFiles(
"keep-object-files", llvm::cl::Optional,
llvm::cl::desc("Keep temporary object files"),
llvm::cl::cat(MullCXXCategory), llvm::cl::init(false));

opt<bool> tool::KeepExecutable(
"keep-executable", llvm::cl::Optional,
llvm::cl::desc("Keep temporary executable file"),
llvm::cl::cat(MullCXXCategory), llvm::cl::init(false));

opt<std::string> tool::GitDiffRef(
"git-diff-ref",
desc("Git branch to run diff against (enables incremental testing)"),
Expand Down Expand Up @@ -324,6 +341,8 @@ void tool::dumpCLIInterface(Diagnostics &diagnostics) {
Option *mutators = &(Option &)Mutators;
Option *reporters = &(Option &)ReportersOption;
std::vector<Option *> mullOptions({
&OutputFile,

&Workers,
&Timeout,
&DryRunOption,
Expand All @@ -336,6 +355,9 @@ void tool::dumpCLIInterface(Diagnostics &diagnostics) {
&DebugEnabled,
&StrictModeEnabled,

&KeepObjectFiles,
&KeepExecutable,

&NoTestOutput,
&NoMutantOutput,
&NoOutput,
Expand Down
4 changes: 4 additions & 0 deletions tools/mull-cxx/CLIOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ using namespace llvm::cl;
extern OptionCategory MullCXXCategory;

extern opt<std::string> InputFile;
extern opt<std::string> OutputFile;

extern opt<unsigned> Workers;
extern opt<unsigned> Timeout;
Expand Down Expand Up @@ -49,6 +50,9 @@ extern opt<bool> NoMutantOutput;

extern opt<bool> EnableAST;

extern opt<bool> KeepObjectFiles;
extern opt<bool> KeepExecutable;

extern opt<std::string> GitDiffRef;
extern opt<std::string> GitProjectRoot;

Expand Down
4 changes: 4 additions & 0 deletions tools/mull-cxx/mull-cxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ int main(int argc, char **argv) {
configuration.timeout = tool::Timeout.getValue();

configuration.executable = tool::InputFile.getValue();
configuration.outputFile = tool::OutputFile.getValue();
configuration.coverageInfo = tool::CoverageInfo.getValue();
configuration.includeNotCovered = tool::IncludeNotCovered.getValue();

configuration.keepObjectFiles = tool::KeepObjectFiles.getValue();
configuration.keepExecutable = tool::KeepExecutable.getValue();

if (tool::Workers) {
mull::ParallelizationConfig parallelizationConfig;
parallelizationConfig.workers = tool::Workers;
Expand Down

0 comments on commit fd5a170

Please sign in to comment.