| 1 | // RUN: %check_clang_tidy %s bugprone-signed-char-misuse %t \ |
| 2 | // RUN: -config='{CheckOptions: \ |
| 3 | // RUN: {bugprone-signed-char-misuse.CharTypdefsToIgnore: "sal_Int8;int8_t"}}' \ |
| 4 | // RUN: -- |
| 5 | |
| 6 | /////////////////////////////////////////////////////////////////// |
| 7 | /// Test cases correctly caught by the check. |
| 8 | |
| 9 | // Check that a simple test case is still caught. |
| 10 | int SimpleAssignment() { |
| 11 | signed char CCharacter = -5; |
| 12 | int NCharacter; |
| 13 | NCharacter = CCharacter; |
| 14 | // CHECK-MESSAGES: [[@LINE-1]]:16: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse] |
| 15 | |
| 16 | return NCharacter; |
| 17 | } |
| 18 | |
| 19 | typedef signed char sal_Char; |
| 20 | |
| 21 | int TypedefNotInIgnorableList() { |
| 22 | sal_Char CCharacter = 'a'; |
| 23 | int NCharacter = CCharacter; |
| 24 | // CHECK-MESSAGES: [[@LINE-1]]:20: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse] |
| 25 | |
| 26 | return NCharacter; |
| 27 | } |
| 28 | |
| 29 | /////////////////////////////////////////////////////////////////// |
| 30 | /// Test cases correctly ignored by the check. |
| 31 | |
| 32 | typedef signed char sal_Int8; |
| 33 | |
| 34 | int OneIgnorableTypedef() { |
| 35 | sal_Int8 CCharacter = 'a'; |
| 36 | int NCharacter = CCharacter; |
| 37 | |
| 38 | return NCharacter; |
| 39 | } |
| 40 | |
| 41 | typedef signed char int8_t; |
| 42 | |
| 43 | int OtherIgnorableTypedef() { |
| 44 | int8_t CCharacter = 'a'; |
| 45 | int NCharacter = CCharacter; |
| 46 | |
| 47 | return NCharacter; |
| 48 | } |
| 49 | |
| 50 | /////////////////////////////////////////////////////////////////// |
| 51 | /// Test cases which should be caught by the check. |
| 52 | |
| 53 | namespace boost { |
| 54 | |
| 55 | template <class T> |
| 56 | class optional { |
| 57 | T *member; |
| 58 | |
| 59 | public: |
| 60 | optional(T value) { |
| 61 | member = new T(value); |
| 62 | } |
| 63 | |
| 64 | T operator*() { return *member; } |
| 65 | }; |
| 66 | |
| 67 | } // namespace boost |
| 68 | |
| 69 | int DereferenceWithTypdef(boost::optional<sal_Int8> param) { |
| 70 | int NCharacter = *param; |
| 71 | // CHECK-MESSAGES: [[@LINE-1]]:20: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse] |
| 72 | |
| 73 | return NCharacter; |
| 74 | } |
| 75 | |