summaryrefslogtreecommitdiff
path: root/test/Sema/compare.c
Commit message (Collapse)AuthorAgeFilesLines
* [Diagnostics] Support -Wtype-limits for GCC compatibilityDavid Bolvansky2019-04-291-0/+1
| | | | | | | | | | | | | | | | | | Summary: GCC's -Wtype-limits (part of -Wextra): Warn if a comparison is always true or always false due to the limited range of the data type Reviewers: rsmith, aaron.ballman, lebedev.ri, thakis Reviewed By: rsmith Subscribers: lebedev.ri, jdoerfert, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D58841 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@359516 91177308-0d34-0410-b5e6-96231b3b80d8
* [PR36008] Avoid -Wsign-compare warning for enum constants inAlex Lorenz2018-02-071-0/+13
| | | | | | | | | | | | | | typeof expressions This commit looks through typeof type at the original expression when diagnosing -Wsign-compare to avoid an unfriendly diagnostic. rdar://36588828 Differential Revision: https://reviews.llvm.org/D42561 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@324514 91177308-0d34-0410-b5e6-96231b3b80d8
* [Sema] -Wtautological-constant-compare is too good. Cripple it.Roman Lebedev2018-01-031-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: The diagnostic was mostly introduced in D38101 by me, as a reaction to wasting a lot of time, see [[ https://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20171009/206427.html | mail ]]. However, the diagnostic is pretty dumb. While it works with no false-positives, there are some questionable cases that are diagnosed when one would argue that they should not be. The common complaint is that it diagnoses the comparisons between an `int` and `long` when compiling for a 32-bit target as tautological, but not when compiling for 64-bit targets. The underlying problem is obvious: data model. In most cases, 64-bit target is `LP64` (`int` is 32-bit, `long` and pointer are 64-bit), and the 32-bit target is `ILP32` (`int`, `long`, and pointer are 32-bit). I.e. the common pattern is: (pseudocode) ``` #include <limits> #include <cstdint> int main() { using T1 = long; using T2 = int; T1 r; if (r < std::numeric_limits<T2>::min()) {} if (r > std::numeric_limits<T2>::max()) {} } ``` As an example, D39149 was trying to fix this diagnostic in libc++, and it was not well-received. This *could* be "fixed", by changing the diagnostics logic to something like `if the types of the values being compared are different, but are of the same size, then do diagnose`, and i even attempted to do so in D39462, but as @rjmccall rightfully commented, that implementation is incomplete to say the least. So to stop causing trouble, and avoid contaminating upcoming release, lets do this workaround: * move these three diags (`warn_unsigned_always_true_comparison`, `warn_unsigned_enum_always_true_comparison`, `warn_tautological_constant_compare`) into it's own `-Wtautological-constant-in-range-compare` * Disable them by default * Make them part of `-Wextra` * Additionally, give `warn_tautological_constant_compare` it's own flag `-Wtautological-type-limit-compare`. I'm not happy about that name, but i can't come up with anything better. This way all three of them can be enabled/disabled either altogether, or one-by-one. Reviewers: aaron.ballman, rsmith, smeenai, rjmccall, rnk, mclow.lists, dim Reviewed By: aaron.ballman, rsmith, dim Subscribers: thakis, compnerd, mehdi_amini, dim, hans, cfe-commits, rjmccall Tags: #clang Differential Revision: https://reviews.llvm.org/D41512 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@321691 91177308-0d34-0410-b5e6-96231b3b80d8
* [Sema] Move some stuff into -Wtautological-unsigned-enum-zero-compareRoman Lebedev2017-09-201-1/+52
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Recommit. Original commit was reverted because buildbots broke. The error was only reproducible in the build with assertions. The problem was that the diagnostic expected true/false as bool, while it was provided as string "true"/"false". Summary: As requested by Sam McCall: > Enums (not new I guess). Typical case: if (enum < 0 || enum > MAX) > The warning strongly suggests that the enum < 0 check has no effect > (for enums with nonnegative ranges). > Clang doesn't seem to optimize such checks out though, and they seem > likely to catch bugs in some cases. Yes, only if there's UB elsewhere, > but I assume not optimizing out these checks indicates a deliberate > decision to stay somewhat compatible with a technically-incorrect > mental model. > If this is the case, should we move these to a > -Wtautological-compare-enum subcategory? Reviewers: rjmccall, rsmith, aaron.ballman, sammccall, bkramer, djasper Reviewed By: aaron.ballman Subscribers: jroelofs, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D37629 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@313745 91177308-0d34-0410-b5e6-96231b3b80d8
* Revert "[Sema] Move some stuff into -Wtautological-unsigned-enum-zero-compare"Roman Lebedev2017-09-191-52/+1
| | | | | | | | | | | | | | | | This reverts commit r313677. Buildbots fail with assertion failure Failing Tests (7): Clang :: Analysis/null-deref-ps.c Clang :: CodeGen/enum.c Clang :: Sema/compare.c Clang :: Sema/outof-range-constant-compare.c Clang :: Sema/tautological-unsigned-enum-zero-compare.c Clang :: Sema/tautological-unsigned-zero-compare.c Clang :: SemaCXX/compare.cpp git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@313683 91177308-0d34-0410-b5e6-96231b3b80d8
* [Sema] Move some stuff into -Wtautological-unsigned-enum-zero-compareRoman Lebedev2017-09-191-1/+52
| | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: As requested by Sam McCall: > Enums (not new I guess). Typical case: if (enum < 0 || enum > MAX) > The warning strongly suggests that the enum < 0 check has no effect > (for enums with nonnegative ranges). > Clang doesn't seem to optimize such checks out though, and they seem > likely to catch bugs in some cases. Yes, only if there's UB elsewhere, > but I assume not optimizing out these checks indicates a deliberate > decision to stay somewhat compatible with a technically-incorrect > mental model. > If this is the case, should we move these to a > -Wtautological-compare-enum subcategory? Reviewers: rjmccall, rsmith, aaron.ballman, sammccall, bkramer, djasper Reviewed By: aaron.ballman Subscribers: jroelofs, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D37629 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@313677 91177308-0d34-0410-b5e6-96231b3b80d8
* [Sema] -Wtautological-compare: handle comparison of unsigned with 0S.Roman Lebedev2017-09-071-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: This is a first half(?) of a fix for the following bug: https://bugs.llvm.org/show_bug.cgi?id=34147 (gcc -Wtype-limits) GCC's -Wtype-limits does warn on comparison of unsigned value with signed zero (as in, with 0), but clang only warns if the zero is unsigned (i.e. 0U). Also, be careful not to double-warn, or falsely warn on comparison of signed/fp variable and signed 0. Yes, all these testcases are needed. Testing: $ ninja check-clang-sema check-clang-semacxx Also, no new warnings for clang stage-2 build. Reviewers: rjmccall, rsmith, aaron.ballman Reviewed By: rjmccall Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D37565 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@312750 91177308-0d34-0410-b5e6-96231b3b80d8
* Enhance -Wtautological-constant-out-of-range-compare to include the name of ↵Ted Kremenek2013-03-151-8/+8
| | | | | | | | the enum constant. This is QoI. Fixes <rdar://problem/13076064>. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@177190 91177308-0d34-0410-b5e6-96231b3b80d8
* Improvements to my patch in r164143 perFariborz Jahanian2012-09-201-16/+16
| | | | | | | Richard's comments. // rdar://12202422 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@164316 91177308-0d34-0410-b5e6-96231b3b80d8
* c: warn when an integer value comparison with anFariborz Jahanian2012-09-181-16/+16
| | | | | | | | | integral expression have the obvious result. Patch reviewed by John McCall off line. // rdar://12202422 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@164143 91177308-0d34-0410-b5e6-96231b3b80d8
* Turn the mixed-sign-comparison diagnostic into a runtime behaviorDouglas Gregor2012-05-011-0/+7
| | | | | | | diagnostic, from Eitan Adler! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@155876 91177308-0d34-0410-b5e6-96231b3b80d8
* Enhance the -Wsign-compare handling to suppress the -Wsign-compare warning ↵Eli Friedman2011-12-151-0/+6
| | | | | | in the case of a shifted bitfield. PR11572. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@146634 91177308-0d34-0410-b5e6-96231b3b80d8
* Make the integer-range analysis recognize ^= correctly,John McCall2011-07-131-0/+15
| | | | | | | | | | | and (while I'm at it) teach it to grok the results of simple assignments. The first is PR10336. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@135034 91177308-0d34-0410-b5e6-96231b3b80d8
* Provide a slightly specialized diagnostic for tautological comparisonsJohn McCall2010-10-061-0/+7
| | | | | | | | of an enum value. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@115725 91177308-0d34-0410-b5e6-96231b3b80d8
* When warning about comparing an unsigned int to being >= 0, don't issue a ↵Ted Kremenek2010-09-231-0/+17
| | | | | | | | | | warning if the zero value was an enum or was expanded from a macro. Fixes: <rdar://problem/8414119> git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@114695 91177308-0d34-0410-b5e6-96231b3b80d8
* Warn about comparisons between arrays and improve self-comparisonDouglas Gregor2010-06-081-2/+3
| | | | | | | warnings, from Troy Straszheim! Fixes PR6163. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@105631 91177308-0d34-0410-b5e6-96231b3b80d8
* Rearchitect -Wconversion and -Wsign-compare. Instead of computing themJohn McCall2010-05-061-10/+10
| | | | | | | | | | | | | | | | "bottom-up" when implicit casts and comparisons are inserted, compute them "top-down" when the full expression is finished. Makes it easier to coordinate warnings and thus implement -Wconversion for signedness conversions without double-warning with -Wsign-compare. Also makes it possible to realize that a signedness conversion is okay because the context is performing the inverse conversion. Also simplifies some logic that was trying to calculate the ultimate comparison/result type and getting it wrong. Also fixes a problem with the C++ explicit casts which are often "implemented" in the AST with a series of implicit cast expressions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@103174 91177308-0d34-0410-b5e6-96231b3b80d8
* Teach -Wsign-compare to treat 1 << blah as "idiomatically non-negative".John McCall2010-04-071-0/+5
| | | | | | | | Fixes a spurious warning in LLVM. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@100595 91177308-0d34-0410-b5e6-96231b3b80d8
* Warn about comparing an unsigned expression with 0 in tautological ways.John McCall2010-03-111-0/+8
| | | | | | | | Patch by mikem! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@98279 91177308-0d34-0410-b5e6-96231b3b80d8
* Insulate these from changes to the default for -Wunreachable-code.Mike Stump2010-01-231-1/+1
| | | | git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@94326 91177308-0d34-0410-b5e6-96231b3b80d8
* Don't assert when dealing with unsigned casts of lvalues. Fixes PR5961.John McCall2010-01-061-0/+8
| | | | git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@92866 91177308-0d34-0410-b5e6-96231b3b80d8
* Significantly rework the calculation of effective integer-expression rangesJohn McCall2010-01-061-0/+33
| | | | | | | | | | | | | | for -Wsign-compare and -Wconversion, and use that coordinated logic to drive both diagnostics. The new logic works more transparently with implicit conversions, conditional operators, etc., as well as bringing -Wconversion's ability to deal with pseudo-closed operations (e.g. arithmetic on shorts) to -Wsign-compare. Fixes PRs 5887, 5937, 5938, and 5939. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@92823 91177308-0d34-0410-b5e6-96231b3b80d8
* -Wsign-compare shouldn't warn when the signed operand is a conditional operatorJohn McCall2010-01-041-0/+3
| | | | | | | | | whose operands are non-negative integer constant expressions. This comes up in LLVM in a few places. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@92525 91177308-0d34-0410-b5e6-96231b3b80d8
* Update tests to use %clang_cc1 instead of 'clang-cc' or 'clang -cc1'.Daniel Dunbar2009-12-151-1/+1
| | | | | | | | | - This is designed to make it obvious that %clang_cc1 is a "test variable" which is substituted. It is '%clang_cc1' instead of '%clang -cc1' because it can be useful to redefine what gets run as 'clang -cc1' (for example, to set a default target). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@91446 91177308-0d34-0410-b5e6-96231b3b80d8
* First pass at implementing C++ enum semantics: calculate (and store) anJohn McCall2009-12-091-0/+5
| | | | | | | | | | | | | | | "integer promotion" type associated with an enum decl, and use this type to determine which type to promote to. This type obeys C++ [conv.prom]p2 and is therefore generally signed unless the range of the enumerators forces it to be unsigned. Kills off a lot of false positives from -Wsign-compare in C++, addressing rdar://7455616 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@90965 91177308-0d34-0410-b5e6-96231b3b80d8
* compare.c also needs a target triple now, and improve some comments while we'reJohn McCall2009-11-061-1/+1
| | | | | | | | at it. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86243 91177308-0d34-0410-b5e6-96231b3b80d8
* Improve the -Wsign-compare heuristics:John McCall2009-11-061-17/+188
| | | | | | | | | | | | | | * If the unsigned type is smaller than the signed type, never warn, because its value will not change when zero-extended to the larger type. * If we're testing for (in)equality, and the unsigned value is an integer constant whose sign bit is not set, never warn, because even though the signed value might change, it can't affect the result of the equality. Also make the comparison test cases much more rigorous, and have them expose the subtle differences between C and C++ here. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86242 91177308-0d34-0410-b5e6-96231b3b80d8
* Turn off -Wsign-compare warnings by defaultDouglas Gregor2009-11-061-1/+1
| | | | git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86233 91177308-0d34-0410-b5e6-96231b3b80d8
* Implement the conditional-operator part of -Wsign-compare. TurnJohn McCall2009-11-051-0/+12
| | | | | | | | | | | | | | DiagnoseSignCompare into Sema::CheckSignCompare and call it from more places. Add some enumerator tests. These seem to expose some oddities in the types we're converting C++ enumerators to; in particular, they're converting to unsigned before int, which seems to contradict 4.5 [conv.prom] p2. Note to self: stop baiting Doug in my commit messages. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86128 91177308-0d34-0410-b5e6-96231b3b80d8
* Implement -Wsign-compare, or at least the actual comparison part of it.John McCall2009-11-051-0/+8
| | | | | | | | | | Conditional operands are next. Fixes part of rdar://problem/7289584. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86083 91177308-0d34-0410-b5e6-96231b3b80d8
* Remove unnecessary include.Eli Friedman2009-08-271-2/+0
| | | | git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@80275 91177308-0d34-0410-b5e6-96231b3b80d8
* Catch a few more cases of illegal comparisons.Eli Friedman2009-08-231-4/+6
| | | | git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@79793 91177308-0d34-0410-b5e6-96231b3b80d8
* Eli points out that we really must diagnose "void* > 0" as an extension. Chris Lattner2009-08-231-2/+4
| | | | | | | | | | Explicitly add it as an EXTENSION instead of an EXTWARN so that it only comes out with -pedantic. Thanks Eli! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@79791 91177308-0d34-0410-b5e6-96231b3b80d8
* tweak some pointer sema checking stuff (which was added to implement PR4175) to Chris Lattner2009-08-221-9/+5
| | | | | | | | | | | | avoid emitting a warning on "someptr > 0". This is obviously questionable (they could use != instead) but is reasonable, and the warning "ordered comparison between pointer and integer" didn't make a ton of sense because 0 is a valid null pointer constant. Just silence the warning in this case, it is unlikely to indicate a bug. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@79743 91177308-0d34-0410-b5e6-96231b3b80d8
* Fix a problem with false diagnostics when comparing distinct NULL pointer ↵Douglas Gregor2009-07-061-0/+7
| | | | | | types, from David Majnemer git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@74850 91177308-0d34-0410-b5e6-96231b3b80d8
* Implement PR4175, catching some questionable comparisons. Patch byChris Lattner2009-06-301-0/+14
| | | | | | | David Majnemer! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@74513 91177308-0d34-0410-b5e6-96231b3b80d8
* Rename clang to clang-cc.Daniel Dunbar2009-03-241-1/+1
| | | | | | | Tests and drivers updated, still need to shuffle dirs. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67602 91177308-0d34-0410-b5e6-96231b3b80d8
* Be a bit stricter about array type compatibility.Eli Friedman2008-02-061-1/+6
| | | | git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@46799 91177308-0d34-0410-b5e6-96231b3b80d8
* rename -parse-ast-print to -ast-printChris Lattner2007-10-111-1/+1
| | | | | | | | | rename -parse-ast-dump to -ast-dump remove -parse-ast, which is redundant with -fsyntax-only git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42852 91177308-0d34-0410-b5e6-96231b3b80d8
* Removed option "-parse-ast-check" from clang driver. This is now implementedTed Kremenek2007-09-261-1/+1
| | | | | | | | | | | | | | using "-parse-ast -verify". Updated all test cases (using a sed script) that invoked -parse-ast-check to now use -parse-ast -verify. Fixed a bug where using "-verify" instead of "-parse-ast-check" would not correctly create the DiagClient needed to accumulate diagnostics. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42365 91177308-0d34-0410-b5e6-96231b3b80d8
* Add Type::getAsBuiltinType() and Type::builtinTypesAreCompatible().Steve Naroff2007-08-271-0/+4
| | | | | | | | | | | | | | | | | | Modified Type::typesAreCompatible() to use the above. This fixes the following bug submitted by Keith Bauer (thanks!). int equal(char *a, const char *b) { return a == b; } Also tweaked Sema::CheckCompareOperands() to ignore the qualifiers when comparing two pointer types (though it doesn't relate directly to this bug). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41476 91177308-0d34-0410-b5e6-96231b3b80d8
* Cases like this:Chris Lattner2007-08-261-0/+8
char *C; C != ((void*)0); Should not warn about incompatible pointer types. Also, make sure to insert an implicit conversion even if the operand is null. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41408 91177308-0d34-0410-b5e6-96231b3b80d8