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

Fix patch reporting for unary operators and CXXAssignConst #1026

Merged
merged 2 commits into from
Jun 2, 2023
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 lib/JunkDetection/CXX/ASTStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ clang::SourceLocation ThreadSafeASTUnit::getLocation(const mull::SourceLocation

clang::SourceLocation
ThreadSafeASTUnit::getLocForEndOfToken(const clang::SourceLocation sourceLocationEnd) {
/// clang::Lexer::getLocForEndOfToken internally calls getLocation, which is known for not beeing
/// clang::Lexer::getLocForEndOfToken internally calls getLocation, which is known for not being
/// thread safe. therefore we need to protect it within the ThreadSafeASTUnit
std::lock_guard<std::mutex> lock(mutex);
clang::SourceManager &sourceManager = ast->getSourceManager();
Expand Down
8 changes: 7 additions & 1 deletion lib/JunkDetection/CXX/CXXJunkDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,19 @@ bool CXXJunkDetector::isJunk(MutationPoint *point) {
/// 1) Remove-Void, CallExpr example: its mutation location and its getStart() are the same.
/// 2) Binary Mutation, BinaryOperator example: its mutation location is
/// BinaryOperator's getOperatorLoc(), i.e. "+", while the
/// [getSourceRange().getBegin()(), getSourceRange().getEnd()] range is the whole "a + b"
/// [getSourceRange().getBegin(), getSourceRange().getEnd()] range is the whole "a + b"
/// expression.
clang::SourceLocation sourceLocationEnd =
(beginLine == mutationLocationBeginLine && beginColumn == mutationLocationBeginColumn)
? mutantExpression->getSourceRange().getEnd()
: location;

// For unary operators we shouldn't consider the location of the whole expression, but only
// the operator, otherwise it breaks patch reporter in weird ways
if (auto unary = llvm::dyn_cast<clang::UnaryOperator>(mutantExpression)) {
sourceLocationEnd = unary->getOperatorLoc();
}

/// Clang AST: how to get more precise debug information in certain cases?
/// http://clang-developers.42468.n3.nabble.com/Clang-AST-how-to-get-more-precise-debug-information-in-certain-cases-td4065195.html
/// https://stackoverflow.com/questions/11083066/getting-the-source-behind-clangs-ast
Expand Down
3 changes: 2 additions & 1 deletion lib/Mutators/CXX/NumberMutators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ std::string NumberAssignConst::ID() {

NumberAssignConst::NumberAssignConst()
: TrivialCXXMutator(getNumberMutators(), MutatorKind::CXX_AssignConst, NumberAssignConst::ID(),
"Replaces 'a = b' with 'a = 42'", "42", "Replaced 'a = b' with 'a = 42'") {}
"Replaces 'a = b' with 'a = 42'", "= 42;",
"Replaced 'a = b' with 'a = 42'") {}

std::string NumberInitConst::ID() {
return "cxx_init_const";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RUN: cd %S && %clang_cxx %sysroot %pass_mull_ir_frontend -g %s -o %s-ir.exe | %f
CHECK-MUTATE-NOT:{{^.*[Ee]rror.*$}}

CHECK-MUTATE:[info] Applying filter: junk (threads: 1)
CHECK-MUTATE:[debug] CXXJunkDetector: mutation "Unary Minus to Noop": {{.*}}sample.cpp:2:10 (end: 2:12)
CHECK-MUTATE:[debug] CXXJunkDetector: mutation "Unary Minus to Noop": {{.*}}sample.cpp:2:10 (end: 2:11)

RUN: (unset TERM; %mull_runner -debug -reporters=IDE -ide-reporter-show-killed %s-ir.exe 2>&1; test $? = 0) | %filecheck %s --dump-input=fail --strict-whitespace --match-full-lines

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ CHECK-MUTATE-NOT:{{^.*[Ee]rror.*$}}
CHECK-MUTATE:[info] Applying filter: junk (threads: 1)

TODO: IDE reporter reports location "!a" but we would rather want to see the location of '!'.
CHECK-MUTATE:[debug] CXXJunkDetector: mutation "Remove Unary Negation": {{.*}}sample.cpp:2:10 (end: 2:12)
CHECK-MUTATE:[debug] CXXJunkDetector: mutation "Remove Unary Negation": {{.*}}sample.cpp:2:10 (end: 2:11)

RUN: (unset TERM; %mull_runner -debug -reporters=IDE -ide-reporter-show-killed %s-ir.exe 2>&1; test $? = 0) | %filecheck %s --dump-input=fail --strict-whitespace --match-full-lines

Expand Down
32 changes: 32 additions & 0 deletions tests-lit/tests/reporters/patch-reporter/cxx_assign_const/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// clang-format off
void assign(int *x, int y) {
x[0] = y;
}

int main() {
int x = 15;
assign(&x, 10);

return 0;
}

/**

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 -O0 %pass_mull_ir_frontend -g Output/sandbox/main.cpp -o Output/main.cpp-ir.exe

RUN: cd %S/Output; (unset TERM; %mull_runner -debug ./main.cpp-ir.exe --report-name test-ir --reporters Patches --reporters IDE; test $? = 0; ls -R %S/Output/test-ir-patches; cd %S/Output/test-ir-patches; cat `ls`) | %filecheck %s --dump-input=fail --strict-whitespace --match-full-lines

CHECK:[debug] Writing Patchfile: {{.*}}
CHECK:[info] Patchfiles can be found at './test{{.*}}-patches'
CHECK:survived-{{.*}}main_cpp{{.*}}
CHECK:--- a/{{.*}}/Output/sandbox/main.cpp 0
CHECK:+++ b/{{.*}}/Output/sandbox/main.cpp 0
CHECK:+{{\s+}}x[0] = 42; y;

*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mutators:
- cxx_assign_const
quiet: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
int main() {
int x = -1;
int y = -x;

return y;
}

// 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 -O0 %pass_mull_ir_frontend -g Output/sandbox/main.cpp -o Output/main.cpp-ir.exe

RUN: cd %S/Output; (unset TERM; %mull_runner -debug ./main.cpp-ir.exe --report-name test-ir --reporters Patches --reporters IDE; test $? = 0; ls -R %S/Output/test-ir-patches; cd %S/Output/test-ir-patches; cat `ls`) | %filecheck %s --dump-input=fail --strict-whitespace --match-full-lines

CHECK:[debug] Writing Patchfile: {{.*}}
CHECK:[info] Patchfiles can be found at './test{{.*}}-patches'
CHECK:killed-{{.*}}main_cpp{{.*}}
CHECK:--- a/{{.*}}/Output/sandbox/main.cpp 0
CHECK:+++ b/{{.*}}/Output/sandbox/main.cpp 0
CHECK:+{{\s+}}int y = x;

*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mutators:
- cxx_minus_to_noop
quiet: false