Tooling: ghc, cabal and hlint against g++ and the sanitizers

The two languages do not only differ in what a compiler accepts; they differ in how many tools a programmer is expected to run before the code is trusted. A C++ program is checked by a compiler, a formatter, a static analyser, a linter and at least one sanitizer, and each of those is a separate program with its own flags. A Haskell program is checked by the compiler, which is the analyser, and by a linter that suggests simpler code.

This page runs both toolchains in the image and shows the real output of each. Two of the three C++ sanitizers work here and the third does not, which is itself worth knowing before a page promises it.

The versions in this image

One command per tool, so that every claim below has a version behind it:

g++ --version | head -1
ghc --version
cabal --version | head -1
hlint --version
ormolu --version
cmake --version | head -1
make --version | head -1

g++ (Alpine 15.2.0) 15.2.0
The Glorious Glasgow Haskell Compilation System, version 9.10.3
cabal-install version 3.16.0.0
HLint v3.10, (C) Neil Mitchell 2006-2025
ormolu 0.8.0.2
using ghc-lib-parser 9.12.2.20250421
cmake version 4.2.3
GNU Make 4.4.1

The C++ tools: format, analyse, lint

A formatter removes the argument about layout. clang-format --dry-run --Werror reports instead of writing, and the exit status is what a build script reads. The file below is one line of unstyled code:

#include <cstdio> int main(){ int a=1;int b=2; std::printf("%d\n",a+b); }

clang-format --dry-run --Werror ugly.cpp 2>&1 | head -6

