Add FLATBUFFERS_STRICT_MODE (#7408)

This commit is contained in:
Derek Bailey
2022-08-06 21:06:14 -07:00
committed by GitHub
parent 950444a343
commit 9a5ff89003
4 changed files with 45 additions and 35 deletions

View File

@@ -19,7 +19,7 @@ jobs:
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: cmake - name: cmake
run: CXX=${{ matrix.cxx }} cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release . run: CXX=${{ matrix.cxx }} cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_STRICT_MODE=ON .
- name: build - name: build
run: make -j run: make -j
- name: test - name: test
@@ -42,7 +42,7 @@ jobs:
- name: Add msbuild to PATH - name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.1 uses: microsoft/setup-msbuild@v1.1
- name: cmake - name: cmake
run: cmake -G "Visual Studio 16 2019" -A x64 -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_CPP17=ON . run: cmake -G "Visual Studio 16 2019" -A x64 -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_CPP17=ON -DFLATBUFFERS_STRICT_MODE=ON .
- name: build - name: build
run: msbuild.exe FlatBuffers.sln /p:Configuration=Release /p:Platform=x64 run: msbuild.exe FlatBuffers.sln /p:Configuration=Release /p:Platform=x64
- name: test - name: test
@@ -61,7 +61,7 @@ jobs:
- name: Add msbuild to PATH - name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.1 uses: microsoft/setup-msbuild@v1.1
- name: cmake - name: cmake
run: cmake -G "Visual Studio 16 2019" -A x64 -DCMAKE_BUILD_TYPE=Release . run: cmake -G "Visual Studio 16 2019" -A x64 -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_STRICT_MODE=ON .
- name: build tool version 15 (VS 2017) - name: build tool version 15 (VS 2017)
run: msbuild.exe FlatBuffers.sln /p:Configuration=Release /p:Platform=x64 /p:VisualStudioVersion=15.0 run: msbuild.exe FlatBuffers.sln /p:Configuration=Release /p:Platform=x64 /p:VisualStudioVersion=15.0
- name: test - name: test
@@ -75,7 +75,7 @@ jobs:
- name: Add msbuild to PATH - name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.1 uses: microsoft/setup-msbuild@v1.1
- name: cmake - name: cmake
run: cmake -G "Visual Studio 14 2015" -A x64 -DCMAKE_BUILD_TYPE=Release . run: cmake -G "Visual Studio 14 2015" -A x64 -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_STRICT_MODE=ON .
- name: build tool version 14 (VS 2015) - name: build tool version 14 (VS 2015)
run: msbuild.exe FlatBuffers.sln /p:Configuration=Release /p:Platform=x64 /p:VisualStudioVersion=14.0 run: msbuild.exe FlatBuffers.sln /p:Configuration=Release /p:Platform=x64 /p:VisualStudioVersion=14.0
- name: test - name: test
@@ -115,7 +115,7 @@ jobs:
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: cmake - name: cmake
run: cmake -G "Xcode" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_FLATC_EXECUTABLE=_build/Release/flatc . run: cmake -G "Xcode" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_FLATC_EXECUTABLE=_build/Release/flatc -DFLATBUFFERS_STRICT_MODE=ON .
- name: build - name: build
# NOTE: we need this _build dir to not have xcodebuild's default ./build dir clash with the BUILD file. # NOTE: we need this _build dir to not have xcodebuild's default ./build dir clash with the BUILD file.
run: xcodebuild -toolchain clang -configuration Release -target flattests SYMROOT=$(PWD)/_build run: xcodebuild -toolchain clang -configuration Release -target flattests SYMROOT=$(PWD)/_build
@@ -142,7 +142,7 @@ jobs:
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: cmake - name: cmake
run: cmake -G "Xcode" -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_FLATC_EXECUTABLE=_build/Release/flatc . run: cmake -G "Xcode" -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_FLATC_EXECUTABLE=_build/Release/flatc -DFLATBUFFERS_STRICT_MODE=ON .
- name: build - name: build
# NOTE: we need this _build dir to not have xcodebuild's default ./build dir clash with the BUILD file. # NOTE: we need this _build dir to not have xcodebuild's default ./build dir clash with the BUILD file.
run: xcodebuild -toolchain clang -configuration Release -target flattests SYMROOT=$(PWD)/_build run: xcodebuild -toolchain clang -configuration Release -target flattests SYMROOT=$(PWD)/_build
@@ -176,7 +176,7 @@ jobs:
java-version: 1.8 java-version: 1.8
- name: set up flatc - name: set up flatc
run: | run: |
cmake -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF . cmake -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF -DFLATBUFFERS_STRICT_MODE=ON .
make -j make -j
echo "${PWD}" >> $GITHUB_PATH echo "${PWD}" >> $GITHUB_PATH
- name: build - name: build
@@ -192,7 +192,7 @@ jobs:
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: cmake - name: cmake
run: CXX=${{ matrix.cxx }} cmake -G "Unix Makefiles" -DFLATBUFFERS_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release . && make -j run: CXX=${{ matrix.cxx }} cmake -G "Unix Makefiles" -DFLATBUFFERS_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_STRICT_MODE=ON . && make -j
- name: Generate - name: Generate
run: scripts/check_generate_code.py run: scripts/check_generate_code.py
- name: Generate gRPC - name: Generate gRPC
@@ -207,7 +207,7 @@ jobs:
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: cmake - name: cmake
run: CXX=${{ matrix.cxx }} cmake -G "Unix Makefiles" -DFLATBUFFERS_CXX_FLAGS="-Wno-unused-parameter -fno-aligned-new" -DFLATBUFFERS_BUILD_BENCHMARKS=ON -DCMAKE_BUILD_TYPE=Release . && make -j run: CXX=${{ matrix.cxx }} cmake -G "Unix Makefiles" -DFLATBUFFERS_CXX_FLAGS="-Wno-unused-parameter -fno-aligned-new" -DFLATBUFFERS_BUILD_BENCHMARKS=ON -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_STRICT_MODE=ON . && make -j
- name: Run benchmarks - name: Run benchmarks
run: ./flatbenchmark --benchmark_repetitions=5 --benchmark_display_aggregates_only=true --benchmark_out_format=console --benchmark_out=benchmarks/results_${{matrix.cxx}} run: ./flatbenchmark --benchmark_repetitions=5 --benchmark_display_aggregates_only=true --benchmark_out_format=console --benchmark_out=benchmarks/results_${{matrix.cxx}}
- name: Upload benchmarks results - name: Upload benchmarks results
@@ -283,7 +283,7 @@ jobs:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: flatc - name: flatc
# FIXME: make test script not rely on flatc # FIXME: make test script not rely on flatc
run: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_INSTALL=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF . && make -j run: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_INSTALL=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF -DFLATBUFFERS_STRICT_MODE=ON . && make -j
- name: test - name: test
working-directory: tests working-directory: tests
run: bash GoTest.sh run: bash GoTest.sh
@@ -331,7 +331,7 @@ jobs:
sdk: stable sdk: stable
- name: flatc - name: flatc
# FIXME: make test script not rely on flatc # FIXME: make test script not rely on flatc
run: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_INSTALL=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF . && make -j run: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_INSTALL=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF -DFLATBUFFERS_STRICT_MODE=ON . && make -j
- name: test - name: test
working-directory: tests working-directory: tests
run: bash DartTest.sh run: bash DartTest.sh

View File

@@ -64,7 +64,7 @@ jobs:
# uses a compiled language # uses a compiled language
- run: | - run: |
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release . cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_STRICT_MODE=ON .
make -j make -j
- name: Perform CodeQL Analysis - name: Perform CodeQL Analysis

View File

@@ -74,6 +74,9 @@ option(FLATBUFFERS_ENABLE_PCH
option(FLATBUFFERS_SKIP_MONSTER_EXTRA option(FLATBUFFERS_SKIP_MONSTER_EXTRA
"Skip generating monster_extra.fbs that contains non-supported numerical\" "Skip generating monster_extra.fbs that contains non-supported numerical\"
types." OFF) types." OFF)
option(FLATBUFFERS_STRICT_MODE
"Build flatbuffers with all warnings as errors (-Werror or /WX)."
OFF)
set(MSVC_LIKE OFF) set(MSVC_LIKE OFF)
if(MSVC OR CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC") if(MSVC OR CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
@@ -344,7 +347,9 @@ if(MSVC_LIKE)
target_compile_options(ProjectConfig target_compile_options(ProjectConfig
INTERFACE INTERFACE
/W4 /W4
/WX $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>:
/WX # Treat all compiler warnings as errors
>
/wd4512 # C4512: assignment operator could not be generated /wd4512 # C4512: assignment operator could not be generated
/wd4316 # C4316: object allocated on the heap may not be aligned /wd4316 # C4316: object allocated on the heap may not be aligned
/wd4456 # C4456: hides previous local declaration /wd4456 # C4456: hides previous local declaration
@@ -356,9 +361,10 @@ else()
target_compile_options(ProjectConfig target_compile_options(ProjectConfig
INTERFACE INTERFACE
-Wall -Wall
-Werror $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>:
-Werror # Treat all compiler warnings as errors
>
-pedantic -pedantic
-Werror
-Wextra -Wextra
-Wno-unused-parameter -Wno-unused-parameter
-Wold-style-cast -Wold-style-cast
@@ -370,20 +376,26 @@ else()
$<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,3.8>: $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,3.8>:
-Wimplicit-fallthrough -Wimplicit-fallthrough
-Wextra-semi -Wextra-semi
-Werror=unused-private-field $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>:
-Werror=unused-private-field
>
> >
> >
$<$<CXX_COMPILER_ID:GNU>: $<$<CXX_COMPILER_ID:GNU>:
$<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,4.4>: $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,4.4>:
-Wunused-result -Wunused-result
-Werror=unused-result
-Wunused-parameter -Wunused-parameter
-Werror=unused-parameter $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>:
-Werror=unused-result
-Werror=unused-parameter
>
> >
$<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,7.0>: $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,7.0>:
-faligned-new -faligned-new
-Werror=implicit-fallthrough=2 $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>:
-Werror=implicit-fallthrough=2
>
> >
$<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,8.0>: $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,8.0>:
-Wextra-semi -Wextra-semi

View File

@@ -31,24 +31,22 @@ run 'flattests' or `flatsampletext`, or it will fail to load its files.*
### Make all warnings into errors ### Make all warnings into errors
By default all Flatbuffers `cmake` targets are build with `-Werror` flag. By default all Flatbuffers `cmake` targets are **not** built with the `-Werror`
With this flag (or `/WX` for MSVC) C++ compiler will treat all warnings as errors. (or `/WX` for MSVC) flag that treats any warning as an error. This allows more
Additionally `-Wall -pedantic -Wextra` (or `/W4` form MSVC) flags are set. flexibility for users of Flatbuffers to use newer compilers and toolsets that
These flags minimize the number of possible defects in code and keep code highly portable. may add new warnings that would cause a build failure.
Using these flags is considered good practice but sometimes it can break dependent projects
if a compiler is upgraded or a toolset is changed.
Usually, newer compiler versions add new compile-time diagnostics that were unavailable before.
These new diagnostic warnings could stop the build process if `-Werror` flag is set.
It is possible to cancel `warnings as errors` flag at `cmake` configuration stage using To enable a stricter build that does treat warnings as errors, set the
`FLATBUFFERS_CXX_FLAGS` option. Compilation flags declared in `FLATBUFFERS_CXX_FLAGS` will be `FLATBUFFERS_STRICT_MODE` `cmake` compliation flag to `ON`.
appended to the project-level `CMAKE_CXX_FLAGS` variable.
Examples:
- GCC and Clang: `cmake . -D FLATBUFFERS_CXX_FLAGS="-Wno-error"` ```
- MSVC: `cmake . -D FLATBUFFERS_CXX_FLAGS="/WX-"` cmake . -DFLATBUFFERS_STRICT_MODE=ON
- MSVC: `cmake . -D FLATBUFFERS_CXX_FLAGS="/Wv <compiler.version>"` ```
Our CI builds run with strict mode on, ensuring the code that is committed to
the project is as portable and warning free as possible. Thus developers
contributing to the project should enable strict mode locally before making a
PR.
## Building with VCPKG ## Building with VCPKG