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

Idea to integrate cppcheck #1581

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,22 @@ jobs:
set -e
set -x
eatmydata apt-get --yes --quiet install ../*.deb

cppcheck:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v2
with:
submodules: true
fetch-depth: 0
- name: Perform Source Code checks that were successful in the past
continue-on-error: true
run: |
set -x
git fetch --recurse-submodules=no https://github.com/linuxcnc/linuxcnc refs/tags/*:refs/tags/*
sudo apt-get -y install cppcheck shellcheck
scripts/cppcheck.sh
- name: Shellcheck
continue-on-error: true
run: |
scripts/shellcheck.sh
42 changes: 42 additions & 0 deletions scripts/cppcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash -e

if ! command -v cppcheck; then
echo "E: Please install the program 'cppcheck' prior to executing this script."
exit 1
fi

nproc=2
if command -v nproc; then
nproc=$(nproc)
fi

# *** C dominated folder ***
echo -n "I (1/4): checking HAL folders with C code only from dir "; pwd
for d in src/hal/classicladder src/hal/components src/hal/drivers \
src/hal/user_comps $(find src/hal/user_comps/ -type d src/hal/utils)
do
(cd "$d" && cppcheck -j $nproc --language=c --force *.h *.c)
done

echo -n "I (2/4): checking EMC folders with C code only from dir "; pwd
for d in src/emc/motion-logger src/emc/tp
do
(cd "$d" && cppcheck -j $nproc --language=c --force *.h *.c)
done
# problematic: src/hal/drivers src/hal/user_comps src/hal/utits

# *** C++ dominated folder ***
echo -n "I (3/4): checking EMC folders with C++ code only from dir "; pwd
for d in src/emc/canterp src/emc/ini src/emc/pythonplugin src/emc/tooldata
do
(cd "$d" && cppcheck -j $nproc --language=c++ --force *.hh *.cc)
done
# problematic iotask src/emc/sai src/emc/task

# *** C++ and C++ in same folder ***
echo -n "I (4/4): checking EMC folders with both C and C++ code from dir "; pwd
for d in src/emc/rs274ngc;
do
(cd "$d" && cppcheck -j $nproc --language=c --force *.h *.c && cppcheck -j $nproc --language=c++ --force *.hh *.cc)
done
# problematic src/emc/kinematics src/emc/nml_intf src/emc/usr_intf
9 changes: 9 additions & 0 deletions scripts/shellcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash -e

if ! command -v shellcheck > /dev/null; then
echo "E: Please install the program 'shellcheck' prior to executing this script."
exit 1
fi

echo Shellcheck
find . -name "*.sh" -exec shellcheck {} \;
20 changes: 9 additions & 11 deletions src/emc/rs274ngc/interp_convert.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5843,16 +5843,10 @@ int Interp::convert_straight_comp2(int move, //!< either G_0 or G_1
double CC_end, //!< C coordinate of end point
double u_end, double v_end, double w_end)
{
double alpha;
double beta;
double end_x, end_y, end_z; /* x-coordinate of actual end point */
double gamma;
double mid_x, mid_y; /* x-coordinate of end of added arc, if needed */
double radius;
CUTTER_COMP side;
double small = TOLERANCE_CONCAVE_CORNER; /* radians, testing corners */
double opx = 0, opy = 0, opz = 0; /* old programmed beginning point */
double theta;
double cx, cy, cz;
int concave;

