Can I make GCC warn on passing too-wide types to functions?

后端 未结 2 1605
南方客
南方客 2020-11-30 12:59

Following is some obviously-defective code for which I think the compiler should emit a diagnostic. But neither gcc nor g++ does, even with all th

相关标签:
2条回答
  • 2020-11-30 13:06

    Use -Wconversion -- the problem is an implicit cast (conversion) from long x to short when the function f(short x) is called [not printf], and -Wconversion will say something like "cast from long to short may alter value".

    ..

    Edit: just saw your note. -Wconversion results in a warning for me, using g++ 4.3.2 on Linux... (4.3.2-1 on Debian)

    0 讨论(0)
  • 2020-11-30 13:13

    I would strongly suggest investing in PC-lint/FlexeLint from Gimpel. The software is made for catching things like this that the compiler just isn't. It is relatively inexpensive and well worth the price. There is an online demo on the site that you can use to evaluate it, here is what it reports for the line in question from your example:

    diy.cpp  14  Info 734: Loss of precision (arg. no. 1) (31 bits to 15 bits)
    
    0 讨论(0)
提交回复
热议问题