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