Expand All @@ -5875,10 +5869,12 @@ int Interp::convert_straight_comp2(int move, //!< either G_0 or G_1
// end already filled out, above
} else {
// some XY motion
side = settings->cutter_comp_side;
radius = settings->cutter_comp_radius; /* will always be positive */
theta = atan2(cy - opy, cx - opx);
alpha = atan2(py - opy, px - opx);
double beta = 0.0; // initializing to avoid confusion over else branch below
double gamma = 0.0; // that does not define value but returns form function.
CUTTER_COMP side = settings->cutter_comp_side;
double radius = settings->cutter_comp_radius; /* will always be positive */
double theta = atan2(cy - opy, cx - opx);
double alpha = atan2(py - opy, px - opx);

if (side == CUTTER_COMP::LEFT) {
if (theta < alpha)
Expand All @@ -5890,8 +5886,10 @@ int Interp::convert_straight_comp2(int move, //!< either G_0 or G_1
alpha = (alpha + (2 * M_PIl));
beta = ((alpha - theta) - M_PI_2l);
gamma = -M_PI_2l;
} else
} else {
ERS(NCE_BUG_SIDE_NOT_RIGHT_OR_LEFT);
// the ERS macro will return from this function
}
end_x = (px + (radius * cos(alpha + gamma)));
end_y = (py + (radius * sin(alpha + gamma)));
mid_x = (opx + (radius * cos(alpha + gamma)));
Expand Down
2 changes: 1 addition & 1 deletion src/emc/rs274ngc/nurbs_additional_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ double nurbs_uj_l(double l, std::vector<double> span_knot_vector, std::vector<do
}
else
{
for(unsigned ii=0; ii<= nurbs_costant.size(); ii=ii+6)
for(unsigned ii=0; ii < nurbs_costant.size() - 5 ; ii=ii+6)
{
if((l <= lenght_vector[0]) && (at2 == 0) && (l>0))
{
Expand Down
5 changes: 3 additions & 2 deletions src/hal/user_comps/mb2hal/mb2hal_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,13 @@ static retCode parse_pin_names(const char * const names_string, mb_tx_t * const
if(name_count >= name_buf_size)
{
name_buf_size += NAME_ALLOC_SIZE;
name_ptrs = realloc(name_ptrs, sizeof(char *) * name_buf_size);
if(name_ptrs == NULL)
char ** tmp = realloc(name_ptrs, sizeof(char *) * name_buf_size);
if(NULL == tmp)
{
ERR(gbl.init_dbg, "Failed allocating memory");
return retERR;
}
name_ptrs = tmp;
}
name_ptrs[name_count++]=name;
name = strtok(NULL, ",");
Expand Down
2 changes: 1 addition & 1 deletion tests/build/ui/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
set -x
g++ -I${HEADERS} \
nml-position-logger.cc \
-L ${LIBDIR} -lnml -llinuxcnc \
-L "${LIBDIR}" -lnml -llinuxcnc \
-o /dev/null
8 changes: 4 additions & 4 deletions tests/halcompile/personalities_mod/test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
set -e

${SUDO} halcompile --personalities=2 --install lincurve_test.comp
Expand Down Expand Up @@ -28,8 +28,8 @@ for INSTALLED_FILE in "${RTLIB_DIR}"/{lincurve_test,logic_test,bitslice_test}"${
done

for HAL in *.hal; do
echo "testing $HAL"
BASE=$(basename $HAL .hal)
echo "testing '$HAL'"
BASE=$(basename "$HAL" .hal)
# use -s to avoid different user assignments in show output
halrun -s $HAL >| $BASE.result
halrun -s "$HAL" >| "$BASE".result
done
6 changes: 3 additions & 3 deletions tests/halcompile/userspace-count-names/test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
set -e

${SUDO} halcompile --install userspace_count_names.comp
Expand All @@ -18,6 +18,6 @@ fi

for HAL in *.hal; do
echo "testing $HAL"
BASE=$(basename $HAL .hal)
halrun $HAL | tr ' ' '\n' >| $BASE.result
BASE=$(basename "$HAL" .hal)
halrun "$HAL" | tr ' ' '\n' >| "$BASE".result
done
2 changes: 1 addition & 1 deletion tests/interp/compile/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ set -xe

g++ -o use-rs274 use-rs274.cc \
-Wall -Wextra -Wno-return-type -Wno-unused-parameter \
-I $HEADERS $PYTHON_CPPFLAGS -L $LIBDIR -Wl,-rpath,$LIBDIR $PYTHON_EXTRA_LDFLAGS $PYTHON_LIBS $PYTHON_EXTRA_LIBS -lrs274
-I $HEADERS $PYTHON_CPPFLAGS -L "$LIBDIR" -Wl,-rpath,$LIBDIR $PYTHON_EXTRA_LDFLAGS $PYTHON_LIBS $PYTHON_EXTRA_LIBS -lrs274
LD_BIND_NOW=YesPlease ./use-rs274
5 changes: 3 additions & 2 deletions tests/interp/g71-endless-loop2/test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
#
set -e

# Demonstrate endless loop when an additional segment is added in the G71 call.
# This is a subset of a file that was supplied in
# https://github.com/LinuxCNC/linuxcnc/issues/2844 and which worked up to and
Expand All @@ -13,7 +14,7 @@ pid=$!
count=5
while [ 0 -lt $count ] && kill -0 $pid > /dev/null 2>&1 ; do
sleep 1
count=$(($count - 1))
count=$((count - 1))
done

if kill -0 $pid > /dev/null 2>&1; then
Expand Down
2 changes: 1 addition & 1 deletion tests/interp/g72-missing-iteration/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pid=$!
count=10
while [ 0 -lt $count ] && kill -0 $pid > /dev/null 2>&1 ; do
sleep 1
count=$(($count - 1))
count=$((count - 1))
done

if kill -0 $pid > /dev/null 2>&1; then
Expand Down
2 changes: 1 addition & 1 deletion tests/interp/plug/absolute/test.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
rs274 -p ${LIBDIR}/linuxcnc/canterp.so -g canon | awk '{$1=""; print}'
rs274 -p "${LIBDIR}"/linuxcnc/canterp.so -g canon | awk '{$1=""; print}'
exit ${PIPESTATUS[0]}
7 changes: 6 additions & 1 deletion tests/linuxcncrsh/test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

set -e

rm -f gcode-output

if nc -z localhost 5007; then
Expand All @@ -18,7 +20,7 @@ while [ $TOGO -gt 0 ]; do
break
fi
sleep 0.25
TOGO=$(($TOGO - 1))
TOGO=$((TOGO - 1))
done
if [ $TOGO -eq 0 ]; then
echo connection to linuxcncrsh timed out
Expand Down Expand Up @@ -57,8 +59,10 @@ function testGet() {
testSet echo off
# ask linuxcncrsh to not read the next command until it's done running
# the current one
# shellcheck disable=SC1010
testSet wait_mode done
# test deprecation mode of set_wait -> wait_mode rename
# shellcheck disable=SC1010
testSet set_wait done

# check default global settings
Expand Down Expand Up @@ -126,6 +130,7 @@ function testGet() {
# test pause
testSet wait_mode received # otherwise pause will stall
testSet pause
# shellcheck disable=SC1010
testSet wait_mode done
testSet resume

Expand Down
6 changes: 3 additions & 3 deletions tests/motion-logger/basic/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
rm -f out.motion-logger*

for SRC in *.in; do
DEST=$(basename ${SRC} .in)
rm -f ${DEST}
egrep -v '(^ *$)|(^ *#)' ${SRC} > ${DEST}
DEST=$(basename "${SRC}" .in)
rm -f "${DEST}"
grep -E -v '(^ *$)|(^ *#)' "${SRC}" > "${DEST}"
done

linuxcnc -r test.ini
Expand Down
8 changes: 5 additions & 3 deletions tests/motion-logger/startup-gcode-abort/test.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/bin/bash

set -e

rm -f out.motion-logger*

for SRC in *.in; do
DEST=$(basename ${SRC} .in)
rm -f ${DEST}
egrep -v '(^ *$)|(^ *#)' ${SRC} > ${DEST}
DEST=$(basename "${SRC}" .in)
rm -f "${DEST}"
grep -E -v '(^ *$)|(^ *#)' "${SRC}" > "${DEST}"
done

linuxcnc -r test.ini
Expand Down
4 changes: 2 additions & 2 deletions tests/remap/remap-io/test.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash -e
export PYTHONUNBUFFERED=1
do_test() {
INI=$1
linuxcnc -r $INI | grep -i m6
INI="$1"
linuxcnc -r "$INI" | grep -i m6
}

echo "********** Testing python remaps"
Expand Down
6 changes: 3 additions & 3 deletions tests/t0/shared-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ fi
(
function introspect() {
SEQUENCE_NUMBER=$1
echo set wait done
echo 'set wait done'
echo "set mdi m100 P6 Q$SEQUENCE_NUMBER" # sequence number
echo 'set mdi m100 P0 Q#5420' # X
echo 'set mdi m100 P1 Q#5421' # Y
echo 'set mdi m100 P2 Q#5422' # Z
echo 'set mdi m100 P3 Q#5400' # toolno
echo 'set mdi m100 P4 Q#5403' # TLO z
echo 'set mdi m100 P5' # blank line
echo set wait done
echo 'set wait done'
}

echo hello EMC mt 1.0
Expand Down Expand Up @@ -660,7 +660,7 @@ fi


# give linuxcnc a second to finish
echo set wait done
echo 'set wait done'

echo shutdown
) | nc localhost 5007
Expand Down
5 changes: 4 additions & 1 deletion tests/trajectory-planner/circular-arcs/build-debug.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#/bin/bash
#!/bin/bash

set -e

cd ../../../src
#Ugly way to force rebuild of kinematics, which assumes that tp_debug isn't
#used anywhere else...
Expand Down
5 changes: 4 additions & 1 deletion tests/trajectory-planner/circular-arcs/build-profile.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#/bin/bash
#!/bin/bash

set -e

cd ../../../src
#Ugly way to force rebuild of kinematics, which assumes that tp_debug isn't
#used anywhere else...
Expand Down
5 changes: 4 additions & 1 deletion tests/trajectory-planner/circular-arcs/build-release.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#/bin/bash
#!/bin/bash

set -e

cd ../../../src
#Ugly way to force rebuild of kinematics, which assumes that tp_debug isn't
#used anywhere else...
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
ln -sf ../../../../configs/sim/core_sim.hal
ln -sf ../../../../configs/sim/axis_manualtoolchange.hal
ln -sf ../../../../configs/sim/sim_spindle_encoder.hal
#!/bin/bash

set -e

ln -sf ../../../../configs/sim/core_sim.hal .
ln -sf ../../../../configs/sim/axis_manualtoolchange.hal .
ln -sf ../../../../configs/sim/sim_spindle_encoder.hal .
5 changes: 3 additions & 2 deletions tests/trajectory-planner/circular-arcs/process_runlog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
#===============================================================================

set -o nounset # Treat unset variables as an error
set -e

if [ -a $1 ]
if [ -a "$1" ]
then
awk '/total movement/ {print $2,$6,$9}' $1 > movement.log
awk '/total movement/ {print $2,$6,$9}' "$1" > movement.log
fi
4 changes: 3 additions & 1 deletion tests/trajectory-planner/circular-arcs/profile-run.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

set -e

function say_done {
espeak "done" 2> /dev/null
}
Expand All @@ -15,7 +17,7 @@ operf rtapi_app > profile.log &
linuxcnc -r circular_arcs.ini &
LOCAL_LCNC_PID=$!
echo $LOCAL_LCNC_PID
(./machine_setup.py $1 && say_done) || say_failed
(./machine_setup.py "$1" && say_done) || say_failed
#fg
#End profiling
pkill -9 axis
Expand Down
Loading
Loading