Commit Graph

60 Commits

Author SHA1 Message Date
Richard Smith efdb50375f PR35214: don't crash if we see an array of unknown bound added to an empty but invalid designator.
llvm-svn: 318258
2017-11-15 03:03:56 +00:00
Richard Smith d6cc198d53 Improve fix for PR28739
Don't try to map an APSInt addend to an int64_t in pointer arithmetic before
bounds-checking it. This gives more consistent behavior (outside C++11, we
consistently use 2s complement semantics for both pointer and integer overflow
in constant expressions) and fixes some cases where in C++11 we would fail to
properly check for out-of-bounds pointer arithmetic (if the 2s complement
64-bit overflow landed us back in-bounds).

In passing, also fix some cases where we'd perform possibly-overflowing
arithmetic on CharUnits (which have a signed underlying type) during constant
expression evaluation.

llvm-svn: 293595
2017-01-31 02:23:02 +00:00
Richard Smith 642a2365fb PR28739: Check that integer values fit into 64 bits before extracting them as 64 bit values for pointer arithmetic.
This fixes various ways to tickle an assertion in constant expression
evaluation when using __int128. Longer term, we need to figure out what should
happen here: either any kind of overflow in offset calculation should result in
a non-constant value or we should truncate to 64 bits. In C++11 onwards, we're
effectively already checking for overflow because we strictly enforce array
bounds checks, but even there some forms of overflow can slip past undetected.

llvm-svn: 293568
2017-01-30 23:30:26 +00:00
Richard Smith ce8eca578d Explicitly permit undefined behavior in constant initializers for global
variables in C, in the cases where we can constant-fold it to a value
regardless (such as floating-point division by zero and signed integer
overflow). Strictly enforcing this rule breaks too much code.

llvm-svn: 254992
2015-12-08 03:21:47 +00:00
Richard Smith 0c6124ba82 PR17381: Treat undefined behavior during expression evaluation as an unmodeled
side-effect, so that we don't allow speculative evaluation of such expressions
during code generation.

This caused a diagnostic quality regression, so fix constant expression
diagnostics to prefer either the first "can't be constant folded" diagnostic or
the first "not a constant expression" diagnostic depending on the kind of
evaluation we're doing. This was always the intent, but didn't quite work
correctly before.

This results in certain initializers that used to be constant initializers to
no longer be; in particular, things like:

  float f = 1e100;

are no longer accepted in C. This seems appropriate, as such constructs would
lead to code being executed if sanitizers are enabled.

llvm-svn: 254574
2015-12-03 01:36:22 +00:00
David Majnemer c378ca5043 [AST] Don't crash when comparing incomplete object
We cannot tell if an object is past-the-end if its type is incomplete.
Zero sized objects satisfy past-the-end criteria and our object might
turn out to be such an object.

This fixes PR24622.

llvm-svn: 246359
2015-08-29 08:32:55 +00:00
George Burgess IV bdb5b2687a Make __builtin_object_size always answer correctly
__builtin_object_size would return incorrect answers for many uses where
type=3. This fixes the inaccuracy by making us emit 0 instead of LLVM's
objectsize intrinsic.

Additionally, there are many cases where we would emit suboptimal (but
correct) answers, such as when arrays are involved. This patch fixes
some of these cases (please see new tests in test/CodeGen/object-size.c
for specifics on which cases are improved)

Resubmit of r245323 with PR24493 fixed.
Patch mostly by Richard Smith.
Differential Revision: http://reviews.llvm.org/D12000
This fixes PR15212.

llvm-svn: 245403
2015-08-19 02:19:07 +00:00
Nico Weber 19999b4816 Revert r245323, it caused PR24493.
llvm-svn: 245342
2015-08-18 20:32:55 +00:00
George Burgess IV 232c76213d Make __builtin_object_size always answer correctly
__builtin_object_size would return incorrect answers for many uses where
type=3. This fixes the inaccuracy by making us emit 0 instead of LLVM's
objectsize intrinsic.

Additionally, there are many cases where we would emit suboptimal (but
correct) answers, such as when arrays are involved. This patch fixes
some of these cases (please see new tests in test/CodeGen/object-size.c
for specifics on which cases are improved)

Patch mostly by Richard Smith.
Differential Revision: http://reviews.llvm.org/D12000
This fixes PR15212.

llvm-svn: 245323
2015-08-18 18:18:27 +00:00
Jonathan Roelofs 104cbf9c32 Fix PR21945: Crash in constant evaluator.
Patch by Косов Евгений!

llvm-svn: 238758
2015-06-01 16:23:08 +00:00
Richard Trieu 3bb8b56a5d PR16074, implement warnings to catch pointer to boolean true and pointer to
null comparison when the pointer is known to be non-null.

