Commit Graph

360 Commits

Author SHA1 Message Date
Douglas Gregor 3168dcf134 The integer type of an enumeration type isn't always canonical
llvm-svn: 139334
2011-09-08 23:29:05 +00:00
Matt Beaumont-Gay 042ce8e9d8 Extend -Wliteral-conversion to catch "int i = -1.234"
llvm-svn: 139326
2011-09-08 22:30:47 +00:00
Douglas Gregor e8bbc12152 Extend the ASTContext constructor to delay the initialization of
builtin types (When requested). This is another step toward making
ASTUnit build the ASTContext as needed when loading an AST file,
rather than doing so after the fact. No actual functionality change (yet).

llvm-svn: 138985
2011-09-02 00:18:52 +00:00
John McCall 29ad95b232 The lvalue-to-rvalue on structs in C++ is actually part
of default argument promotion and needs to happen unconditionally.
This is particularly semantically important in C++0x.

llvm-svn: 138691
2011-08-27 01:09:30 +00:00
Matt Beaumont-Gay 335e653fe6 Improve the correctness and accuracy of the message for -Wdynamic-class-memaccess
llvm-svn: 138074
2011-08-19 20:40:18 +00:00
Ted Kremenek 18db5d4e9d Enhance -Wstrl-incorrect-size to not report a FIXIT for destinations that are flexible arrays or have size 1.
llvm-svn: 138004
2011-08-18 22:48:41 +00:00
Ted Kremenek 6865f77fdd Reapply r137903, but fix the definition of size_t in the test case to use __SIZE_TYPE__ (and hence be portable).
Also, change the warning to -Wstrl-incorrect-size.

llvm-svn: 137980
2011-08-18 20:55:45 +00:00
Ted Kremenek 13fd55d85f Revert r137903, "Add experimental -Wstrlcpy-size warning that looks to see if the size argument for strlcpy/strlcat is the size of the *source*, and not the size of the *destination*. This warning is off by default (for now)."
This currently doesn't work on Windows.

llvm-svn: 137920
2011-08-18 02:18:02 +00:00
Ted Kremenek d5fe9e4d97 Add experimental -Wstrlcpy-size warning that looks to see if the size argument for strlcpy/strlcat is the size of the *source*, and not the size of the *destination*. This warning is off by default (for now).
Warning logic provided by Geoff Keating.

llvm-svn: 137903
2011-08-17 23:40:36 +00:00
Kaelyn Uhrain 0fb0bb179a Add a test case for the divide-by-zero fix in r137234
llvm-svn: 137240
2011-08-10 19:47:25 +00:00
Kaelyn Uhrain e535376b14 Make sure ptrarith_typesize is at least 1 to avoid division by zero
llvm-svn: 137234
2011-08-10 18:49:28 +00:00
Benjamin Kramer 8c54367935 Only look at decls after the current one when checking if it's the last field in a record.
llvm-svn: 137009
2011-08-06 03:04:42 +00:00
Kaelyn Uhrain 2e7aa5ae1f Perform array bounds checking in more situations and properly handle special
case situations with the unary operators & and *. Also extend the array bounds
checking to work with pointer arithmetic; the pointer arithemtic checking can
be turned on using -Warray-bounds-pointer-arithmetic.

The changes to where CheckArrayAccess gets called is based on some trial &
error and a bunch of digging through source code and gdb backtraces in order
to have the check performed under as many situations as possible (such as for
variable initializers, arguments to function calls, and within conditional in
addition to the simpler cases of the operands to binary and unary operator)
while not being called--and triggering warnings--more than once for a given
ArraySubscriptExpr.

llvm-svn: 136997
2011-08-05 23:18:04 +00:00
Chandler Carruth 28389f0cdf Flesh out the -Warray-bounds detection of C89 tail-padded one-element
arrays. This now suppresses the warning only in the case of
a one-element array as the last field in a struct where the array size
is a literal '1' rather than any macro expansion or template parameter.

This doesn't distinguish between the language standard in use to allow
code which dates from C89 era to compile without the warning even in C99
and C++ builds. We could add a separate warning (under a different flag)
with fixit hints to switch to a flexible array, but its not clear that
this would be desirable. Much of the code using this idiom is striving
for maximum portability.

