diff options
author | Guillaume Chatelet <gchatelet@google.com> | 2018-11-26 16:25:55 +0000 |
---|---|---|
committer | Guillaume Chatelet <gchatelet@google.com> | 2018-11-26 16:25:55 +0000 |
commit | 10a7ee704417a9af6150c574f32befc32c3e2182 (patch) | |
tree | 1136a948215962280fa959da065ba6aed8d2d741 /clang-tools-extra/test/clang-tidy/cppcoreguidelines-narrowing-conversions-long-is-32bits.cpp | |
parent | b9e4852c92c0fb5cf3ab21d5a9ef3805758233b8 (diff) | |
download | bcm5719-llvm-10a7ee704417a9af6150c574f32befc32c3e2182.tar.gz bcm5719-llvm-10a7ee704417a9af6150c574f32befc32c3e2182.zip |
[clang-tidy] Improving narrowing conversions
Summary:
Newly flagged narrowing conversions:
- integer to narrower signed integer (this is compiler implementation defined),
- integer - floating point narrowing conversions,
- floating point - integer narrowing conversions,
- constants with narrowing conversions (even in ternary operator).
Reviewers: hokein, alexfh, aaron.ballman, JonasToth
Reviewed By: aaron.ballman, JonasToth
Subscribers: lebedev.ri, courbet, nemanjai, xazax.hun, kbarton, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D53488
llvm-svn: 347570
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/cppcoreguidelines-narrowing-conversions-long-is-32bits.cpp')
-rw-r--r-- | clang-tools-extra/test/clang-tidy/cppcoreguidelines-narrowing-conversions-long-is-32bits.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/cppcoreguidelines-narrowing-conversions-long-is-32bits.cpp b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-narrowing-conversions-long-is-32bits.cpp new file mode 100644 index 00000000000..dcf1848a30f --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-narrowing-conversions-long-is-32bits.cpp @@ -0,0 +1,23 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-narrowing-conversions %t \ +// RUN: -- -- -target x86_64-unknown-linux -m32 + +static_assert(sizeof(int) * 8 == 32, "int is 32-bits"); +static_assert(sizeof(long) * 8 == 32, "long is 32-bits"); +static_assert(sizeof(long long) * 8 == 64, "long long is 64-bits"); + +void narrow_integer_to_signed_integer_is_not_ok() { + int i; // i.e. int32_t + long l; // i.e. int32_t + long long ll; // i.e. int64_t + + unsigned int ui; // i.e. uint32_t + unsigned long ul; // i.e. uint32_t + unsigned long long ull; // i.e. uint64_t + + i = l; // int and long are the same type. + i = ll; // int64_t does not fit in an int32_t + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions] + ll = ul; // uint32_t fits into int64_t + ll = ull; // uint64_t does not fit in an int64_t + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'unsigned long long' to signed type 'long long' is implementation-defined [cppcoreguidelines-narrowing-conversions] +} |