cpp-best-practices / gui_starter_template

A template CMake project to get you started with C++ and tooling

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ensure standard-conformant builds

pcolaianni opened this issue · comments

Dear community, I would like to achieve the following:

  • if the code has any warnings generated by either -Wpedantic or /permissive-, the build should fail.

With clang++.exe I can use -pedantic-errors. And I can easily add that to CompilerWarnings.cmake.

Unfortunately, it's not been easy to achieve the same behaviour with clang-cl.exe and /permissive-: the warnings are generated, but the build does not fail with an error. And clang-cl.exe does not support -pedantic-errors.

This behavious is in contradiction with what cl.exe does.
If one adds /std:c++17 /permissive- to cl.exe's options in the command line, the build errors out.

The following code is not C++17 standard compliant, because f's parameter s is missing const:

#include <iostream>
#include <filesystem>

void f(char* s)
{
  std::cout << s;
}

int main(int argc, const char **argv)
{
  std::filesystem::path p;
  f("this should fail");
  return 0;
}

Clang++: https://godbolt.org/z/zs9KWM (failing with -pedantic-errors)
MSVC: https://godbolt.org/z/bq7hxj (failing with /permissive-)

I can't find clang-cl in Compiler Explorer. Maybe there's a flag I can pass to clang?

If one copy/pastes that code in a file and tries to compile it with clang-cl.exe and /permissive- /std:c++17, the build won't error out. It'll only report a warning.

A warning might be enough in very clean codebases. Or, better, codebases that have always had -Werror (or equivalent) enabled.

But it is not enough in other situations, for example in the presence of legacy code. In such a case, the "pedantic" warnings are just lost in the noise.

Am I doing something wrong with these tools, or are you experiencing the same behaviour?
How would you achieve the desired result?

Clang-cl is new to me, so please take anything I say with a big grain of salt.

I had a look at the the llvm docs for supported command-line options (https://clang.llvm.org/docs/UsersManual.html#id9), and it sounds to me like the options they support are a subset of the full MSVC command-line options list. The docs provide a full listing of the options supported, and /permissive- is not on it.

A direct quote:

"Please file a bug for any valid cl.exe flags that clang-cl does not understand."

How would you achieve the desired result?

I would do two things:

  1. File the bug report. The old adage "it's not the compiler, it's you" should not apply to clang-cl.exe, because it's a compatibility layer, not a compiler. Until the bug is fixed, clang-cl.exe will not do the job you want it to.
  2. Use multiple builds to make sure you're getting all the warnings you need. To get the /permissive- flag, I would use the standard Visual Studio cl.exe compiler until clang-cl.exe is patched. To my knowledge, cl.exe can warn for things that gcc and clang cannot: See Cpp Weekly #46. Additionally, I would compile regularly with clang and gcc as well: this is what the Travis-CI, Appveyor and Github-Actions builds are for.

Thanks David, that is very helpful information.

I filed the bug; hopefully it'll be easy to fix.

I like the idea of having builds for each supported platform. That is probably the best way to go, in any case.

As this issue is not found about the cpp_starter_project, I am closing it.

No problem, thank you for filing the bug with llvm!