Commit Graph

3716 Commits

Author SHA1 Message Date
Richard Smith 84f6dcf2b5 constexpr:
* support the gcc __builtin_constant_p() ? ... : ... folding hack in C++11
  * check for unspecified values in pointer comparisons and pointer subtractions

llvm-svn: 149578
2012-02-02 01:16:57 +00:00
Douglas Gregor 2e15c844a5 Make sure that imported definitions get completed before we add
anything into the corresponding DeclContext. Co-hacked with Sean;
fixes <rdar://problem/10768928>.

llvm-svn: 149535
2012-02-01 21:00:38 +00:00
Anna Zaks 314cd09b5c Add a new compiler warning, which flags anti-patterns used as the size
argument in strncat.

The warning is ignored by default since it needs more qualification. 

TODO: The warning message and the note are messy when
strncat is a builtin due to the macro expansion.

llvm-svn: 149524
2012-02-01 19:08:57 +00:00
Richard Smith 1b470417e4 constexpr: check for overflow in pointer subtraction.
This is a mess. According to the C++11 standard, pointer subtraction only has
undefined behavior if the difference of the array indices does not fit into a
ptrdiff_t.

However, common implementations effectively perform a char* subtraction first,
and then divide the result by the element size, which can cause overflows in
some cases. Those cases are not considered to be undefined behavior by this
change; perhaps they should be.

llvm-svn: 149490
2012-02-01 08:10:20 +00:00
Argyrios Kyrtzidis c3c9ee5623 Remove redundant checks in CXXRecordDecl::isCLike(), as suggested by Sebastian.
llvm-svn: 149476
2012-02-01 06:36:44 +00:00
Richard Smith c8042323e1 constexpr: overflow checking for integral and floating-point arithmetic.
llvm-svn: 149473
2012-02-01 05:53:12 +00:00
Richard Smith 47b349328b constexpr: require 'this' to point to an object in a constexpr method call.
llvm-svn: 149467
2012-02-01 02:39:43 +00:00
Richard Smith 7bb0067c06 constexpr: add support for comparisons of pointer-to-members.
llvm-svn: 149463
2012-02-01 01:42:44 +00:00
Richard Smith 000e9aa7ed constexpr: Treat INT_MIN % -1 as undefined behavior in C++11. Technically, it
isn't, but this is just a (reported) defect in the wording.

llvm-svn: 149448
2012-01-31 23:24:19 +00:00
Matt Beaumont-Gay ecc05b0dc3 Pacify gcc's -Wreturn-type.
A separate unreachable message will make it easier to debug if either of the
unreachables is reached.

llvm-svn: 149402
2012-01-31 18:59:25 +00:00
Richard Smith de21b245c6 constexpr: the result of a relational operator between pointers to void is
unspecified unless the pointers are equal; therefore, such a comparison is not
a constant expression unless the pointers are equal.

llvm-svn: 149366
2012-01-31 06:41:30 +00:00
Richard Smith fe800031ec constexpr: catch a collection of integral undefined behaviors:
-INT_MIN and INT_MIN / -1
  Shift by a negative or too large quantity
  Left shift of negative value
  Overflow in left shift

llvm-svn: 149344
2012-01-31 04:08:20 +00:00
Richard Smith 006bfc91e8 constexpr: remove integral conversion overflow checking introduced in r149286.
As Eli points out, this is implementation-defined, and the way we define it
makes this fine.

llvm-svn: 149327
2012-01-31 01:47:46 +00:00
Eli Friedman 290e6ba165 Clean up switch in Expr::CanThrow. No functional change.
llvm-svn: 149321
2012-01-31 01:21:45 +00:00
Richard Smith 911e142f03 constexpr: disallow signed integer overflow in integral conversions in constant
expressions in C++11.

llvm-svn: 149286
2012-01-30 22:27:01 +00:00
John McCall a023b6924a Per discussion on cxx-abi-dev, don't drop leading zeroes from the
mangling of floating-point literals.  I just went ahead and
reimplemented toString() here;  if someone wants to generalize
the library routine to do this, or feels strongly that we should
be post-processing, please feel free.

