Skip to content

Commit

Permalink
Feature memcheck seperate rules (#25)
Browse files Browse the repository at this point in the history
* adds suppressions to codedup
* add trace
* fix realpath basesrc in codedup
* remove dbg messages
* clean old gnco for coverage target
* fix symlink compatible cleaning
* adds interactive memcheck tules
  • Loading branch information
psycofdj committed Aug 28, 2017
1 parent 61abf96 commit 3aef3c1
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cache:
- apt

install:
- sudo apt-get install debhelper cmake python-all lintian
- sudo apt-get install debhelper cmake python-all lintian fakeroot

script:
- export PATH=/usr/bin:${PATH}
Expand Down
57 changes: 42 additions & 15 deletions src/codedup/FindCodeDupRule.cmake
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
add_custom_target(codedup)
add_custom_target(codedup-clean)

set(CodeDupRule_DEFAULT_PMD_VERSION "5.7.0" CACHE STRING "CodeDupRule PDM installed version")
set(CodeDupRule_DEFAULT_PMD_HOME "/usr/share/pmd-bin-\${CodeDupRule_PMD_VERSION}" CACHE STRING "CodeDupRule location of PDM installation")
set(CodeDupRule_DEFAULT_INPUT "\${CMAKE_CURRENT_SOURCE_DIR}/src" CACHE STRING "CodeDupRule default list of input source directories")
set(CodeDupRule_DEFAULT_FILE_PATTERNS "*.cc;*.hh;*.hxx" CACHE STRING "CodeDupRule default list of wildcard patterns to search in INPUT directories")
set(CodeDupRule_DEFAULT_EXCLUDE_PATTERNS "\${CMAKE_CURRENT_SOURCE_DIR}/unit/.*" CACHE STRING "CodeDupRule default list of regexp to exclude from analysis")
set(CodeDupRule_DEFAULT_MIN_TOKENS "100" CACHE STRING "CodeDupRule default minimum token length which should be reported as a duplicate")
set(CodeDupRule_DEFAULT_ARGS "--skip-lexical-errors" CACHE STRING "CodeDupRule default additional arguments to give to PMD")
set(CodeDupRule_DEFAULT_PMD_VERSION "5.7.0" CACHE STRING "CodeDupRule PDM installed version")
set(CodeDupRule_DEFAULT_PMD_HOME "/usr/share/pmd-bin-\${CodeDupRule_PMD_VERSION}" CACHE STRING "CodeDupRule location of PDM installation")
set(CodeDupRule_DEFAULT_INPUT "\${CMAKE_CURRENT_SOURCE_DIR}/src" CACHE STRING "CodeDupRule default list of input source directories")
set(CodeDupRule_DEFAULT_FILE_PATTERNS "*.cc;*.hh;*.hxx" CACHE STRING "CodeDupRule default list of wildcard patterns to search in INPUT directories")
set(CodeDupRule_DEFAULT_EXCLUDE_PATTERNS "\${CMAKE_CURRENT_SOURCE_DIR}/unit/.*" CACHE STRING "CodeDupRule default list of regexp to exclude from analysis")
set(CodeDupRule_DEFAULT_MIN_TOKENS "100" CACHE STRING "CodeDupRule default minimum token length which should be reported as a duplicate")
set(CodeDupRule_DEFAULT_ARGS "--skip-lexical-errors" CACHE STRING "CodeDupRule default additional arguments to give to PMD")
set(CodeDupRule_DEFAULT_SUPRESSION "\${CMAKE_CURRENT_SOURCE_DIR}/src/codedup.suppr" CACHE STRING "CodeDupRule default supression list for legit code duplications")

xtdmake_eval(CodeDupRule_PMD_VERSION2 "${CodeDupRule_DEFAULT_PMD_VERSION}")
set(CodeDupRule_PMD_VERSION "${CodeDupRule_PMD_VERSION2}" CACHE STRING "")
Expand Down Expand Up @@ -80,6 +81,8 @@ else()
xtdmake_set_default(CodeDupRule ARGS)
xtdmake_set_default(CodeDupRule EXCLUDE_PATTERNS)
xtdmake_set_default_if_exists(CodeDupRule INPUT)
xtdmake_set_default_if_exists(CodeDupRule SUPRESSION)
get_filename_component(l_realSrcDir ${CMAKE_CURRENT_SOURCE_DIR} REALPATH)

file(GLOB_RECURSE CodeDupRule_PMD_CLASSPATH "${CodeDupRule_PMD_HOME}/lib/*.jar")
string(REPLACE ";" ":" CodeDupRule_PMD_CLASSPATH "${CodeDupRule_PMD_CLASSPATH}")
Expand All @@ -94,7 +97,6 @@ else()
foreach(c_res ${l_files})
foreach(c_exclude_pattern ${CodeDupRule_EXCLUDE_PATTERNS})
if (NOT ${c_res} MATCHES ${c_exclude_pattern})

get_filename_component(l_abspath ${c_res} REALPATH)
list(APPEND l_args --files ${l_abspath})
endif()
Expand Down Expand Up @@ -122,28 +124,53 @@ else()
endif()

add_custom_command(
COMMENT "Generating ${module} codedup HTML and XML reports"
COMMENT "Generating ${module} codedup reports"
OUTPUT
${CodeDupRule_OUTPUT}/codedup.xml
DEPENDS
${CodeDupRule_DEPENDS}
${XTDMake_HOME}/codedup/FindCodeDupRule.cmake
COMMAND mkdir -p ${CodeDupRule_OUTPUT}
COMMAND ${Java_EXECUTABLE} -cp ${CodeDupRule_PMD_CLASSPATH} net.sourceforge.pmd.cpd.CPD --minimum-tokens ${CodeDupRule_MIN_TOKENS} --format xml --language cpp ${l_args} ${CodeDupRule_ARGS} > ${CodeDupRule_OUTPUT}/codedup.xml || true
VERBATIM)

add_custom_command(
COMMENT "Filtering suppressions ${module} codedup reports"
OUTPUT
${CodeDupRule_OUTPUT}/codedup_clean.xml
DEPENDS
${CodeDupRule_SUPRESSION}
${CodeDupRule_OUTPUT}/codedup.xml
${XTDMake_HOME}/codedup/FindCodeDupRule.cmake
${XTDMake_HOME}/codedup/filter.py
COMMAND
${XTDMake_HOME}/codedup/filter.py
--supression "${CodeDupRule_SUPRESSION}"
--basesrc "${l_realSrcDir}"
--report "${CodeDupRule_OUTPUT}/codedup.xml"
--output "${CodeDupRule_OUTPUT}/codedup_clean.xml"
VERBATIM)

add_custom_command(
COMMENT "Generating ${module} codedup HTML and XML reports"
OUTPUT
${CodeDupRule_OUTPUT}/index.html
${CodeDupRule_OUTPUT}/status.json
DEPENDS
${CodeDupRule_DEPENDS}
${CodeDupRule_OUTPUT}/codedup_clean.xml
${XTDMake_HOME}/codedup/stylesheet.xsl
${XTDMake_HOME}/codedup/status.py
${XTDMake_HOME}/codedup/FindCodeDupRule.cmake
COMMAND mkdir -p ${CodeDupRule_OUTPUT}
COMMAND ${Java_EXECUTABLE} -cp ${CodeDupRule_PMD_CLASSPATH} net.sourceforge.pmd.cpd.CPD --minimum-tokens ${CodeDupRule_MIN_TOKENS} --format xml --language cpp ${l_args} ${CodeDupRule_ARGS} > ${CodeDupRule_OUTPUT}/codedup.xml || true
COMMAND ${Xsltproc_EXECUTABLE} ${XTDMake_HOME}/codedup/stylesheet.xsl ${CodeDupRule_OUTPUT}/codedup.xml > ${CodeDupRule_OUTPUT}/index.html
COMMAND ${XTDMake_HOME}/codedup/status.py --module ${module} --input-file=${CodeDupRule_OUTPUT}/codedup.xml --output-file=${CodeDupRule_OUTPUT}/status.json
COMMAND ${Xsltproc_EXECUTABLE} ${XTDMake_HOME}/codedup/stylesheet.xsl ${CodeDupRule_OUTPUT}/codedup_clean.xml > ${CodeDupRule_OUTPUT}/index.html
COMMAND ${XTDMake_HOME}/codedup/status.py --module ${module} --input-file=${CodeDupRule_OUTPUT}/codedup_clean.xml --output-file=${CodeDupRule_OUTPUT}/status.json
VERBATIM)

add_custom_target(${module}-codedup
DEPENDS
${CodeDupRule_OUTPUT}/index.html
${CodeDupRule_OUTPUT}/codedup.xml
${CodeDupRule_OUTPUT}/codedup_clean.xml
${CodeDupRule_OUTPUT}/status.json)

add_custom_target(${module}-codedup-clean
COMMAND rm -rf ${CodeDupRule_OUTPUT})
add_dependencies(codedup ${module}-codedup)
Expand Down
69 changes: 69 additions & 0 deletions src/codedup/filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/python

import sys
import os
import json
import argparse
import xml.etree.ElementTree as ET


def target_matches(p_target, p_suppr):
return ((p_suppr["file"] == p_target["file"]) and
(p_suppr["from"] <= p_target["from"]) and
(p_suppr["to"] >= p_target["to"]))


def target_found_in_supprs(p_target, p_supprs):
for c_suppr in p_supprs:
if target_matches(p_target, c_suppr):
return True;
return False;

def targets_found_in_supprs(p_targets, p_supprs):
for c_target in p_targets:
if not target_found_in_supprs(c_target, p_supprs):
return False
return True;

def targets_found_in_directives(p_targets, p_directives):
for c_directive in p_directives:
if targets_found_in_supprs(p_targets, c_directive):
return True
return False;


l_parser = argparse.ArgumentParser()
l_parser.add_argument("--report", action="store", help ="path to PMD xml report", required=True)
l_parser.add_argument("--output", action="store", help ="path to filtered report to write", required=True)
l_parser.add_argument("--supression", action="store", help ="path to supression list", required=True)
l_parser.add_argument("--basesrc", action="store", help ="base directory of analysed files", required=True)
l_result = l_parser.parse_args()

l_data = []
if len(l_result.supression):
l_suppFile = open(l_result.supression, "r")
l_content = l_suppFile.read().decode("utf-8")
l_data = json.loads(l_content)

l_base = l_result.basesrc
l_intput = open(l_result.report, "r")
l_tree = ET.parse(l_intput)
l_dups = l_tree.findall("./duplication")

for c_dup in l_dups:
l_lines = int(c_dup.get("lines", 0))
l_files = c_dup.findall("./file")
l_targets = []
for c_file in l_files:
l_targets.append({
u"from" : int(c_file.get("line", 0)),
u"to" : l_lines + int(c_file.get("line", 0)),
u"file" : unicode(c_file.get("path", "").replace(l_base, "").lstrip("/"))
})
if targets_found_in_directives(l_targets, l_data):
l_tree.getroot().remove(c_dup)
pass

l_tree.write(l_result.output)


6 changes: 6 additions & 0 deletions src/coverage/FindCovRule.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ xtdmake_find_program(Genhtml
VERSION_OPT "--version"
VERSION_POS 3)


set(CovRule_DEFAULT_EXCLUDE_PATTERNS "Test*.*" CACHE STRING "CovRule default file exclude wildcards")
set(CovRule_DEFAULT_MIN_PERCENT "30" CACHE STRING "CovRule default mimunim coverage percentage to consider task successful")
set(CovRule_FOUND 0)

execute_process(
COMMAND bash ${XTDMake_HOME}/coverage/clean-gcno.sh ${PROJECT_BINARY_DIR}
)


if (NOT Lcov_FOUND OR NOT Genhtml_FOUND OR NOT CheckRule_FOUND)
message(STATUS "Found module CovRule : FALSE (unmet required dependencies)")
if (CovRule_FIND_REQUIRED)
Expand Down
21 changes: 21 additions & 0 deletions src/coverage/clean-gcno.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash


l_buildDir="$1"

l_files=$(find -L "${l_buildDir}" -name '*.gcno')
for c_file in $(echo ${l_files}); do
#echo "processing file : ${c_file}"
l_items=$(gcov ${c_file} 2>/dev/null | grep File | sort | cut -d"'" -f2)
l_found=1
for c_item in $(echo ${l_items}); do
#echo "-> check item : ${c_item}"
if [ ! -f ${c_item} ]; then
l_found=0
fi
done
if [ "${l_found}" -eq 0 ]; then
echo "deleting ${c_file}"
rm -f ${c_file}
fi
done
25 changes: 25 additions & 0 deletions src/memcheck/FindMemcheckRule.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ else()
get_property(c_test_args TEST ${c_test} PROPERTY ARGS)

if (Valgrind_VERSION VERSION_LESS 3.9.0)
add_custom_target(${module}-memcheck-ut-${c_test}
COMMAND valgrind
--tool=memcheck
--leak-check=full
--num-callers=50
--show-reachable=no
--gen-suppressions=all
${l_supprs}
${MemcheckRule_EXTRA_ARGS}
--
./${c_test} ${c_test_args}
VERBATIM)
add_custom_command(
COMMENT "Performing memory analysis for ${module} : ${c_test}"
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${c_test}.memcheck.xml
Expand All @@ -81,6 +93,19 @@ else()
./${c_test} ${c_test_args} > /dev/null 2>&1 || true
VERBATIM)
else()
add_custom_target(${module}-memcheck-ut-${c_test}
COMMAND valgrind
--tool=memcheck
--leak-check=full
--show-leak-kinds=all
--num-callers=500
--show-reachable=no
--gen-suppressions=all
${l_supprs}
${MemcheckRule_EXTRA_ARGS}
--
./${c_test} ${c_test_args}
VERBATIM)
add_custom_command(
COMMENT "Performing memory analysis for ${module} : ${c_test}"
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${c_test}.memcheck.xml
Expand Down

0 comments on commit 3aef3c1

Please sign in to comment.