llvm/clang/test/Driver/analyzer-target-enabled-checkers.cpp
Devin Coughlin 8beac28564 [analyzer] Add checker modeling gtest APIs.
gtest is a widely-used unit-testing API. It provides macros for unit test
assertions:

  ASSERT_TRUE(p != nullptr);

that expand into an if statement that constructs an object representing
the result of the assertion and returns when the assertion is false:

  if (AssertionResult gtest_ar_ = AssertionResult(p == nullptr))
      ;
  else
    return ...;

Unfortunately, the analyzer does not model the effect of the constructor
precisely because (1) the copy constructor implementation is missing from the
the header (so it can't be inlined) and (2) the boolean-argument constructor
is constructed into a temporary (so the analyzer decides not to inline it since
it doesn't reliably call temporary destructors right now).

This results in false positives because the analyzer does not realize that the
the assertion must hold along the non-return path.

This commit addresses the false positives by explicitly modeling the effects
of the two un-inlined constructors on the AssertionResult state.

I've added a new package, "apiModeling", for these kinds of checkers that
model APIs but don't emit any diagnostics. I envision all the checkers in
this package always being on by default.

This addresses the false positives reported in PR30936.

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

rdar://problem/22705813

llvm-svn: 290143
2016-12-19 22:50:31 +00:00

61 lines
3.3 KiB
C++

// Tests for static analyzer checkers that the driver enables by default based
// on the target triple.
// RUN: %clang -### -target x86_64-apple-darwin10 --analyze %s 2>&1 | FileCheck --check-prefix=CHECK-DARWIN %s
// CHECK-DARWIN: "-analyzer-checker=core"
// CHECK-DARWIN-SAME: "-analyzer-checker=apiModeling"
// CHECK-DARWIN-SAME: "-analyzer-checker=unix"
// CHECK-DARWIN-SAME: "-analyzer-checker=osx"
// CHECK-DARWIN-SAME: "-analyzer-checker=deadcode"
// CHECK-DARWIN-SAME: "-analyzer-checker=cplusplus"
// CHECK-DARWIN-SAME: "-analyzer-checker=security.insecureAPI.UncheckedReturn"
// CHECK-DARWIN-SAME: "-analyzer-checker=security.insecureAPI.getpw"
// CHECK-DARWIN-SAME: "-analyzer-checker=security.insecureAPI.gets"
// CHECK-DARWIN-SAME: "-analyzer-checker=security.insecureAPI.mktemp"
// CHECK-DARWIN-SAME: "-analyzer-checker=security.insecureAPI.mkstemp"
// CHECK-DARWIN-SAME: "-analyzer-checker=security.insecureAPI.vfork"
// CHECK-DARWIN-SAME: "-analyzer-checker=nullability.NullPassedToNonnull"
// CHECK-DARWIN-SAME: "-analyzer-checker=nullability.NullReturnedFromNonnull"
// RUN: %clang -### -target x86_64-unknown-linux --analyze %s 2>&1 | FileCheck --check-prefix=CHECK-LINUX %s
// CHECK-LINUX: "-analyzer-checker=core"
// CHECK-LINUX-SAME: "-analyzer-checker=apiModeling"
// CHECK-LINUX-SAME: "-analyzer-checker=unix"
// CHECK-LINUX-NOT: "-analyzer-checker=osx"
// CHECK-LINUX-SAME: "-analyzer-checker=deadcode"
// CHECK-LINUX-SAME: "-analyzer-checker=cplusplus"
// CHECK-LINUX-SAME: "-analyzer-checker=security.insecureAPI.UncheckedReturn"
// CHECK-LINUX-SAME: "-analyzer-checker=security.insecureAPI.getpw"
// CHECK-LINUX-SAME: "-analyzer-checker=security.insecureAPI.gets"
// CHECK-LINUX-SAME: "-analyzer-checker=security.insecureAPI.mktemp"
// CHECK-LINUX-SAME: "-analyzer-checker=security.insecureAPI.mkstemp"
// CHECK-LINUX-SAME: "-analyzer-checker=security.insecureAPI.vfork"
// CHECK-LINUX-SAME: "-analyzer-checker=nullability.NullPassedToNonnull"
// CHECK-LINUX-SAME: "-analyzer-checker=nullability.NullReturnedFromNonnull"
// RUN: %clang -### -target x86_64-windows --analyze %s 2>&1 | FileCheck --check-prefix=CHECK-WINDOWS %s
// CHECK-WINDOWS: "-analyzer-checker=core"
// CHECK-WINDOWS-SAME: "-analyzer-checker=apiModeling"
// CHECK-WINDOWS-SAME: "-analyzer-checker=unix.API"
// CHECK-WINDOWS-SAME: "-analyzer-checker=unix.Malloc"
// CHECK-WINDOWS-SAME: "-analyzer-checker=unix.MallocSizeof"
// CHECK-WINDOWS-SAME: "-analyzer-checker=unix.MismatchedDeallocator"
// CHECK-WINDOWS-SAME: "-analyzer-checker=unix.cstring.BadSizeArg"
// CHECK-WINDOWS-SAME: "-analyzer-checker=unix.cstring.NullArg"
// CHECK-WINDOWS-NOT: "-analyzer-checker=osx"
// CHECK-WINDOWS-SAME: "-analyzer-checker=deadcode"
// CHECK-WINDOWS-SAME: "-analyzer-checker=cplusplus"
// CHECK-WINDOWS-SAME: "-analyzer-checker=security.insecureAPI.UncheckedReturn"
// CHECK-WINDOWS-SAME: "-analyzer-checker=security.insecureAPI.getpw"
// CHECK-WINDOWS-SAME: "-analyzer-checker=security.insecureAPI.gets"
// CHECK-WINDOWS-SAME: "-analyzer-checker=security.insecureAPI.mktemp"
// CHECK-WINDOWS-SAME: "-analyzer-checker=security.insecureAPI.mkstemp"
// CHECK-WINDOWS-SAME: "-analyzer-checker=security.insecureAPI.vfork"
// CHECK-WINDOWS-SAME: "-analyzer-checker=nullability.NullPassedToNonnull"
// CHECK-WINDOWS-SAME: "-analyzer-checker=nullability.NullReturnedFromNonnull"