ugly.cpp:2:11: error: code should be clang-formatted [-Wclang-format-violations]
int main(){
          ^
ugly.cpp:2:12: error: code should be clang-formatted [-Wclang-format-violations]
int main(){
           ^
status 0

The static analyser is clang-tidy, and it enables no checks until it is asked to. clang-analyzer-* is the group that simulates the program along its paths, and it finds the null dereference below without running anything:

#include <cstdio> int main() { int* value = nullptr; *value = 42; std::printf("%d\n", *value); }

clang-tidy --checks='clang-analyzer-*' null.cpp -- -std=c++23 2>&1 | head -14

1 warning generated.
/tmp/sanitizers/null.cpp:5:12: warning: Dereference of null pointer (loaded from variable 'value') [clang-analyzer-core.NullDereference]
    5 |     *value = 42;
      |      ~~~~~ ^
/tmp/sanitizers/null.cpp:4:5: note: 'value' initialized to a null pointer value
    4 |     int* value = nullptr;
      |     ^~~~~~~~~~
/tmp/sanitizers/null.cpp:5:12: note: Dereference of null pointer (loaded from variable 'value')
    5 |     *value = 42;
      |      ~~~~~ ^
status 0

Run without the analyser, the same program dies with SIGSEGV and status 139 in the first statement that writes through the pointer, which is what the report above describes in words before it happens.

cppcheck is the second opinion, a separate project with its own checks and no configuration file. On the out-of-bounds write of the sanitizer chapter it names the index and the line:

#include <cstdio> int main() { int* numbers = new int[3]; numbers[0] = 1; numbers[3] = 2; std::printf("%d\n", numbers[0] + numbers[3]); delete[] numbers; }

cppcheck --enable=all --quiet overflow.cpp 2>&1 | head -8

overflow.cpp:1:2: information: Include file: <cstdio> not found. Please note: Standard library headers do not need to be provided to get proper results. [missingIncludeSystem]
#include <cstdio>
 ^
overflow.cpp:6:12: error: Array 'numbers[3]' accessed at index 3, which is out of bounds. [arrayIndexOutOfBounds]
    numbers[3] = 2;
           ^
overflow.cpp:7:45: error: Array 'numbers[3]' accessed at index 3, which is out of bounds. [arrayIndexOutOfBounds]
    std::printf("%d\n", numbers[0] + numbers[3]);
status 0

Sanitizers: two work, one does not

A sanitizer is a compiler instrumentation pass: the program is built with extra checks and reports at run time what the language calls undefined. AddressSanitizer catches the memory errors, and it found the write at index 3 of an array of three:

g++ -std=c++23 -O0 -g -fsanitize=address overflow.cpp -o overflow-asan
./overflow-asan 2>&1 | head -8

=================================================================
==3279==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6c2d0b6a001c at pc 0x583a5dcca29b bp 0x7ffe48a51930 sp 0x7ffe48a51920
WRITE of size 4 at 0x6c2d0b6a001c thread T0
    #0 0x583a5dcca29a in main /tmp/sanitizers/overflow.cpp:6
    #1 0x700d0c196153 in libc_start_main_stage2 src/env/__libc_start_main.c:95

0x6c2d0b6a001c is located 0 bytes after 12-byte region [0x6c2d0b6a0010,0x6c2d0b6a001c)
allocated by thread T0 here:
status 0

The report names the kind of error, the line, and the allocation the address belongs to. The program would have printed a number and exited 0 without it, which is the point of the tool.

UndefinedBehaviorSanitizer catches arithmetic and conversion errors. Signed overflow is undefined in C++ and a sanitized build says so at the line:

#include <cstdio> int main() { const int limit = 2147483647; volatile int step = 1; const int sum = limit + step; std::printf("%d\n", sum); }

g++ -std=c++23 -O0 -g -fsanitize=undefined ubsan.cpp -o ubsan-u
./ubsan-u 2>&1 | head -5

ubsan.cpp:6:29: runtime error: signed integer overflow: 1 + 2147483647 cannot be represented in type 'int'
-2147483648
status 0

The program continues and prints the wrapped value, which is the difference from AddressSanitizer: UBSan reports and goes on unless -fno-sanitize-recover turns the report into a failure.

ThreadSanitizer is the one that is missing in practice. The compiler accepts the flag and produces a binary, and the binary cannot start in this container because the runtime needs a system call the sandbox does not allow:

#include <cstdio> #include <thread> long counter = 0; int main() { std::thread first([] { for (int i = 0; i < 1000; ++i) ++counter; }); std::thread second([] { for (int i = 0; i < 1000; ++i) ++counter; }); first.join(); second.join(); std::printf("%ld\n", counter); }

g++ -std=c++23 -O1 -g -fsanitize=thread -pthread race.cpp -o race-tsan
./race-tsan 2>&1 | head -4

ThreadSanitizer: CHECK failed: tsan_platform_linux.cpp:290 "((personality(old_personality | 0x0040000))) != ((-1))" (0xffffffffffffffff, 0xffffffffffffffff) (tid=3295)
status 0

So the race on this page is not diagnosed by a tool, and the concurrency page says so instead of showing a report it never got. valgrind is in the image and it does find a leak: on a program that allocates a thousand integers and never frees them, --leak-check=summary reports definitely lost: 4,000 bytes in 1 blocks.

The build tool C++ needs

One file is compiled by hand; a project needs a build description. CMake is the one in this image, and the description is four lines plus the program:

cmake_minimum_required(VERSION 3.20) project(hello CXX) set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_executable(hello hello.cpp)

#include <cstdio> int main() { std::puts("hello, world"); }

cmake -S . -B build 2>&1 | head -12

-- The CXX compiler identification is GNU 15.2.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (1.2s)
-- Generating done (0.0s)
-- Build files have been written to: /tmp/sanitizers/hello/build
status 0

The configure step writes a build folder for this compiler and this machine, and every later build reuses it. The build itself is one command and it names the steps it takes:

cmake --build build 2>&1 | head -8
./build/hello; echo "status $?"

[ 50%] Building CXX object CMakeFiles/hello.dir/hello.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello
hello, world
status 0

That two-step shape is the one C++ has had for decades: configure, then build, then run. A Haskell project has the same two steps with different names, and a single-file program needs neither.

The Haskell side

ghc is the compiler and, with -Wall, the analyser: it reports unused bindings, incomplete patterns, missing signatures and type defaults, which is most of what a C++ programmer runs three tools to find. cabal is the build tool and the package manager in one, so the two-step shape above is cabal build and cabal test, with the dependencies in the project file rather than in a system.

hlint is the linter and ormolu the formatter. The pair maps onto clang-tidy and clang-format, and the page on hlint and ormolu runs both with real output. What Haskell does not have is the sanitizer layer, because safe Haskell has no undefined behaviour to detect at run time: an out-of-bounds list index is an exception, head [] is an exception, and a signed overflow on Int is defined to wrap. The undefined behaviour that remains is in the FFI and in unsafePerformIO, and for that there is valgrind, the same as for C++.

What is not here

A page that runs tools should say which ones it could not run. This is the list for the two toolchains, as found in this image:

Tool C++ Haskell
compiler g++ 15.2.0 GHC 9.10.3
formatter clang-format 22.1.3 ormolu 0.8.0.2 (fourmolu is not here)
linter clang-tidy 22.1.3, cppcheck 2.21.0 hlint 3.10
build tool cmake 4.2.3 and make 4.4.1 (ninja is not here) cabal-install 3.16.0.0
memory checker AddressSanitizer, valgrind 3.25.1 +RTS -s, and valgrind through the FFI
race checker ThreadSanitizer, which does not start here none: STM makes the race unrepresentable in safe code
debugger gdb gdb with the same limits, and ghci with :break

The two absences that matter are ThreadSanitizer and the Haskell package ecosystem: there is no network here, so the Haskell side runs on the boot libraries alone, which the testing group covers with the real error a missing package produces.

The Haskell lesson on the compiler flags is GHC flags, and the last page in this set is When Haskell is the right choice, and when it is not.

Every command on this page was run in this container: g++ 15.2.0 (Alpine 15.2.0), GHC 9.10.3, cabal-install 3.16.0.0, hlint 3.10, ormolu 0.8.0.2, cmake 4.2.3 and make 4.4.1, on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image