Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline patch fix #913

Merged
merged 3 commits into from
Nov 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/mull/Reporters/SourceCodeReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SourceCodeReader {
SourceCodeReader();
std::string getContext(const mull::SourceLocation &sourceLocation);
std::string getSourceLineWithCaret(const SourceLocation &sourceLocation);
std::string getSourceLine(const SourceLocation &sourceLocation);
std::vector<std::string> getSourceLines(const SourceLocation &sourceLocation, const SourceLocation &sourceEndLocation);
private:
SourceManager sourceManager;
};
Expand Down
20 changes: 11 additions & 9 deletions lib/Reporters/PatchesReporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void mull::PatchesReporter::reportResults(const Result &result) {
"_");
;
const auto mutator = factory.getMutator(mutant.getMutatorIdentifier());
const std::string sourceLine = sourceCodeReader.getSourceLine(sourceLocation);
const std::vector<std::string> sourceLines = sourceCodeReader.getSourceLines(sourceLocation, sourceEndLocation);
const std::string sourcePath = std::regex_replace(sourceLocation.filePath, basePathRegex, "");

const std::string prefix = [&mutationExecutionResult]() {
Expand All @@ -98,14 +98,16 @@ void mull::PatchesReporter::reportResults(const Result &result) {
}();

diagnostics.debug(std::string("Writing Patchfile: ") + filename.c_str());
std::ofstream myfile{ filename };
myfile << "--- a" << sourcePath << " 0"
<< "\n"
<< "+++ b" << sourcePath << " 0"
<< "\n"
<< "@@ -" << sourceLocation.line << ",1 +" << sourceLocation.line << ",1 @@\n"
<< "-" << sourceLine << "+" << sourceLine.substr(0, sourceLocation.column - 1)
<< mutator->getReplacement() << sourceLine.substr(sourceEndLocation.column - 1);
const int lines = sourceEndLocation.line - sourceLocation.line + 1;
std::ofstream myfile{filename};
myfile << "--- a" << sourcePath << " 0" << "\n"
<< "+++ b" << sourcePath << " 0" << "\n"
<< "@@ -" << sourceLocation.line << "," << lines << " +" << sourceLocation.line << ",1 @@\n";
for (auto& currentLine : sourceLines){
myfile << "-" << currentLine;
}
myfile << "+" << sourceLines.front().substr(0, sourceLocation.column-1)
<< mutator->getReplacement() << sourceLines.back().substr(sourceEndLocation.column-1) ;
myfile << std::accumulate(
mullInformation.begin(),
mullInformation.end(),
Expand Down
10 changes: 6 additions & 4 deletions lib/Reporters/SourceCodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ std::string SourceCodeReader::getSourceLineWithCaret(const SourceLocation &sourc
return ss.str();
}

std::string SourceCodeReader::getSourceLine(const SourceLocation &sourceLocation) {
auto line = sourceManager.getLine(sourceLocation);
assert(sourceLocation.column < line.size());
return line;
std::vector<std::string> SourceCodeReader::getSourceLines(const SourceLocation &sourceLocation, const SourceLocation &sourceEndLocation) {
std::vector<std::string> lines;
for (SourceLocation temp{sourceLocation}; temp.line <= sourceEndLocation.line; temp.line++){
lines.push_back(sourceManager.getLine(temp));
}
return lines;
}
41 changes: 41 additions & 0 deletions tests-lit/tests/patch-reporter/multiline_patch/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
void foobar(int b, bool x) {
b += x?4:5;
}

int main() {
// clang-format off
foobar (4,
false);
foobar (4 +
7 +
9 +
10,
false);
// clang-format on
return 0;
}

// clang-format off
/**

RUN: cd %S
RUN: mkdir -p %S/Output/sandbox
RUN: cp %S/main.cpp %S/Output/sandbox/main.cpp
RUN: cd %S/Output/sandbox

/// We cd to the the test directory and compile using relative paths.
RUN: cd %S; %clang_cxx %sysroot -fembed-bitcode -g -O0 Output/sandbox/main.cpp -o Output/main.cpp.exe

RUN: cd %S/Output && echo $PATH; (unset TERM; %mull_cxx -mutators=cxx_remove_void_call -linker-flags="%sysroot" --linker=%clang_cxx -debug main.cpp.exe --report-name test --reporters Patches --reporters IDE; test $? = 0; ls -R %S/Output/test-patches; cat %S/Output/test-patches/survived-Output_sandbox_main_cpp-cxx_remove_void_call-L7-C3.patch; cat %S/Output/test-patches/survived-Output_sandbox_main_cpp-cxx_remove_void_call-L9-C3.patch) | %filecheck %s --dump-input=fail --strict-whitespace --match-full-lines

CHECK:[debug] Writing Patchfile: {{.*}}
CHECK:[info] Patchfiles can be found at './test-patches'
CHECK:{{.*}}main_cpp-cxx_remove_void{{.*}}
CHECK:--- a{{.*}}/Output/sandbox/main.cpp 0
CHECK:-{{\s+}}false);
CHECK:-{{\s+}}7 +
CHECK:-{{\s+}}9 +
CHECK:-{{\s+}}false);
CHECK:+{{\s+}};

*/