1 // RUN: %check_clang_tidy -check-suffix=DEFAULT %s \
2 // RUN: cppcoreguidelines-narrowing-conversions %t -- \
3 // RUN: -config='{CheckOptions: [ \
4 // RUN: ]}'
5
6 // RUN: %check_clang_tidy -check-suffix=IGNORED %s \
7 // RUN: cppcoreguidelines-narrowing-conversions %t -- \
8 // RUN: -config='{CheckOptions: [ \
9 // RUN: {key: cppcoreguidelines-narrowing-conversions.IgnoreConversionFromTypes, value: "global_size_t;nested_size_type"} \
10 // RUN: ]}'
11
12 // We use global_size_t instead of 'size_t' because windows predefines size_t.
13 typedef long long global_size_t;
14
15 struct vector {
16 typedef long long nested_size_type;
17
sizevector18 global_size_t size() const { return 0; }
19 };
20
narrowing_global_size_t()21 void narrowing_global_size_t() {
22 int i;
23 global_size_t j;
24 i = j;
25 // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'global_size_t' (aka 'long long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
26 // IGNORED: Warning is disabled with IgnoreConversionFromTypes=global_size_t.
27 }
28
narrowing_size_type()29 void narrowing_size_type() {
30 int i;
31 vector::nested_size_type j;
32 i = j;
33 // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'vector::nested_size_type' (aka 'long long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
34 // IGNORED: Warning is disabled with IgnoreConversionFromTypes=nested_size_type.
35 }
36
narrowing_size_method()37 void narrowing_size_method() {
38 vector v;
39 int i, j;
40 i = v.size();
41 // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'global_size_t' (aka 'long long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
42 // IGNORED: Warning is disabled with IgnoreConversionFromTypes=global_size_t.
43
44 i = j + v.size();
45 // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
46 // IGNORED: Warning is disabled with IgnoreConversionFromTypes=global_size_t.
47 }
48
narrowing_size_method_binary_expr()49 void narrowing_size_method_binary_expr() {
50 int i;
51 int j;
52 vector v;
53 i = j + v.size();
54 // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
55 // IGNORED: Warning is disabled with IgnoreConversionFromTypes=global_size_t.
56 }
57
narrowing_size_method_binary_op()58 void narrowing_size_method_binary_op() {
59 int i, j;
60 vector v;
61 i += v.size();
62 // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: narrowing conversion from 'global_size_t' (aka 'long long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
63 // IGNORED: Warning is disabled with IgnoreConversionFromTypes=global_size_t.
64
65 i += j + v.size();
66 // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
67 // IGNORED: Warning is disabled with IgnoreConversionFromTypes=global_size_t.
68 }
69
most_narrowing_is_not_ok()70 void most_narrowing_is_not_ok() {
71 int i;
72 long long j;
73 i = j;
74 // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
75 // CHECK-MESSAGES-IGNORED: :[[@LINE-2]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
76 }
77