Commit Graph

24 Commits

Author SHA1 Message Date
Aaron Ballman 9d36551d7e Allow the nonnull attribute to be inherited as a parameter in the redefinition of a function. Fixes PR30828.
Patch by Matt Bettinson.

llvm-svn: 297592
2017-03-12 22:30:07 +00:00
Nick Lewycky bc85ec8656 Add a "declared 'nonnull' here" note to warnings where an expression is checked against null.
llvm-svn: 272755
2016-06-15 05:18:39 +00:00
George Burgess IV 850269a47e [Sema] Add warning when comparing nonnull and null
Currently, we emit warnings in some cases where nonnull function
parameters are compared against null. This patch extends this support
to warn when comparing the result of `returns_nonnull` functions
against null.

More specifically, we will now warn cases like:

int *foo() __attribute__((returns_nonnull));
int main() {
  if (foo() == NULL) {} // warning: will always evaluate to false
}

Differential Revision: http://reviews.llvm.org/D15324

llvm-svn: 255058
2015-12-08 22:02:00 +00:00
Aaron Ballman 2521f36e5d When checking for nonnull parameter attributes, also check the ParmVarDecl since the attribute may reside there, instead of just on the FunctionDecl. Fixes PR21668.
llvm-svn: 224039
2014-12-11 19:35:42 +00:00
Fariborz Jahanian ef202d96c7 [Sema] Patch to issue warning on comparing parameters with
nonnull attribute when comparison is always true/false. 
Original patch by Steven Wu. I have added extra code to prevent issuing of
warning  when the nonnull parameter is modified prior to the comparison.
This addition prevents false positives in the most obvious cases.
There may still be false positive warnings in some cases (as one of my tests
indicates), but benefit far outweighs such cases. rdar://18712242

llvm-svn: 222264
2014-11-18 21:57:54 +00:00
Fariborz Jahanian d6efd3e0d0 This patch reverts r220496 which issues warning on comparing
parameters with nonnull attribute when comparison is always
true/false. Patch causes false positive when parameter is
modified in the function.

llvm-svn: 221163
2014-11-03 17:03:07 +00:00
Fariborz Jahanian 4c8cb14c1a patch to issue warning on comparing parameters with
nonnull attribute when comparison is always
true/false. Patch by Steven Wu with few fixes and minor
refactoring and adding tests by me. rdar://18712242

llvm-svn: 220496
2014-10-23 19:00:10 +00:00
Richard Smith 588bd9b7f8 Fix representation of __attribute__((nonnull)) to support correctly modeling
the no-arguments case. Don't expand this to an __attribute__((nonnull(A, B,
C))) attribute, since that does the wrong thing for function templates and
varargs functions.

In passing, fix a grammar error in the diagnostic, a crash if
__attribute__((nonnull(N))) is applied to a varargs function,
a bug where the same null argument could be diagnosed multiple
times if there were multiple nonnull attributes referring to it,
and a bug where nonnull attributes would not be accumulated correctly
across redeclarations.

llvm-svn: 216520
2014-08-27 04:59:42 +00:00
Aaron Ballman 6288d0648a The returns_nonnull attribute does not require a function prototype because it affects only the return value, not any arguments. In turn, asking for a function or method result type should not require a function prototype either, so getFunctionOrMethodResultType has been relaxed.
llvm-svn: 212827
2014-07-11 16:31:29 +00:00
Jordan Rose c939907a5a 'nonnull(1)' on a block parameter should apply to the block's argument.
Thanks to r199467, __attribute__((nonnull)) (without arguments) can apply
directly to parameters, instead of being applied to the whole function.
However, the old form of nonnull (with an argument index) could also apply
to the arguments of function and block pointers, and both of these can be
passed as parameters.

Now, if 'nonnull' with an argument is found on a parameter, /and/ the
parameter is a function or block pointer, it is handled the old way.

PR18795

