How to hint to GCC that a line should be unreachable at compile time?

前端 未结 4 451
渐次进展
渐次进展 2021-01-04 03:20

It\'s common for compilers to provide a switch to warn when code is unreachable. I\'ve also seen macros for some libraries, that provide assertions for unreachable code.

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-04 04:10

    If your compiler does not have the warning that you need, it can be complemented with a static analyzer. The kind of analyzer I am talking about would have its own annotation language and/or recognize C assert, and use these for hints of properties that should be true at specific points of the execution. If there isn't a specific annotation for unreachable statements, you could probably use assert (false);.

    I am not personally familiar with them but Klokwork and CodeSonar are two famous analyzers. Goanna is a third one.

提交回复
热议问题