Commit Graph

2563 Commits

Author SHA1 Message Date
Artem Dergachev 22e28f4078 [analyzer] Fix a crash on accessing a field within a literal-initialized union.
Because in case of unions we currently default-bind compound values in the
store, this quick fix avoids the crash for this case.

Patch by Ilya Palachev and independently by Alexander Shaposhnikov!

Differential Revision: https://reviews.llvm.org/D26442

llvm-svn: 287618
2016-11-22 04:29:23 +00:00
Devin Coughlin 8693adfd46 [analyzer] Add check for when block is called with too few arguments.
The CallAndMessageChecker has an existing check for when a function pointer
is called with too few arguments. Extend this logic to handle the block
case, as well. While we're at it, do a drive-by grammar correction
("less" --> "fewer") on the diagnostic text.

llvm-svn: 287001
2016-11-15 18:40:46 +00:00
Devin Coughlin e4224cc9f7 [analyzer] Fix crash in NullabilityChecker calling block with too few arguments
Fix a crash when checking parameter nullability on a block invocation
with fewer arguments than the block declaration requires.

rdar://problem/29237566

llvm-svn: 286901
2016-11-14 22:46:02 +00:00
Devin Coughlin 8fa568d761 [analyzer] Update 'Automated' to 'Automatic' from r286694.
ARC is 'Automatic Reference Counting' and not 'Automated Reference Counting'.

llvm-svn: 286700
2016-11-12 01:50:04 +00:00
Devin Coughlin 3dc29a52c7 [analyzer] Fix copy-pasta in NullableReturnedFromNonnullChecker checker name.
The name of the NullableReturnedFromNonnullChecker in Checkers.td
was accidentally "NullablePassedToNonnull", which made it impossible
to explicitly turn the checker on.

rdar://problem/28354459

llvm-svn: 286697
2016-11-12 01:23:01 +00:00
Devin Coughlin d1fe08b8a9 [analyzer] Improve misleading RetainCountChcker diagnostic under ARC
Under automated reference counting the analyzer treats a methods -- even those
starting with  'copy' and friends -- as returning an unowned value. This is
because ownership of CoreFoundation objects must be transferred to ARC
with __bridge_transfer or CFBridgingRelease() before being returned as
ARC-managed bridged objects.

Unfortunately this could lead to a poor diagnostic inside copy methods under
ARC where the analyzer would complain about a leak of a returned CF value inside
a method "whose name does not start with 'copy'" -- even though the name did
start with 'copy'.

This commit improves the diagnostic under ARC to say inside a method "returned
from a method managed by Automated Reference Counting".

rdar://problem/28849667

llvm-svn: 286694
2016-11-12 01:03:06 +00:00
Devin Coughlin e477a0e5ec [analyzer] Teach RetainCountChecker about VTCompressionSessionEncodeFrame()
The context argument passed to VideoToolbox's
VTCompressionSessionEncodeFrame() function is ultimately passed to a callback
supplied when creating the compression session and so may be freed by that
callback.  To suppress false positives in this case, teach the retain count
checker to stop tracking that argument.

This isn't suppressed by the usual callback context mechanism because the call
to VTCompressionSessionEncodeFrame() doesn't include the callback itself.

rdar://problem/27685213

llvm-svn: 286633
2016-11-11 21:31:38 +00:00
Serge Pavlov a67a4d2f3c Make output of -ast-print a valid C++ code.
Output generated by option -ast-print looks like C/C++ code, and it
really is for plain C. For C++ the produced output was not valid C++
code, but the differences were small. With this change the output
is fixed and can be compiled. Tests are changed so that output produced
by -ast-print is compiled again with the same flags and both outputs are
compared.

Option -ast-print is extensively used in clang tests but it itself
was tested poorly, existing tests only checked that compiler did not
crash. There are unit tests in file DeclPrinterTest.cpp, but they test
only terse output mode.

Differential Revision: https://reviews.llvm.org/D26452

llvm-svn: 286439
2016-11-10 08:49:37 +00:00
Artem Dergachev 47fec16cd1 [analyzer] StdLibraryFunctions: provide platform-specific function summaries.
Because standard functions can be defined differently on different platforms,
this commit introduces a method for constructing summaries with multiple
variants, whichever matches better. It is also useful for supporting overloads.

Differential Revision: https://reviews.llvm.org/D25940

llvm-svn: 285852
2016-11-02 19:35:20 +00:00
Devin Coughlin 05c03845e7 [analyzer] Fix capitalization in ObjCSuperDealloc checker diagnostic.
Change "use of 'self'..." to "Use of 'self'...". The convention is to
start diagnostics with a capital letter.

