Skip to content

Commit 0f10453

Browse files
farmerpikihsutter
andauthored
Initial clang-tidy configuration (#1253)
* clang-tidy: misc * added consts, fixed a initialization to pass clang-tidy # Conflicts: # source/reflect.h * added consts, masked a lint in reflect.h2 to pass clang-tidy * clang-tidy: modernize * clang-tidy: performance * replace const with constexpr in initialization of stable version strings * apply suggested changes * Style: Use `i8` and `u8` aliases * Update cppfront.cpp Signed-off-by: Herb Sutter <[email protected]> --------- Signed-off-by: Herb Sutter <[email protected]> Co-authored-by: Herb Sutter <[email protected]>
1 parent 0522898 commit 0f10453

File tree

11 files changed

+491
-489
lines changed

11 files changed

+491
-489
lines changed

.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Checks: 'performance-*, modernize-*, misc-*, -misc-definitions-in-headers, -misc-non-private-member-variables-in-classes, -misc-no-recursion, -misc-include-cleaner, -misc-use-anonymous-namespace, -modernize-use-trailing-return-type, -modernize-use-nodiscard, -modernize-use-emplace, -modernize-loop-convert, -modernize-pass-by-value, -modernize-use-equals-delete, -modernize-use-equals-default, -modernize-use-override, -modernize-avoid-c-arrays, -modernize-raw-string-literal, -modernize-concat-nested-namespaces, -performance-inefficient-string-concatenation'

include/cpp2util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,12 +1859,12 @@ constexpr auto is_narrowing_v =
18591859
// [dcl.init.list] 7.1
18601860
(std::is_floating_point_v<From> && std::is_integral_v<To>) ||
18611861
// [dcl.init.list] 7.2
1862-
(std::is_floating_point_v<From> && std::is_floating_point_v<To> && sizeof(From) > sizeof(To)) ||
1862+
(std::is_floating_point_v<From> && std::is_floating_point_v<To> && sizeof(From) > sizeof(To)) || // NOLINT(misc-redundant-expression)
18631863
// [dcl.init.list] 7.3
18641864
(std::is_integral_v<From> && std::is_floating_point_v<To>) ||
18651865
(std::is_enum_v<From> && std::is_floating_point_v<To>) ||
18661866
// [dcl.init.list] 7.4
1867-
(std::is_integral_v<From> && std::is_integral_v<To> && sizeof(From) > sizeof(To)) ||
1867+
(std::is_integral_v<From> && std::is_integral_v<To> && sizeof(From) > sizeof(To)) || // NOLINT(misc-redundant-expression)
18681868
(std::is_enum_v<From> && std::is_integral_v<To> && sizeof(From) > sizeof(To)) ||
18691869
// [dcl.init.list] 7.5
18701870
(std::is_pointer_v<From> && std::is_same_v<To, bool>)

source/common.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct source_line
6565
{
6666
std::string text;
6767

68-
enum class category { empty, preprocessor, comment, import, cpp1, cpp2, rawstring };
68+
enum class category : u8 { empty, preprocessor, comment, import, cpp1, cpp2, rawstring };
6969
category cat;
7070

7171
bool all_tokens_are_densely_spaced = true; // to be overridden in lexing if they're not
@@ -128,7 +128,7 @@ struct source_position
128128

129129
struct comment
130130
{
131-
enum class comment_kind { line_comment = 0, stream_comment };
131+
enum class comment_kind : u8 { line_comment = 0, stream_comment };
132132

133133
comment_kind kind;
134134
source_position start;
@@ -141,7 +141,7 @@ struct comment
141141
struct string_parts {
142142
struct cpp_code { std::string text; };
143143
struct raw_string { std::string text; };
144-
enum adds_sequences { no_ends = 0, on_the_beginning = 1, on_the_end = 2, on_both_ends = 3 };
144+
enum adds_sequences : u8 { no_ends = 0, on_the_beginning = 1, on_the_end = 2, on_both_ends = 3 };
145145

146146
string_parts(const std::string& beginseq,
147147
const std::string& endseq,
@@ -892,8 +892,8 @@ class cmdline_processor
892892
-> void
893893
{
894894
help_requested = true;
895-
std::string_view a = __DATE__;
896-
std::string_view b = __TIME__;
895+
constexpr std::string_view a = __DATE__;
896+
constexpr std::string_view b = __TIME__;
897897
std::unordered_map<std::string_view, char> m = { {"Jan",'1'}, {"Feb",'2'}, {"Mar",'3'}, {"Apr",'4'}, {"May",'5'}, {"Jun",'6'}, {"Jul",'7'}, {"Aug",'8'}, {"Sep",'9'}, {"Oct",'A'}, {"Nov",'B'}, {"Dec",'C'} };
898898

899899
auto stamp = std::to_string(atoi(&a[9])-15);

source/cppfront.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ auto main(
7272
}
7373
else {
7474
std::cerr << arg.text << " - ambiguous compiler flag name, did you mean one of these?\n";
75-
for (auto a : ambiguous) {
75+
for (auto const& a : ambiguous) {
7676
std::cerr << " " << arg.text.front() << a << "\n";
7777
}
7878
}
@@ -137,7 +137,7 @@ auto main(
137137
auto total_time = print_with_thousands(t.elapsed().count());
138138
std::cout << "\n Time " << total_time << " ms";
139139

140-
std::multimap< long long, std::string_view, std::greater<long long> > sorted_timers;
140+
std::multimap< long long, std::string_view, std::greater<> > sorted_timers;
141141
for (auto [name, t] : timers) {
142142
sorted_timers.insert({t.elapsed().count(), name});
143143
}

source/io.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ class braces_tracker
504504
//
505505
// line current line being processed
506506
//
507-
enum class preprocessor_conditional {
507+
enum class preprocessor_conditional : u8 {
508508
none = 0, pre_if, pre_else, pre_endif
509509
};
510510
auto starts_with_preprocessor_if_else_endif(
@@ -635,7 +635,7 @@ auto process_cpp_line(
635635
i+=2;
636636
if (i < ssize(line) - 1)
637637
{
638-
if (auto paren_pos = line.find("(", i);
638+
if (auto paren_pos = line.find('(', i);
639639
paren_pos != line.npos
640640
)
641641
{
@@ -841,7 +841,7 @@ class source
841841
static const int max_line_len = 90'000;
842842
// do not reduce this - I encountered an 80,556-char
843843
// line in real world code during testing
844-
char buf[max_line_len];
844+
char buf[max_line_len] {0};
845845

846846
public:
847847
//-----------------------------------------------------------------------
@@ -854,7 +854,6 @@ class source
854854
)
855855
: errors{ errors_ }
856856
, lines( 1 ) // extra blank to avoid off-by-one everywhere
857-
, buf{0}
858857
{
859858
}
860859

source/lex.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace cpp2 {
3333
//-----------------------------------------------------------------------
3434
//
3535

36-
enum class lexeme : std::int8_t {
36+
enum class lexeme : i8 {
3737
SlashEq,
3838
Slash,
3939
LeftShiftEq,
@@ -1496,7 +1496,7 @@ auto lex_line(
14961496
auto R_pos = i + 1;
14971497
auto seq_pos = i + 3;
14981498

1499-
if (auto paren_pos = line.find("(", seq_pos); paren_pos != line.npos) {
1499+
if (auto paren_pos = line.find('(', seq_pos); paren_pos != line.npos) {
15001500
auto opening_seq = line.substr(i, paren_pos - i + 1);
15011501
auto closing_seq = ")"+line.substr(seq_pos, paren_pos-seq_pos)+"\"";
15021502

@@ -1717,7 +1717,7 @@ auto lex_line(
17171717
if (peek(j-2) == 'R') {
17181718
auto seq_pos = i + j;
17191719

1720-
if (auto paren_pos = line.find("(", seq_pos); paren_pos != line.npos) {
1720+
if (auto paren_pos = line.find('(', seq_pos); paren_pos != line.npos) {
17211721
auto opening_seq = line.substr(i, paren_pos - i + 1);
17221722
auto closing_seq = ")"+line.substr(seq_pos, paren_pos-seq_pos)+"\"";
17231723

source/parse.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ struct template_argument;
130130

131131
struct primary_expression_node
132132
{
133-
enum active { empty=0, identifier, expression_list, id_expression, declaration, inspect, literal };
133+
enum active : u8 { empty=0, identifier, expression_list, id_expression, declaration, inspect, literal };
134134
std::variant<
135135
std::monostate,
136136
token const*,
@@ -669,7 +669,7 @@ auto binary_expression_node<Name, Term>::is_standalone_expression() const
669669
}
670670

671671

672-
enum class passing_style { in=0, copy, inout, out, move, forward, invalid };
672+
enum class passing_style : u8 { in=0, copy, inout, out, move, forward, invalid };
673673
auto to_passing_style(token const& t) -> passing_style {
674674
if (t.type() == lexeme::Identifier) {
675675
if (t == "in" ) { return passing_style::in; }
@@ -1179,7 +1179,7 @@ struct template_args_tag { };
11791179

11801180
struct template_argument
11811181
{
1182-
enum active { empty=0, expression, type_id };
1182+
enum active : u8 { empty=0, expression, type_id };
11831183
source_position comma;
11841184
std::variant<
11851185
std::monostate,
@@ -1365,7 +1365,7 @@ struct type_id_node
13651365
int dereference_cnt = {};
13661366
token const* suspicious_initialization = {};
13671367

1368-
enum active { empty=0, qualified, unqualified, function, keyword };
1368+
enum active : u8 { empty=0, qualified, unqualified, function, keyword };
13691369
std::variant<
13701370
std::monostate,
13711371
std::unique_ptr<qualified_id_node>,
@@ -1666,7 +1666,7 @@ struct id_expression_node
16661666
{
16671667
source_position pos;
16681668

1669-
enum active { empty=0, qualified, unqualified };
1669+
enum active : u8 { empty=0, qualified, unqualified };
16701670
std::variant<
16711671
std::monostate,
16721672
std::unique_ptr<qualified_id_node>,
@@ -2153,7 +2153,7 @@ struct statement_node
21532153
// type(s) used in a std::unique_ptr as a member
21542154
~statement_node();
21552155

2156-
enum active { expression=0, compound, selection, declaration, return_, iteration, using_, contract, inspect, jump };
2156+
enum active : u8 { expression=0, compound, selection, declaration, return_, iteration, using_, contract, inspect, jump };
21572157
std::variant<
21582158
std::unique_ptr<expression_statement_node>,
21592159
std::unique_ptr<compound_statement_node>,
@@ -2281,7 +2281,7 @@ struct parameter_declaration_node
22812281
passing_style pass = passing_style::in;
22822282
int ordinal = 1;
22832283

2284-
enum class modifier { none=0, implicit, virtual_, override_, final_ };
2284+
enum class modifier : u8 { none=0, implicit, virtual_, override_, final_ };
22852285
modifier mod = modifier::none;
22862286

22872287
std::unique_ptr<declaration_node> declaration;
@@ -2483,7 +2483,7 @@ struct function_type_node
24832483
passing_style pass = passing_style::move;
24842484
};
24852485

2486-
enum active { empty = 0, id, list };
2486+
enum active : u8 { empty = 0, id, list };
24872487
std::variant<
24882488
std::monostate,
24892489
single_type_id,
@@ -2862,7 +2862,7 @@ struct alias_node
28622862
token const* type = {};
28632863
std::unique_ptr<type_id_node> type_id; // for objects
28642864

2865-
enum active : std::uint8_t { a_type, a_namespace, an_object };
2865+
enum active : u8 { a_type, a_namespace, an_object };
28662866
std::variant<
28672867
std::unique_ptr<type_id_node>,
28682868
std::unique_ptr<id_expression_node>,
@@ -2903,7 +2903,7 @@ struct alias_node
29032903
};
29042904

29052905

2906-
enum class accessibility { default_ = 0, public_, protected_, private_ };
2906+
enum class accessibility : u8 { default_ = 0, public_, protected_, private_ };
29072907

29082908
auto to_string(accessibility a)
29092909
-> std::string
@@ -2932,7 +2932,7 @@ struct declaration_node
29322932
std::unique_ptr<unqualified_id_node> identifier;
29332933
accessibility access = accessibility::default_;
29342934

2935-
enum active : std::uint8_t { a_function, an_object, a_type, a_namespace, an_alias };
2935+
enum active : u8 { a_function, an_object, a_type, a_namespace, an_alias };
29362936
std::variant<
29372937
std::unique_ptr<function_type_node>,
29382938
std::unique_ptr<type_id_node>,
@@ -3353,7 +3353,7 @@ struct declaration_node
33533353
auto parent_is_polymorphic() const -> bool
33543354
{ return parent_declaration && parent_declaration->is_polymorphic(); }
33553355

3356-
enum which {
3356+
enum which : u8 {
33573357
functions = 1,
33583358
objects = 2,
33593359
types = 4,
@@ -3421,7 +3421,7 @@ struct declaration_node
34213421
{
34223422
// Convert the gather_ results to const*
34233423
auto tmp = gather_type_scope_declarations(w);
3424-
return std::vector<declaration_node const*>(tmp.begin(), tmp.end());
3424+
return {tmp.begin(), tmp.end()};
34253425
}
34263426

34273427

@@ -6105,7 +6105,7 @@ class parser
61056105
// Remember current position, because we may need to backtrack
61066106
auto start_pos = pos;
61076107

6108-
bool inside_initializer = (
6108+
const bool inside_initializer = (
61096109
peek(-1) && peek(-1)->type() == lexeme::Assignment
61106110
);
61116111
auto open_paren = &curr();
@@ -8069,7 +8069,7 @@ class parser
80698069
)
80708070
-> std::unique_ptr<compound_statement_node>
80718071
{
8072-
bool is_braced = curr().type() == lexeme::LeftBrace;
8072+
const bool is_braced = curr().type() == lexeme::LeftBrace;
80738073
if (
80748074
!is_braced
80758075
&& !allow_single_unbraced_statement

0 commit comments

Comments
 (0)