Skip to content

Commit 47fcfe5

Browse files
authored
Integrate clang-tidy (#981)
* configure clang-tidy and run under CI * pin CI to clang-tidy-17
1 parent 99569f8 commit 47fcfe5

File tree

9 files changed

+45
-28
lines changed

9 files changed

+45
-28
lines changed

.clang-tidy

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Checks: >
2+
bugprone-*,
3+
-bugprone-easily-swappable-parameters,
4+
-bugprone-exception-escape,
5+
-bugprone-implicit-widening-of-multiplication-result,
6+
-bugprone-narrowing-conversions,
7+
-bugprone-suspicious-include,
8+
-bugprone-unhandled-exception-at-new,
9+
clang-analyzer-*,
10+
11+
# Turn all the warnings from the checks above into errors.
12+
WarningsAsErrors: '*'
13+
# Check first-party (non-system, non-vendored) headers.
14+
HeaderFilterRegex: '.*'
15+
ExcludeHeaderFilterRegex: 'build/_deps/'
16+
SystemHeaders: false

.github/workflows/lint_and_format_check.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,10 @@ jobs:
3636
name: Lint with Ruff
3737
with:
3838
version: 0.6.0
39+
40+
- name: Run clang-tidy
41+
run: >
42+
cmake -B build -DADA_TESTING=ON -DCMAKE_CXX_CLANG_TIDY=clang-tidy-17 &&
43+
cmake --build build -j=4
44+
env:
45+
CXX: clang++-17

cmake/add-cpp-test.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function(add_cpp_test TEST_NAME)
3030
add_library(${TEST_NAME} STATIC ${ARGS_SOURCES})
3131
else(ARGS_LIBRARY)
3232
add_executable(${TEST_NAME} ${ARGS_SOURCES})
33+
set_source_files_properties(${ARGS_SOURCES} PROPERTIES SKIP_LINTING ON)
3334
endif(ARGS_LIBRARY)
3435

3536
# Add test

include/ada/url-inl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ inline std::ostream &operator<<(std::ostream &out, const ada::url &u) {
5555
out.protocol_end = uint32_t(get_protocol().size());
5656

5757
// Trailing index is always the next character of the current one.
58+
// NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
5859
size_t running_index = out.protocol_end;
5960

6061
if (host.has_value()) {

src/ada_idna.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9374,12 +9374,14 @@ bool is_label_valid(const std::u32string_view label) {
93749374
for (size_t i = 0; i <= last_non_nsm_char; i++) {
93759375
const direction d = find_direction(label[i]);
93769376

9377+
// NOLINTBEGIN(bugprone-assignment-in-if-condition)
93779378
// In an RTL label, if an EN is present, no AN may be present, and vice
93789379
// versa.
93799380
if ((d == direction::EN && ((has_en = true) && has_an)) ||
93809381
(d == direction::AN && ((has_an = true) && has_en))) {
93819382
return false;
93829383
}
9384+
// NOLINTEND(bugprone-assignment-in-if-condition)
93839385

93849386
if (!(d == direction::R || d == direction::AL || d == direction::AN ||
93859387
d == direction::EN || d == direction::ES || d == direction::CS ||

src/parser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ result_type parse_url_impl(std::string_view user_input,
394394

395395
// If c is U+002F (/), then set state to relative slash state.
396396
if ((input_position != input_size) &&
397+
// NOLINTNEXTLINE(bugprone-branch-clone)
397398
(url_data[input_position] == '/')) {
398399
ada_log(
399400
"RELATIVE_SCHEME if c is U+002F (/), then set state to relative "

src/unicode.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ bool percent_encode(const std::string_view input, const uint8_t character_set[],
476476
}
477477
ada_log("percent_encode appending ", std::distance(input.begin(), pointer),
478478
" bytes");
479+
// NOLINTNEXTLINE(bugprone-suspicious-stringview-data-usage)
479480
out.append(input.data(), std::distance(input.begin(), pointer));
480481
ada_log("percent_encode processing ", std::distance(pointer, input.end()),
481482
" bytes");
@@ -510,6 +511,7 @@ bool to_ascii(std::optional<std::string>& out, const std::string_view plain,
510511
std::string percent_encode(const std::string_view input,
511512
const uint8_t character_set[], size_t index) {
512513
std::string out;
514+
// NOLINTNEXTLINE(bugprone-suspicious-stringview-data-usage)
513515
out.append(input.data(), index);
514516
auto pointer = input.begin() + index;
515517
for (; pointer != input.end(); pointer++) {

src/url.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ ada_really_inline void url::parse_path(std::string_view input) {
566566
if (has_search()) {
567567
answer.append(",\n");
568568
answer.append("\t\"query\":\"");
569+
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
569570
helpers::encode_json(query.value(), back);
570571
answer.append("\"");
571572
}

tests/CMakeLists.txt

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,27 @@ if(MSVC AND BUILD_SHARED_LIBS)
4949
"$<TARGET_FILE_DIR:basic_fuzzer>") # <--this is out-file path
5050
endif()
5151

52+
macro(add_gtest_test exe cpp)
53+
add_executable(${exe} ${cpp})
54+
target_link_libraries(${exe} PRIVATE simdjson GTest::gtest_main)
55+
gtest_discover_tests(${exe} PROPERTIES TEST_DISCOVERY_TIMEOUT 600)
56+
set_source_files_properties(${cpp} PROPERTIES SKIP_LINTING ON)
57+
endmacro()
58+
5259
if(MSVC AND BUILD_SHARED_LIBS)
5360
message(STATUS "For some tests we use Google Test and it fails when building a DLL.")
5461
message(STATUS "Thus the tests are disabled. Sorry.")
5562
else()
5663
include(GoogleTest)
57-
add_executable(wpt_url_tests wpt_url_tests.cpp)
58-
if(ADA_INCLUDE_URL_PATTERN)
59-
add_executable(wpt_urlpattern_tests wpt_urlpattern_tests.cpp)
60-
endif()
61-
add_executable(url_components url_components.cpp)
62-
add_executable(basic_tests basic_tests.cpp)
63-
add_executable(from_file_tests from_file_tests.cpp)
64-
add_executable(ada_c ada_c.cpp)
65-
add_executable(url_search_params url_search_params.cpp)
66-
67-
target_link_libraries(wpt_url_tests PRIVATE simdjson GTest::gtest_main)
64+
add_gtest_test(wpt_url_tests wpt_url_tests.cpp)
6865
if(ADA_INCLUDE_URL_PATTERN)
69-
target_link_libraries(wpt_urlpattern_tests PRIVATE simdjson GTest::gtest_main)
66+
add_gtest_test(wpt_urlpattern_tests wpt_urlpattern_tests.cpp)
7067
endif()
71-
target_link_libraries(url_components PRIVATE simdjson GTest::gtest_main)
72-
target_link_libraries(basic_tests PRIVATE simdjson GTest::gtest_main)
73-
target_link_libraries(from_file_tests PRIVATE simdjson GTest::gtest_main)
74-
target_link_libraries(ada_c PRIVATE simdjson GTest::gtest_main)
75-
target_link_libraries(url_search_params PRIVATE simdjson GTest::gtest_main)
76-
77-
gtest_discover_tests(wpt_url_tests PROPERTIES TEST_DISCOVERY_TIMEOUT 600)
78-
if(ADA_INCLUDE_URL_PATTERN)
79-
gtest_discover_tests(wpt_urlpattern_tests PROPERTIES TEST_DISCOVERY_TIMEOUT 600)
80-
endif()
81-
gtest_discover_tests(url_components PROPERTIES TEST_DISCOVERY_TIMEOUT 600)
82-
gtest_discover_tests(basic_tests PROPERTIES TEST_DISCOVERY_TIMEOUT 600)
83-
gtest_discover_tests(from_file_tests PROPERTIES TEST_DISCOVERY_TIMEOUT 600)
84-
gtest_discover_tests(ada_c PROPERTIES TEST_DISCOVERY_TIMEOUT 600)
85-
gtest_discover_tests(url_search_params PROPERTIES TEST_DISCOVERY_TIMEOUT 600)
68+
add_gtest_test(url_components url_components.cpp)
69+
add_gtest_test(basic_tests basic_tests.cpp)
70+
add_gtest_test(from_file_tests from_file_tests.cpp)
71+
add_gtest_test(ada_c ada_c.cpp)
72+
add_gtest_test(url_search_params url_search_params.cpp)
8673

8774
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
8875
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9)
@@ -102,5 +89,4 @@ else()
10289
target_compile_definitions(basic_tests PRIVATE _CRT_SECURE_NO_WARNINGS)
10390
target_compile_definitions(url_search_params PRIVATE _CRT_SECURE_NO_WARNINGS)
10491
endif()
105-
10692
endif()

0 commit comments

Comments
 (0)