Skip to content

Commit 9f46d20

Browse files
authored
Add ability to treat headers as system to suppress warning. Also enable warnings on the example target (#10)
1 parent 35eb32b commit 9f46d20

File tree

6 files changed

+96
-8
lines changed

6 files changed

+96
-8
lines changed

CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ endif()
1212

1313
option(PLOTLYPP_BUILD_EXAMPLES "Build the examples" ${MAIN_PROJECT})
1414
option(PLOTLYPP_BUILD_MODULES "Build Plotly++ as a C++ module" OFF)
15+
option(PLOTLYPP_SYSTEM_INCLUDE "Include as system headers (skip for clang-tidy)." OFF)
1516

1617
if(MAIN_PROJECT)
1718
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
@@ -46,6 +47,10 @@ else()
4647
endif()
4748
set(JSON_TARGET nlohmann_json::nlohmann_json)
4849

50+
if (PLOTLYPP_SYSTEM_INCLUDE)
51+
set(PLOTLYPP_INCLUDE_TYPE "SYSTEM")
52+
endif()
53+
4954
if(PLOTLYPP_BUILD_MODULES)
5055
# Build as C++20 module
5156
add_library(plotlypp STATIC)
@@ -68,7 +73,7 @@ else()
6873
endif()
6974

7075
target_link_libraries(plotlypp ${SCOPE} ${JSON_TARGET})
71-
target_include_directories(plotlypp ${SCOPE}
76+
target_include_directories(plotlypp ${PLOTLYPP_INCLUDE_TYPE} ${SCOPE}
7277
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
7378
$<INSTALL_INTERFACE:include>
7479
)

examples/3d_charts.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ Figure gen3dSurfaceTorus() {
122122

123123
Eigen::MatrixXd u(kNumPoints, kNumPoints);
124124
Eigen::MatrixXd v(kNumPoints, kNumPoints);
125-
for (int i = 0; i < kNumPoints; ++i) {
126-
for (int j = 0; j < kNumPoints; ++j) {
125+
for (auto i = 0u; i < kNumPoints; ++i) {
126+
for (auto j = 0u; j < kNumPoints; ++j) {
127127
u(i, j) = ugrid[i][j];
128128
v(i, j) = vgrid[i][j];
129129
}

examples/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ add_library(math_utils
2424
target_include_directories(math_utils PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
2525
target_compile_features(math_utils PUBLIC cxx_std_17)
2626

27+
include(Warnings.cmake)
2728

2829
add_executable(example
2930
example.cpp
@@ -42,9 +43,22 @@ target_link_libraries(example
4243
Eigen3::Eigen
4344
)
4445

46+
target_include_directories(example SYSTEM PRIVATE
47+
$<TARGET_PROPERTY:Eigen3::Eigen,INTERFACE_INCLUDE_DIRECTORIES>
48+
)
49+
4550
if(PLOTLYPP_BUILD_MODULES)
4651
add_executable(module_example module_example.cpp)
4752
target_link_libraries(module_example PRIVATE plotlypp)
4853
# Must use at least C++20 for modules
4954
target_compile_features(module_example PRIVATE cxx_std_20)
5055
endif()
56+
57+
set_target_warnings(example)
58+
target_compile_options(example PRIVATE
59+
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:
60+
# Clang complains with -Wpedantic. For external users we also have the PLOTLYPP_SYSTEM_INCLUDE CMake option
61+
# to treat all plotlypp headers as system headers.
62+
-Wno-overlength-strings
63+
>
64+
)

examples/Warnings.cmake

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
set(MSVC_WARNINGS
3+
/W4 # Baseline reasonable warnings
4+
# /Wall # Also warns on files included from the standard library, so it's not very useful and creates too many extra warnings.
5+
/permissive-
6+
/w14062 # enumerator 'identifier' in switch of enum 'enumeration' is not handled, ie -Wswitch equivalent
7+
/w14242 # 'identifier': conversion from 'type1' to 'type1', possible loss of data
8+
/w14254 # 'operator': conversion from 'type1:field_bits' to 'type2:field_bits', possible loss of data
9+
/w14263 # 'function': member function does not override any base class virtual member function
10+
/w14265 # 'classname': class has virtual functions, but destructor is not virtual instances of this class may not be destructed correctly
11+
/w14287 # 'operator': unsigned/negative constant mismatch
12+
/we4289 # nonstandard extension used: 'variable': loop control variable declared in the for-loop is used outside the for-loop scope
13+
/w14296 # 'operator': expression is always 'boolean_value'
14+
/w14311 # 'variable': pointer truncation from 'type1' to 'type2'
15+
/w14545 # expression before comma evaluates to a function which is missing an argument list
16+
/w14546 # function call before comma missing argument list
17+
/w14547 # 'operator': operator before comma has no effect; expected operator with side-effect
18+
/w14549 # 'operator': operator before comma has no effect; did you intend 'operator'?
19+
/w14555 # expression has no effect; expected expression with side- effect
20+
/w14619 # pragma warning: there is no warning number 'number'
21+
/w14640 # Enable warning on thread un-safe static member initialization
22+
/w14826 # Conversion from 'type1' to 'type_2' is sign-extended. This may cause unexpected runtime behavior.
23+
/w14905 # wide string literal cast to 'LPSTR'
24+
/w14906 # string literal cast to 'LPWSTR'
25+
/w14928 # illegal copy-initialization; more than one user-defined conversion has been implicitly applied
26+
)
27+
28+
29+
set(GCC_WARNINGS
30+
-Wall
31+
-Wextra # reasonable and standard
32+
-Wpedantic
33+
-Wshadow # warn the user if a variable declaration shadows one from a parent context, may cause warnings in 3rd party libs
34+
-Wnon-virtual-dtor # warn the user if a class with virtual functions has a non-virtual destructor. This helps catch hard to track down memory errors
35+
-Wold-style-cast # warn for c-style casts
36+
-Wcast-align # warn for potential performance problem casts
37+
-Wunused # warn on anything being unused
38+
-Woverloaded-virtual # warn if you overload (not override) a virtual function
39+
-Wconversion # warn on type conversions that may lose data
40+
-Wsign-conversion # warn on sign conversions
41+
-Wnull-dereference # warn if a null dereference is detected
42+
-Wdouble-promotion # warn if float is implicit promoted to double
43+
-Wformat=2 # warn on security issues around functions that format output (ie printf)
44+
# Maybe GCC only
45+
-Wmisleading-indentation # warn if indentation implies blocks where blocks do not exist
46+
-Wduplicated-cond # warn if if / else chain has duplicated conditions
47+
-Wduplicated-branches # warn if if / else branches have duplicated code
48+
-Wlogical-op # warn about logical operations being used where bitwise were probably wanted
49+
-Wuseless-cast # warn if you perform a cast to the same type
50+
-Wpedantic
51+
)
52+
53+
function(set_target_warnings target_name)
54+
target_compile_options(${target_name} PRIVATE
55+
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:
56+
-Werror
57+
-Wno-unknown-warning-option # Silences errors for the flags below
58+
${GCC_WARNINGS}
59+
>
60+
$<$<CXX_COMPILER_ID:GNU>:
61+
-Werror
62+
${GCC_WARNINGS}
63+
>
64+
$<$<CXX_COMPILER_ID:MSVC>:
65+
/WX
66+
${MSVC_WARNINGS}
67+
>
68+
)
69+
endfunction()

examples/subplots.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Figure gen3dSubplots() {
7070

7171
std::vector<std::vector<double>> z(x_grid.size(), std::vector<double>(x_grid[0].size()));
7272

73-
for (auto i = 0; i < x_grid.size(); ++i) {
73+
for (auto i = 0u; i < x_grid.size(); ++i) {
7474
for (size_t j = 0; j < x_grid[0].size(); ++j) {
7575
double x_val = x_grid[i][j];
7676
double y_val = y_grid[i][j];
@@ -137,9 +137,9 @@ VolcanoData generate_data(int n) {
137137
std::normal_distribution<> elev_dist(1000.0, 5000.0);
138138
std::uniform_real_distribution<> lon_dist(-180.0, 180.0);
139139
std::uniform_real_distribution<> lat_dist(-70.0, 80.0);
140-
std::uniform_int_distribution<> status_dist(0, 1);
141-
std::uniform_int_distribution<> type_dist(0, 3);
142-
std::uniform_int_distribution<> country_dist(0, 5);
140+
std::uniform_int_distribution<unsigned int> status_dist(0, 1);
141+
std::uniform_int_distribution<unsigned int> type_dist(0, 3);
142+
std::uniform_int_distribution<unsigned int> country_dist(0, 5);
143143

144144
std::vector<std::string> statuses = {"Historical", "Holocene"};
145145
std::vector<std::string> types = {"Stratovolcano", "Shield volcano", "Submarine volcano", "Caldera"};

include/plotlypp/figure.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Figure {
5151
void toHtml(std::ostream& os) const {
5252
// Serialize and sanitize for HTML embedding.
5353
// This escapes '<' to '\u003c' so the browser never confuses JSON data for HTML tags.
54-
auto html_safe_serialize = [this](const auto& json) {
54+
auto html_safe_serialize = [](const auto& json) {
5555
std::string s = serialize(json);
5656
// Escape '<' to '\u003c' so the browser never confuses JSON data for HTML tags.
5757
size_t pos = 0;

0 commit comments

Comments
 (0)