【问题标题】:Problem with strong type-checking in lint (warning 632)lint 中的强类型检查问题(警告 632)
【发布时间】:2011-10-28 04:35:13
【问题描述】:

我正在努力完善一些现有的 C 代码,以便将其移植到新的编译器(嵌入式软件,我们正在切换硬件)。所以我试图用 lint 清理当前代码,但我被 lint 认定为强类型违规的分配难住了。

我得到的错误:

--- Module:   GenericFileName.c 
GenericFileName.c  ...  Warning 632: Assignment to strong type
(SubStructureType_T) in context: assignment

它所引用的代码行(为便于阅读而更改了名称):

void foo(void)
{
    extern const StructureType_T parent;    
    const SubStructureType_T *localChild;

    localChild = parent.child;   //<-- lint complains about this assignment
    ...
}

StructureType_T的相关部分:

typedef struct
{   
    const struct SubStructureType_T *child;
    ...
}StructureType_T;

最后,启用强类型检查的 lint 选项:

-strong(AcXJcb)

任何见解将不胜感激。我已经四处寻找帮助,但没有找到太多。我猜 lint 是一个相当古老的工具。感谢阅读!

【问题讨论】:

  • parent 实际上(全局)在哪里以及如何定义您已要求外部链接的?
  • 糟糕,非常好的问题。我应该把它包括在内。它看起来像这样:'const StructureType_T parent = { 0,0,0,...,0}'。它存在于另一个模块中。
  • const StructureType_T parent = { 0,0,0,...,0};
  • 这没什么用。既然您说它是一个旧工具,请尝试在指针的初始化列表中设置为 NULL 而不是 0
  • 这有关系吗?我在#pragma tsection .parent 声明之后得到了定义。由于应用程序的重要性质,我们使用这种类型的东西来非常谨慎地确定某些地址的存储位置。

标签: c lint strong-typing


【解决方案1】:

const SubStructureType_T,如foo,还是const struct SubStructureType_T,如typedef?请注意,关键字“struct”只出现在第二个定义中。

它们是一样的吗?

【讨论】:

  • 哦,当然。我需要在*localChild 的声明中包含struct。我现在觉得很傻。谢谢!
  • +1,我敢打赌就是这样。详情见问题Difference between struct and typedef struct
  • @Adam @pmg 绝对。因为我需要使用前向声明,所以 SubStructureType_T 的定义如下:typedef struct SubStructure_T{ ... } SubStructure_T;。我不确定如果没有struct,任何编译器都会错误地尝试使localChild 变得奇怪,但是这个修复让lint 很高兴。再次感谢。
  • typedef 远离代码并在每次需要时输入额外的struct 有什么害处?不要回答:修辞问题:)
猜你喜欢
  • 2011-08-07
  • 2018-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 2010-11-11
  • 1970-01-01
  • 2016-12-24
相关资源
最近更新 更多