Skip to content

Commit c3248d4

Browse files
committed
Fix most of the "dangerousTypeCast" cppcheck warnings by using C++ casts.
1 parent 335e200 commit c3248d4

18 files changed

Lines changed: 297 additions & 302 deletions

File tree

src/emc/task/emctaskmain.cc

Lines changed: 89 additions & 89 deletions
Large diffs are not rendered by default.

src/emc/tooldata/tooldata_mmap.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ typedef struct {
5858

5959
#define TOOL_MMAP_STRIDE sizeof(CANON_TOOL_TABLE)
6060
//---------------------------------------------------------------------
61-
#define HPTR() (tooldata_header_t*)( tool_mmap_base \
61+
#define HPTR() reinterpret_cast<tooldata_header_t*>( tool_mmap_base \
6262
+ TOOL_MMAP_HEADER_OFFSET)
6363

64-
#define TPTR(idx) (CANON_TOOL_TABLE*)( tool_mmap_base \
64+
#define TPTR(idx) reinterpret_cast<CANON_TOOL_TABLE*>( tool_mmap_base \
6565
+ TOOL_MMAP_HEADER_OFFSET \
6666
+ TOOL_MMAP_HEADER_SIZE \
6767
+ idx * TOOL_MMAP_STRIDE)

src/emc/usr_intf/halui.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ static int emcTaskNmlGet()
298298
emcStatus = 0;
299299
retval = -1;
300300
} else {
301-
emcStatus = (EMC_STAT *) emcStatusBuffer->get_address();
301+
emcStatus = reinterpret_cast<EMC_STAT *>(emcStatusBuffer->get_address());
302302
}
303303
}
304304

src/hal/hal_priv.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,15 @@ RTAPI_END_DECLS
140140
#ifdef __cplusplus
141141
template<class T>
142142
bool hal_shmchk(T *t) {
143-
char *c = (char*)t;
143+
char *c = reinterpret_cast<char*>(t);
144144
return c > hal_shmem_base && c < hal_shmem_base + HAL_SIZE;
145145
}
146146

147147
template<class T>
148-
int hal_shmoff(T *t) { return t ? (char*)t - hal_shmem_base : 0; }
148+
int hal_shmoff(T *t) { return t ? reinterpret_cast<char*>(t) - hal_shmem_base : 0; }
149149

150150
template<class T>
151-
T *hal_shmptr(int p) { return p ? (T*)(hal_shmem_base + p) : nullptr; }
151+
T *hal_shmptr(int p) { return p ? reinterpret_cast<T*>(hal_shmem_base + p) : nullptr; }
152152

