mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-17 06:46:36 +00:00

Previously, it only ever fired for zeros which formed null pointers. Now, hilariously, in C++98 this was almost anything. Including tricks like warning on the divisor in this code: typedef char c3[3]; size_t f(c3* ptr) { return (sizeof(ptr) / sizeof(*ptr)) / (size_t)(!(sizeof(ptr) % sizeof(*ptr))); } Why the RHS of the outer divide is a null pointer constant is a sordid tale of sorrow. Anyways, the committee fixed this for C++11 and onward as part of core isssue 903, and Richard recently implemented this fix causing the warning to go away here (and elsewhere). This patch restores the warning here and adds it for numerous other somewhat obvious gaffes: int g(int x) { return x / (int)(0.0); } The patch is essentially just using the full power of our constant folding in Clang to produce the warning, but insisting that it must fold to an *integer* which is zero so that we don't get false positives anywhere. llvm-svn: 183970
22 lines
1.0 KiB
C++
22 lines
1.0 KiB
C++
// RUN: %clang_cc1 -verify %s
|
|
// RUN: %clang_cc1 -std=c++11 -verify %s
|
|
// RUN: %clang_cc1 -std=c++1y -verify %s
|
|
|
|
void div() {
|
|
(void)(42 / 0); // expected-warning{{division by zero is undefined}}
|
|
(void)(42 / false); // expected-warning{{division by zero is undefined}}
|
|
(void)(42 / !1); // expected-warning{{division by zero is undefined}}
|
|
(void)(42 / (1 - 1)); // expected-warning{{division by zero is undefined}}
|
|
(void)(42 / !(1 + 1)); // expected-warning{{division by zero is undefined}}
|
|
(void)(42 / (int)(0.0)); // expected-warning{{division by zero is undefined}}
|
|
}
|
|
|
|
void rem() {
|
|
(void)(42 % 0); // expected-warning{{remainder by zero is undefined}}
|
|
(void)(42 % false); // expected-warning{{remainder by zero is undefined}}
|
|
(void)(42 % !1); // expected-warning{{remainder by zero is undefined}}
|
|
(void)(42 % (1 - 1)); // expected-warning{{remainder by zero is undefined}}
|
|
(void)(42 % !(1 + 1)); // expected-warning{{remainder by zero is undefined}}
|
|
(void)(42 % (int)(0.0)); // expected-warning{{remainder by zero is undefined}}
|
|
}
|