Skip to content

Commit

Permalink
reimplement diff using cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
ksco committed Aug 29, 2023
1 parent 7f273e4 commit 51cf69c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 28 deletions.
2 changes: 1 addition & 1 deletion core/ir/aarch64/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ instr_encode_arch(dcontext_t *dcontext, instr_t *instr, byte *copy_pc, byte *fin
instr_disassemble_to_buffer(dcontext, instr, disas_instr,
MAX_INSTR_DIS_SZ);
SYSLOG_INTERNAL_ERROR("Internal Error: Failed to encode instruction:"
" '%s'\n",
" '%s'",
disas_instr);
}
});
Expand Down
2 changes: 1 addition & 1 deletion core/ir/riscv64/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ instr_encode_arch(dcontext_t *dcontext, instr_t *instr, byte *copy_pc, byte *fin
instr_disassemble_to_buffer(dcontext, instr, disas_instr,
MAX_INSTR_DIS_SZ);
SYSLOG_INTERNAL_ERROR("Internal Error: Failed to encode instruction:"
" '%s'\n",
" '%s'",
disas_instr);
}
});
Expand Down
2 changes: 1 addition & 1 deletion suite/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1933,7 +1933,7 @@ elseif (RISCV64)
set(api.ir-static_runcmp "${CMAKE_CURRENT_SOURCE_DIR}/runcmp.cmake")
set(api.ir-static_runcmp_capture "stderr")
# Internal error lines are ignored as they are not printed in static builds.
set(api.ir-static_runcmp_ignore_matching_lines "^[><]")
set(api.ir-static_runcmp_ignore_matching_lines "^<.+>")
endif ()

if (ARM)
Expand Down
3 changes: 1 addition & 2 deletions suite/tests/api/ir_riscv64.expect
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ c.xor a5 -> fp
c.sub a5 -> fp
test_integer_arith complete
lui 0x2a -> a0
<Internal Error: Failed to encode instruction: 'auipc 0x0000004000016234 -> a0'
>
<Internal Error: Failed to encode instruction: 'auipc 0x0000004000016234 -> a0'>
jalr a1 0x2a -> a0
c.li 31 -> a1
c.lui 1 -> a1
Expand Down
71 changes: 48 additions & 23 deletions suite/tests/runcmp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,57 @@ if ("${capture}" STREQUAL "stderr")
set(output "${cmd_err}")
endif ()

set(tmp "${cmp}-out")
file(WRITE "${tmp}" "${output}")
# Get expected output, we assume it has already been processed w/
# regex => literal, etc.
file(READ "${cmp}" expect)

# We do not support regex in expect file b/c ctest can't handle big regex:
# "RegularExpression::compile(): Expression too big."
# Convert file contents into a CMake list, where each element in the list is one
# line of the file, empty lines are ignored.
string(REGEX REPLACE "^[\n]+" "" output "${output}")
string(REGEX REPLACE "[\n]+$" "" output "${output}")
string(REPLACE "\n\n" "\n" output "${output}")
STRING(REGEX REPLACE ";" "\\\\;" output "${output}")
STRING(REGEX REPLACE "\n" ";" output "${output}")

# We used to implement a "diff -b" via cmake regex on the output and expect
# file, but that gets really slow (30s for 750KB strings) so now we require
# strict matches per line.
string(REGEX REPLACE "^[\n]+" "" expect "${expect}")
string(REGEX REPLACE "[\n]+$" "" expect "${expect}")
string(REPLACE "\n\n" "\n" expect "${expect}")
STRING(REGEX REPLACE ";" "\\\\;" expect "${expect}")
STRING(REGEX REPLACE "\n" ";" expect "${expect}")

# Use diff -I to get the ability to skip some lines.
# Filter out all the ignored lines.
set(filtered_expect)
if(NOT "${ignore_matching_lines}" STREQUAL "")
foreach (item ${expect})
string(REGEX MATCH "${ignore_matching_lines}" match_result "${item}")
if (NOT match_result)
list(APPEND filtered_expect ${item})
endif ()
endforeach ()
endif ()

list (LENGTH filtered_expect filtered_expect_length)
list (LENGTH output output_length)

set(diffcmd "diff")
if (ignore_matching_lines)
execute_process(COMMAND ${diffcmd} -I ${ignore_matching_lines} ${tmp} ${cmp}
RESULT_VARIABLE dcmd_result
ERROR_VARIABLE dcmd_err
OUTPUT_VARIABLE dcmd_out)
else ()
execute_process(COMMAND ${diffcmd} ${tmp} ${cmp}
RESULT_VARIABLE dcmd_result
ERROR_VARIABLE dcmd_err
OUTPUT_VARIABLE dcmd_out)
set(lists_identical TRUE)
if (NOT ${filtered_expect_length} EQUAL ${output_length})
set(lists_identical FALSE)
endif ()

if (dcmd_result)
message(STATUS "diff: ${dcmd_out}")
message(FATAL_ERROR "output in ${tmp} failed to match expected output in ${cmp}")
endif (dcmd_result)
if (lists_identical)
foreach (index RANGE ${filtered_expect_length})
list(GET filtered_expect ${index} item1)
list(GET output ${index} item2)
if (NOT ${item1} STREQUAL ${item2})
set(lists_identical FALSE)
message(STATUS "The first difference:")
message(STATUS "< ${item1}")
message(STATUS "> ${item2}")
break ()
endif ()
endforeach ()
endif ()

if (NOT lists_identical)
message(FATAL_ERROR "failed to match expected output")
endif ()

0 comments on commit 51cf69c

Please sign in to comment.