Please remove multiple warnings from compiler about optimisation,
variable conversion, signed overflow and many more potential errors.

1. Optimisation solutions from GCC:
If you would like to add extra warning attributes to compile SQLite with
gcc (and many others modern compilers) you will see the warnings about
pure, const and other optimisation suggestion. All those optimizations
can add more performance to final product due to various assembly

Static build of the last SQLite amalgamation version 3.20.0 with gcc -03
optimisation which uses the following parameters:
 -Wsuggest-attribute=const -Wsuggest-attribute=pure
-Wsuggest-attribute=noreturn -Wsuggest-attribute=format

has  87 recommendations as for “pure” functions.

2. Error warnings:

With -Wall -Wextra -Wpedantic -Wshadow options we can see extra warnings
about potential errors as follows:

217 warnings “is not defined” like:

SQLite3.c:74:5: warning: "SQLITE_4_BYTE_ALIGNED_MALLOC" is not defined

20 warnings “cast discards __attribute__((noreturn))” like:

SQLite3.c:55734:10: warning: cast discards ‘__attribute__((noreturn))’
qualifier from pointer target type [-Wcast-qual]
   memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));

768 warnings “may change the sign of the result” like:

SQLite3.c:19330:11: warning: conversion to ‘unsigned int’ from ‘int’ may
change the sign of the result [-Wsign-conversion]

75 warnings “signed overflow” like:
SQLite3.c:61116:3: warning: assuming signed overflow does not occur when
reducing constant in comparison [-Wstrict-overflow]

As a result I would like to recommend to build SQLite with all extra
warning options. In total, I have counted unbelievable 2628 warnings of
potential errors without any efforts from my side.

3. How to reproduce:

a) amalgamation SQLite version 3.20.0

b) gcc version:
$ gcc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
--prefix=/usr --program-suffix=-5 --enable-shared
--enable-linker-build-id --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --libdir=/usr/lib
--enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-libmpx --enable-plugin
--with-system-zlib --disable-browser-plugin --enable-java-awt=gtk
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc
--enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)

с) Lubuntu 16.04

d) Files:
$ ls
main.c    Makefile sqlite3.c  sqlite3.h

$ cat Makefile
# Compiler flags
CC ?= cc

CFLAGS += -pipe -std=c11
CFLAGS += -fbuiltin
EXE = sqlite_test
STATIC = -static
STRIP = -s

WFLAGS += -Wall -Wextra -Wpedantic -Wshadow
WFLAGS += -Wconversion -Wsign-conversion -Winit-self -Wunreachable-code
WFLAGS += -Wformat-nonliteral -Wformat-security -Wmissing-include-dirs
WFLAGS += -Wswitch-default -Wtrigraphs -Wstrict-overflow=5
WFLAGS += -Wfloat-equal -Wundef -Wshadow
WFLAGS += -Wbad-function-cast -Wcast-qual -Wcast-align
WFLAGS += -Wwrite-strings
WFLAGS += -Winline

ifneq ($(CC), clang)
WFLAGS += -Wlogical-op
CFLAGS += -finline-functions
CFLAGS += -flto
# Perform a number of minor optimizations that are relatively expensive.
CFLAGS += -fexpensive-optimizations
# Attempt to remove redundant extension instructions. This is especially
helpful for the x86-64 architecture, which implicitly zero-extends in
64-bit registers after writing to their lower 32-bit half.
CFLAGS += -free

# Project files
SRCS = $(wildcard *.c)
HDRS = $(wildcard *.h)
# Exclude a file
OBJS = $(SRCS:.c=.o)

# Release build settings
RELDIR = release
RELOBJS = $(addprefix $(RELDIR)/, $(OBJS))
RELCFLAGS = -O3 -funroll-loops -DNDEBUG
RELCFLAGS += -march=native
# GCC only options
ifneq ($(CC), clang)
RELWFLAGS += -Wsuggest-attribute=const -Wsuggest-attribute=pure
-Wsuggest-attribute=noreturn -Wsuggest-attribute=format

.PHONY: all clean release

# Default build
all: release

# Release rules
release: $(RELEXE)

    echo "$(RELEXE) linked."

$(RELDIR)/%.o: %.c $(HDRS)
    mkdir -p $(RELDIR)
    $(CC) -c $(CFLAGS) $(WFLAGS) $(RELWFLAGS) $(RELCFLAGS) -o $@ $<
    echo $<" compiled."

    rm -rf $(RELEXE) $(RELOBJS)
    test -d $(RELDIR) && rm -d $(RELDIR) || true

$ cat ./main.c
#include "sqlite3.h"
#include <stdio.h>

int main(void){

    printf("SQLite version: %s\n",sqlite3_libversion());

    return 0;


e) Run:
$ ./release/sqlite_test
SQLite version: 3.21.0

sqlite-users mailing list

Reply via email to