rdar://problem/28322494

llvm-svn: 285759
2016-11-01 22:16:39 +00:00
Artem Dergachev 75f9d3ac7e [analyzer] Allow undefined values in performTrivialCopy.
Reading from a garbage pointer should be modeled as garbage,
and performTrivialCopy should be able to deal with any SVal input.

Patch by Ilya Palachev!

Differential Revision: https://reviews.llvm.org/D25727

llvm-svn: 285640
2016-10-31 21:11:20 +00:00
Artem Dergachev a21df23fd8 [analyzer] MacOSXAPIChecker: Improve warnings for __block vars in dispatch_once.
The checker already warns for __block-storage variables being used as a
dispatch_once() predicate, however it refers to them as local which is not quite
accurate, so we fix that.

Differential Revision: https://reviews.llvm.org/D26159

llvm-svn: 285637
2016-10-31 21:04:54 +00:00
Artem Dergachev aacc03c918 [analyzer] MacOSXAPIChecker: Disallow dispatch_once_t in ivars and heap.
Unlike global/static variables, calloc etc. functions that allocate ObjC
objects behave differently in terms of memory barriers, and hacks that make
dispatch_once as fast as it possibly could be start failing.

Differential Revision: https://reviews.llvm.org/D25909

llvm-svn: 285605
2016-10-31 17:27:26 +00:00
Artem Dergachev e14d881808 [analyzer] NumberObjectConversion: support more types, misc updates.
Support CFNumberRef and OSNumber objects, which may also be accidentally
converted to plain integers or booleans.

Enable explicit boolean casts by default in non-pedantic mode.

Improve handling for warnings inside macros.

Improve error messages.

Differential Revision: https://reviews.llvm.org/D25731

llvm-svn: 285533
2016-10-31 03:08:48 +00:00
Anna Zaks 5b2b39065c [analyzer] Report CFNumberGetValue API misuse
This patch contains 2 improvements to the CFNumber checker:
 - Checking of CFNumberGetValue misuse.
 - Treating all CFNumber API misuse errors as non-fatal. (Previously we treated errors that could cause uninitialized memory as syncs and the truncation errors as non-fatal.)

This implements a subset of functionality from https://reviews.llvm.org/D17954.

Differential Revision: https://reviews.llvm.org/D25876

llvm-svn: 285253
2016-10-26 22:51:47 +00:00
Artem Dergachev a7609fbb64 [analyzer] Fix an ARM buildbot after r284960.
I guess we should always specify triples in all analyzer tests, regardless.

llvm-svn: 284969
2016-10-24 12:54:27 +00:00
Artem Dergachev bba497fb65 [analyzer] Add StdLibraryFunctions checker.
This checker does not emit reports, however it influences the analysis
by providing complete summaries for, or otherwise improving modeling of,
various standard library functions.

This should reduce the number of infeasible paths explored during analysis.
The custom function summary format used in this checker is superior to
body farms by causing less unnecessary state splits,
which would result in better analysis performance.

Differential Revision: https://reviews.llvm.org/D20811

llvm-svn: 284960
2016-10-24 09:41:38 +00:00
Reid Kleckner 2e1538f282 Remove 24 instances of 'REQUIRES: shell'
Tests fall into one of the following categories:

- The requirement was unnecessary

