Author Topic: Using compilers as static checkers  (Read 2765 times)

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Using compilers as static checkers
« on: March 27, 2019, 04:33:40 PM »
So I came across the "-fsyntax-only" option supported by GCC/Clang/Mingw. With this flag set, the compiler will process the source code, output any warnings, errors, or diagnostic messages, but won't attempt to create any output files. It's a nice quick way to scan source code for errors, without the compiler littering your directories with object files.

It's also faster than a full compile too. And less disk access or churning if you still use an old mechanical monster.

As the open source compilers tend to be a little more strict about error checking than MSVC, that might be a good way to scan a project for errors, or deviations from standard C++, even if the project is Windows only.

I ran Mingw on some Outpost 2 level code, and it came back with some warnings about passing string literals to a char* parameter. Technically, string literals are read-only, so they should only be accepted by const char* parameters.

Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 2352
  • OPHD Lead Developer
    • LairWorks Entertainment
Re: Using compilers as static checkers
« Reply #1 on: March 28, 2019, 06:41:41 PM »
On the flip side, if you're on Windows and using Visual Studio, a really great static analyzer that integrates really well with Visual Studio is PVS-Studio. It's only caveat is that it is in fact a paid product but they do offer it for free use for open-source projects. Granted you don't have to open-source your project, you just have to tag your source files with a specific comment header -- it's a little dishonest to do it that way if you're not a truly open-source project but ethics aside, it's helped me find a lot of weird potential issues in my own code and its analyzer engine is backed by enterprise development. So there's that. :D