Tests were also fleshed out a bit, and the diagnostic itself tweaked to
be more pretty w.r.t. single elment arrays. This is more ugly than
I would like due to APInt's not being supported by the diagnostic
rendering engine.

A pseudo-patch for this was proposed by Nicola Gigante, but I reworked
it both for several correctness issues and for code style.

Sorry this was so long in coming.

llvm-svn: 136965
2011-08-05 09:10:50 +00:00
Chandler Carruth 126b155429 Finally getting around to re-working this to more accurately white-list
1-element character arrays which are serving as flexible arrays. This is
the initial step, which is to restrict the 1-element array whitelist to
arrays that are member declarations. I'll refine it from here based on
the proposed patch.

llvm-svn: 136964
2011-08-05 08:07:29 +00:00
Matt Beaumont-Gay 3c48990a46 Extend memset/memcpy/memmove checking to include memcmp
llvm-svn: 136950
2011-08-05 00:22:34 +00:00
Chris Lattner f51dae0378 disable array bounds overflow warning for cases where an array
has a single element.  This disables the warning in cases where
there is a clear bug, but this is really rare (who uses arrays
with one element?) and it also silences a large class of false
positive issues with C89 code that is using tail padding in structs.

A better version of this patch would detect when an array is in
a tail position in a struct, but at least patch fixes the huge
false positives that are hitting postgres and other code.

llvm-svn: 136724
2011-08-02 21:44:23 +00:00
Douglas Gregor fb65e592e0 Add support for C++0x unicode string and character literals, from Craig Topper!
llvm-svn: 136210
2011-07-27 05:40:30 +00:00
Kaelyn Uhrain c45dcd2594 Revert r136046 while fixing handling of e.g. &foo[index_one_past_size]
llvm-svn: 136113
2011-07-26 18:36:36 +00:00
Chandler Carruth c22845abe2 Cleanup the stray comments and variables I could dig out of Sema to
refer to 'expansion' instead of 'instantiation'.

llvm-svn: 136060
2011-07-26 05:40:03 +00:00
Kaelyn Uhrain 980bdb9dfb Expand array bounds checking to work in the presence of unary & and *,
and to work with pointer arithmetic in addition to array indexing.

The new pointer arithmetic porition of the array bounds checking can be
turned on by -Warray-bounds-pointer-arithmetic (and is off by default).

llvm-svn: 136046
2011-07-26 01:52:28 +00:00
Chris Lattner 0e62c1cc0b remove unneeded llvm:: namespace qualifiers on some core types now that LLVM.h imports
them into the clang namespace.

llvm-svn: 135852
2011-07-23 10:55:15 +00:00
Richard Trieu bb43dec255 Remove warning for conditional operands of differend signedness from -Wsign-compare. Cases that previously warn on this will have a different warning emitted from -Wsign-conversion.
llvm-svn: 135664
2011-07-21 02:46:28 +00:00
Jeffrey Yasskin d0f079dad4 Use the new APFloat::convertToInt(APSInt) function to simplify uses of
convertToInt(integerParts*) and make them more reliable.

