With These Flags You Dont Even Need Unit Testing

🚀 The chance of your software having some kind of problem is almost zero.


With These Flags You Dont Even Need Unit Testing


We have already published once about flags for GCC/Clang that talks about the fundamental flags for building software, which are:

g -Wpedantic -Wall -Werror -Wextra -fsanitize=address

However, although they already help a lot, you can still improve your code with Static Analyzers and also flags to check conversions and other possible bugs.

An ideal compiler for this is clang++, as it displays more warnings than g++.

What are they:

-g -O0 # Debug info + no optimizations (more readable)
-fno-omit-frame-pointer # Better for stack traces and debuggers

Useful warnings:

-Wconversion # Converts dangerous implicits (int → float, etc)
-Wsign-conversion # Converts between signed/unsigned
-Wnull-dereference # Detects use of null pointers
-Wdouble-promotion # Detects implicit promotions to double
-Wformat=2 # Checks printf/scanf more strictly
-Wcast-align # Warns about casts with possible alignment problems

In addition to:

Complementary sanitizers:

-fsanitize=undefined # UB: overflows, null deref, invalid shift, etc
-fsanitize=leak # Detects memory leaks

Also -Wshadow and -Wfloat-conversion.

Note: Use -O1 if you want a little optimization without losing too much clarity in the debug.


I used them together to debug Terlang and after compiling with these flags I got several Warnings that were transformed into errors, below the image at the time of compilation:

alt text

I used the debug.ter file and resolved all these errors/Warnings according to this commit, but in this commit there are also other modifications and additional implementations.

In short, I created a debug.ter file with all the flags, e.g.:

/*
 TER/TERLANG
 By: Marcos Oliveira
*/

//Defs
auto compiler = "clang++ -stdlib=libc++ -O0 -fno-omit-frame-pointer -Wfloat-conversion"
auto add_flags = "-Wsign-conversion -Wnull-dereference -Wdouble-promotion -Wformat=2 -Wcast-align -Wconversion"
auto sanitize = " -fsanitize=address -fsanitize=undefined -fsanitize=leak"
auto flags = "-g -Wall -Werror -Wextra -Wshadow -Wpedantic"
auto build = compiler + " " + add_flags + " " + sanitize + " " + " " + flags + " ./src/*.cpp"

// Display full command
output(build)
exec(build)

So, when you are in development mode I recommend compiling with all these flags, however, when you are in release mode it is best to remove all of them, as they compromise performance and significantly increase the size of the binary.


cpp clanguage terlang compilers gcc clang


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles