Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit 26a39b1

Browse files
authored
Merge pull request #181 from cpp-best-practices/cleanup_static_checks_and_code_samples
2 parents c1d6acc + 72ad940 commit 26a39b1

24 files changed

Lines changed: 235 additions & 125 deletions

.clang-format

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ BreakBeforeTernaryOperators: true
4040
BreakConstructorInitializers: BeforeColon
4141
BreakConstructorInitializersBeforeComma: false
4242
BreakStringLiterals: true
43-
ColumnLimit: 0
43+
ColumnLimit: 120
4444
CommentPragmas: '^ IWYU pragma:'
4545
CompactNamespaces: false
4646
ConstructorInitializerAllOnOneLineOrOnePerLine: false
@@ -79,7 +79,7 @@ ObjCSpaceAfterProperty: true
7979
ObjCSpaceBeforeProtocolList: false
8080
PointerAlignment: Right
8181
ReflowComments: true
82-
SortIncludes: false
82+
SortIncludes: true
8383
SortUsingDeclarations: false
8484
SpaceAfterCStyleCast: false
8585
SpaceAfterTemplateKeyword: false
@@ -92,7 +92,7 @@ SpacesInCStyleCastParentheses: false
9292
SpacesInContainerLiterals: true
9393
SpacesInParentheses: false
9494
SpacesInSquareBrackets: false
95-
Standard: Cpp11
95+
Standard: c++20
9696
TabWidth: 8
9797
UseTab: Never
9898

.clang-tidy

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
---
2-
Checks: '*,-fuchsia-*,-google-*,-zircon-*,-abseil-*,-modernize-use-trailing-return-type,-llvm*'
2+
Checks: "*,
3+
-abseil-*,
4+
-altera-*,
5+
-android-*,
6+
-fuchsia-*,
7+
-google-*,
8+
-llvm*,
9+
-modernize-use-trailing-return-type,
10+
-zircon-*,
11+
-readability-else-after-return,
12+
-readability-static-accessed-through-instance,
13+
-readability-avoid-const-params-in-decls,
14+
-cppcoreguidelines-non-private-member-variables-in-classes,
15+
-misc-non-private-member-variables-in-classes,
16+
"
317
WarningsAsErrors: ''
418
HeaderFilterRegex: ''
519
FormatStyle: none
620

721

22+
23+
24+
25+

.cmake-format.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ separate_ctrl_name_with_space: false
1717
separate_fn_name_with_space: false
1818
tab_size: 2
1919

20+
markup:
21+
enable_markup: false
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: auto-clang-format
2+
on: [pull_request]
3+
4+
jobs:
5+
build:
6+
runs-on: ubuntu-latest
7+
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: DoozyX/clang-format-lint-action@v0.13
11+
with:
12+
source: '.'
13+
exclude: './third_party ./external'
14+
extensions: 'h,cpp,hpp'
15+
clangFormatVersion: 12
16+
inplace: True
17+
- uses: EndBug/add-and-commit@v4
18+
with:
19+
author_name: Clang Robot
20+
author_email: robot@example.com
21+
message: ':art: Committing clang-format changes'
22+
env:
23+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/ci.yml

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ on:
88