This catches the array to pointer decay, function to pointer decay and
address of variables.  This does not catch address of function since this
has been previously used to silence a warning.

Pointer to bool conversion is under -Wbool-conversion.
Pointer to null comparison is under -Wtautological-pointer-compare, a sub-group
of -Wtautological-compare.

void foo() {
  int arr[5];
  int x;
  // warn on these conditionals
  if (foo);
  if (arr);
  if (&x);
  if (foo == null);
  if (arr == null);
  if (&x == null);

  if (&foo);  // no warning
}

llvm-svn: 202216
2014-02-26 02:36:06 +00:00
Eli Friedman 4eafb6b77b Don't treat overflow in floating-point conversions as a hard error in constant evaluation. <rdar://problem/11874571>.
llvm-svn: 160394
2012-07-17 21:03:05 +00:00
Eli Friedman 2f5b7c542e Per Richard's comments on r154794, add the checks necessary to handle constant-folding relational comparisons safely in case the user is using -fwrapv or equivalent.
llvm-svn: 154849
2012-04-16 19:23:57 +00:00
Eli Friedman 6c31cb4d8d Make constant evaluation for pointer comparisons work correctly for some uncommon cases. <rdar://problem/10962435>.
llvm-svn: 154794
2012-04-16 04:30:08 +00:00
Eli Friedman e9c64d104f Add test for a construct we currently reject, constant-evaluating a load from a constant string. Given that gcc doesn't accept this, we should continue to not accept it, even though it was accidentally supported by clang for a brief period.
llvm-svn: 154564
2012-04-11 23:32:29 +00:00
Eli Friedman ebea9aff0d Fix a crash in the diangostic code in EvalConstant. PR12043.
llvm-svn: 151100
2012-02-21 22:41:33 +00:00
Richard Smith a07ed4addb constexpr: evaluate (bool)&x as true when x is a local variable or a temporary.
llvm-svn: 149045
2012-01-26 04:47:34 +00:00
Richard Smith 42d3af9d95 When folding the size of a global scope VLA to a constant, require the array
bound to not have side effects(!). Add constant-folding support for expressions
of void type, to ensure that we can still fold ((void)0, 1) as an array bound.

llvm-svn: 146000
2011-12-07 00:43:50 +00:00
Richard Smith 10f4d06b81 PR11391: Don't try to evaluate the LHS of a _Complex assignment as an rvalue.
llvm-svn: 144799
2011-11-16 17:22:48 +00:00
Richard Smith cf74da76db Fix PR11385: A pointer constant expression which has been cast via an integer is
not safely derived. Don't allow lvalue-to-rvalue conversions on the result of
dereferencing such a pointer.

llvm-svn: 144783
2011-11-16 07:18:12 +00:00
Eli Friedman 47133bed4a Add missing casts to AST.
llvm-svn: 144455
2011-11-12 03:56:23 +00:00
Richard Smith e9e20dd302 Constant expression evaluation: although we don't know whether a literal will
be at the same address as another object, we do know it won't alias a null
pointer.

llvm-svn: 143674
2011-11-04 01:10:57 +00:00
Richard Smith cecf184e64 When constant-folding, don't look at the initializer of a global const variable
if it's marked as weak: that definition may not end up being used.

llvm-svn: 143496
2011-11-01 21:06:14 +00:00
Richard Smith dd78544d44 Refactoring and test for r143360. Support for array rvalue to pointer decay is
needed for C++11, and will follow later.

llvm-svn: 143363
2011-10-31 20:57:44 +00:00
Richard Smith 390cd49906 Fix assert on constant expression evaluation of floating point increment.
llvm-svn: 143320
2011-10-30 23:17:09 +00:00
Richard Smith 9c8d1c5c64 Don't crash if a GCC binary conditional is used in a constant expression on an
integer-cast pointer value.

llvm-svn: 143299
2011-10-29 22:55:55 +00:00
Richard Smith 472d495372 Fix assertion in constant expression evaluation. The LHS of a floating-point
binary operator isn't an rvalue if it's an assignment operator.

llvm-svn: 143250
2011-10-28 23:26:52 +00:00
Chris Lattner f35de48c90 when compiling in a GNU mode (e.g. gnu99) treat VLAs with a size that can be folded to a constant
as constant size arrays.  This has slightly different semantics in some insane cases, but allows
us to accept some constructs that GCC does.  Continue to be pedantic in -std=c99 and other
modes.  This addressed rdar://8733881 - error "variable-sized object may not be initialized"; g++ accepts same code

