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
optimisations.

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
-Wmissing-format-attribute

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
[-Wundef]
 #if SQLITE_4_BYTE_ALIGNED_MALLOC
     ^

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.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
5.4.0-6ubuntu1~16.04.4'
--with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs
--enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++
--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
--enable-gtk-cairo
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre
--enable-java-home
--with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64
--with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64
--with-arch-directory=amd64
--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
SQLITEFLAGS += -DSQLITE_THREADSAFE=0
SQLITEFLAGS += -DSQLITE_OMIT_LOAD_EXTENSION
CFLAGS += $(SQLITEFLAGS)
EXE = sqlite_test
STATIC = -static
STRIP = -s

WFLAGS += -Wall -Wextra -Wpedantic -Wshadow
WFLAGS += -Wconversion -Wsign-conversion -Winit-self -Wunreachable-code
-Wformat-y2k
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
endif

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

#
# Release build settings
#
RELDIR = release
RELEXE = $(RELDIR)/$(EXE)
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
-Wmissing-format-attribute
endif

.PHONY: all clean release

# Default build
all: release

#
# Release rules
#
release: $(RELEXE)

$(RELEXE): $(RELOBJS)
    $(CC) $(CFLAGS) $(WFLAGS) $(RELWFLAGS) $(RELCFLAGS) $(STATIC)
$(STRIP) -o $(RELEXE) $^ $(LDFLAGS) $(RELLDFLAGS) $(TCMALLOC)
    echo "$(RELEXE) linked."

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


clean:
    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
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to