llvm-svn: 135279
2011-07-15 17:03:07 +00:00
John McCall 5143181ff9 Teach -Wconversion, -Wsign-compare etc. about division and remainder.
llvm-svn: 135208
2011-07-14 22:39:48 +00:00
John McCall c368838b20 Make the integer-range analysis recognize ^= correctly,
and (while I'm at it) teach it to grok the results of simple
assignments.

The first is PR10336.

llvm-svn: 135034
2011-07-13 06:35:24 +00:00
John McCall 4db5c3c83a In ARC, reclaim all return values of retainable type, not just those
where we have an immediate need of a retained value.

As an exception, don't do this when the call is made as the immediate
operand of a __bridge retain.  This is more in the way of a workaround
than an actual guarantee, so it's acceptable to be brittle here.

rdar://problem/9504800

llvm-svn: 134605
2011-07-07 06:58:02 +00:00
Fariborz Jahanian 5f98da0ea2 objc-arc: Check on a variety of unsafe assignment of retained
objects.  // rdar://9495837

llvm-svn: 133806
2011-06-24 18:25:34 +00:00
Argyrios Kyrtzidis cff00d9c12 Rename objc_lifetime -> objc_ownership, and modify diagnostics to talk about 'ownership', not 'lifetime'.
rdar://9477613.

llvm-svn: 133779
2011-06-24 00:08:59 +00:00
Chandler Carruth 889ed86d73 Fix some grammar nits in the comments from Nick.
llvm-svn: 133571
2011-06-21 23:04:20 +00:00
Douglas Gregor fe31481f68 Introduce a new AST node describing reference binding to temporaries.
MaterializeTemporaryExpr captures a reference binding to a temporary
value, making explicit that the temporary value (a prvalue) needs to
be materialized into memory so that its address can be used. The
intended AST invariant here is that a reference will always bind to a
glvalue, and MaterializeTemporaryExpr will be used to convert prvalues
into glvalues for that binding to happen. For example, given

  const int& r = 1.0;

The initializer of "r" will be a MaterializeTemporaryExpr whose
subexpression is an implicit conversion from the double literal "1.0"
to an integer value. 

IR generation benefits most from this new node, since it was
previously guessing (badly) when to materialize temporaries for the
purposes of reference binding. There are likely more refactoring and
cleanups we could perform there, but the introduction of
MaterializeTemporaryExpr fixes PR9565, a case where IR generation
would effectively bind a const reference directly to a bitfield in a
struct. Addresses <rdar://problem/9552231>.

llvm-svn: 133521
2011-06-21 17:03:29 +00:00
Douglas Gregor 18739c343c Teach the warning about non-POD memset/memcpy/memmove to deal with the
__builtin_ versions of these functions as well as the normal function
versions, so that it works on platforms where memset/memcpy/memmove
are macros that map down to the builtins (e.g., Darwin). Fixes
<rdar://problem/9372688>.

llvm-svn: 133173
2011-06-16 17:56:04 +00:00
Chandler Carruth 8b9e5a72cb Rework the warning for 'memset(p, 0, sizeof(p))' where 'p' is a pointer
and the programmer intended to write 'sizeof(*p)'. There are several
elements to the new version:

1) The actual expressions are compared in order to more accurately flag
   the case where the pattern that works for an array has been used, or
   a '*' has been omitted.
2) Only do a loose type-based check for record types. This prevents us
   from warning when we happen to be copying around chunks of data the
   size of a pointer and the pointer types for the sizeof and
   source/dest match.
3) Move all the diagnostics behind the runtime diagnostic filter. Not
   sure this is really important for this particular diagnostic, but
   almost everything else in SemaChecking.cpp does so.
4) Make the wording of the diagnostic more precise and informative. At
   least to my eyes.
5) Provide highlighting for the two expressions which had the unexpected
   similarity.
6) Place this diagnostic under a flag: -Wsizeof-pointer-memaccess

This uses the Stmt::Profile system for computing #1. Because of the
potential cost, this is guarded by the warning flag. I'd be interested
in feedback on how bad this is in practice; I would expect it to be
quite cheap in practice. Ideas for a cheaper / better way to do this are
also welcome.

The diagnostic wording could likely use some further wordsmithing.
Suggestions welcome here. The goals I had were to: clarify that its the
interaction of 'memset' and 'sizeof' and give more reasonable
suggestions for a resolution.

An open question is whether these diagnostics should have the note
attached for silencing by casting the dest/source pointer to void*.

llvm-svn: 133155
2011-06-16 09:09:40 +00:00
Chandler Carruth a05e09ba48 Skip both character pointers and void pointers when diagnosing bad
argument types for mem{set,cpy,move}. Character pointers, much like void
pointers, often point to generic "memory", so trying to check whether
they match the type of the argument to 'sizeof' (or other checks) is
unproductive and often results in false positives.

Nico, please review; does this miss any of the bugs you were trying to
find with this warning? The array test case you had should be caught by
the array-specific sizeof warning I think.

llvm-svn: 133136
2011-06-16 02:00:04 +00:00
John McCall 31168b077c Automatic Reference Counting.
Language-design credit goes to a lot of people, but I particularly want
to single out Blaine Garst and Patrick Beard for their contributions.

Compiler implementation credit goes to Argyrios, Doug, Fariborz, and myself,
in no particular order.