llvm-svn: 132983
2011-06-14 06:38:10 +00:00
Carl Norum 58d489fc6e Fix tests to account for new warning "expected ';' at end of declaration list". Sorry, folks!
llvm-svn: 127188
2011-03-07 22:57:45 +00:00
Argyrios Kyrtzidis e84389bf68 Properly do a float -> _Complex double conversion, fixes rdar://8875946.
llvm-svn: 123759
2011-01-18 18:49:33 +00:00
Chris Lattner 17c0eac879 fix PR7885, rejecting invalid uses of __builtin_constant_p.
llvm-svn: 116317
2010-10-12 17:47:42 +00:00
Eli Friedman 9571953f36 PR7884: Fix the implementations of __real__ and __imag__ on real floats.
llvm-svn: 111080
2010-08-14 20:52:13 +00:00
Douglas Gregor cb2b662283 Make the "unused result" warning a warning about run-time behavior, so
that we don't warn when there isn't going to be any computation anyway.

llvm-svn: 108442
2010-07-15 18:47:04 +00:00
Argyrios Kyrtzidis 639ffb0c07 Fix rdar://8139785 "implement warning on dead expression in comma operator"
As a bonus, fix the warning for || and && operators; it was emitted even if one of the operands had side effects, e.g:

x || test_logical_foo1();

emitted a bogus "expression result unused" for 'x'.

llvm-svn: 107274
2010-06-30 10:53:14 +00:00
Eli Friedman d5c9399696 Fix for PR6274: teach constant folding to evaluate __builtin_expect.
llvm-svn: 96054
2010-02-13 00:10:10 +00:00
Daniel Dunbar 8fbe78f6fc Update tests to use %clang_cc1 instead of 'clang-cc' or 'clang -cc1'.
- 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).

llvm-svn: 91446
2009-12-15 20:14:24 +00:00
Eli Friedman 141fbf3f36 Add constant evaluation for comma operator with floating-point operand. Fixes
PR5449.

llvm-svn: 88885
2009-11-16 04:25:37 +00:00
Sebastian Redl 87869bc435 Make ASTContext::getIntWidth return 1 for all boolean type variations, not just for the unqualified, unaliased bool.
llvm-svn: 86174
2009-11-05 21:10:57 +00:00
Eli Friedman 334046a134 PR4351: Add constant evaluation for constructs like "foo == NULL", where
foo has a constant address.

llvm-svn: 73321
2009-06-14 02:17:33 +00:00
Eli Friedman 4a4fefcd29 PR4326: Handle constant evaluation for void* pointer subtraction
correctly.

llvm-svn: 72886
2009-06-04 20:04:03 +00:00
Eli Friedman a38da57cd6 PR4097: add logic to Evaluate to handle pointer equality comparisons.
llvm-svn: 70317
2009-04-28 19:17:36 +00:00
Eli Friedman d3a5a9d7fa Add handling for complex->int, int->complex float, and float->complex
int.  Note that constant int->complex float and float->complex int casts
were being miscompiled.

llvm-svn: 69821
2009-04-22 19:23:09 +00:00
Chris Lattner 1f02e054a9 Fix PR4027 + rdar://6808859, we were rejecting implicit casts of
aggregates even though we already accept explicit ones.  Easy fix.

llvm-svn: 69661
2009-04-21 05:19:11 +00:00
Daniel Dunbar a45cf5b6b0 Rename clang to clang-cc.
Tests and drivers updated, still need to shuffle dirs.

llvm-svn: 67602
2009-03-24 02:24:46 +00:00
Eli Friedman 6400433980 Minor enhancements to Evaluate.
llvm-svn: 67503
2009-03-23 04:38:34 +00:00
Anders Carlsson 6c885805cd Fix invalid VLAs/VMs in Sema::ActOnVariableDeclarator, so that the variable will have the right type by the time the initializer is checked. This ensures that code like
int a[(int)(1.0 / 1.0) = { 1 } will work.

Eli, please review.
 

llvm-svn: 65725
2009-02-28 21:56:50 +00:00
Eli Friedman a1c7b6c5f6 Fix obvious shortcoming in the implementations of Evaluate for
integer __real__ and __imag__.  Not sure how I missed this.

llvm-svn: 65677
2009-02-28 03:59:05 +00:00
Daniel Dunbar 79e042a8b5 Evaluation of unary deref could call integer evaluator on non-integral
expr; hilarity ensued.
 - PR3640.

llvm-svn: 65234
2009-02-21 18:14:20 +00:00
Eli Friedman 0b8337c30b Add support for * (unary dereference) operator to ExprConstant.
llvm-svn: 65105
2009-02-20 01:57:15 +00:00
Anders Carlsson 0756c97dae Emit the correct diagnostics when we constant fold an array size to a negative value.
llvm-svn: 65023
2009-02-19 06:30:50 +00:00