153153
template<class T>
154154
class hal_shmfield {

src/hal/halmodule.cc

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ static int pyhal_init(PyObject *_self, PyObject *args, PyObject *kw) {
234234
(void)kw;
235235
char *name;
236236
char *prefix = 0;
237-
halobject *self = (halobject *)_self;
237+
halobject *self = reinterpret_cast<halobject *>(_self);
238238

239239
if(!PyArg_ParseTuple(args, "s|s:hal.component", &name, &prefix)) return -1;
240240

@@ -276,7 +276,7 @@ static void pyhal_exit_impl(halobject *self) {
276276
}
277277

278278
static void pyhal_delete(PyObject *_self) {
279-
halobject *self = (halobject *)_self;
279+
halobject *self = reinterpret_cast<halobject *>(_self);
280280
pyhal_exit_impl(self);
281281
Py_TYPE(self)->tp_free(self);
282282
}
@@ -477,7 +477,7 @@ static PyObject * pyhal_create_pin(halobject *self, char *name, hal_type_t type,
477477
static PyObject *pyhal_new_param(PyObject *_self, PyObject *o) {
478478
char *name;
479479
int type, dir;
480-
halobject *self = (halobject *)_self;
480+
halobject *self = reinterpret_cast<halobject *>(_self);
481481

482482
if(!PyArg_ParseTuple(o, "sii", &name, &type, &dir))
483483
return NULL;
@@ -494,7 +494,7 @@ static PyObject *pyhal_new_param(PyObject *_self, PyObject *o) {
494494
static PyObject *pyhal_new_pin(PyObject *_self, PyObject *o) {
495495
char *name;
496496
int type, dir;
497-
halobject *self = (halobject *)_self;
497+
halobject *self = reinterpret_cast<halobject *>(_self);
498498

499499
if(!PyArg_ParseTuple(o, "sii", &name, &type, &dir))
500500
return NULL;
@@ -509,7 +509,7 @@ static PyObject *pyhal_new_pin(PyObject *_self, PyObject *o) {
509509

510510
static PyObject *pyhal_get_pin(PyObject *_self, PyObject *o) {
511511
char *name;
512-
halobject *self = (halobject *)_self;
512+
halobject *self = reinterpret_cast<halobject *>(_self);
513513

514514
if(!PyArg_ParseTuple(o, "s", &name))
515515
return NULL;
@@ -524,7 +524,7 @@ static PyObject *pyhal_get_pin(PyObject *_self, PyObject *o) {
524524

525525
static PyObject *pyhal_get_pins(PyObject *_self, PyObject * /*o*/) {
526526
char *name;
527-
halobject *self = (halobject *)_self;
527+
halobject *self = reinterpret_cast<halobject *>(_self);
528528

529529
EXCEPTION_IF_NOT_LIVE(NULL);
530530

@@ -540,7 +540,7 @@ static PyObject *pyhal_get_pins(PyObject *_self, PyObject * /*o*/) {
540540

541541
static PyObject *pyhal_ready(PyObject *_self, PyObject * /*o*/) {
542542
// hal_ready did not exist in EMC 2.0.x, make it a no-op
543-
halobject *self = (halobject *)_self;
543+
halobject *self = reinterpret_cast<halobject *>(_self);
544544
EXCEPTION_IF_NOT_LIVE(NULL);
545545
int res = hal_ready(self->hal_id);
546546
if(res) return pyhal_error(res);
@@ -549,51 +549,51 @@ static PyObject *pyhal_ready(PyObject *_self, PyObject * /*o*/) {
549549

550550
static PyObject *pyhal_unready(PyObject *_self, PyObject * /*o*/) {
551551
// hal_ready did not exist in EMC 2.0.x, make it a no-op
552-
halobject *self = (halobject *)_self;
552+
halobject *self = reinterpret_cast<halobject *>(_self);
553553
EXCEPTION_IF_NOT_LIVE(NULL);
554554
int res = hal_unready(self->hal_id);
555555
if(res) return pyhal_error(res);
556556
return Py_None;
557557
}
558558

559559
static PyObject *pyhal_exit(PyObject *_self, PyObject * /*o*/) {
560-
halobject *self = (halobject *)_self;
560+
halobject *self = reinterpret_cast<halobject *>(_self);
561561
pyhal_exit_impl(self);
562562
return Py_None;
563563
}
564564

565565
static PyObject *pyhal_repr(PyObject *_self) {
566-
halobject *self = (halobject *)_self;
566+
halobject *self = reinterpret_cast<halobject *>(_self);
567567
return PyUnicode_FromFormat("<hal component %s(%d) with %d pins and params>",
568568
self->name, self->hal_id, (int)self->items->size());
569569
}
570570

571571
static PyObject *pyhal_getattro(PyObject *_self, PyObject *attro) {
572572
PyObject *result;
573-
halobject *self = (halobject *)_self;
573+
halobject *self = reinterpret_cast<halobject *>(_self);
574574
EXCEPTION_IF_NOT_LIVE(NULL);
575575

576-
result = PyObject_GenericGetAttr((PyObject*)self, attro);
576+
result = PyObject_GenericGetAttr(reinterpret_cast<PyObject*>(self), attro);
577577
if(result) return result;
578578

579579
PyErr_Clear();
580580
return pyhal_read_common(find_item(self, PyUnicode_AsUTF8(attro)));
581581
}
582582

583583
static int pyhal_setattro(PyObject *_self, PyObject *attro, PyObject *v) {
584-
halobject *self = (halobject *)_self;
584+
halobject *self = reinterpret_cast<halobject *>(_self);
585585
EXCEPTION_IF_NOT_LIVE(-1);
586586
return pyhal_write_common(find_item(self, PyUnicode_AsUTF8(attro)), v);
587587
}
588588

589589
static Py_ssize_t pyhal_len(PyObject *_self) {
590-
halobject* self = (halobject*)_self;
590+
halobject* self = reinterpret_cast<halobject*>(_self);
591591
EXCEPTION_IF_NOT_LIVE(-1);
592592
return self->items->size();
593593
}
594594

595595
static PyObject *pyhal_get_prefix(PyObject *_self, PyObject *args) {
596-
halobject* self = (halobject*)_self;
596+
halobject* self = reinterpret_cast<halobject*>(_self);
597597
if(!PyArg_ParseTuple(args, "")) return NULL;
598598
EXCEPTION_IF_NOT_LIVE(NULL);
599599

@@ -606,7 +606,7 @@ static PyObject *pyhal_get_prefix(PyObject *_self, PyObject *args) {
606606

607607
static PyObject *pyhal_set_prefix(PyObject *_self, PyObject *args) {
608608
char *newprefix;
609-
halobject* self = (halobject*)_self;
609+
halobject* self = reinterpret_cast<halobject*>(_self);
610610
if(!PyArg_ParseTuple(args, "s", &newprefix)) return NULL;
611611
EXCEPTION_IF_NOT_LIVE(NULL);
612612

@@ -741,7 +741,7 @@ static const char * param_dir2name(hal_param_dir_t type) {
741741
}
742742

743743
static PyObject *pyhalpin_repr(PyObject *_self) {
744-
pyhalitem *pyself = (pyhalitem *) _self;
744+
pyhalitem *pyself = reinterpret_cast<pyhalitem *>(_self);
745745
halitem *self = &pyself->pin;
746746

747747
const char * name = "(null)";
@@ -761,45 +761,45 @@ static int pyhalpin_init(PyObject * /*_self*/, PyObject *, PyObject *) {
761761
}
762762

763763
static void pyhalpin_delete(PyObject *_self) {
764-
pyhalitem *self = (pyhalitem *)_self;
764+
pyhalitem *self = reinterpret_cast<pyhalitem *>(_self);
765765

766766
if(self->name) free(self->name);
767767

768768
PyObject_Del(self);
769769
}
770770

771771
static PyObject * pyhal_pin_set(PyObject * _self, PyObject * value) {
772-
pyhalitem * self = (pyhalitem *) _self;
772+
pyhalitem * self = reinterpret_cast<pyhalitem *>(_self);
773773
if (pyhal_write_common(&self->pin, value) == -1)
774774
return NULL;
775775
return Py_None;
776776
}
777777

778778
static PyObject * pyhal_pin_get(PyObject * _self, PyObject *) {
779-
pyhalitem * self = (pyhalitem *) _self;
779+
pyhalitem * self = reinterpret_cast<pyhalitem *>(_self);
780780
return pyhal_read_common(&self->pin);
781781
}
782782

783783
static PyObject * pyhal_pin_get_type(PyObject * _self, PyObject *) {
784-
pyhalitem * self = (pyhalitem *) _self;
784+
pyhalitem * self = reinterpret_cast<pyhalitem *>(_self);
785785
return PyLong_FromLong(self->pin.type);
786786
}
787787

788788
static PyObject * pyhal_pin_get_dir(PyObject * _self, PyObject *) {
789-
pyhalitem * self = (pyhalitem *) _self;
789+
pyhalitem * self = reinterpret_cast<pyhalitem *>(_self);
790790
if (self->pin.is_pin)
791791
return PyLong_FromLong(self->pin.dir.pindir);
792792
else
793793
return PyLong_FromLong(self->pin.dir.paramdir);
794794
}
795795

796796
static PyObject * pyhal_pin_is_pin(PyObject * _self, PyObject *) {
797-
pyhalitem * self = (pyhalitem *) _self;
797+
pyhalitem * self = reinterpret_cast<pyhalitem *>(_self);
798798
return PyBool_FromLong(self->pin.is_pin);
799799
}
800800

801801
static PyObject * pyhal_pin_get_name(PyObject * _self, PyObject *) {
802-
pyhalitem * self = (pyhalitem *) _self;
802+
pyhalitem * self = reinterpret_cast<pyhalitem *>(_self);
803803
if (!self->name)
804804
return Py_None;
805805
return PyUnicode_FromString(self->name);
@@ -886,7 +886,7 @@ static PyObject * pyhal_pin_new(halitem * pin, const char * name) {
886886
else
887887
pypin->name = NULL;
888888

889-
return (PyObject *) pypin;
889+
return reinterpret_cast<PyObject *>(pypin);
890890
}
891891

892892
PyObject *pin_has_writer(PyObject * /*self*/, PyObject *args) {
@@ -1536,7 +1536,7 @@ struct shmobject {
15361536
};
15371537

15381538
static int pyshm_init(PyObject *_self, PyObject *args, PyObject * /*kw*/) {
1539-
shmobject *self = (shmobject *)_self;
1539+
shmobject *self = reinterpret_cast<shmobject *>(_self);
15401540
self->comp = 0;
15411541
self->shm_id = -1;
15421542

@@ -1559,7 +1559,7 @@ static int pyshm_init(PyObject *_self, PyObject *args, PyObject * /*kw*/) {
15591559
}
15601560

15611561
static void pyshm_delete(PyObject *_self) {
1562-
shmobject *self = (shmobject *)_self;
1562+
shmobject *self = reinterpret_cast<shmobject *>(_self);
15631563
if(self->comp && self->shm_id > 0)
15641564
rtapi_shmem_delete(self->shm_id, self->comp->hal_id);
15651565
Py_XDECREF(self->comp);
@@ -1570,8 +1570,8 @@ static int shm_buffer_getbuffer(PyObject *obj, Py_buffer *view, int /*flags*/) {
15701570
PyErr_SetString(PyExc_ValueError, "NULL view in getbuffer");
15711571
return -1;
15721572
}
1573-
shmobject* self = (shmobject *)obj;
1574-
view->obj = (PyObject*)self;
1573+
shmobject* self = reinterpret_cast<shmobject *>(obj);
1574+
view->obj = reinterpret_cast<PyObject*>(self);
15751575
view->buf = (void*)self->buf;
15761576
view->len = self->size;
15771577
view->readonly = 0;
@@ -1580,22 +1580,22 @@ static int shm_buffer_getbuffer(PyObject *obj, Py_buffer *view, int /*flags*/) {
15801580
}
15811581

15821582
static PyObject *pyshm_repr(PyObject *_self) {
1583-
shmobject *self = (shmobject *)_self;
1583+
shmobject *self = reinterpret_cast<shmobject *>(_self);
15841584
return PyUnicode_FromFormat("<shared memory buffer key=%08x id=%d size=%ld>",
15851585
self->key, self->shm_id, (unsigned long)self->size);
15861586
}
15871587

15881588
static PyObject *shm_setsize(PyObject *_self, PyObject *args) {
1589-
shmobject *self = (shmobject *)_self;
1589+
shmobject *self = reinterpret_cast<shmobject *>(_self);
15901590
if(!PyArg_ParseTuple(args, "k", &self->size)) return NULL;
15911591
return Py_None;
15921592
}
15931593

15941594

15951595
static PyObject *shm_getbuffer(PyObject *_self, PyObject * /*dummy*/) {
15961596

1597-
shmobject *self = (shmobject *)_self;
1598-
return (PyObject*)PyMemoryView_FromObject((PyObject*)self);
1597+
shmobject *self = reinterpret_cast<shmobject *>(_self);
1598+
return (PyObject*)PyMemoryView_FromObject(reinterpret_cast<PyObject*>(self));
15991599
}
16001600

16011601
static PyObject *set_msg_level(PyObject * /*_self*/, PyObject *args) {
@@ -1698,7 +1698,7 @@ static int pystream_init(PyObject *_self, PyObject *args, PyObject * /*kw*/) {
16981698
int depth=0;
16991699
char *typestring=NULL;
17001700

1701-
streamobj *self = (streamobj *)_self;
1701+
streamobj *self = reinterpret_cast<streamobj *>(_self);
17021702
self->sampleno = 0;
17031703

17041704
// creating a new stream
@@ -1751,7 +1751,7 @@ static int pystream_init(PyObject *_self, PyObject *args, PyObject * /*kw*/) {
17511751
}
17521752

17531753
PyObject *stream_read(PyObject *_self, PyObject * /*unused*/) {
1754-
streamobj *self = (streamobj *)_self;
1754+
streamobj *self = reinterpret_cast<streamobj *>(_self);
17551755
int n = PyBytes_Size(self->pyelt);
17561756
if(n <= 0)
17571757
return Py_None;
@@ -1781,7 +1781,7 @@ PyObject *stream_read(PyObject *_self, PyObject * /*unused*/) {
17811781
}
17821782

17831783
PyObject *stream_write(PyObject *_self, PyObject *args) {
1784-
streamobj *self = (streamobj *)_self;
1784+
streamobj *self = reinterpret_cast<streamobj *>(_self);
17851785
PyObject *data;
17861786
if(!PyArg_ParseTuple(args, "O!:hal.stream.write", &PyTuple_Type, &data))
17871787
return NULL;

src/hal/user_comps/xhc-hb04.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ int xhc_encode_float(float v, unsigned char *buf)
191191
unsigned short int_part = int_v / 10000;
192192
unsigned short fract_part = int_v % 10000;
193193
if (v < 0) fract_part = fract_part | 0x8000;
194-
*(short *)buf = int_part;
195-
*((short *)buf+1) = fract_part;
194+
*reinterpret_cast<short *>(buf) = int_part;
195+
*(reinterpret_cast<short *>(buf)+1) = fract_part;
196196
return 4;
197197
}
198198

199199
int xhc_encode_s16(int v, unsigned char *buf)
200200
{
201-
*(short *)buf = v;
201+
*reinterpret_cast<short *>(buf) = v;
202202
return 2;
203203
}
204204

src/hal/user_comps/xhc-whb04b-6/xhc-whb04b6.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,9 @@ void XhcWhb04b6Component::printInputData(const UsbInPackage& inPackage, std::ost
384384
out << " | ";
385385
printPushButtonText(inPackage.buttonKeyCode2, inPackage.buttonKeyCode1, out);
386386
out << " | ";
387-
printRotaryButtonText((KeyCode*)&mKeyCodes.Feed, inPackage.rotaryButtonFeedKeyCode, out);
387+
printRotaryButtonText(reinterpret_cast<const KeyCode*>(&mKeyCodes.Feed), inPackage.rotaryButtonFeedKeyCode, out);
388388
out << " | ";
389-
printRotaryButtonText((KeyCode*)&mKeyCodes.Axis, inPackage.rotaryButtonAxisKeyCode, out);
389+
printRotaryButtonText(reinterpret_cast<const KeyCode*>(&mKeyCodes.Axis), inPackage.rotaryButtonAxisKeyCode, out);
390390
out << " | " << std::setfill(' ') << std::setw(3) << static_cast<short>(inPackage.stepCount) << " | " << std::hex
391391
<< std::setfill('0')
392392
<< std::setw(2) << static_cast<unsigned short>(inPackage.crc);

src/hal/utils/halcmd_commands.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,7 +2039,7 @@ static void print_thread_info(char **patterns)
20392039
n = 1;
20402040
while (list_entry != list_root) {
20412041
/* print the function info */
2042-
fentry = (hal_funct_entry_t *) list_entry;
2042+
fentry = reinterpret_cast<hal_funct_entry_t *>(list_entry);
20432043
funct = SHMPTR(fentry->funct_ptr);
20442044
/* scriptmode only uses one line per thread, which contains:
20452045
thread period, FP flag, name, then all functs separated by spaces */
@@ -2842,7 +2842,7 @@ static void save_threads(FILE *dst)
28422842
list_entry = list_next(list_root);
28432843
while (list_entry != list_root) {
28442844
/* print the function info */
2845-
fentry = (hal_funct_entry_t *) list_entry;
2845+
fentry = reinterpret_cast<hal_funct_entry_t *>(list_entry);
28462846
funct = SHMPTR(fentry->funct_ptr);
28472847
fprintf(dst, "addf %s %s\n", funct->name, tptr->name);
28482848
list_entry = list_next(list_entry);

src/libnml/buffer/physmem.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ int PHYSMEM_HANDLE::read(void *_to, long _read_size)
103103
char *from;
104104
from = ((char *) local_address) + offset;
105105
if (_read_size == 2) {
106-
short *sfrom = (short *) from;
106+
short *sfrom = reinterpret_cast<short *>(from);
107107
short sval;
108108
sval = *sfrom;
109109
short *sto = (short *) _to;
@@ -153,7 +153,7 @@ int PHYSMEM_HANDLE::write(void *_from, long _write_size)
153153
char *to;
154154
to = ((char *) local_address) + offset;
155155
if (_write_size == 2) {
156-
short *sto = (short *) to;
156+
short *sto = reinterpret_cast<short *>(to);
157157
short sval = *(short *) _from;
158158
*sto = sval;
159159
} else {

0 commit comments

Comments
 (0)