开源改变世界

cppcheck:struct iocontrol_str v1 和 v2 在源代码树中同名 #1593

推推 grbl 3年前 (2023-01-30) 339次浏览
打开
smoe 打开了这个问题 2022 年 2 月 11 日 · 0 条评论
打开

cppcheck:struct iocontrol_str v1 和 v2 在源代码树中同名#1593

smoe 打开了这个问题 2022 年 2 月 11 日 · 0 条评论

注释

cppcheck:struct iocontrol_str v1 和 v2 在源代码树中同名 #1593
贡献者

Cppcheck 在 emc/iotask 中发出了一些声音,我倾向于同意这里可以改进一些东西。

$ cppcheck iotask/*.cc
Checking iotask/ioControl.cc ...
Checking iotask/ioControl.cc: TOOL_NML...
1/2 files checked 45% done
Checking iotask/ioControl_v2.cc ...
Checking iotask/ioControl_v2.cc: TOOL_NML...
2/2 files checked 100% done
iotask/ioControl.cc:90:1: error: The one definition rule is violated, different classes/structs have the same name 'iocontrol_str' [ctuOneDefinitionRuleViolation]
struct iocontrol_str {
^
iotask/ioControl_v2.cc:140:1: note: The one definition rule is violated, different classes/structs have the same name 'iocontrol_str'
struct iocontrol_str {
^
iotask/ioControl.cc:90:1: note: The one definition rule is violated, different classes/structs have the same name 'iocontrol_str'
struct iocontrol_str {

然后我查看了这些(大)结构定义,将它们导出到文件 a 和 b 中,经过一些格式化后发现 v2 的唯一区别确实只是之前的扩展:

>     // v2 protocol
>     hal_bit_t *emc_abort;
>     hal_bit_t *emc_abort_ack;
>     hal_s32_t *emc_reason;
>     hal_bit_t *toolchanger_fault;
>     hal_bit_t *toolchanger_fault_ack;
>     hal_s32_t *toolchanger_reason;
>     hal_bit_t *start_change;
>     hal_bit_t *start_change_ack;
>     hal_bit_t *toolchanger_faulted;
>     hal_bit_t *toolchanger_clear_fault;
>     hal_s32_t *state;

在 .cc 文件中定义,只有一个变量iocontrol_data定义为指向它的指针。该(全局)变量和结构名称都保持不变。将变量声明为静态变量对 cppcheck 没有帮助(但它应该有,对吧?)。

我承认这里没有最终的答案。我的直接想法是提出一个 iocontrol typedef,它是两个版本的联合,但我从来都不是联合的粉丝。此外,如果 iocontrols 流被协议化到一个文件中,那么在单独的包含文件中提供数据类型可能会很方便。

我的第一反应是重命名之前的定义。

diff --git a/src/emc/iotask/ioControl.cc b/src/emc/iotask/ioControl.cc
index f97d5f9243..8e0db70ded 100644
--- a/src/emc/iotask/ioControl.cc
+++ b/src/emc/iotask/ioControl.cc
@@ -87,7 +87,7 @@ static int      random_toolchanger  = 0;
 static tooldb_t io_db_mode          = DB_NOTUSED;
 static char     db_program[LINELEN] = {0};
 
-struct iocontrol_str {
+struct iocontrol_str_v1 {
     hal_bit_t *user_enable_out;        /* output, TRUE when EMC wants stop */
     hal_bit_t *emc_enable_in;        /* input, TRUE on any external stop */
     hal_bit_t *user_request_enable;        /* output, used to reset ENABLE latch */
@@ -295,7 +295,7 @@ static int iocontrol_hal_init(void)
     }
 
     /* STEP 2: allocate shared memory for iocontrol data */
-    iocontrol_data = (iocontrol_str *) hal_malloc(sizeof(iocontrol_str));
+    iocontrol_data = (iocontrol_str_v1 *) hal_malloc(sizeof(iocontrol_str_v1));
     if (iocontrol_data == 0) {
         rtapi_print_msg(RTAPI_MSG_ERR,
                         "IOCONTROL: ERROR: hal_malloc() failed\n");

但我可能会遗漏一些东西?!?

非常感谢
史蒂芬

免费注册 在 GitHub 上加入此对话。已有帐户? 登录评论
标签
还没有
项目

还没有

发展

没有分支机构或拉取请求

1名参加者
cppcheck:struct iocontrol_str v1 和 v2 在源代码树中同名 #1593

喜欢 (0)