llvm-svn: 201162
2014-02-11 17:27:59 +00:00
Ted Kremenek ef9e7f8059 Add basic checking for returning null from functions/methods marked 'returns_nonnull'.
This involved making CheckReturnStackAddr into a static function, which
is now called by a top-level return value checking routine called
CheckReturnValExpr.

llvm-svn: 199790
2014-01-22 06:10:28 +00:00
Aaron Ballman fc1951c505 Making some minor improvements to r199626.
llvm-svn: 199663
2014-01-20 14:19:44 +00:00
Ted Kremenek dbf62e3eee Wire up basic parser/sema support for attribute 'returns_nonnull'.
This attribute is supported by GCC.  More generally it should
probably be a type attribute, but this behavior matches 'nonnull'.

This patch does not include warning logic for checking if a null
value is returned from a function annotated with this attribute.
That will come in subsequent patches.

llvm-svn: 199626
2014-01-20 05:50:47 +00:00
Aaron Ballman d6432f838e Adding a test case for nonnull being attached to something other than a function, Objective-C method, or parameter.
llvm-svn: 199496
2014-01-17 14:38:58 +00:00
Ted Kremenek 9aedc159ef Enhance attribute 'nonnull' to be applicable to parameters directly (infix).
This allows the following syntax:

  void baz(__attribute__((nonnull)) const char *str);

instead of:

  void baz(const char *str) __attribute__((nonnull(1)));

This also extends to Objective-C methods.

The checking logic in Sema is not as clean as I would like.  Effectively
now we need to check both the FunctionDecl/ObjCMethodDecl and the parameters,
so the point of truth is spread in two places, but the logic isn't that
cumbersome.

Implements <rdar://problem/14691443>.

llvm-svn: 199467
2014-01-17 06:24:56 +00:00
Aaron Ballman cedaaea691 This diagnostic did not accept arguments, and did not have any test coverage. Parameterized the diagnostic, and made it more consistent with other attribute diagnostic wordings. Added test coverage.
Since this warning was generalized, it was also given a sensible warning group flag and the corresponding test was updated to reflect this.

llvm-svn: 198053
2013-12-26 17:07:49 +00:00
Aaron Ballman be50eb86e6 Refactor some attributes to use checkFunctionOrMethodArgumentIndex instead of using custom logic. No functional changes intended.
llvm-svn: 187398
2013-07-30 00:48:57 +00:00
Aaron Ballman bf5cf8c6ea This test was missing its -verify argument.
llvm-svn: 186847
2013-07-22 17:55:04 +00:00
Fariborz Jahanian f4aa279ce8 Handle nonnull attribute with optional argument number on
functions with arguments of transparent unions type.
// rdar://9584012

llvm-svn: 133941
2011-06-27 21:12:03 +00:00
Ted Kremenek d4adebbbd6 Handle some more fallout with the conversion of using PointerType for
Objective-C pointers to using ObjCObjectPointerType.

Now the checking for 'attribute ((nonnull))' in Sema doesn't emit an error when
trying to apply that attribute to a parameter that is an Objective-C pointer
(this is a regression).

To prevent this regression from occuring in the future, the 'nonnull.c' test was
moved to test/SemaObjC and renamed 'nonnull.m'. I also enhanced the tests to
show that function calls involved a NULL Objective-C pointer constant does not
trigger a warning. This is consistent with GCC, but should likely be fixed.

llvm-svn: 75856
2009-07-15 23:23:54 +00:00
Chris Lattner ae5342094a merge two tests.
llvm-svn: 72392
2009-05-25 18:20: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
Ted Kremenek c4f6d90ba1 Tidy up sema processing of attribute "nonull":
- warn about nonnull being applied to functions with no pointer arguments
- continue processing argument list in the attribute when we encounter a non-pointer parameter being marked as nonnull
- when no argument list is specified, only mark pointers as nonnull.  This fixes PR 2732 and radar 6188814.

llvm-svn: 55610
2008-09-01 19:57:52 +00:00
Ted Kremenek 5224e6a81d Add test case for nonnull attribute.
Fix indexing bug.

llvm-svn: 53882
2008-07-21 22:09:15 +00:00