forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Diagnostics] Warn if ?: with integer constants always evaluates to true
Extracted from D63082. GCC has this warning under -Wint-in-bool-context, but as noted in the D63082's review, we should put it under TautologicalConstantCompare. llvm-svn: 372531
- Loading branch information
1 parent
914c4c3
commit fb21817
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// RUN: %clang_cc1 -x c -fsyntax-only -verify -Wtautological-constant-compare %s | ||
// RUN: %clang_cc1 -x c -fsyntax-only -verify %s | ||
// RUN: %clang_cc1 -x c++ -fsyntax-only -verify -Wtautological-constant-compare %s | ||
// RUN: %clang_cc1 -x c++ -fsyntax-only -verify %s | ||
|
||
#define ONE 1 | ||
#define TWO 2 | ||
|
||
#define TERN(c, l, r) c ? l : r | ||
|
||
#ifdef __cplusplus | ||
typedef bool boolean; | ||
#else | ||
typedef _Bool boolean; | ||
#endif | ||
|
||
void test(boolean a) { | ||
boolean r; | ||
r = a ? (1) : TWO; | ||
r = a ? 3 : TWO; // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}} | ||
r = a ? -2 : 0; // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}} | ||
r = a ? 3 : -2; // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}} | ||
r = a ? 0 : TWO; | ||
r = a ? 3 : ONE; // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}} | ||
r = a ? ONE : 0; | ||
r = a ? 0 : -0; | ||
r = a ? 1 : 0; | ||
r = a ? ONE : 0; | ||
r = a ? ONE : ONE; | ||
r = TERN(a, 4, 8); // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}} | ||
r = TERN(a, -1, -8); // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}} | ||
} |