99
env:
1010
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
11-
BUILD_TYPE: RelWithDebInfo
1211
# Conan cache environment variables
1312
CONAN_SYSREQUIRES_MODE: enabled
1413
CONAN_USER_HOME: "${{ github.workspace }}/conan-cache"
@@ -19,18 +18,70 @@ jobs:
1918
runs-on: ${{ matrix.os }}
2019
strategy:
2120
fail-fast: false
21+
22+
# Recommendations:
23+
# * support at least 2 operating systems
24+
# * support at least 2 compilers
25+
# * make sure all supported configurations for your project are built
26+
#
27+
# Disable/enable builds in this list to meet the above recommendations
28+
# and your own projects needs
2229
matrix:
2330
os:
24-
- windows-2019
2531
- ubuntu-20.04
2632
- macos-10.15
33+
- windows-2019
2734
compiler:
2835
# you can specify the version after `-` like "llvm-13.0.0".
2936
- llvm
3037
- gcc
38+
generator:
39+
- "Ninja Multi-Config"
40+
build_type:
41+
- Release
42+
- Debug
43+
developer_mode:
44+
- ON
45+
- OFF
46+
47+
exclude:
48+
# mingw is determined by this author to be too buggy to support
49+
- os: windows-2019
50+
compiler: gcc
51+
3152
include:
32-
- os: "windows-latest"
33-
compiler: "msvc"
53+
# This exists solely to make sure a non-multiconfig build works
54+
- os: ubuntu-20.04
55+
compiler: gcc
56+
generator: "Unix Makefiles"
57+
build_type: Debug
58+
developer_mode: On
59+
60+
- os: windows-2022
61+
compiler: msvc
62+
generator: "Visual Studio 17 2022"
63+
build_type: Debug
64+
developer_mode: On
65+
- os: windows-2022
66+
compiler: msvc
67+
generator: "Visual Studio 17 2022"
68+
build_type: Release
69+
developer_mode: On
70+
- os: windows-2022
71+
compiler: msvc
72+
generator: "Visual Studio 17 2022"
73+
build_type: Debug
74+
developer_mode: Off
75+
- os: windows-2022
76+
compiler: msvc
77+
generator: "Visual Studio 17 2022"
78+
build_type: Release
79+
developer_mode: Off
80+
81+
82+
83+
84+
3485
steps:
3586
- uses: actions/checkout@v2
3687

@@ -46,9 +97,9 @@ jobs:
4697
${{ env.XDG_CACHE_HOME }}/vcpkg/archives
4798
${{ env.LOCALAPPDATA }}\vcpkg\archives
4899
${{ env.APPDATA }}\vcpkg\archives
49-
key: ${{ runner.os }}-${{ matrix.compiler }}-${{env.BUILD_TYPE}}-${{ hashFiles('**/CMakeLists.txt') }}-${{ hashFiles('./conanfile.txt')}}-${{ hashFiles('./vcpkg.json')}}
100+
key: ${{ runner.os }}-${{ matrix.compiler }}-${{matrix.build_type}}-${{matrix.generator}}-${{matrix.developer_mode}}-${{ hashFiles('**/CMakeLists.txt') }}-${{ hashFiles('./conanfile.txt')}}-${{ hashFiles('./vcpkg.json')}}
50101
restore-keys: |
51-
${{ runner.os }}-${{ matrix.compiler }}-${{env.BUILD_TYPE}}-
102+
${{ runner.os }}-${{ matrix.compiler }}-${{matrix.build_type}}-${{matrix.generator}}-${{matrix.developer_mode}}
52103
53104
- name: Setup Cpp
54105
uses: aminya/setup-cpp@v1
@@ -61,6 +112,7 @@ jobs:
61112
conan: true
62113
vcpkg: false
63114
ccache: true
115+
clangtidy: true
64116

65117
cppcheck: true
66118

@@ -69,27 +121,27 @@ jobs:
69121

70122
- name: Configure CMake
71123
run: |
72-
cmake -S . -B ./build -DCMAKE_BUILD_TYPE:STRING=${{env.BUILD_TYPE}}
124+
cmake -S . -B ./build -G "${{matrix.generator}}" -DCMAKE_BUILD_TYPE:STRING=${{matrix.build_type}} -DENABLE_DEVELOPER_MODE:BOOL=${{matrix.developer_mode}}
73125
74126
- name: Build
75127
# Execute the build. You can specify a specific target with "--target <NAME>"
76128
run: |
77-
cmake --build ./build --config ${{env.BUILD_TYPE}}
129+
cmake --build ./build --config ${{matrix.build_type}}
78130
79131
- name: Unix - Test and coverage
80132
if: runner.os != 'Windows'
81133
working-directory: ./build
82134
# Execute tests defined by the CMake configuration.
83135
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
84136
run: |
85-
ctest -C ${{env.BUILD_TYPE}}
137+
ctest -C ${{matrix.build_type}}
86138
gcovr -j ${{env.nproc}} --delete --root ../ --print-summary --xml-pretty --xml coverage.xml .
87139
88140
- name: Windows - Test and coverage
89141
if: runner.os == 'Windows'
90142
working-directory: ./build
91143
run: |
92-
OpenCppCoverage.exe --sources cpp_starter_project --export_type cobertura:coverage.xml --cover_children -- ctest -C ${{env.BUILD_TYPE}}
144+
OpenCppCoverage.exe --sources cpp_starter_project --export_type cobertura:coverage.xml --cover_children -- ctest -C ${{matrix.build_type}}
93145
94146
- name: Publish to codecov
95147
uses: codecov/codecov-action@v2

