Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Being extremely pedantic with the way your code is compiled

I would like to find out which is the most extreme error checking flag combination for g++ (4.7). We are not using the new C++11 specification, since we need to cross compile the code with older compilers, and these older compilers (mostly g++ 4.0) often cause problems which simply are ignored by the g++4.7.

Right now we use the following set of flags:

-Wall -Wcomment -Wformat -Winit-self -ansi -pedantic-errors \
-Wno-long-long -Wmissing-include-dirs -Werror -Wextra

but this combination does not identify issues such as a double being passed in to a function which expects int, or comparison between signed and unsigned int and this causes the old compiler to choke on it.

I have read through the documentation and -Wsign-compare should be enabled by -Wextra but in practice seems this is not the case, so I might have missed something...

like image 536
Ferenc Deak Avatar asked Jan 29 '13 08:01

Ferenc Deak


People also ask

What are pedantic errors?

Generate errors if code violates strict ISO C and ISO C++. If you use the -pedantic-errors option, the compiler does not use any language feature that conflicts with strict ISO C or ISO C++. The compiler generates an error if your code violates strict ISO language standard.

What does pedantic mean in GCC?

The -pedantic option tells GCC to issue warnings in such cases; -pedantic-errors says to make them errors instead. This does not mean that all non-ISO constructs get warnings or errors. See Options to Request or Suppress Warnings, for more detail on these and related command-line options.

What is a compile warning?

Compiler warnings are messages produced by a compiler regarding program code fragments to be considered by the developer, as they may contain errors. Unlike compilation errors, warnings don't interrupt the compilation process.

What is the significance of the wall parameter in a C++ compilation instruction?

It's short for "warn all" -- it turns on (almost) all the warnings that g++ can tell you about.


1 Answers

The -ansi is alias for the default standard without GNU extensions. I'd suggest instead being explicit using -std=c++98, but it should be default for g++ -ansi, so not really different.

But generally I've never seen anything that would be accepted by newer gcc and rejected by older gcc on the grounds of being invalid. I suspect any such problem is a bug in the older compiler or it's standard library. Gcc does not have warnings for things that are correct, but didn't work with older versions of it, so you don't have any other option than to test with the older version.

As for the specific issues you mention:

  • Passing double to function that expects int is not an error. It might be undefined behaviour though. -Wconversion should help.
  • Comparing signed with unsigned is also well defined, also always worked as defined and in case of equality comparisons actually makes programmers write worse code (comparing unsigned variable larger than int with -1 is something else than comparing it with -1u). So I actually always compile with -Wno-sign-compare.

The compiler should not print warnings for headers found in directories given with -isystem instead of -I, so that should let you silence the warning for Qt headers and keep it enabled for your own code. So you should be able to use -Wconversion.

like image 182
Jan Hudec Avatar answered Oct 01 '22 12:10

Jan Hudec