Skip to content

Commit 2dd94cc

Browse files
committed
Enable clang-tidy checks for core libraries
1 parent 68bfbf7 commit 2dd94cc

File tree

7 files changed

+58
-33
lines changed

7 files changed

+58
-33
lines changed

.clang-tidy

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ Checks: >
2121
-clang-diagnostic-unused-command-line-argument,
2222
-modernize-use-trailing-return-type,
2323
-fuchsia-trailing-return,
24-
-cppcoreguidelines-pro-type-vararg
24+
-cppcoreguidelines-pro-type-vararg,
25+
-*-magic-numbers
2526
WarningsAsErrors: "*"
2627
FormatStyle: file
2728
CheckOptions:
@@ -39,8 +40,8 @@ CheckOptions:
3940
value: lower_case
4041
- key: readability-identifier-naming.VariableCase
4142
value: lower_case
42-
- key: readability-identifier-naming.GlobalVariablePrefix
43-
value: g_
43+
# - key: readability-identifier-naming.GlobalVariablePrefix
44+
# value: g_
4445
- key: readability-identifier-naming.MemberCase
4546
value: lower_case
4647
- key: readability-identifier-naming.PrivateMemberPrefix

lib/cmake/generate.cmake

+6-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ function(write_cpp_intro target_name target_path)
5252
5353
#include <array>
5454
#include <cstddef>
55+
#include <optional>
56+
#include <span>
57+
#include <string_view>
5558
5659
using crl::DirectoryEntry;
5760
using crl::FileEntry;
@@ -247,10 +250,9 @@ endfunction()
247250
function(write_gperf_input_file target_name resource_files target_path)
248251
file(WRITE "${target_path}"
249252
"%{
250-
#include <${target_name}.h>
251-
252253
#include <cstring>
253254
255+
// NOLINTBEGIN(misc-unused-parameters, cppcoreguidelines-avoid-goto)
254256
namespace ${target_name} {
255257
256258
%}
@@ -271,6 +273,8 @@ struct PerfectHashResult { const char *name; std::span<const std::byte> data; };
271273
endforeach()
272274
file(APPEND "${target_path}"
273275
"%%
276+
// NOLINTEND(misc-unused-parameters, cppcoreguidelines-avoid-goto)
277+
274278
std::optional<std::span<const std::byte>> get_file_ph(std::string_view path) {
275279
const PerfectHashResult *f = PerfectHash::in_word_set(path.data(), path.size());
276280
if (f == nullptr) {

lib/crl/src/crl.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#include <crl/crl.h>
22

3+
#include <cstddef>
4+
#include <optional>
5+
#include <span>
6+
#include <string_view>
7+
38
namespace crl {
49

510
std::optional<std::span<const std::byte>>
@@ -15,7 +20,7 @@ get_file(const crl::DirectoryEntry &directory, std::string_view path) {
1520
}
1621
return {};
1722
}
18-
std::string_view sub_d_name = path.substr(0, pos);
23+
const std::string_view sub_d_name = path.substr(0, pos);
1924
bool found_match = false;
2025
for (const auto &sub_d : d->subdirectories) {
2126
if (sub_d->name == sub_d_name) {

lib/crl/src/crlv.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
#include <crl/crlv.h>
22

3+
#include <crl/crl.h>
34
#include <crl/utils/strv.h>
45

6+
#include <cstddef>
7+
#include <optional>
8+
#include <span>
9+
#include <string_view>
10+
511
namespace crl {
612

713
std::optional<std::span<const std::byte>>

lib/crl/src/utils_strv.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include <crl/utils/strv.h>
22

3+
#include <cstddef>
4+
#include <span>
5+
#include <string_view>
6+
37
namespace crl::utils::strv {
48

59
size_t sizev(std::span<std::string_view> strv) {
@@ -22,9 +26,10 @@ size_t findv(std::span<std::string_view> strv, size_t start, size_t len,
2226
continue;
2327
}
2428

25-
size_t process_len = std::min(s.size() - start, remaining_len);
26-
auto sub_s = s.substr(start, process_len);
27-
size_t p = sub_s.find(x);
29+
const size_t d = s.size() - start;
30+
const size_t process_len = d < remaining_len ? d : remaining_len;
31+
const auto sub_s = s.substr(start, process_len);
32+
const size_t p = sub_s.find(x);
2833

2934
if (p != std::string_view::npos) {
3035
return pos + start + p;
@@ -53,7 +58,8 @@ bool equalv(std::span<std::string_view> strv, size_t start, size_t len,
5358
continue;
5459
}
5560

56-
size_t process_len = std::min(s.size() - start, remaining_len);
61+
const size_t d = s.size() - start;
62+
const size_t process_len = d < remaining_len ? d : remaining_len;
5763

5864
if (pos + process_len > x.size()) {
5965
return false;

tests/CMakeLists.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
cmake_minimum_required(VERSION 3.30)
1+
cmake_minimum_required(VERSION 3.24)
22
project(crl-tests)
33

44
# GoogleTest requires at least C++14
55
set(CMAKE_CXX_STANDARD 20)
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7+
set(CLANG_TIDY_COMMAND "clang-tidy")
78

89
include(FetchContent)
910
FetchContent_Declare(
@@ -23,10 +24,14 @@ enable_testing()
2324

2425
add_subdirectory(.. crl)
2526

27+
set_target_properties(crl PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}")
28+
2629
add_executable(test_utils_strv
2730
"src/test_utils_strv.cpp"
2831
)
2932

33+
set_target_properties(test_utils_strv PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}")
34+
3035
target_link_libraries(test_utils_strv
3136
PRIVATE
3237
GTest::gtest_main

tests/src/test_utils_strv.cpp

+20-22
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,56 @@
22

33
#include <crl/utils/strv.h>
44

5+
#include <array>
6+
#include <cstddef>
7+
#include <string_view>
8+
59
TEST(Crlv, sizev) {
610
using crl::utils::strv::sizev;
711

8-
std::string_view b0;
9-
std::string_view b1 = "1";
10-
std::string_view b4 = "1234";
12+
const std::string_view b0;
13+
const std::string_view b1 = "1";
14+
const std::string_view b4 = "1234";
1115

1216
{
1317
std::array<std::string_view, 0> b{};
14-
size_t s = sizev(b);
15-
ASSERT_EQ(s, 0);
18+
ASSERT_EQ(sizev(b), 0);
1619
}
1720

1821
{
1922
std::array<std::string_view, 1> b{b0};
20-
size_t s = sizev(b);
21-
ASSERT_EQ(s, 0);
23+
ASSERT_EQ(sizev(b), 0);
2224
}
2325

2426
{
2527
std::array<std::string_view, 1> b{b1};
26-
size_t s = sizev(b);
27-
ASSERT_EQ(s, 1);
28+
ASSERT_EQ(sizev(b), 1);
2829
}
2930

3031
{
3132
std::array<std::string_view, 1> b{b4};
32-
size_t s = sizev(b);
33-
ASSERT_EQ(s, 4);
33+
ASSERT_EQ(sizev(b), 4);
3434
}
3535

3636
{
3737
std::array<std::string_view, 3> b{b0, b4, b0};
38-
size_t s = sizev(b);
39-
ASSERT_EQ(s, 4);
38+
ASSERT_EQ(sizev(b), 4);
4039
}
4140

4241
{
4342
std::array<std::string_view, 3> b{b4, b1, b4};
44-
size_t s = sizev(b);
45-
ASSERT_EQ(s, 9);
43+
ASSERT_EQ(sizev(b), 9);
4644
}
4745
}
4846

4947
TEST(Crlv, findv) {
5048
using crl::utils::strv::findv;
5149
using crl::utils::strv::sizev;
5250

53-
std::string_view b1 = "logs";
54-
std::string_view b2 = "/";
55-
std::string_view b3 = "source";
56-
std::string_view b4 = "/t.txt";
51+
const std::string_view b1 = "logs";
52+
const std::string_view b2 = "/";
53+
const std::string_view b3 = "source";
54+
const std::string_view b4 = "/t.txt";
5755
std::array<std::string_view, 4> b{b1, b2, b3, b4};
5856

5957
size_t p = findv(b, 0, sizev(b), '/');
@@ -72,9 +70,9 @@ TEST(Crlv, findv) {
7270

7371
TEST(Crlv, equalv) {
7472
using crl::utils::strv::equalv;
75-
std::string_view b0;
76-
std::string_view b1 = "x";
77-
std::string_view b4 = "1234";
73+
const std::string_view b0;
74+
const std::string_view b1 = "x";
75+
const std::string_view b4 = "1234";
7876

7977
{
8078
std::array<std::string_view, 0> b{};

0 commit comments

Comments
 (0)