- Additional quoting was required for backslashes in paths (see "sed -e
  's/\\/\\\\/g'") in the sanitizer tests.

- OpenMP used 'REQUIRES: shell' as a proxy for the test failing on
  Windows. Those tests fail there reliably, so use XFAIL instead.

I tried not to remove shell requirements that were added to suppress
flaky test failures, but if I screwed up, we can add it back as needed.

llvm-svn: 284793
2016-10-20 23:11:45 +00:00
Daniel Marjamaki fa1bf447d9 alpha.core.UnreachableCode - don't warn about unreachable code inside macro
In macros, 'do {...} while (0)' is often used. Don't warn about the condition 0 when it is unreachable.

Differential Revision: https://reviews.llvm.org/D25606

llvm-svn: 284477
2016-10-18 13:16:53 +00:00
Artem Dergachev 940c770d27 [analyzer] Add NumberObjectConversion checker.
When dealing with objects that represent numbers, such as Objective-C NSNumber,
the language provides little protection from accidentally interpreting
the value of a pointer to such object as the value of the number represented
by the object. Results of such mis-interpretation may be unexpected.

The checker attempts to fill this gap in cases when the code is obviously
incorrect.

With "Pedantic" option enabled, this checker enforces a coding style to
completely prevent errors of this kind (off by default).

Differential Revision: https://reviews.llvm.org/D22968

llvm-svn: 284473
2016-10-18 11:06:28 +00:00
Richard Smith 391fb8662a [c++1z] Include "noexcept" in builtin function types where appropriate. Fixes
an assertion failure looking up a matching ::operator delete for
__builtin_operator_delete.

llvm-svn: 284458
2016-10-18 07:13:55 +00:00
Devin Coughlin 684d19d879 Revert "Revert "[analyzer] Make MallocChecker more robust against custom redeclarations""
This reverts commit r284340 to reapply r284335. The bot breakage was due to
an unrelated change in the polybench test suite.

llvm-svn: 284351
2016-10-16 22:19:03 +00:00
Devin Coughlin eeb8d20db4 Revert "[analyzer] Make MallocChecker more robust against custom redeclarations"
This reverts commit r284335.

It appears to be causing test-suite compile-time and execution-time
performance measurements to take longer than expected on several bots.
This is surprising, because r284335 is a static-analyzer-only change.

llvm-svn: 284340
2016-10-16 19:26:07 +00:00
Devin Coughlin 62ce463a35 [analyzer] Make MallocChecker more robust against custom redeclarations
Add additional checking to MallocChecker to avoid crashing when memory
routines have unexpected numbers of arguments. You wouldn't expect to see much
of this in normal code (-Wincompatible-library-redeclaration warns on this),
but, for example, CMake tests can generate these.

This is PR30616.

rdar://problem/28631974

llvm-svn: 284335
2016-10-16 17:26:06 +00:00
Devin Coughlin 4fba10c394 Revert "[analyzer] Re-apply r283093 "Add extra notes to ObjCDeallocChecker""
Revert:
r283662: [analyzer] Re-apply r283093 "Add extra notes to ObjCDeallocChecker"
r283660: [analyzer] Fix build error after r283660 - remove constexpr strings.

It was causing an internal build bot to fail. It looks like in some cases
adding an extra note can cause scan-build plist output to drop a diagnostic
altogether.

llvm-svn: 284317
2016-10-16 00:30:08 +00:00
Devin Coughlin 0bd37a1a36 [analyzer] DeallocChecker: Don't warn about directly-set IBOutlet ivars on macOS
On macOS (but not iOS), if an ObjC property has no setter, the nib-loading code
for an IBOutlet is documented as directly setting the backing ivar without
retaining the value -- even if the property is 'retain'.

This resulted in false positives from the DeallocChecker for code that did not
release such ivars in -dealloc.

To avoid these false positives, treat IBOutlet ivars that back a property
without a setter as having an unknown release requirement in macOS.

rdar://problem/28507353

llvm-svn: 284084
2016-10-12 23:57:05 +00:00
Artem Dergachev 4eca0de7b7 [analyzer] Re-apply r283094 "Improve CloneChecker diagnostics"
The parent commit (r283092) was reverted before and now finally landed.

llvm-svn: 283661
2016-10-08 10:54:30 +00:00
Artem Dergachev 46209e1dd0 [analyzer] Re-apply r283093 "Add extra notes to ObjCDeallocChecker"
The parent commit (r283092) was reverted before and now finally landed.

llvm-svn: 283660
2016-10-08 10:53:00 +00:00
Daniel Marjamaki d99ebc03f4 [analyzer] Don't merge different return nodes in ExplodedGraph
Returns when calling an inline function should not be merged in the ExplodedGraph unless they are same.

Differential Revision: https://reviews.llvm.org/D25326

llvm-svn: 283554
2016-10-07 14:21:08 +00:00
Anton Yartsev 0509d047f4 [analyzer] Add explanation why analyzer report is not generated (fix for PR12421).
Currently if the path diagnostic consumer (e.g HTMLDiagnostics and PlistDiagnostics) do not support cross file diagnostics then the path diagnostic report is silently omitted in the case of cross file diagnostics. The patch adds a little verbosity to Clang in this case.
The patch also adds help entry for the "--analyzer-output" driver option.

llvm-svn: 283499
2016-10-06 21:42:21 +00:00
Richard Smith 0511d23aeb PR22924, PR22845, some of CWG1464: When checking the initializer for an array
new expression, distinguish between the case of a constant and non-constant
initializer. In the former case, if the bound is erroneous (too many
initializer elements, bound is negative, or allocated size overflows), reject,
and take the bound into account when determining whether we need to
default-construct any elements. In the remanining cases, move the logic to
check for default-constructibility of trailing elements into the initialization
code rather than inventing a bogus array bound, to cope with cases where the
number of initialized elements is not the same as the number of initializer
list elements (this can happen due to string literal initialization or brace
elision).

This also fixes rejects-valid and crash-on-valid errors when initializing a
new'd array of character type from a braced string literal.

llvm-svn: 283406
2016-10-05 22:41:02 +00:00
Artem Dergachev 9cb5614c29 [analyzer] Improve "Assuming..." diagnostic pieces for logical operators.
Logical short-circuit operators now act like other branch conditions.

If the symbolic value of the left-hand side is not known to be true or false
(based on the previous execution path), the "Assuming" event piece is added
in order to explain that the analyzer is adding a new assumption.

Additionally, when the assumption is made against the right-hand side of
the logical operator (i.e. when the operator itself acts as a condition
in another CFG terminator), the "Assuming..." piece is written out for the
right-hand side of the operator rather than for the whole operator.
This allows expression-specific diagnostic message text to be constructed.

Differential Revision: https://reviews.llvm.org/D25092

llvm-svn: 283302
2016-10-05 08:28:25 +00:00
Artem Dergachev 0c33406aaa [analyzer] Add "Assuming..." diagnostic pieces for unsupported conditions.
In the analyzer's path-sensitive reports, when a report goes through a branch
and the branch condition cannot be decided to be definitely true or false
(based on the previous execution path), an event piece is added that tells the
user that a new assumption is added upon the symbolic value of the branch
condition. For example, "Assuming 'a' is equal to 3".

The text of the assumption is hand-crafted in various manners depending on
the AST expression. If the AST expression is too complex and the text of
the assumption fails to be constructed, the event piece is omitted.
This causes loss of information and misunderstanding of the report.

Do not omit the event piece even if the expression is too complex;
add a piece with a generic text instead.

Differential Revision: https://reviews.llvm.org/D23300

llvm-svn: 283301
2016-10-05 08:19:49 +00:00
Vitaly Buka 1a8d52d1ae Revert "[analyzer] Improve CloneChecker diagnostics" as its depends on reverted r283092
This reverts commit r283094.

llvm-svn: 283182
2016-10-04 02:40:35 +00:00
Vitaly Buka b3814f6f38 Revert "[analyzer] Add extra notes to ObjCDeallocChecker" as its depends on reverted r283092
This reverts commit r283093.

llvm-svn: 283181
2016-10-04 02:36:58 +00:00
Daniel Marjamaki fd1b3814b3 [StaticAnalyser] Add test case to ensure that unreachable code is found.
https://reviews.llvm.org/D24905

llvm-svn: 283096
2016-10-03 09:45:35 +00:00
Daniel Marjamaki 042a3c5a2d [StaticAnalyzer] Fix UnreachableCode false positives.
When there is 'do { } while (0);' in the code the ExplodedGraph and UnoptimizedCFG did not match.

Differential Revision: https://reviews.llvm.org/D24759

llvm-svn: 283095
2016-10-03 08:28:51 +00:00
Artem Dergachev c87d2a613e [analyzer] Improve CloneChecker diagnostics
Highlight code clones referenced by the warning message with the help of
the extra notes feature recently introduced in r283092.

Change warning text to more clang-ish. Remove suggestions from the copy-paste
error checker diagnostics, because currently our suggestions are strictly 50%
wrong (we do not know which of the two code clones contains the error), and
for that reason we should not sound as if we're actually suggesting this.
Hopefully a better solution would bring them back.

Make sure the suspicious clone pair structure always mentions
the correct variable for the second clone.

Differential Revision: https://reviews.llvm.org/D24916

llvm-svn: 283094
2016-10-03 08:11:50 +00:00
Artem Dergachev 918602df8d [analyzer] Add extra notes to ObjCDeallocChecker
The report is now highlighting instance variables and properties
referenced by the warning message with the help of the
extra notes feature recently introduced in r283092.

Differential Revision: https://reviews.llvm.org/D24915

llvm-svn: 283093
2016-10-03 08:03:51 +00:00
Daniel Marjamaki 2593b402ce [StaticAnalyzer] Fix false positives for vardecls that are technically unreachable but they are needed.
Example:

    switch (x) {
      int a;  // <- This is unreachable but needed
    case 1:
      a = ...

Differential Revision: https://reviews.llvm.org/D24905

llvm-svn: 282574
2016-09-28 10:39:53 +00:00
Daniel Marjamaki 13264ebea4 [analyzer] Improve CastToStruct checker so it can also detect widening casts of struct data
Example:

struct AB {
  int A;
  int B;
};

struct ABC {
  int A;
  int B;
  int C;
};

void f() {
  struct AB Data;
  struct ABC *P = (struct ABC *)&Data;
}

Differential Revision: https://reviews.llvm.org/D23508

llvm-svn: 282411
2016-09-26 15:17:18 +00:00
Alexander Shaposhnikov a1fead293f [analyzer] Fix crash in RetainCountChecker::checkEndFunction
The class BodyFarm creates bodies for
OSAtomicCompareAndSwap*, objc_atomicCompareAndSwap*, dispatch_sync*, dispatch_once*
and for them the flag isBodyAutosynthesized is set to true.

This diff
1. makes AnalysisConsumer::HandleCode skip the autosynthesized code
2. replaces assert(LCtx->getParent()) in RetainCountChecker::checkEndFunction
by assert(!LCtx->inTopFrame()) (minor cleanup)

Test plan: make -j8 check-clang-analysis

Differential revision: https://reviews.llvm.org/D24792

llvm-svn: 282293
2016-09-23 20:49:01 +00:00
Anna Zaks c154f7bc37 [analyzer] Add a checker that detects blocks in critical sections
This checker should find the calls to blocking functions (for example: sleep, getc, fgets,read,recv etc.) inside a critical section. When sleep(x) is called while a mutex is held, other threads cannot lock the same mutex. This might take some time, leading to bad performance or even deadlock.

Example:

mutex_t m;

void f() {
  sleep(1000); // Error: sleep() while m is locked! [f() is called from foobar() while m is locked]
  // do some work
}

void foobar() {
  lock(m);
  f();
  unlock(m);
}

A patch by zdtorok (Zoltán Dániel Török)!

Differential Revision: https://reviews.llvm.org/D21506

llvm-svn: 282011
2016-09-20 20:28:50 +00:00
Gabor Horvath 7304027c6d [analyzer] Calculate extent size for memory regions allocated by new expression.
ArrayBoundChecker did not detect out of bounds memory access errors in case an
array was allocated by the new expression. This patch resolves this issue.

Patch by Daniel Krupp!

Differential Revision: https://reviews.llvm.org/D24307

llvm-svn: 281934
2016-09-19 20:39:52 +00:00
Alexander Shaposhnikov 6956d29034 [analyzer] Fix ExprEngine::VisitMemberExpr
AST may contain intermediate ParenExpr nodes
between MemberExpr and ArrayToPointerDecay.
This diff adjusts the check in ExprEngine::VisitMemberExpr accordingly.
Test plan: make -j8 check-clang-analysis

Differential revision: https://reviews.llvm.org/D24484

llvm-svn: 281373
2016-09-13 19:17:20 +00:00
Aleksei Sidorin 29afb1937b [analyzer] ExprEngine: remove second call to PreStmt<CastExpr>
This patch also introduces AnalysisOrderChecker which is intended for testing
of callback call correctness.

Differential Revision: https://reviews.llvm.org/D23804

llvm-svn: 280367
2016-09-01 13:55:38 +00:00
Aleksei Sidorin e1beaf1ee7 [analyzer] Add more FileIDs to PlistDiagnostic map to avoid assertion
Some FileIDs that may be used by PlistDiagnostics were not added while building
a list of pieces. This caused assertion violation in GetFID() function.
This patch adds some missing FileIDs to avoid the assertion. It also contains
small refactoring of PlistDiagnostics::FlushDiagnosticsImpl().

Patch by Aleksei Sidorin, Ilya Palachev.

Differential Revision: https://reviews.llvm.org/D22090

llvm-svn: 280360
2016-09-01 12:25:16 +00:00
Richard Smith 53851923d5 Fix all tests under test/CXX (and test/Analysis) to pass if clang's default
C++ language standard is not C++98.

llvm-svn: 280309
2016-08-31 23:24:08 +00:00
Devin Coughlin 184996bbb4 [analyzer] Use lazily created buffer in EmptyLocalizationContextChecker
Fix a crash when relexing the underlying memory buffer to find incorrect
arguments to NSLocalizedString(). With precompiled headers, the raw
buffer may be NULL. Instead, use the source manager to get the buffer,
which will lazily create the buffer for precompiled headers.

rdar://problem/27429091

llvm-svn: 280174
2016-08-30 23:07:14 +00:00
Artem Dergachev 5f94b089e9 [analyzer] Fix CloneDetector crash on calling methods of class templates.
If a call expression represents a method call of a class template,
and the method itself isn't templated, then the method may be considered
to be a template instantiation without template specialization arguments.

No longer crash when we could not find template specialization arguments.

Patch by Raphael Isemann!

Differential Revision: https://reviews.llvm.org/D23780

llvm-svn: 279529
2016-08-23 16:42:00 +00:00