Rokiのチラ裏

学生による学習のログ

GCCインデントミスの警告機能

$ cat test.cpp
int main()
{
    auto f=[]{};
    auto b=[]{};
    if(true)
        f();
        b();
}
$ g++ -std=c++0x -Wmisleading-indentation test.cpp
test.cpp: 関数 ‘int main()’ 内:
test.cpp:5:2: 警告: thisif’ clause does not guard... [-Wmisleading-indentation]
  if(true)
  ^~
test.cpp:7:3: 備考: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘if’
   b();
   ^

...とまあ、-Wmisleading-indentationオプションを指定すると、インデントのミスを警告してくれる。


以下、余談。(読み飛ばし推奨)

Finding Bugs In The First C++ Compiler - What does Bjarne Think!というC++30周年記念の企画記事を見ていて、偉大なるBjarne氏のリプを拝見していたら、

The if-then-else bug (or not) is odd. I read the source, it's not just misformatted, it's wrong, but curiously, that doesn't matter: the difference is just a slight difference in the error message used before terminating. No wonder I did not spot it.

とあったので、今のGCCとかにはインデントミスの警告とかないものなのかなと思っていてコミットログを見たら、、、あった、ブログにメモろう。
...という筋書きでこの雑なエントリは書かれた。

余談の、さらに余談となるが、上記のリンクで

The paranoia test is in the compiler's main loop. My thought was that if anything when wrong with the software or hardware, one of those tests were likely to fail. At least once, it caught the effect of a bug in the code generator used to build Cfront. I think all significant programs should have a "paranoia test" against "impossible" errors.

とあるように、大規模なプログラムから不可能なエラーを発見するためのパラノイアテストの重要さを学んだ。