llvm-svn: 133103
2011-06-15 23:02:42 +00:00
Nico Weber c5e7386983 Warn on memset(ptr, 0, sizeof(ptr)). Diagnostic wording by Jordy Rose.
llvm-svn: 132996
2011-06-14 16:14:58 +00:00
Chris Lattner ee7286f02d fix rdar://9546171 - -Wshorten-64-to-32 shouldn't warn on vector bitcasts.
llvm-svn: 132975
2011-06-14 04:51:15 +00:00
Chandler Carruth ff455bbc3c Fix a broken index left over from before this function was converted to
handle memcpy and memmove. Spotted by Nico.

llvm-svn: 132902
2011-06-13 05:00:35 +00:00
Chandler Carruth ac6872655b Clean up the "non-POD memaccess" stuff some. This adds a properly named
diagnostic group to cover the cases where we have definitively bad
behavior: dynamic classes.

It also rips out the existing support for POD-based checking. This
didn't work well, and triggered too many false positives. I'm looking
into a possibly more principled way to warn on the fundamental buggy
construct here. POD-ness isn't the critical aspect anyways, so a clean
slate is better. This also removes some silliness from the code until
the new checks arrive.

llvm-svn: 132534
2011-06-03 06:23:57 +00:00
Richard Trieu beaf34531e Add a new warning on NULL pointer constant to integer conversion.
This path was reviewed by Chandler Carruth at http://codereview.appspot.com/4538074/

llvm-svn: 132297
2011-05-29 19:59:02 +00:00
Douglas Gregor 61b6e49ee1 A few more is(Un)signedIntegerType/is(Un)signedOrEnumerationType cleanups.
llvm-svn: 131793
2011-05-21 16:28:01 +00:00
Douglas Gregor 3bb2a81539 Extend -Wnon-pod-memset to also encompass memcpy() and memmove(),
checking both the source and the destination operands, renaming the
warning group to -Wnon-pod-memaccess and tweaking the diagnostic text
in the process.

llvm-svn: 130786
2011-05-03 20:37:33 +00:00
Douglas Gregor a74926b518 Separate the -Wnon-pod-memset warnings into two separate warnings:
- a default-on warning for pointers to dynamic classes (= classes with vtables)
  - a default-off warning for other non-POD types

llvm-svn: 130781
2011-05-03 20:05:22 +00:00
Douglas Gregor 953beb4715 Only check the use of memset() if we're refering to a C function named
'memset' with external linkage.

llvm-svn: 130770
2011-05-03 18:11:37 +00:00
Chandler Carruth c37485e6cb Relax the non-POD memset warning to use the less restrictive C++11
definition of POD. Specifically, this allows certain non-aggregate
types due to their data members being private.

The representation of C++11 POD testing is pretty gross. Any suggestions
for improvements there are welcome. Especially the name
'isCXX11PODType()' seems truly unfortunate.

llvm-svn: 130492
2011-04-29 09:46:08 +00:00
Ted Kremenek b5fabb2f9f Convert assertion in memset checking to a runtime check (because real code may provide a deviant definition of memset).
llvm-svn: 130368
2011-04-28 01:38:02 +00:00
Chandler Carruth ede9d33cde Heh, funny thing, 'void' isn't a POD type. Nice of us to suggest it to
silence this warning. ;]

Fixed that obvious bug and added a bit more testing as well.

llvm-svn: 130318
2011-04-27 18:48:59 +00:00
Chandler Carruth 53caa4d4fa Add a warning (-Wnon-pod-memset) for calls to memset() with
a destination pointer that points to a non-POD type. This can flag such
horrible bugs as overwriting vptrs when a previously POD structure is
suddenly given a virtual method, or creating objects that crash on
practically any use by zero-ing out a member when its changed from
a const char* to a std::string, etc.

llvm-svn: 130299
2011-04-27 07:05:31 +00:00
Richard Smith dda56e4b4a Support for C++11 (non-template) alias declarations.
llvm-svn: 129567
2011-04-15 14:24:37 +00:00
Chris Lattner 57540c5be0 fix a bunch of comment typos found by codespell. Patch by
Luis Felipe Strano Moraes!

llvm-svn: 129559
2011-04-15 05:22:18 +00:00