llvm-svn: 149256
2012-01-30 18:36:31 +00:00
Douglas Gregor 44ba7899c4 Allow the external AST source to provide a layout without specifying
the alignment (because it's not encoded in DWARF). In this case, make
an educated guess at the alignment.

llvm-svn: 149161
2012-01-28 00:53:29 +00:00
Abramo Bagnara 7945c981b9 Added source location for the template keyword in AST template-id expressions.
llvm-svn: 149127
2012-01-27 09:46:47 +00:00
Richard Smith 253c2a390a constexpr: Implement the [dcl.constexpr]p5 check for whether a constexpr
function definition can produce a constant expression. This also provides the
last few checks for [dcl.constexpr]p3 and [dcl.constexpr]p4.

llvm-svn: 149108
2012-01-27 01:14:48 +00:00
Argyrios Kyrtzidis b2e31e4443 Change HasMutableFields to HasOnlyCMembers and consider that a tag inside
another tag does not break C-like-ness. rdar://10756831

llvm-svn: 149071
2012-01-26 18:28:08 +00:00
Fariborz Jahanian 194904e499 objc: 'id' and block pointer compare in mergeTypes is
made symmetrical. // rdar://10734265

llvm-svn: 149065
2012-01-26 17:08:50 +00:00
NAKAMURA Takumi b8efa1e091 AST/ExprConstant.cpp: Silence a warning on ms cl.exe. "bool" does not prefer to be compared to integer.
llvm-svn: 149059
2012-01-26 09:37:36 +00:00
Douglas Gregor e9fc377a44 Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.

The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.

Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.

Fixes the Clang side of <rdar://problem/10169539>.

llvm-svn: 149055
2012-01-26 07:55:45 +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
Peter Collingbourne dad62f1bce Simplify {Record,Enum}Type::classof.
llvm-svn: 149038
2012-01-26 03:33:46 +00:00
Fariborz Jahanian 26d8371b07 objc: allow block pointer matching 'id' type when
they show up as argument types of two block pointers.
// rdar://10734265

llvm-svn: 149007
2012-01-26 00:45:38 +00:00
Richard Smith 1b78b3d1b1 constexpr: add support for anonymous struct and union members in literal types.
llvm-svn: 148987
2012-01-25 22:15:11 +00:00
Richard Smith 928be491e0 Fix PR11848: decree that an alias template contains an unexpanded parameter pack
iff its substitution contains an unexpanded parameter pack. This has the effect
that we now reject declarations such as this (which we used to crash when
expanding):

  template<typename T> using Int = int;
  template<typename ...Ts> void f(Int<Ts> ...ints);

The standard is inconsistent on how this case should be treated.

llvm-svn: 148905
2012-01-25 02:14:59 +00:00
Douglas Gregor 63db9717a5 Only try to import a definition if there is an definition to import.
llvm-svn: 148894
2012-01-25 01:13:20 +00:00
Douglas Gregor 95d8283931 When importing a RecordDecl as a DeclContext, make sure that we pull
in the definition as well.

llvm-svn: 148831
2012-01-24 18:36:04 +00:00
Douglas Gregor 2aa53777e7 Teach the AST importer about redeclaration chains for Objective-C
classes and protocols, implementing lazy-import semantics for both.

llvm-svn: 148816
2012-01-24 17:42:07 +00:00
Argyrios Kyrtzidis 034e83ca17 In CXXRecordDecl::isCLike(), also check for PODness.
llvm-svn: 148765
2012-01-24 01:37:11 +00:00
Argyrios Kyrtzidis 5c4e065a9a Introduce CXXRecordDecl::isCLike() that is true if the class is C-like,
without C++-specific features.

Use it to set the language to C++ when indexing non-C-like structs.
rdar://10732579

llvm-svn: 148708
2012-01-23 16:58:45 +00:00
David Blaikie e4d798f078 More dead code removal (using -Wunreachable-code)
llvm-svn: 148577
2012-01-20 21:50:17 +00:00
Ted Kremenek f2a2f5f2bf Add ability to specifiy 'restrict' on parameters of builtins, and correct this oversight for scanf functions.
llvm-svn: 148573
2012-01-20 21:40:12 +00:00
Richard Smith 66e05fe630 Move narrowing conversion detection code from SemaInit to SemaOverload, ready
for it to be used in converted constant expression checking, and fix a couple
of issues:
 - Conversion operators implicitly invoked prior to the narrowing conversion
   were not being correctly handled when determining whether a constant value
   was narrowed.
 - For conversions from floating-point to integral types, the diagnostic text
   incorrectly always claimed that the source expression was not a constant
   expression.

llvm-svn: 148381
2012-01-18 05:21:49 +00:00
Richard Smith 9cf080fba3 A call to strlen is not a constant expression, even if we're treating it as a
builtin.

llvm-svn: 148374
2012-01-18 03:06:12 +00:00
Anna Zaks 28db7ceabd Constify FunctionDecl::getmemoryFunctionKind().
llvm-svn: 148369
2012-01-18 02:45:01 +00:00
Richard Smith 4ef685becf Enable constant evaluation of implicit calls to constexpr conversion operators.
llvm-svn: 148333
2012-01-17 21:17:26 +00:00
Douglas Gregor d53ae83516 Delay the creation of the built-in Objective-C class 'Protocol' by
moving it from a "special type" to a predefined declaration, as we do
for id, Class, and SEL.

llvm-svn: 148313
2012-01-17 18:09:05 +00:00
David Blaikie 8a40f700e6 Remove unreachable code in Clang. (replace with llvm_unreachable where appropriate or when GCC requires it)
llvm-svn: 148292
2012-01-17 06:56:22 +00:00
David Blaikie f47fa304a4 Remove unnecessary default cases in switches over enums.
This allows -Wswitch-enum to find switches that need updating when these enums are modified.

llvm-svn: 148281
2012-01-17 02:30:50 +00:00
Anna Zaks 2212270e71 Use Builtin ID as the return value
for FunctionDecl::getMemoryFunctionKind().

This is a follow up on the Chris's review for r148142: We don't want to
pollute FunctionDecl with an extra enum. (To make this work, added
memcmp and family to the library builtins.)

llvm-svn: 148267
2012-01-17 00:37:07 +00:00
David Chisnall fa35df628a Some improvements to the handling of C11 atomic types:
- Add atomic-to/from-nonatomic cast types
- Emit atomic operations for arithmetic on atomic types
- Emit non-atomic stores for initialisation of atomic types, but atomic stores and loads for every other store / load
- Add a __atomic_init() intrinsic which does a non-atomic store to an _Atomic() type.  This is needed for the corresponding C11 stdatomic.h function.
- Enables the relevant __has_feature() checks.  The feature isn't 100% complete yet, but it's done enough that we want people testing it.

Still to do:

- Make the arithmetic operations on atomic types (e.g. Atomic(int) foo = 1; foo++;) use the correct LLVM intrinsic if one exists, not a loop with a cmpxchg.
- Add a signal fence builtin
- Properly set the fenv state in atomic operations on floating point values
- Correctly handle things like _Atomic(_Complex double) which are too large for an atomic cmpxchg on some platforms (this requires working out what 'correctly' means in this context)
- Fix the many remaining corner cases

llvm-svn: 148242
2012-01-16 17:27:18 +00:00
Douglas Gregor c03c52ea01 When deserializing the definition of a C++ class/ObjC class/ObjC
protocol, record the definition pointer in the canonical declaration
for that entity, and then propagate that definition pointer from the
canonical declaration to all other deserialized declarations. This
approach works well even when deserializing declarations that didn't
know about the original definition, which can occur with modules.

A nice bonus from this definition-deserialization approach is that we
no longer need update records when a definition is added, because the
redeclaration chains ensure that the if any declaration is loaded, the
definition will also get loaded.

llvm-svn: 148223
2012-01-15 18:08:05 +00:00
Richard Smith bd1530949d decltype(e) is type-dependent if e is instantiation-dependent. Scary but true.
Don't consider decltype(e) for an instantiation-dependent, but not
type-dependent, e to be non-type-dependent but canonical(!).

llvm-svn: 148210
2012-01-15 06:24:57 +00:00
Richard Smith b19ac0d6ca constexpr: casts to void* are allowed in constant expressions, don't set the
designator invalid. (Since we can't read the value of such a pointer, this only
affects the quality of diagnostics.)

llvm-svn: 148208
2012-01-15 03:25:41 +00:00
Eli Friedman 839192fd29 Change linkage computation so it doesn't depend on FunctionDecl::isExternC or VarDecl::isExternC, and instead queries what it actually cares about: whether the given declaration is inside an extern "C" context. Fundamentally, figuring out whether a function/variable uses C linkage requires knowing the linkage, and the logic in FunctionDecl::isExternC and VarDecl::isExternC was getting it wrong. Given that, fix FunctionDecl::isExternC and VarDecl::isExternC to use much simpler implementations that depend on the fixed linkage computation.
Fixes a regression to test/SemaCXX/linkage.cpp caused by a new warning exposing the fact that the internal state was wrong.

llvm-svn: 148207
2012-01-15 01:23:58 +00:00
Douglas Gregor ec9fd13c77 De-virtualize getPreviousDecl() and getMostRecentDecl() when we know
we have a redeclarable type, and only use the new virtual versions
(getPreviousDeclImpl() and getMostRecentDeclImpl()) when we don't have
that type information. This keeps us from penalizing users with strict
type information (and is the moral equivalent of a "final" method).

Plus, settle on the names getPreviousDecl() and getMostRecentDecl()
throughout.

llvm-svn: 148187
2012-01-14 16:38:05 +00:00
Douglas Gregor 463c8e7070 Add a FIXME for mutation of the common pointer of a RedeclarableTemplateDecl. It is not clear that it's worth delaying the allocation of said pointer
llvm-svn: 148182
2012-01-14 15:30:55 +00:00