CMakeLists.txt

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ cmake_minimum_required(VERSION 3.16)
44
# and the targets that do not specify a standard.
55
# If not set, the latest supported standard for your compiler is used
66
# You can later set fine-grained standards for each target using `target_compile_features`
7-
# set(CMAKE_CXX_STANDARD 17)
7+
# Note: linking together projects compiled with different C++ standards may work, but
8+
# it is not recommended because of possible issues with ABI
9+
set(CMAKE_CXX_STANDARD 20)
810

9-
# Add project_options v0.13.1
11+
# strongly encouraged to enable this globally to avoid conflicts between
12+
# -Wpedantic being enabled and -std=c++20 and -std=gnu++20 for example
13+
# when compiling with PCH enabled
14+
set(CMAKE_CXX_EXTENSIONS OFF)
15+
16+
# Add project_options v0.14.2
1017
# https://github.com/cpp-best-practices/project_options
1118
include(FetchContent)
12-
FetchContent_Declare(_project_options URL https://github.com/cpp-best-practices/project_options/archive/refs/tags/v0.13.1.zip)
19+
FetchContent_Declare(_project_options
20+
URL https://github.com/cpp-best-practices/project_options/archive/refs/tags/v0.14.2.zip)
1321
FetchContent_MakeAvailable(_project_options)
1422
include(${_project_options_SOURCE_DIR}/Index.cmake)
1523

@@ -18,34 +26,58 @@ include(${_project_options_SOURCE_DIR}/Index.cmake)
1826
# run_vcpkg()
1927

2028
# Set the project name and language
21-
project(myproject LANGUAGES CXX)
29+
project(myproject LANGUAGES CXX C)
30+
31+
get_property(BUILDING_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
32+
if(BUILDING_MULTI_CONFIG)
33+
if(NOT CMAKE_BUILD_TYPE)
34+
# Make sure that all supported configuration types have their
35+
# associated conan packages available. You can reduce this
36+
# list to only the configuration types you use, but only if one
37+
# is not forced-set on the command line for VS
38+
message(TRACE "Setting up multi-config build types")
39+
set(CMAKE_CONFIGURATION_TYPES
40+
Debug
41+
Release
42+
RelWithDebInfo
43+
MinSizeRel
44+
CACHE STRING "Enabled build types" FORCE)
45+
else()
46+
message(TRACE "User chose a specific build type, so we are using that")
47+
set(CMAKE_CONFIGURATION_TYPES
48+
${CMAKE_BUILD_TYPE}
49+
CACHE STRING "Enabled build types" FORCE)
50+
endif()
51+
endif()
52+
53+
include(${_project_options_SOURCE_DIR}/src/DynamicProjectOptions.cmake)
54+
55+
# defaulted_project_options sets recommended defaults and provides user and developer
56+
# modes and full GUI support for choosing options at configure time
57+
58+
# for more flexibility, look into project_options() macro
59+
60+
# Any default can be overridden
61+
# set(<feature_name>_DEFAULT <value>) - set default for both user and developer modes
62+
# set(<feature_name>_DEVELOPER_DEFAULT <value>) - set default for developer mode
63+
# set(<feature_name>_USER_DEFAULT <value>) - set default for user mode
2264

2365
# Initialize project_options variable related to this project
2466
# This overwrites `project_options` and sets `project_warnings`
2567
# uncomment the options to enable them:
26-
project_options(
27-
ENABLE_CACHE
28-
# WARNINGS_AS_ERRORS
29-
ENABLE_CPPCHECK
30-
ENABLE_CLANG_TIDY
31-
ENABLE_CONAN
32-
ENABLE_COVERAGE
33-
# ENABLE_IPO
34-
# ENABLE_INCLUDE_WHAT_YOU_USE
35-
# ENABLE_PCH
36-
# PCH_HEADERS
37-
# ENABLE_DOXYGEN
38-
# ENABLE_USER_LINKER
39-
# ENABLE_BUILD_WITH_TIME_TRACE
40-
# ENABLE_UNITY
41-
# ENABLE_SANITIZER_ADDRESS
42-
# ENABLE_SANITIZER_LEAK
43-
# ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
44-
# ENABLE_SANITIZER_THREAD
45-
# ENABLE_SANITIZER_MEMORY
46-
# CONAN_OPTIONS
68+
dynamic_project_options(
69+
# Note: PCH is disabled by default in developer mode because these headers become
70+
# globally included and they can mask other errors
71+
PCH_HEADERS
72+
<vector>
73+
<string> # This is a list of headers to pre-compile, here are some common ones
74+
# CONAN_OPTIONS # Extra options to pass to conan
75+
# MSVC_WARNINGS # Override the defaults for the MSVC warnings
76+
# CLANG_WARNINGS # Override the defaults for the CLANG warnings
77+
# GCC_WARNINGS # Override the defaults for the GCC warnings
4778
)
48-
target_compile_features(project_options INTERFACE cxx_std_17)
79+
80+
target_compile_features(project_options INTERFACE cxx_std_${CMAKE_CXX_STANDARD})
4981

5082
# Adding the src:
5183
add_subdirectory(src)
@@ -64,3 +96,13 @@ if(ENABLE_FUZZING)
6496
message("Building Fuzz Tests, using fuzzing sanitizer https://www.llvm.org/docs/LibFuzzer.html")
6597
add_subdirectory(fuzz_test)
6698
endif()
99+
100+
# If MSVC is being used, and ASAN is enabled, we need to set the debugger environment
101+
# so that it behaves well with MSVC's debugger, and we can run the target from visual studio
102+
if(MSVC)
103+
get_all_targets(all_targets)
104+
set_target_properties(${all_targets} PROPERTIES VS_DEBUGGER_ENVIRONMENT "PATH=$(VC_ExecutablePath_x64);%PATH%")
105+
endif()
106+
107+
# set the startup project for the "play" button in MSVC
108+
set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT intro)

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# cpp_starter_project
22

3-
[![Build status](https://ci.appveyor.com/api/projects/status/d1tbhi2frii45rcl/branch/main?svg=true)](https://ci.appveyor.com/project/cpp-best-practices/cpp-starter-project/branch/main)
43
![CI](https://github.com/cpp-best-practices/cpp_starter_project/workflows/ci/badge.svg)
54
[![codecov](https://codecov.io/gh/cpp-best-practices/cpp_starter_project/branch/main/graph/badge.svg)](https://codecov.io/gh/cpp-best-practices/cpp_starter_project)
65
[![Language grade: C++](https://img.shields.io/lgtm/grade/cpp/github/cpp-best-practices/cpp_starter_project)](https://lgtm.com/projects/g/cpp-best-practices/cpp_starter_project/context:cpp)

conanfile.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,4 @@ spdlog/1.9.2
99
# sdl2/2.0.1
1010

1111
[generators]
12-
cmake_find_package
13-
cmake
14-
gcc
15-
txt
16-
visual_studio
12+
cmake_find_package_multi

fuzz_test/fuzz_tester.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
#include <fmt/format.h>
12
#include <iterator>
23
#include <utility>
3-
#include <fmt/format.h>
44

55
[[nodiscard]] auto sum_values(const uint8_t *Data, size_t Size)
66
{
@@ -17,6 +17,6 @@
1717
// cppcheck-suppress unusedFunction symbolName=LLVMFuzzerTestOneInput
1818
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
1919
{
20-
fmt::print("Value sum: {}, len{}\n", sum_values(Data,Size), Size);
20+
fmt::print("Value sum: {}, len{}\n", sum_values(Data, Size), Size);
2121
return 0;
2222
}

src/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
# Nana
1919
# add_subdirectory(nana)
2020

21-
find_package(fmt)
22-
find_package(spdlog)
23-
find_package(docopt)
21+
find_package(fmt CONFIG)
22+
find_package(spdlog CONFIG)
23+
find_package(docopt CONFIG)
2424

2525
# Generic test that uses conan libs
2626
add_executable(intro main.cpp)

0 commit comments

Comments
 (0)