blob: 72f01f0b8422b49d9faaacef66588735bd2cc98b [file] [log] [blame]
// Part of the Crubit project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "nullability/inference/collect_evidence.h"
#include <cassert>
#include <memory>
#include <string>
#include <vector>
#include "nullability/inference/augmented_test_inputs.h"
#include "nullability/inference/inference.proto.h"
#include "nullability/inference/slot_fingerprint.h"
#include "nullability/pragma.h"
#include "nullability/type_nullability.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclBase.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchersMacros.h"
#include "clang/Analysis/FlowSensitive/WatchedLiteralsSolver.h"
#include "clang/Basic/LLVM.h"
#include "clang/Testing/TestAST.h"
#include "third_party/llvm/llvm-project/clang/unittests/Analysis/FlowSensitive/TestingSupport.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Testing/Support/Error.h"
#include "third_party/llvm/llvm-project/third-party/unittest/googlemock/include/gmock/gmock.h" // IWYU pragma: keep
#include "third_party/llvm/llvm-project/third-party/unittest/googletest/include/gtest/gtest.h"
namespace clang::tidy::nullability {
namespace {
using ::clang::ast_matchers::cxxConstructorDecl;
using ::clang::ast_matchers::functionDecl;
using ::clang::ast_matchers::hasName;
using ::clang::ast_matchers::isDefaultConstructor;
using ::clang::ast_matchers::isImplicit;
using ::clang::ast_matchers::isTemplateInstantiation;
using ::clang::ast_matchers::match;
using ::clang::ast_matchers::parameterCountIs;
using ::clang::ast_matchers::selectFirst;
using ::clang::ast_matchers::unless;
using ::clang::ast_matchers::varDecl;
using ::testing::_;
using ::testing::AllOf;
using ::testing::Contains;
using ::testing::ElementsAre;
using ::testing::IsEmpty;
using ::testing::IsSupersetOf;
using ::testing::Not;
using ::testing::ResultOf;
using ::testing::SizeIs;
using ::testing::UnorderedElementsAre;
test::EnableSmartPointers Enable;
constexpr llvm::StringRef CheckMacroDefinitions = R"cc(
// Bodies must reference the first param so that args are in the AST, but
// otherwise don't matter.
#define CHECK(x) (x)
#define CHECK_NE(a, b) (a, b)
)cc";
MATCHER_P3(isEvidenceMatcher, SlotMatcher, KindMatcher, SymbolMatcher, "") {
return SlotMatcher.Matches(static_cast<Slot>(arg.slot())) &&
KindMatcher.Matches(arg.kind()) && SymbolMatcher.Matches(arg.symbol());
}
MATCHER_P(functionNamed, Name, "") {
return llvm::StringRef(arg.usr()).contains(
("@" + llvm::StringRef(Name) + "#").str());
}
/// Matches a non-static field with the given name.
/// The name should be of the form "MyStruct::field", but it should be qualified
/// only by the enclosing type, not any namespaces.
MATCHER_P(fieldNamed, TypeQualifiedFieldName, "") {
const auto [TypeName, FieldName] =
llvm::StringRef(TypeQualifiedFieldName).split("::");
return arg.usr().ends_with(("@S@" + TypeName + "@FI@" + FieldName).str()) ||
arg.usr().ends_with(("@U@" + TypeName + "@FI@" + FieldName).str());
}
/// Matches a static field with the given name.
/// The name should be of the form "MyStruct::field" (see also comment for
/// `fieldNamed()`).
MATCHER_P(staticFieldNamed, TypeQualifiedFieldName, "") {
const auto [TypeName, FieldName] =
llvm::StringRef(TypeQualifiedFieldName).split("::");
return arg.usr().ends_with(("@S@" + TypeName + "@" + FieldName).str());
}
MATCHER_P(globalVarNamed, Name, "") {
return arg.usr() == ("c:@" + llvm::StringRef(Name)).str();
}
testing::Matcher<const Evidence&> evidence(
testing::Matcher<Slot> S, testing::Matcher<Evidence::Kind> Kind,
testing::Matcher<const Symbol&> SymbolMatcher = functionNamed("target")) {
return isEvidenceMatcher(S, Kind, SymbolMatcher);
}
std::vector<Evidence> collectFromDefinition(
clang::TestAST& AST, const Decl& Definition,
const NullabilityPragmas& Pragmas,
PreviousInferences InputInferences = {}) {
std::vector<Evidence> Results;
USRCache UsrCache;
// Can't assert from within a non-void helper function, so only EXPECT.
EXPECT_THAT_ERROR(
collectEvidenceFromDefinition(
Definition,
evidenceEmitter([&](const Evidence& E) { Results.push_back(E); },
UsrCache, AST.context()),
UsrCache, Pragmas, InputInferences),
llvm::Succeeded());
return Results;
}
std::vector<Evidence> collectFromDefinitionNamed(
llvm::StringRef TargetName, llvm::StringRef Source,
PreviousInferences InputInferences = {}) {
NullabilityPragmas Pragmas;
clang::TestAST AST(getAugmentedTestInputs(Source, Pragmas));
const Decl& Definition =
*dataflow::test::findValueDecl(AST.context(), TargetName);
return collectFromDefinition(AST, Definition, Pragmas, InputInferences);
}
/// Provides a default function-name-cased value for TargetName in
/// collectEvidenceFromDefinitionNamed, which puts TargetName first for
/// readability.
std::vector<Evidence> collectFromTargetFuncDefinition(
llvm::StringRef Source, PreviousInferences InputInferences = {}) {
return collectFromDefinitionNamed("target", Source, InputInferences);
}
template <typename MatcherT>
std::vector<Evidence> collectFromDefinitionMatching(
MatcherT Matcher, llvm::StringRef Source,
PreviousInferences InputInferences = {}) {
NullabilityPragmas Pragmas;
clang::TestAST AST(getAugmentedTestInputs(Source, Pragmas));
const Decl& Definition =
*selectFirst<Decl>("d", match(Matcher.bind("d"), AST.context()));
return collectFromDefinition(AST, Definition, Pragmas, InputInferences);
}
std::vector<Evidence> collectFromTargetDecl(llvm::StringRef Source) {
std::vector<Evidence> Results;
NullabilityPragmas Pragmas;
clang::TestAST AST(getAugmentedTestInputs(Source, Pragmas));
USRCache USRCache;
collectEvidenceFromTargetDeclaration(
*dataflow::test::findValueDecl(AST.context(), "target"),
evidenceEmitter([&](const Evidence& E) { Results.push_back(E); },
USRCache, AST.context()),
Pragmas);
return Results;
}
TEST(CollectEvidenceFromDefinitionTest, Location) {
llvm::StringRef Code = "void target(int *p) { *p; }";
// 12345678901234567890123456
// 0 1 2
auto Evidence = collectFromTargetFuncDefinition(Code);
ASSERT_THAT(Evidence, ElementsAre(evidence(paramSlot(0),
Evidence::UNCHECKED_DEREFERENCE)));
EXPECT_EQ("input.cc:1:23", Evidence.front().location());
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest, Location) {
llvm::StringRef Code =
"#include <memory>\nvoid target(std::unique_ptr<int> p) { *p; }";
// 123456789012345678901234567890123456789012
// 0 1 2 3 4
auto Evidence = collectFromTargetFuncDefinition(Code);
ASSERT_THAT(Evidence, ElementsAre(evidence(paramSlot(0),
Evidence::UNCHECKED_DEREFERENCE)));
EXPECT_EQ("input.cc:2:39", Evidence.front().location());
}
TEST(CollectEvidenceFromDefinitionTest, NoParams) {
static constexpr llvm::StringRef Src = R"cc(
void target() {}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest, OneParamUnused) {
static constexpr llvm::StringRef Src = R"cc(
void target(int *p0) {}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest, OneParamUsedWithoutRestriction) {
static constexpr llvm::StringRef Src = R"cc(
void takesUnknown(int *unknown) {}
void target(int *p0) { takesUnknown(p0); }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Not(Contains(evidence(_, _, functionNamed("target")))));
}
TEST(CollectEvidenceFromDefinitionTest, Deref) {
static constexpr llvm::StringRef Src = R"cc(
void target(int *p0, int *p1) {
int a = *p0;
if (p1 != nullptr) {
int b = *p1;
}
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE)));
}
TEST(CollectEvidenceFromDefinitionTest, DerefArrow) {
static constexpr llvm::StringRef Src = R"cc(
struct S {
int x;
int y();
};
void target(S *a, S *b) {
a->x;
b->y();
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE),
evidence(paramSlot(1), Evidence::UNCHECKED_DEREFERENCE)));
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest, Deref) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
struct S {
int x;
int y();
};
void target(std::unique_ptr<S> p) {
*p;
p->x;
p->y();
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE),
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE),
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE)));
}
TEST(CollectEvidenceFromDefinitionTest, DerefOfNonnull) {
static constexpr llvm::StringRef Src = R"cc(
void target(Nonnull<int *> p) {
*p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest, DereferenceBeforeAssignment) {
static constexpr llvm::StringRef Src = R"cc(
void target(int *p) {
*p;
int i = 1;
p = &i;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE)));
}
TEST(CollectEvidenceFromDefinitionTest, DereferenceAfterAssignment) {
static constexpr llvm::StringRef Src = R"cc(
void target(int *p) {
int i = 1;
p = &i;
*p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest, DereferenceAfterAssignmentFromReturn) {
static constexpr llvm::StringRef Src = R"cc(
int& getIntRef();
int* getIntPtr();
void target(int* p) {
p = &getIntRef();
*p;
p = getIntPtr();
*p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Not(Contains(evidence(_, _, functionNamed("target")))));
}
TEST(CollectEvidenceFromDefinitionTest, DerefOfPtrRef) {
static constexpr llvm::StringRef Src = R"cc(
void target(int *&p0, int *&p1) {
int a = *p0;
if (p1 != nullptr) {
int b = *p1;
}
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE)));
}
TEST(CollectEvidenceFromDefinitionTest, UnrelatedCondition) {
static constexpr llvm::StringRef Src = R"cc(
void target(int *p0, int *p1, int *p2, bool b) {
if (b) {
int a = *p0;
int b = *p1;
} else {
int a = *p0;
int c = *p2;
}
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE),
evidence(paramSlot(1), Evidence::UNCHECKED_DEREFERENCE),
// We collect two Evidence values for two dereferences of p0
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE),
evidence(paramSlot(2), Evidence::UNCHECKED_DEREFERENCE)));
}
TEST(CollectEvidenceFromDefinitionTest, LaterDeref) {
static constexpr llvm::StringRef Src = R"cc(
void target(int *p0) {
if (p0 == nullptr) {
(void)0;
} else {
(void)0;
}
int a = *p0;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE)));
}
TEST(CollectEvidenceFromDefinitionTest, DerefBeforeGuardedDeref) {
static constexpr llvm::StringRef Src = R"cc(
void target(int *p0) {
int a = *p0;
if (p0 != nullptr) {
int b = *p0;
}
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE)));
}
TEST(CollectEvidenceFromDefinitionTest, DerefAndOrCheckOfCopiedPtr) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p, int* q) {
int* a = p;
*a;
int* b = q;
if (q) {
*b;
}
if (b) {
*q;
}
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE)));
}
TEST(CollectEvidenceFromDefinitionTest, FirstSufficientSlotOnly) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p, int* q) {
// Marking either of p or q Nonnull is sufficient to avoid dereferencing
// without a check. We choose to record evidence only for the first
// sufficient slot which can be Nonnull without the dereference becoming
// dead code.
int* a;
if (p) {
a = p;
} else {
a = q;
}
*a;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE)));
}
TEST(CollectEvidenceFromDefinitionTest,
FirstSufficientSlotNotContradictingFlowConditions) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p, int* q) {
// Marking p Nonnull would make the dereference dead, so we collect
// evidence for q being Nonnull instead, since it is also sufficient.
if (!p) {
*q;
}
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(1), Evidence::UNCHECKED_DEREFERENCE)));
}
TEST(CollectEvidenceFromDefinitionTest, EarlyReturn) {
static constexpr llvm::StringRef Src = R"cc(
void target(int *p0) {
if (!p0) {
return;
}
int a = *p0;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest, UnreachableCode) {
static constexpr llvm::StringRef Src = R"cc(
void target(int *p0, int *p1, int *p2, int *p3) {
if (true) {
int a = *p0;
} else {
int a = *p1;
}
if (false) {
int a = *p2;
}
return;
int a = *p3;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE)));
}
TEST(CollectEvidenceFromDefinitionTest, PointerToMemberField) {
static constexpr llvm::StringRef Src = R"cc(
struct S {};
void target(int S::*p) {
S s;
s.*p;
(&s)->*p;
}
)cc";
// Pointers to members are not supported pointer types, so no evidence is
// collected. If they become a supported pointer type, this test should start
// failing.
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest, PointerToMemberMethod) {
static constexpr llvm::StringRef Src = R"cc(
struct S {};
void target(void (S::*p)()) {
S s;
(s.*p)();
((&s)->*p)();
}
)cc";
// Pointers to members are not supported pointer types, so no evidence is
// collected. If they become a supported pointer type, this test should start
// failing.
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest, PointerToMemberMethodArgs) {
static constexpr llvm::StringRef Src = R"cc(
struct S {};
void target(void (S::*p)(Nonnull<int*> i, int* j), int* q) {
S s;
(s.*p)(q, nullptr);
((&s)->*p)(q, nullptr);
}
)cc";
// Pointers to members are not supported pointer types, so no evidence is
// collected for `p` or `j`. If they become a supported pointer type, this
// test should start failing.
// TODO(b/309625642) We should still collect evidence for the use of `q` as an
// argument for param `i`.
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest, CheckMacro) {
llvm::Twine Src = CheckMacroDefinitions + R"cc(
void target(int* p, int* q, int* r, int* s, int* t, int* u, int* v) {
// should collect evidence for params from these calls
CHECK(p);
CHECK(q != nullptr);
int* a = nullptr;
CHECK(r != a);
CHECK(a != s);
bool b = t != nullptr;
CHECK(b);
// should not crash when analyzing these calls
CHECK(u == v);
CHECK(u != v);
CHECK(1);
struct S {
operator bool() const { return true; }
};
CHECK(S());
CHECK(true);
CHECK(false); // must come last because it's detected as causing the rest
// of the function to be dead.
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src.str()),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ABORT_IF_NULL),
evidence(paramSlot(1), Evidence::ABORT_IF_NULL),
evidence(paramSlot(2), Evidence::ABORT_IF_NULL),
evidence(paramSlot(3), Evidence::ABORT_IF_NULL),
evidence(paramSlot(4), Evidence::ABORT_IF_NULL)));
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest, CheckMacro) {
llvm::Twine Src = CheckMacroDefinitions + R"cc(
#include <memory>
void target(std::unique_ptr<int> p, std::unique_ptr<int> q,
std::unique_ptr<int> r) {
CHECK(p);
CHECK(!!q);
CHECK(r.get());
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src.str()),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ABORT_IF_NULL),
evidence(paramSlot(1), Evidence::ABORT_IF_NULL),
evidence(paramSlot(2), Evidence::ABORT_IF_NULL)));
}
TEST(CollectEvidenceFromDefinitionTest, CheckNEMacro) {
llvm::Twine Src = CheckMacroDefinitions + R"cc(
void target(int* p, int* q, int* r, int* s) {
// should collect evidence for params from these calls
CHECK_NE(p, nullptr);
CHECK_NE(nullptr, q);
int* a = nullptr;
CHECK_NE(a, r);
CHECK_NE(s, a);
// should not crash when analyzing these calls
CHECK_NE(a, 0);
int i = 1;
CHECK_NE(i, 0);
bool b = true;
CHECK_NE(true, false);
struct S {
bool operator==(const S&) const { return false; }
};
CHECK_NE(S(), S());
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src.str()),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ABORT_IF_NULL),
evidence(paramSlot(1), Evidence::ABORT_IF_NULL),
evidence(paramSlot(2), Evidence::ABORT_IF_NULL),
evidence(paramSlot(3), Evidence::ABORT_IF_NULL)));
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest, CheckNEMacro) {
llvm::Twine Src = CheckMacroDefinitions + R"cc(
#include <memory>
void target(std::unique_ptr<int> p, std::unique_ptr<int> q,
std::unique_ptr<int> r, std::unique_ptr<int> s) {
CHECK_NE(p, nullptr);
CHECK_NE(nullptr, q);
if (!r) {
CHECK_NE(s, r);
}
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src.str()),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ABORT_IF_NULL),
evidence(paramSlot(1), Evidence::ABORT_IF_NULL),
evidence(paramSlot(3), Evidence::ABORT_IF_NULL)));
}
TEST(CollectEvidenceFromDefinitionTest, NullableArgPassed) {
static constexpr llvm::StringRef Src = R"cc(
void callee(int *q);
void target(Nullable<int *> p) { callee(p); }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Contains(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest, NonnullArgPassed) {
static constexpr llvm::StringRef Src = R"cc(
void callee(int *q);
void target(Nonnull<int *> p) { callee(p); }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Contains(evidence(paramSlot(0), Evidence::NONNULL_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest, UnknownArgPassed) {
static constexpr llvm::StringRef Src = R"cc(
void callee(int *q);
void target(int *p) { callee(p); }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Contains(evidence(paramSlot(0), Evidence::UNKNOWN_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest, UnknownButProvablyNullArgPassed) {
static constexpr llvm::StringRef Src = R"cc(
void callee(int *q);
void target(int *p) {
if (p == nullptr) {
callee(p);
}
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Contains(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest, CheckedArgPassed) {
static constexpr llvm::StringRef Src = R"cc(
void callee(int *q);
void target(int *p) {
if (p) callee(p);
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Contains(evidence(paramSlot(0), Evidence::NONNULL_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest, NullptrPassed) {
static constexpr llvm::StringRef Src = R"cc(
void callee(int* q);
void target() {
callee(nullptr);
int* p = nullptr;
callee(nullptr);
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("callee")),
evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest, NonPtrArgPassed) {
static constexpr llvm::StringRef Src = R"cc(
void callee(int q);
void target(int p) { callee(p); }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest, ReferenceArgsPassed) {
static constexpr llvm::StringRef Src = R"cc(
void constCallee(int* const& a, int* const& b, int* const& c);
void mutableCallee(int*& a, int*& b, int*& c);
void target(Nullable<int*> p, Nonnull<int*> q, int* r) {
constCallee(p, q, r);
mutableCallee(p, q, r);
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::NULLABLE_REFERENCE_ARGUMENT,
functionNamed("constCallee")),
evidence(paramSlot(1), Evidence::NONNULL_REFERENCE_ARGUMENT,
functionNamed("constCallee")),
evidence(paramSlot(2), Evidence::UNKNOWN_REFERENCE_ARGUMENT,
functionNamed("constCallee")),
// TODO(b/344872191) Should be NULLABLE_REFERENCE_ARGUMENT.
evidence(paramSlot(0), Evidence::UNKNOWN_REFERENCE_ARGUMENT,
functionNamed("mutableCallee")),
// TODO(b/344872191) Should be NONNULL_REFERENCE_ARGUMENT.
evidence(paramSlot(1), Evidence::UNKNOWN_REFERENCE_ARGUMENT,
functionNamed("mutableCallee")),
evidence(paramSlot(2), Evidence::UNKNOWN_REFERENCE_ARGUMENT,
functionNamed("mutableCallee"))));
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest, ArgsAndParams) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
#include <utility>
void callee(std::unique_ptr<int> p, Nonnull<std::unique_ptr<int>> q,
Nullable<std::unique_ptr<int>>& r);
void target(Nullable<std::unique_ptr<int>> a, std::unique_ptr<int> b,
std::unique_ptr<int> c) {
callee(std::move(a), std::move(b), c);
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("callee")),
evidence(paramSlot(1), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
evidence(paramSlot(2), Evidence::ASSIGNED_TO_MUTABLE_NULLABLE,
functionNamed("target")),
evidence(paramSlot(1), Evidence::UNKNOWN_ARGUMENT,
functionNamed("callee")),
evidence(paramSlot(2), Evidence::UNKNOWN_REFERENCE_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest,
DefaultArgumentsProduceNoEvidenceFromDefinition) {
static constexpr llvm::StringRef Src = R"cc(
int* getDefault();
void hasDefaultUnannotatedFunc(int* = getDefault());
int* q = nullptr;
void hasDefaultUnannotatedVariable(int* = getDefault());
int i = 1;
void hasDefaultExpressionOfVariable(int* = &i);
void target() {
hasDefaultUnannotatedFunc();
hasDefaultUnannotatedVariable();
hasDefaultExpressionOfVariable();
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest, NullableReturn) {
static constexpr llvm::StringRef Src = R"cc(
int* target() { return nullptr; }
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE, Evidence::NULLABLE_RETURN,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, NullableButCheckedReturn) {
static constexpr llvm::StringRef Src = R"cc(
int* target(Nullable<int*> p) {
if (p) return p;
// no return in this path to avoid irrelevant evidence, and this still
// compiles, as the lack of return in a path is only a warning.
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE, Evidence::NONNULL_RETURN,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, NonnullReturn) {
static constexpr llvm::StringRef Src = R"cc(
int* target(Nonnull<int*> p) {
return p;
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE, Evidence::NONNULL_RETURN,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, UnknownReturn) {
static constexpr llvm::StringRef Src = R"cc(
int* target(int* p) { return p; }
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE, Evidence::UNKNOWN_RETURN,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, UnknownButProvablyNullReturn) {
static constexpr llvm::StringRef Src = R"cc(
int* target(int* p) {
if (p == nullptr) {
return p;
}
// no return in this path to avoid irrelevant evidence, and this still
// compiles, as the lack of return in a path is only a warning.
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE, Evidence::NULLABLE_RETURN,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, MultipleReturns) {
static constexpr llvm::StringRef Src = R"cc(
int* target(Nonnull<int*> p, Nullable<int*> q, bool b, bool c) {
if (b) return q;
if (c) return nullptr;
return p;
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE, Evidence::NULLABLE_RETURN,
functionNamed("target")),
evidence(SLOT_RETURN_TYPE, Evidence::NULLABLE_RETURN,
functionNamed("target")),
evidence(SLOT_RETURN_TYPE, Evidence::NONNULL_RETURN,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, ReferenceReturns) {
static constexpr llvm::StringRef Src = R"cc(
int*& target(Nonnull<int*>& p, Nullable<int*>& q, int*& r, bool a, bool b) {
if (a) return p;
if (b) return q;
return r;
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(SLOT_RETURN_TYPE, Evidence::NULLABLE_REFERENCE_RETURN),
evidence(SLOT_RETURN_TYPE, Evidence::NONNULL_REFERENCE_RETURN),
evidence(SLOT_RETURN_TYPE, Evidence::UNKNOWN_REFERENCE_RETURN)));
}
TEST(CollectEvidenceFromDefinitionTest, FromReturnAnnotation) {
static constexpr llvm::StringRef Src = R"cc(
Nonnull<int*> target(int* a) {
return a;
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest,
FromPreviouslyInferredReturnAnnotation) {
static constexpr llvm::StringRef Src = R"cc(
int* target(int* a) { return a; }
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(
Src, {.Nonnull = {fingerprint("c:@F@target#*I#", 0)}}),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
// We still collect evidence for the return type in case iteration
// turns up new evidence to contradict a previous inference. Only
// nullabilities written in source code are considered unchangeable.
evidence(SLOT_RETURN_TYPE, Evidence::UNKNOWN_RETURN,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, FromAutoReturnAnnotationByPragma) {
static constexpr llvm::StringRef Src = R"cc(
#pragma nullability file_default nonnull
int* getNonnull();
// The pragma applies to the int* deduced for the `auto` return type,
// making the return type Nonnull<int*>.
auto target(NullabilityUnknown<int*> a, bool b) {
if (b) return a;
return getNonnull();
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target"))));
}
// This is a crash repro related to functions with AttributedTypeLocs.
TEST(CollectEvidenceFromDefinitionTest, FromReturnInAttributedFunction) {
static constexpr llvm::StringRef Src = R"cc(
struct AStruct {
const char* target() [[clang::lifetimebound]] { return nullptr; }
};
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE, Evidence::NULLABLE_RETURN,
functionNamed("target"))));
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest, MultipleReturns) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
std::unique_ptr<int> target(Nonnull<std::unique_ptr<int>> p,
Nullable<std::unique_ptr<int>> q,
std::unique_ptr<int> r, bool a, bool b,
bool c) {
if (a) return nullptr;
if (b) return p;
if (c) return q;
return r;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(SLOT_RETURN_TYPE, Evidence::NULLABLE_RETURN),
evidence(SLOT_RETURN_TYPE, Evidence::NONNULL_RETURN),
evidence(SLOT_RETURN_TYPE, Evidence::NULLABLE_RETURN),
evidence(SLOT_RETURN_TYPE, Evidence::UNKNOWN_RETURN)));
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest, FromReturnAnnotation) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
Nonnull<std::unique_ptr<int>> target(std::unique_ptr<int> a) {
return a;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL)));
}
TEST(CollectEvidenceFromDefinitionTest, FunctionCallDereferenced) {
static constexpr llvm::StringRef Src = R"cc(
int* makePtr();
void target() { *makePtr(); }
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
Contains(evidence(SLOT_RETURN_TYPE, Evidence::UNCHECKED_DEREFERENCE,
functionNamed("makePtr"))));
}
TEST(CollectEvidenceFromDefinitionTest,
FunctionCallResultDereferencedAfterAssignedLocally) {
static constexpr llvm::StringRef Src = R"cc(
int* makePtr();
void target() {
auto p = makePtr();
*p;
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
Contains(evidence(SLOT_RETURN_TYPE, Evidence::UNCHECKED_DEREFERENCE,
functionNamed("makePtr"))));
}
TEST(CollectEvidenceFromDefinitionTest,
FunctionCallResultDereferencedAfterAssignedLocallyAndChecked) {
static constexpr llvm::StringRef Src = R"cc(
int* makePtr();
void target() {
auto p = makePtr();
if (p) *p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest,
FunctionCallResultDereferencedAfterUnrelatedConditionChecked) {
static constexpr llvm::StringRef Src = R"cc(
int* makePtr();
void target(bool cond) {
auto p = makePtr();
if (cond) *p;
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
Contains(evidence(SLOT_RETURN_TYPE, Evidence::UNCHECKED_DEREFERENCE,
functionNamed("makePtr"))));
}
TEST(CollectEvidenceFromDefinitionTest, FunctionCallDereferencedWithArrow) {
static constexpr llvm::StringRef Src = R"cc(
struct S {
void member();
};
S* makePtr();
void target() { makePtr()->member(); }
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
Contains(evidence(SLOT_RETURN_TYPE, Evidence::UNCHECKED_DEREFERENCE,
functionNamed("makePtr"))));
}
TEST(CollectEvidenceFromDefinitionTest,
AlreadyNonnullFunctionCallDereferenced) {
static constexpr llvm::StringRef Src = R"cc(
Nonnull<int*> makeNonnullPtr();
void target() { *makeNonnullPtr(); }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest, FunctionPointerCall) {
static constexpr llvm::StringRef Src = R"cc(
void target(void (*f)()) { f(); }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0),
Evidence::UNCHECKED_DEREFERENCE,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, ConstAccessorDereferencedAfterCheck) {
static constexpr llvm::StringRef Src = R"cc(
struct S {
int* accessor() const { return i; }
int* i = nullptr;
};
void target() {
S s;
if (s.accessor() != nullptr) {
*s.accessor();
}
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
// Special modeling of accessors is not implemented for accessors returning
// references.
TEST(CollectEvidenceFromDefinitionTest,
ReferenceConstAccessorDereferencedAfterCheck) {
static constexpr llvm::StringRef Src = R"cc(
struct S {
int* const& accessor() const { return i; }
int* i = nullptr;
};
void target() {
S s;
if (s.accessor() != nullptr) {
*s.accessor();
}
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE,
Evidence::UNCHECKED_DEREFERENCE,
functionNamed("accessor"))));
}
TEST(CollectEvidenceFromDefinitionTest,
ConstAccessorOnTwoDifferentObjectsDereferencedAfterCheck) {
static constexpr llvm::StringRef Src = R"cc(
struct S {
int* const& accessor() const { return i; }
int* i = nullptr;
};
S makeS();
void target() {
if (makeS().accessor() != nullptr) {
*makeS().accessor();
}
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE,
Evidence::UNCHECKED_DEREFERENCE,
functionNamed("accessor"))));
}
TEST(CollectEvidenceFromDefinitionTest, MemberCallOperatorReturnDereferenced) {
static constexpr llvm::StringRef Src = R"cc(
struct S {
int* operator()();
};
void target() {
S s;
*s();
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
Contains(evidence(SLOT_RETURN_TYPE, Evidence::UNCHECKED_DEREFERENCE,
functionNamed("operator()"))));
}
TEST(CollectEvidenceFromDefinitionTest, MemberOperatorCall) {
static constexpr llvm::StringRef Src = R"cc(
struct S {
bool operator+(int*);
};
void target() { S{} + nullptr; }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Contains(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("operator+"))));
}
TEST(CollectEvidenceFromDefinitionTest, NonMemberOperatorCall) {
static constexpr llvm::StringRef Src = R"cc(
struct S {};
bool operator+(const S&, int*);
void target() { S{} + nullptr; }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Contains(evidence(paramSlot(1), Evidence::NULLABLE_ARGUMENT,
functionNamed("operator+"))));
}
TEST(CollectEvidenceFromDefinitionTest, VarArgs) {
static constexpr llvm::StringRef Src = R"cc(
void callee(int*...);
void target() { callee(nullptr, nullptr); }
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest, MemberOperatorCallVarArgs) {
static constexpr llvm::StringRef Src = R"cc(
struct S {
bool operator()(int*...);
};
void target() { S{}(nullptr, nullptr); }
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("operator()"))));
}
TEST(CollectEvidenceFromDefinitionTest, ConstructorCall) {
static constexpr llvm::StringRef Src = R"cc(
struct S {
S(Nonnull<int*> a);
};
void target(int* p) { S s(p); }
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
evidence(paramSlot(0), Evidence::UNKNOWN_ARGUMENT,
functionNamed("S"))));
}
TEST(CollectEvidenceFromDefinitionTest, NonTargetConstructorCall) {
static constexpr llvm::StringRef Src = R"cc(
template <typename T>
struct S {
// Not a target due to templating, but the annotation here can still
// provide evidence for `p` from the call in `target`'s body.
S(Nonnull<T*> a);
};
void target(int* p) { S s(p); }
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, ConstructorWithBaseInitializer) {
static constexpr llvm::StringRef Src = R"cc(
struct TakeNonnull {
explicit TakeNonnull(Nonnull<int *>);
};
struct target : TakeNonnull {
target(int *i) : TakeNonnull(i) {}
};
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Contains(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, ConstructorWithDelegatingConstructor) {
static constexpr llvm::StringRef Src = R"cc(
struct target {
target(int* i);
target() : target(nullptr){};
};
)cc";
EXPECT_THAT(collectFromDefinitionMatching(
functionDecl(hasName("target"), parameterCountIs(0)), Src),
Contains(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, VariadicConstructorCall) {
static constexpr llvm::StringRef Src = R"cc(
struct S {
S(Nonnull<int*> i, ...);
};
void target(int* p, int* q) { S s(p, q); }
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
evidence(paramSlot(0), Evidence::UNKNOWN_ARGUMENT,
functionNamed("S"))));
}
TEST(CollectEvidenceFromDefinitionTest, FieldInitializerFromAssignmentToType) {
static constexpr llvm::StringRef Src = R"cc(
struct Target {
Target(int *Input) : I(Input) {}
Nonnull<int *> I;
};
)cc";
EXPECT_THAT(
collectFromDefinitionNamed("Target", Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("Target"))));
}
TEST(CollectEvidenceFromDefinitionTest, DefaultFieldInitializerNullptr) {
static constexpr llvm::StringRef Src = R"cc(
struct Target {
Target() {};
int* I = nullptr;
};
)cc";
EXPECT_THAT(collectFromDefinitionMatching(
cxxConstructorDecl(isDefaultConstructor()), Src),
UnorderedElementsAre(evidence(
Slot(0), Evidence::NULLPTR_DEFAULT_MEMBER_INITIALIZER,
fieldNamed("Target::I"))));
}
TEST(CollectEvidenceFromDefinitionTest, DefaultFieldInitializerNullable) {
static constexpr llvm::StringRef Src = R"cc(
Nullable<int*> G;
struct Target {
Target() {};
int* I = G;
};
)cc";
EXPECT_THAT(
collectFromDefinitionMatching(cxxConstructorDecl(isDefaultConstructor()),
Src),
UnorderedElementsAre(evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
fieldNamed("Target::I"))));
}
TEST(CollectEvidenceFromDefinitionTest,
IndirectFieldInitializerFromAssignmentToType) {
static constexpr llvm::StringRef Src = R"cc(
struct Target {
Target(int *Input) : I(Input) {}
struct {
Nonnull<int *> I;
};
};
)cc";
EXPECT_THAT(
collectFromDefinitionNamed("Target", Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("Target"))));
}
TEST(CollectEvidenceFromDefinitionTest, IndirectFieldDefaultFieldInitializer) {
static constexpr llvm::StringRef Src = R"cc(
struct Target {
Target() {}
struct {
int* I = nullptr;
};
};
// Use the implicitly-declared default constructor, so that it will be
// generated.
Target T;
)cc";
EXPECT_THAT(
collectFromDefinitionMatching(
cxxConstructorDecl(isDefaultConstructor(), hasName("Target")), Src),
UnorderedElementsAre(
evidence(Slot(0), Evidence::NULLPTR_DEFAULT_MEMBER_INITIALIZER,
fieldNamed("Target@Sa::I"))));
}
TEST(CollectEvidenceFromDefinitionTest, FieldInitializedWithNullable) {
static constexpr llvm::StringRef Src = R"cc(
struct Target {
Target(Nullable<int *> Input) : I(Input) {}
int *I;
};
)cc";
EXPECT_THAT(
collectFromDefinitionNamed("Target", Src),
UnorderedElementsAre(evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
fieldNamed("Target::I"))));
}
TEST(EvidenceEmitterTest, UnionFieldInitializedWithNullable) {
static constexpr llvm::StringRef Src = R"cc(
struct Target {
Target() : Field{nullptr} {};
union UnionType {
int* I;
bool* B;
} Field;
};
)cc";
EXPECT_THAT(
collectFromDefinitionNamed("Target", Src),
UnorderedElementsAre(evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
fieldNamed("UnionType::I"))));
}
TEST(CollectEvidenceFromDefinitionTest, FieldInitializerCallsFunction) {
static constexpr llvm::StringRef Src = R"cc(
int* getIntPtr(int*);
struct Target {
Target() : I(getIntPtr(nullptr)) {}
Nonnull<int*> I;
};
)cc";
EXPECT_THAT(collectFromDefinitionNamed("Target", Src),
UnorderedElementsAre(
evidence(SLOT_RETURN_TYPE, Evidence::ASSIGNED_TO_NONNULL,
functionNamed("getIntPtr")),
evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("getIntPtr"))));
}
TEST(CollectEvidenceFromDefinitionTest, DefaultFieldInitializerCallsFunction) {
static constexpr llvm::StringRef Src = R"cc(
int* getIntPtr(int*);
struct Target {
Target() = default;
Nonnull<int*> I = getIntPtr(nullptr);
};
// Use the explicitly-declared but still implicitly-defined default
// constructor, so that it will be generated.
Target T;
)cc";
EXPECT_THAT(collectFromDefinitionMatching(
cxxConstructorDecl(isDefaultConstructor()), Src),
UnorderedElementsAre(
evidence(SLOT_RETURN_TYPE, Evidence::ASSIGNED_TO_NONNULL,
functionNamed("getIntPtr")),
evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("getIntPtr"))));
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest,
FieldInitializerFromAssignmentToType) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
#include <utility>
struct Target {
Target(std::unique_ptr<int> Input) : I(std::move(Input)) {}
Nonnull<std::unique_ptr<int>> I;
};
)cc";
EXPECT_THAT(
collectFromDefinitionMatching(
cxxConstructorDecl(unless(isImplicit()), hasName("Target")), Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("Target"))));
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest,
FieldInitializedFromNullable) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
#include <utility>
struct Target {
Target(Nullable<std::unique_ptr<int>> Input) : I(std::move(Input)) {}
std::unique_ptr<int> I;
};
)cc";
EXPECT_THAT(
collectFromDefinitionMatching(
cxxConstructorDecl(unless(isImplicit()), hasName("Target")), Src),
UnorderedElementsAre(evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
fieldNamed("Target::I"))));
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest,
DefaultFieldInitializerNullptr) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
#include <utility>
struct Target {
std::unique_ptr<int> I = nullptr;
};
// Use the implicitly-declared default constructor, so that it will be
// generated.
Target T;
)cc";
EXPECT_THAT(
collectFromDefinitionMatching(
cxxConstructorDecl(isDefaultConstructor(), hasName("Target")), Src),
UnorderedElementsAre(
evidence(Slot(0), Evidence::NULLPTR_DEFAULT_MEMBER_INITIALIZER,
fieldNamed("Target::I"))));
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest,
DefaultFieldInitializerAbsentOnlyImplicitConstructor) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
#include <utility>
struct Target {
std::unique_ptr<int> I;
};
// Use the implicitly-declared default constructor, so that it will be
// generated.
Target T;
)cc";
EXPECT_THAT(
collectFromDefinitionMatching(
cxxConstructorDecl(isDefaultConstructor(), hasName("Target")), Src),
// By the end of the constructor body, the field is still only
// default-initialized, which for smart pointers means it is null.
UnorderedElementsAre(evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
fieldNamed("Target::I"))));
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest,
DefaultFieldInitializerAbsentInitializedInConstructor) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
#include <utility>
struct Target {
Target(int Input) { I = std::make_unique<int>(Input); }
std::unique_ptr<int> I;
};
)cc";
EXPECT_THAT(
collectFromDefinitionMatching(
cxxConstructorDecl(unless(isImplicit()), hasName("Target")), Src),
// No evidence collected from constructor body, which assigns a Nonnull
// value, and no evidence collected from *implicit* member initializer
// which default constructs to null.
IsEmpty());
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest,
DefaultFieldInitializerAbsentConditionalAssignmentInConstructor) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
#include <utility>
struct Target {
Target(int Input) {
if (Input != 0) {
I = std::make_unique<int>(Input);
}
}
std::unique_ptr<int> I;
};
)cc";
EXPECT_THAT(
collectFromDefinitionMatching(
cxxConstructorDecl(unless(isImplicit()), hasName("Target")), Src),
// By the end of the constructor body, the field is still potentially
// default-initialized, which for smart pointers means it may be null.
UnorderedElementsAre(evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
fieldNamed("Target::I"))));
}
// This is a crash repro.
TEST(SmartPointerCollectEvidenceFromDefinitionTest,
ConstructorExitingWithUnmodeledField) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
#include <utility>
struct Target {
// other.Field is not modeled and has no null state attached. Its value is
// coped into this.Field, leaving it without null state at the end of the
// constructor.
Target(Target& other) { *this = other; }
Target& operator=(const Target& other);
std::unique_ptr<int> Field;
};
)cc";
EXPECT_THAT(
collectFromDefinitionMatching(
cxxConstructorDecl(unless(isImplicit()), hasName("Target")), Src),
IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest, PassedToNonnull) {
static constexpr llvm::StringRef Src = R"cc(
void callee(Nonnull<int*> i);
void target(int* p) { callee(p); }
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
evidence(paramSlot(0), Evidence::UNKNOWN_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest, PassedToNonnullRef) {
static constexpr llvm::StringRef Src = R"cc(
void callee(Nonnull<int*>& i);
void target(int* p) { callee(p); }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
evidence(paramSlot(0), Evidence::UNKNOWN_REFERENCE_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest, PassedToNonnullInMemberFunction) {
static constexpr llvm::StringRef Src = R"cc(
struct S {
void callee(Nonnull<int*> i);
};
void target(int* p) {
S s;
s.callee(p);
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
evidence(paramSlot(0), Evidence::UNKNOWN_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest, PassedToNonnullInFunctionPointerParam) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p, void (*callee)(Nonnull<int*> i)) {
callee(p);
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
evidence(paramSlot(1), Evidence::UNCHECKED_DEREFERENCE,
functionNamed("target"))));
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest,
PassedToNonnullInFunctionPointerParam) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
#include <utility>
void target(std::unique_ptr<int*> p,
void (*callee)(Nonnull<std::unique_ptr<int*>> i)) {
callee(std::move(p));
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
evidence(paramSlot(1), Evidence::UNCHECKED_DEREFERENCE,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, PassedToNonnullInFunctionPointerField) {
static constexpr llvm::StringRef Src = R"cc(
struct MyStruct {
void (*callee)(Nonnull<int*>);
};
void target(int* p) { MyStruct().callee(p); }
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
evidence(Slot(0), Evidence::UNCHECKED_DEREFERENCE,
fieldNamed("MyStruct::callee"))));
}
TEST(CollectEvidenceFromDefinitionTest,
PassedToNonnullInFunctionPointerFromAddressOfFunctionDecl) {
static constexpr llvm::StringRef Src = R"cc(
void callee(Nonnull<int*> i);
void target(int* p) { (&callee)(p); }
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
evidence(paramSlot(0), Evidence::UNKNOWN_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest,
PassedToNonnullInFunctionReferenceParam) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p, void (&callee)(Nonnull<int*> i)) {
callee(p);
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest,
PassedToNonnullInFunctionPointerReferenceParam) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p, void (*&callee)(Nonnull<int*> i)) {
callee(p);
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
evidence(paramSlot(1), Evidence::UNCHECKED_DEREFERENCE,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, FunctionCallPassedToNonnull) {
static constexpr llvm::StringRef Src = R"cc(
void callee(Nonnull<int*> i);
int* makeIntPtr();
void target() { callee(makeIntPtr()); }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(SLOT_RETURN_TYPE, Evidence::ASSIGNED_TO_NONNULL,
functionNamed("makeIntPtr")),
evidence(paramSlot(0), Evidence::UNKNOWN_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest,
FunctionCallPassedToNonnullFunctionPointer) {
static constexpr llvm::StringRef Src = R"cc(
int* makeIntPtr();
void target(void (*callee)(Nonnull<int*> i)) { callee(makeIntPtr()); }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(SLOT_RETURN_TYPE, Evidence::ASSIGNED_TO_NONNULL,
functionNamed("makeIntPtr")),
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest,
FunctionCallPassedToNonnullFunctionPointerTargetNotAnInferenceTarget) {
static constexpr llvm::StringRef Src = R"cc(
int* makeIntPtr();
template <typename T>
void target(void (*callee)(Nonnull<T*> i), int* a) {
callee(makeIntPtr());
*a;
}
void instantiate() {
target<int>([](Nonnull<int*> i) {}, nullptr);
}
)cc";
// Doesn't collect any evidence for target from target's body, only collects
// some for makeIntPtr.
EXPECT_THAT(
collectFromDefinitionMatching(
functionDecl(hasName("target"), isTemplateInstantiation()), Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE,
Evidence::ASSIGNED_TO_NONNULL,
functionNamed("makeIntPtr"))));
}
TEST(CollectEvidenceFromDefinitionTest, PassedToNullable) {
static constexpr llvm::StringRef Src = R"cc(
void callee(Nullable<int*> i);
void target(int* p) { callee(p); }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Not(Contains(evidence(_, _, functionNamed("target")))));
}
TEST(CollectEvidenceFromDefinitionTest, PassedToNullableRef) {
static constexpr llvm::StringRef Src = R"cc(
void callee(Nullable<int*>& i);
void target(int* p) { callee(p); }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_TO_MUTABLE_NULLABLE,
functionNamed("target")),
evidence(paramSlot(0), Evidence::UNKNOWN_REFERENCE_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest,
PassedToNullableRefFromStoredFunctionCall) {
static constexpr llvm::StringRef Src = R"cc(
void callee(Nullable<int*>& i);
int* producer();
void target() {
auto p = producer();
callee(p);
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(SLOT_RETURN_TYPE, Evidence::ASSIGNED_TO_MUTABLE_NULLABLE,
functionNamed("producer")),
evidence(paramSlot(0), Evidence::UNKNOWN_REFERENCE_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest, PassedToNullableRefFromFunctionCall) {
static constexpr llvm::StringRef Src = R"cc(
void callee(Nullable<int*>& i);
int*& producer();
void target() { callee(producer()); }
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(SLOT_RETURN_TYPE, Evidence::ASSIGNED_TO_MUTABLE_NULLABLE,
functionNamed("producer")),
evidence(paramSlot(0), Evidence::UNKNOWN_REFERENCE_ARGUMENT,
functionNamed("callee"))));
}
TEST(CollectEvidenceFromDefinitionTest,
InitializationOfAndAssignmentToNonnull) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p, int* q, int* r) {
Nonnull<int*> a = p, b = q;
a = r;
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
evidence(paramSlot(1), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
evidence(paramSlot(2), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target"))));
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest,
InitializationOfAndAssignmentToNonnull) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
#include <utility>
struct S {
Nonnull<std::unique_ptr<int>> a;
Nonnull<std::unique_ptr<int>>& getRef();
};
void target(std::unique_ptr<int> p, Nonnull<std::unique_ptr<int>> q,
std::unique_ptr<int> r, std::unique_ptr<int> s,
std::unique_ptr<int> t) {
q = std::move(p);
S AnS;
AnS.a = std::move(r);
AnS.getRef() = std::move(s);
Nonnull<std::unique_ptr<int>> nonnull = std::move(t);
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL),
evidence(paramSlot(2), Evidence::ASSIGNED_TO_NONNULL),
evidence(paramSlot(3), Evidence::ASSIGNED_TO_NONNULL),
evidence(paramSlot(4), Evidence::ASSIGNED_TO_NONNULL)));
}
TEST(CollectEvidenceFromDefinitionTest, InitializationOfNonnullRefFromRef) {
static constexpr llvm::StringRef Src = R"cc(
void target(int*& p) {
Nonnull<int*>& a = p;
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest,
NonnullNonTargetInitializedFromFunctionCall) {
static constexpr llvm::StringRef Src = R"cc(
int* makeIntPtr();
template <typename T>
void target() {
Nonnull<T*> p = makeIntPtr();
}
void instantiate() { target<int>(); }
)cc";
// Doesn't collect any evidence for target from target's body, only collects
// some for makeIntPtr.
EXPECT_THAT(
collectFromDefinitionMatching(
functionDecl(hasName("target"), isTemplateInstantiation()), Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE,
Evidence::ASSIGNED_TO_NONNULL,
functionNamed("makeIntPtr"))));
}
TEST(CollectEvidenceFromDefinitionTest,
InitializationOfAndAssignmentToNullableOrUnknown) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p, int* q, int* r) {
Nullable<int*> a = p;
int* b = q;
NullabilityUnknown<int*> c = r;
q = r;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest, InitializationOfNullableRef) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p) {
Nullable<int*>& a = p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_TO_MUTABLE_NULLABLE,
functionNamed("target"))));
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest,
InitializationOfNullableRef) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
void target(std::unique_ptr<int> p) {
Nullable<std::unique_ptr<int>>& a = p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_TO_MUTABLE_NULLABLE,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, InitializationOfNullableRefFromRef) {
static constexpr llvm::StringRef Src = R"cc(
void target(int*& p) {
Nullable<int*>& a = p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_TO_MUTABLE_NULLABLE,
functionNamed("target"))));
}
// Ternary expressions are not currently modeled correctly by the analysis, but
// are necessary to test the case of multiple connected decls.
//
// DISABLED until ternary expressions are handle.
TEST(CollectEvidenceFromDefinitionTest,
DISABLED_InitializationOfNullableRefAllConnectedDecls) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p, int* q, bool b) {
Nullable<int*>& x = b ? p : q;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_TO_MUTABLE_NULLABLE,
functionNamed("target")),
evidence(paramSlot(1), Evidence::ASSIGNED_TO_MUTABLE_NULLABLE,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, AssignedFromNullptr) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p, int* q) {
q = nullptr;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(1),
Evidence::ASSIGNED_FROM_NULLABLE,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, AssignedFromNullptrIndirect) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p) {
int* a = nullptr;
p = a;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0),
Evidence::ASSIGNED_FROM_NULLABLE,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, AssignedFromZero) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p, int* q) {
q = 0;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(1),
Evidence::ASSIGNED_FROM_NULLABLE,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, AssignedFromNullable) {
static constexpr llvm::StringRef Src = R"cc(
Nullable<int*> getNullable();
void target(int* p) { p = getNullable(); }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0),
Evidence::ASSIGNED_FROM_NULLABLE,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, AssignedFromLocalNullable) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p) {
Nullable<int*> a;
p = a;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0),
Evidence::ASSIGNED_FROM_NULLABLE,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, AssignedFromNullableMemberCallExpr) {
static constexpr llvm::StringRef Src = R"cc(
struct S {
int*& getPtrRef();
};
void target(S AnS) { AnS.getPtrRef() = nullptr; }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE,
Evidence::ASSIGNED_FROM_NULLABLE,
functionNamed("getPtrRef"))));
}
TEST(CollectEvidenceFromDefinitionTest, AssignedFromNullptrMultipleOperators) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p) {
*&p = nullptr;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0),
Evidence::ASSIGNED_FROM_NULLABLE,
functionNamed("target"))));
}
// This is a regression test for a bug where we collected ASSIGNED_FROM_NULLABLE
// evidence for the return type of `foo`, because the LHS type of the assignment
// was already nullable, and so any formula does imply that the LHS type of the
// assignment is nullable.
TEST(CollectEvidenceFromDefinitionTest,
AnnotatedLocalAssignedFromNullableAfterFunctionReturn) {
static constexpr llvm::StringRef Src = R"cc(
int* foo();
void target() {
Nullable<int*> p = foo();
p = nullptr;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest,
IrrelevantAssignmentsAndInitializations) {
static constexpr llvm::StringRef Src = R"cc(
struct S {
S(int* i);
};
void target(int* p) {
int* a = p; // No useful information.
// We don't collect if types on either side are not a supported pointer
// type.
int* b = 0;
int c = 4;
bool d = false;
S e = a;
// We don't collect from compound assignments.
b += 8;
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
// From the constructor call constructing an S; no evidence from
// assignments or initializations.
UnorderedElementsAre(evidence(paramSlot(0), Evidence::UNKNOWN_ARGUMENT,
functionNamed("S"))));
}
TEST(CollectEvidenceFromDefinitionTest, Arithmetic) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* a, int* b, int* c, int* d, int* e, int* f, int* g,
int* h) {
a += 1;
b -= 2;
c + 3;
d - 4;
e++;
++f;
g--;
--h;
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ARITHMETIC, functionNamed("target")),
evidence(paramSlot(1), Evidence::ARITHMETIC, functionNamed("target")),
evidence(paramSlot(2), Evidence::ARITHMETIC, functionNamed("target")),
evidence(paramSlot(3), Evidence::ARITHMETIC, functionNamed("target")),
// TODO(b/349795343): Re-enable this expectation when we use a
// pre-transfer-function evidence collection callback.
// evidence(paramSlot(4), Evidence::ARITHMETIC,
// functionNamed("target")),
evidence(paramSlot(5), Evidence::ARITHMETIC, functionNamed("target")),
// TODO(b/349795343): Re-enable this expectation when we use a
// pre-transfer-function evidence collection callback.
// evidence(paramSlot(6), Evidence::ARITHMETIC,
// functionNamed("target")),
evidence(paramSlot(7), Evidence::ARITHMETIC,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, Fields) {
llvm::Twine Src = CheckMacroDefinitions + R"cc(
#include <memory>
void takesNonnull(Nonnull<int*>);
void takesMutableNullable(Nullable<int*>&);
struct S {
int* Deref;
int* AssignedToNonnull;
int* AssignedToMutableNullable;
int* AbortIfNull;
int* AbortIfNullBool;
int* AbortIfNullNE;
int* AssignedFromNullable;
int* Arithmetic;
std::unique_ptr<int> SmartDeref;
};
void target(S AnS) {
*AnS.Deref;
takesNonnull(AnS.AssignedToNonnull);
takesMutableNullable(AnS.AssignedToMutableNullable);
CHECK(AnS.AbortIfNull);
CHECK(AnS.AbortIfNullBool != nullptr);
CHECK_NE(AnS.AbortIfNullNE, nullptr);
AnS.AssignedFromNullable = nullptr;
AnS.Arithmetic += 4;
*AnS.SmartDeref;
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src.str()),
IsSupersetOf(
{evidence(Slot(0), Evidence::UNCHECKED_DEREFERENCE,
fieldNamed("S::Deref")),
evidence(Slot(0), Evidence::ASSIGNED_TO_NONNULL,
fieldNamed("S::AssignedToNonnull")),
evidence(Slot(0), Evidence::ASSIGNED_TO_MUTABLE_NULLABLE,
fieldNamed("S::AssignedToMutableNullable")),
evidence(Slot(0), Evidence::ABORT_IF_NULL,
fieldNamed("S::AbortIfNull")),
evidence(Slot(0), Evidence::ABORT_IF_NULL,
fieldNamed("S::AbortIfNullBool")),
evidence(Slot(0), Evidence::ABORT_IF_NULL,
fieldNamed("S::AbortIfNullNE")),
evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
fieldNamed("S::AssignedFromNullable")),
evidence(Slot(0), Evidence::ARITHMETIC, fieldNamed("S::Arithmetic")),
evidence(Slot(0), Evidence::UNCHECKED_DEREFERENCE,
fieldNamed("S::SmartDeref"))}));
}
TEST(CollectEvidenceFromDefinitionTest, StaticMemberVariables) {
llvm::Twine Src = CheckMacroDefinitions + R"cc(
#include <memory>
void takesNonnull(Nonnull<int*>);
void takesMutableNullable(Nullable<int*>&);
struct MyStruct {
static int* Deref;
static int* AssignedToNonnull;
static int* AssignedToMutableNullable;
static int* AbortIfNull;
static int* AbortIfNullBool;
static int* AbortIfNullNE;
static int* AssignedFromNullable;
static int* Arithmetic;
static std::unique_ptr<int> SmartDeref;
};
void target() {
*MyStruct::Deref;
takesNonnull(MyStruct::AssignedToNonnull);
takesMutableNullable(MyStruct::AssignedToMutableNullable);
CHECK(MyStruct::AbortIfNull);
CHECK(MyStruct::AbortIfNullBool != nullptr);
CHECK_NE(MyStruct::AbortIfNullNE, nullptr);
MyStruct::AssignedFromNullable = nullptr;
MyStruct::Arithmetic += 4;
*MyStruct::SmartDeref;
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src.str()),
IsSupersetOf(
{evidence(Slot(0), Evidence::UNCHECKED_DEREFERENCE,
staticFieldNamed("MyStruct::Deref")),
evidence(Slot(0), Evidence::ASSIGNED_TO_NONNULL,
staticFieldNamed("MyStruct::AssignedToNonnull")),
evidence(Slot(0), Evidence::ASSIGNED_TO_MUTABLE_NULLABLE,
staticFieldNamed("MyStruct::AssignedToMutableNullable")),
evidence(Slot(0), Evidence::ABORT_IF_NULL,
staticFieldNamed("MyStruct::AbortIfNull")),
evidence(Slot(0), Evidence::ABORT_IF_NULL,
staticFieldNamed("MyStruct::AbortIfNullBool")),
evidence(Slot(0), Evidence::ABORT_IF_NULL,
staticFieldNamed("MyStruct::AbortIfNullNE")),
evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
staticFieldNamed("MyStruct::AssignedFromNullable")),
evidence(Slot(0), Evidence::ARITHMETIC,
staticFieldNamed("MyStruct::Arithmetic")),
evidence(Slot(0), Evidence::UNCHECKED_DEREFERENCE,
staticFieldNamed("MyStruct::SmartDeref"))}));
}
TEST(CollectEvidenceFromDefinitionTest, Globals) {
llvm::Twine Src = CheckMacroDefinitions + R"cc(
#include <memory>
void takesNonnull(Nonnull<int*>);
void takesMutableNullable(Nullable<int*>&);
int* Deref;
int* AssignedToNonnull;
int* AssignedToMutableNullable;
int* AbortIfNull;
int* AbortIfNullBool;
int* AbortIfNullNE;
int* AssignedFromNullable;
int* Arithmetic;
std::unique_ptr<int> SmartDeref;
void target() {
*Deref;
takesNonnull(AssignedToNonnull);
takesMutableNullable(AssignedToMutableNullable);
CHECK(AbortIfNull);
CHECK(AbortIfNullBool != nullptr);
CHECK_NE(AbortIfNullNE, nullptr);
AssignedFromNullable = nullptr;
Arithmetic += 4;
*SmartDeref;
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src.str()),
IsSupersetOf({evidence(Slot(0), Evidence::UNCHECKED_DEREFERENCE,
globalVarNamed("Deref")),
evidence(Slot(0), Evidence::ASSIGNED_TO_NONNULL,
globalVarNamed("AssignedToNonnull")),
evidence(Slot(0), Evidence::ASSIGNED_TO_MUTABLE_NULLABLE,
globalVarNamed("AssignedToMutableNullable")),
evidence(Slot(0), Evidence::ABORT_IF_NULL,
globalVarNamed("AbortIfNull")),
evidence(Slot(0), Evidence::ABORT_IF_NULL,
globalVarNamed("AbortIfNullBool")),
evidence(Slot(0), Evidence::ABORT_IF_NULL,
globalVarNamed("AbortIfNullNE")),
evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
globalVarNamed("AssignedFromNullable")),
evidence(Slot(0), Evidence::ARITHMETIC,
globalVarNamed("Arithmetic")),
evidence(Slot(0), Evidence::UNCHECKED_DEREFERENCE,
globalVarNamed("SmartDeref"))}));
}
TEST(CollectEvidenceFromDefinitionTest, GlobalInit) {
static constexpr llvm::StringRef Src = R"cc(
int* getPtr();
Nullable<int*> getNullableFromNonnull(Nonnull<int*>);
int* Target = static_cast<int*>(getNullableFromNonnull(getPtr()));
)cc";
EXPECT_THAT(collectFromDefinitionNamed("Target", Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::UNKNOWN_ARGUMENT,
functionNamed("getNullableFromNonnull")),
evidence(SLOT_RETURN_TYPE, Evidence::ASSIGNED_TO_NONNULL,
functionNamed("getPtr")),
evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
globalVarNamed("Target"))));
}
TEST(CollectEvidenceFromDefinitionTest, GlobalInitFromGlobalAnnotation) {
static constexpr llvm::StringRef Src = R"cc(
int* foo();
Nonnull<int*> Target = foo();
)cc";
EXPECT_THAT(collectFromDefinitionNamed("Target", Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE,
Evidence::ASSIGNED_TO_NONNULL,
functionNamed("foo"))));
}
TEST(CollectEvidenceFromDefinitionTest, GlobalSmartImplicitInit) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
// This has an implicit init because of default construction.
std::unique_ptr<int> Target;
)cc";
EXPECT_THAT(
collectFromDefinitionNamed("Target", Src),
UnorderedElementsAre(evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
globalVarNamed("Target"))));
}
TEST(CollectEvidenceFromDefinitionTest, GlobalSmartExplicitInit) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
std::unique_ptr<int> Target = nullptr;
)cc";
EXPECT_THAT(
collectFromDefinitionNamed("Target", Src),
UnorderedElementsAre(evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
globalVarNamed("Target"))));
}
TEST(CollectEvidenceFromDefinitionTest, StaticInitInClass) {
static constexpr llvm::StringRef Src = R"cc(
struct MyStruct {
inline static int* Target = nullptr;
};
)cc";
EXPECT_THAT(
collectFromDefinitionNamed("Target", Src),
UnorderedElementsAre(evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
staticFieldNamed("MyStruct::Target"))));
}
AST_MATCHER(VarDecl, hasInit) { return Node.hasInit(); }
TEST(CollectEvidenceFromDefinitionTest, StaticInitOutOfClass) {
static constexpr llvm::StringRef Src = R"cc(
struct MyStruct {
static int* Target;
};
int* MyStruct::Target = nullptr;
)cc";
EXPECT_THAT(collectFromDefinitionMatching(varDecl(hasInit()), Src),
UnorderedElementsAre(
evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
staticFieldNamed("MyStruct::Target"))));
}
TEST(CollectEvidenceFromDefinitionTest, NoEvidenceForLocals) {
static constexpr llvm::StringRef Src = R"cc(
void target() {
int* p = nullptr;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest, FunctionCallInLoop) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p) {
for (int i = 0; i < 3; ++i) {
target(nullptr);
}
for (int i = 0; i < 3; ++i) {
target(&i);
}
for (int i = 0; i < 3; ++i) {
target(p);
}
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("target")),
evidence(paramSlot(0), Evidence::UNKNOWN_ARGUMENT,
functionNamed("target")),
evidence(paramSlot(0), Evidence::NONNULL_ARGUMENT,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, OutputParameterPointerToPointer) {
static constexpr llvm::StringRef Src = R"cc(
void maybeModifyPtr(int** a);
void target(int* p) {
maybeModifyPtr(&p);
*p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Not(Contains(evidence(_, _, functionNamed("target")))));
}
TEST(CollectEvidenceFromDefinitionTest, OutputParameterReferenceToPointer) {
static constexpr llvm::StringRef Src = R"cc(
void maybeModifyPtr(int*& a);
void target(int* p) {
maybeModifyPtr(p);
*p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Not(Contains(evidence(_, _, functionNamed("target")))));
}
TEST(CollectEvidenceFromDefinitionTest,
OutputParameterReferenceToConstPointer) {
static constexpr llvm::StringRef Src = R"cc(
void dontModifyPtr(int* const& a);
void target(int* p) {
dontModifyPtr(p);
*p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Contains(evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest,
OutputParameterReferenceToPointerToPointer) {
static constexpr llvm::StringRef Src = R"cc(
void maybeModifyPtr(int**& a);
void target(int** p) {
maybeModifyPtr(p);
*p;
**p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Not(Contains(evidence(_, _, functionNamed("target")))));
}
TEST(CollectEvidenceFromDefinitionTest, OutputParameterPointerToConstPointer) {
static constexpr llvm::StringRef Src = R"cc(
void dontModifyPtr(int* const* a);
void target(int* p) {
dontModifyPtr(&p);
*p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Contains(evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest,
OutputParameterConstPointerToPointerToConst) {
static constexpr llvm::StringRef Src = R"cc(
// Outer pointer and int are const, but inner pointer can still be modified.
void maybeModifyPtr(const int** const a);
void target(const int* p) {
maybeModifyPtr(&p);
*p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Not(Contains(evidence(_, _, functionNamed("target")))));
}
TEST(CollectEvidenceFromDefinitionTest, PassAsOutputParameterOrDereference) {
static constexpr llvm::StringRef Src = R"cc(
void maybeModifyPtr(int** a);
void target(int* p, bool b) {
if (b) {
maybeModifyPtr(&p);
} else {
*p;
}
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Contains(evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest,
ConditionallyPassAsOutputParameterAlwaysDereference) {
static constexpr llvm::StringRef Src = R"cc(
void maybeModifyPtr(int** a);
void target(int* p, bool b) {
if (b) maybeModifyPtr(&p);
*p; // Because we model p as Unknown post-output-parameter-use, adding an
// annotation would not be considered sufficient to make this
// dereference safe, so we do not collect evidence for p.
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Not(Contains(evidence(_, _, functionNamed("target")))));
}
TEST(CollectEvidenceFromDefinitionTest, FromGlobalLabmdaBodyForGlobal) {
static constexpr llvm::StringRef Src = R"cc(
int* p;
auto Lambda = []() { *p; };
)cc";
EXPECT_THAT(
collectFromDefinitionNamed("operator()", Src),
UnorderedElementsAre(evidence(Slot(0), Evidence::UNCHECKED_DEREFERENCE,
globalVarNamed("p"))));
}
// TODO(b/315967534) Collect for captured function parameters, specifically from
// the unchecked dereference of `foo`'s parameter.
TEST(CollectEvidenceFromDefinitionTest, FromLocalLambdaForCapturedParam) {
static constexpr llvm::StringRef Src = R"cc(
void foo(int* p) {
auto Lambda = [&p]() { *p; };
}
)cc";
EXPECT_THAT(collectFromDefinitionNamed("operator()", Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest, FromLocalLambdaForCalledFunction) {
static constexpr llvm::StringRef Src = R"cc(
int* bar();
void foo() {
auto Lambda = []() { *bar(); };
}
)cc";
EXPECT_THAT(collectFromDefinitionNamed("operator()", Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE,
Evidence::UNCHECKED_DEREFERENCE,
functionNamed("bar"))));
}
TEST(CollectEvidenceFromDefinitionTest, ForLambdaParamOrReturn) {
static constexpr llvm::StringRef Src = R"cc(
auto Lambda = [](int* p) -> int* {
*p;
return nullptr;
};
)cc";
EXPECT_THAT(collectFromDefinitionNamed("operator()", Src),
UnorderedElementsAre(
evidence(SLOT_RETURN_TYPE, Evidence::NULLABLE_RETURN,
functionNamed("operator()")),
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE,
functionNamed("operator()"))));
}
TEST(CollectEvidenceFromDefinitionTest, AggregateInitialization) {
static constexpr llvm::StringRef Header = R"cc(
struct Base {
bool* BaseB;
Nonnull<char*> BaseC;
};
struct MyStruct : public Base {
int* I;
bool* B;
};
)cc";
const llvm::Twine BracesAggInit = Header + R"cc(
void target(Nullable<bool*> Bool, char* Char) {
MyStruct{Bool, Char, nullptr, Bool};
}
)cc";
// New aggregate initialization syntax in C++20
const llvm::Twine ParensAggInit = Header + R"cc(
void target(Nullable<bool*> Bool, char* Char) {
MyStruct(Base(Bool, Char), nullptr, Bool);
}
)cc";
auto ExpectedEvidenceMatcher =
UnorderedElementsAre(evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
fieldNamed("Base::BaseB")),
evidence(paramSlot(1), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
fieldNamed("MyStruct::I")),
evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
fieldNamed("MyStruct::B")));
EXPECT_THAT(collectFromTargetFuncDefinition(BracesAggInit.str()),
ExpectedEvidenceMatcher);
EXPECT_THAT(collectFromTargetFuncDefinition(ParensAggInit.str()),
ExpectedEvidenceMatcher);
}
TEST(SmartPointerCollectEvidenceFromDefinitionTest, AggregateInitialization) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
#include <utility>
struct MyStruct {
std::unique_ptr<int> p;
Nonnull<std::unique_ptr<int>> q;
std::unique_ptr<int> r;
};
void target(Nullable<std::unique_ptr<int>> a, std::unique_ptr<int> b) {
MyStruct{std::move(a), std::move(b), nullptr};
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
fieldNamed("MyStruct::p")),
evidence(paramSlot(1), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target")),
evidence(Slot(0), Evidence::ASSIGNED_FROM_NULLABLE,
fieldNamed("MyStruct::r"))));
}
// This is a crash repro related to aggregate initialization.
TEST(CollectEvidenceFromDefinitionTest, NonRecordInitListExpr) {
static constexpr llvm::StringRef Src = R"cc(
void target() { int a[3] = {}; }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src), IsEmpty());
}
TEST(CollectEvidenceFromDefinitionTest,
SmartPointerAnalysisProvidesEvidenceForRawPointer) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
void foo(int*);
void target(Nullable<std::unique_ptr<int>> p) { foo(p.get()); }
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
Contains(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("foo"))));
}
// Evidence for return type nonnull-ness should flow only from derived to base,
// so we collect evidence for the base but not the derived.
TEST(CollectEvidenceFromDefinitionTest, FromVirtualDerivedForReturnNonnull) {
static constexpr llvm::StringRef Src = R"cc(
struct Base {
virtual int* foo();
};
struct Derived : public Base {
int* foo() override {
static int i;
return &i;
}
};
void target() {
Derived D;
*D.foo();
}
)cc";
EXPECT_THAT(
collectFromDefinitionNamed("Derived::foo", Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE, Evidence::NONNULL_RETURN,
functionNamed("Derived@F@foo"))));
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE,
Evidence::UNCHECKED_DEREFERENCE,
functionNamed("Derived@F@foo"))));
}
TEST(CollectEvidenceFromDefinitionTest, FromVirtualDerivedForReturnNullable) {
static constexpr llvm::StringRef Src = R"cc(
struct Base {
virtual int* foo();
};
struct Derived : public Base {
int* foo() override { return nullptr; }
};
)cc";
EXPECT_THAT(
collectFromDefinitionNamed("Derived::foo", Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE, Evidence::NULLABLE_RETURN,
functionNamed("Derived@F@foo")),
evidence(SLOT_RETURN_TYPE, Evidence::NULLABLE_RETURN,
functionNamed("Base@F@foo"))));
// We don't currently have any evidence kinds that can force a non-reference
// top-level pointer return type to be nullable from its usage, so no other
// expectation.
}
TEST(CollectEvidenceFromDefinitionTest, FromVirtualDerivedForParamNonnull) {
static constexpr llvm::StringRef Src = R"cc(
struct Base {
virtual void foo(int* p);
};
struct Derived : public Base {
void foo(int* p) override { *p; }
};
void target() {
int i;
Derived D;
D.foo(&i);
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::NONNULL_ARGUMENT,
functionNamed("Derived@F@foo")),
evidence(paramSlot(0), Evidence::NONNULL_ARGUMENT,
functionNamed("Base@F@foo"))));
EXPECT_THAT(collectFromDefinitionNamed("Derived::foo", Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE,
functionNamed("Derived@F@foo")),
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE,
functionNamed("Base@F@foo"))));
}
// Evidence for parameter nullable-ness should flow only from base to derived,
// so we collect evidence for the derived but not the base.
TEST(CollectEvidenceFromDefinitionTest, FromVirtualDerivedForParamNullable) {
static constexpr llvm::StringRef Src = R"cc(
struct Base {
virtual void foo(int* p);
};
struct Derived : public Base {
void foo(int* p) override { p = nullptr; }
};
void target() {
Derived D;
D.foo(nullptr);
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("Derived@F@foo"))));
EXPECT_THAT(collectFromDefinitionNamed("Derived::foo", Src),
UnorderedElementsAre(evidence(paramSlot(0),
Evidence::ASSIGNED_FROM_NULLABLE,
functionNamed("Derived@F@foo"))));
}
TEST(CollectEvidenceFromDefinitionTest, FromVirtualBaseForReturnNonnull) {
static constexpr llvm::StringRef Src = R"cc(
struct Base {
virtual int* foo() {
static int i;
return &i;
}
};
struct Derived : public Base {
int* foo() override;
};
void target() {
Base B;
*B.foo();
}
)cc";
EXPECT_THAT(
collectFromDefinitionNamed("Base::foo", Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE, Evidence::NONNULL_RETURN,
functionNamed("Base@F@foo")),
evidence(SLOT_RETURN_TYPE, Evidence::NONNULL_RETURN,
functionNamed("Derived@F@foo"))));
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(SLOT_RETURN_TYPE, Evidence::UNCHECKED_DEREFERENCE,
functionNamed("Base@F@foo")),
evidence(SLOT_RETURN_TYPE, Evidence::UNCHECKED_DEREFERENCE,
functionNamed("Derived@F@foo"))));
}
// Evidence for return type nullable-ness should flow only from derived to base,
// so we collect evidence for the base but not the derived.
TEST(CollectEvidenceFromDefinitionTest, FromVirtualBaseForReturnNullable) {
static constexpr llvm::StringRef Src = R"cc(
struct Base {
virtual int* foo() { return nullptr; }
};
struct Derived : public Base {
int* foo() override;
};
)cc";
EXPECT_THAT(
collectFromDefinitionNamed("Base::foo", Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE, Evidence::NULLABLE_RETURN,
functionNamed("Base@F@foo"))));
// We don't currently have any evidence kinds that can force a non-reference
// top-level pointer return type to be nullable from its usage, so no other
// expectation.
}
// Evidence for parameter nonnull-ness should flow only from derived to base, so
// we collect evidence for the base but not the derived.
TEST(CollectEvidenceFromDefinitionTest, FromVirtualBaseForParamNonnull) {
static constexpr llvm::StringRef Src = R"cc(
struct Base {
virtual void foo(int* p) { *p; }
};
struct Derived : public Base {
void foo(int* p) override;
};
void target() {
int i;
Base B;
B.foo(&i);
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::NONNULL_ARGUMENT,
functionNamed("Base@F@foo"))));
EXPECT_THAT(collectFromDefinitionNamed("Base::foo", Src),
UnorderedElementsAre(evidence(paramSlot(0),
Evidence::UNCHECKED_DEREFERENCE,
functionNamed("Base@F@foo"))));
}
TEST(CollectEvidenceFromDefinitionTest, FromVirtualBaseForParamNullable) {
static constexpr llvm::StringRef Src = R"cc(
struct Base {
virtual void foo(int* p) { p = nullptr; }
};
struct Derived : public Base {
void foo(int* p) override;
};
void target() {
Base B;
B.foo(nullptr);
}
)cc";
EXPECT_THAT(
collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("Base@F@foo")),
evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("Derived@F@foo"))));
EXPECT_THAT(collectFromDefinitionNamed("Base::foo", Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_FROM_NULLABLE,
functionNamed("Base@F@foo")),
evidence(paramSlot(0), Evidence::ASSIGNED_FROM_NULLABLE,
functionNamed("Derived@F@foo"))));
}
TEST(CollectEvidenceFromDefinitionTest, FromVirtualDerivedMultipleLayers) {
static constexpr llvm::StringRef Src = R"cc(
struct Base {
virtual int* foo();
};
struct Derived : public Base {
virtual int* foo();
};
struct DerivedDerived : public Derived {
int* foo() override { return nullptr; };
};
)cc";
EXPECT_THAT(
collectFromDefinitionNamed("DerivedDerived::foo", Src),
UnorderedElementsAre(evidence(SLOT_RETURN_TYPE, Evidence::NULLABLE_RETURN,
functionNamed("DerivedDerived@F@foo")),
evidence(SLOT_RETURN_TYPE, Evidence::NULLABLE_RETURN,
functionNamed("Derived@F@foo")),
evidence(SLOT_RETURN_TYPE, Evidence::NULLABLE_RETURN,
functionNamed("Base@F@foo"))));
}
TEST(CollectEvidenceFromDefinitionTest, FromVirtualBaseMultipleLayers) {
static constexpr llvm::StringRef Src = R"cc(
struct Base {
virtual void foo(int* p) { p = nullptr; }
};
struct Derived : public Base {
virtual void foo(int*);
};
struct DerivedDerived : public Derived {
void foo(int*) override;
};
)cc";
EXPECT_THAT(collectFromDefinitionNamed("Base::foo", Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_FROM_NULLABLE,
functionNamed("DerivedDerived@F@foo")),
evidence(paramSlot(0), Evidence::ASSIGNED_FROM_NULLABLE,
functionNamed("Derived@F@foo")),
evidence(paramSlot(0), Evidence::ASSIGNED_FROM_NULLABLE,
functionNamed("Base@F@foo"))));
}
TEST(CollectEvidenceFromDefinitionTest, NotInferenceTarget) {
static constexpr llvm::StringRef Src = R"cc(
void isATarget(Nonnull<int*> a);
template <typename T>
T* target(T* p) {
*p;
Nonnull<int*> a = p;
isATarget(p);
target<T>(nullptr);
target<int>(nullptr);
return nullptr;
}
void instantiate() { target<int>(nullptr); }
)cc";
// Doesn't collect any evidence for target from target's body, only collects
// some for isATarget.
EXPECT_THAT(
collectFromDefinitionMatching(
functionDecl(hasName("target"), isTemplateInstantiation()), Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::UNKNOWN_ARGUMENT,
functionNamed("isATarget"))));
}
TEST(CollectEvidenceFromDefinitionTest, PropagatesPreviousInferences) {
static constexpr llvm::StringRef Src = R"cc(
void calledWithToBeNullable(int* x);
void calledWithToBeNonnull(int* a);
void target(int* p, int* q) {
target(nullptr, q);
calledWithToBeNullable(p);
*q;
calledWithToBeNonnull(q);
}
)cc";
std::string TargetUsr = "c:@F@target#*I#S0_#";
std::vector ExpectedBothRoundResults = {
evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
AllOf(functionNamed("target"),
// Double-check that target's usr is as expected before we
// use it to create SlotFingerprints.
ResultOf([](Symbol S) { return S.usr(); }, TargetUsr))),
evidence(paramSlot(1), Evidence::UNCHECKED_DEREFERENCE,
functionNamed("target")),
};
std::vector ExpectedSecondRoundResults = {
evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("calledWithToBeNullable")),
evidence(paramSlot(0), Evidence::NONNULL_ARGUMENT,
functionNamed("calledWithToBeNonnull"))};
// Only proceed if we have the correct USR for target and the first round
// results contain the evidence needed to produce our expected inferences and
// do not contain the evidence only found from propagating inferences from the
// first round.
auto FirstRoundResults = collectFromTargetFuncDefinition(Src);
ASSERT_THAT(FirstRoundResults, IsSupersetOf(ExpectedBothRoundResults));
for (const auto& E : ExpectedSecondRoundResults) {
ASSERT_THAT(FirstRoundResults, Not(Contains(E)));
}
EXPECT_THAT(collectFromTargetFuncDefinition(
Src, {/*Nullable=*/{fingerprint(TargetUsr, paramSlot(0))},
/*Nonnull=*/{fingerprint(TargetUsr, paramSlot(1))}}),
AllOf(IsSupersetOf(ExpectedBothRoundResults),
IsSupersetOf(ExpectedSecondRoundResults)));
}
TEST(CollectEvidenceFromDefinitionTest,
AnalysisUsesPreviousInferencesForSlotsOutsideTargetDefinition) {
static constexpr llvm::StringRef Src = R"cc(
int* returnsToBeNonnull(int* a) {
return a;
}
int* target(int* q) {
*q;
return returnsToBeNonnull(q);
}
)cc";
std::string TargetUsr = "c:@F@target#*I#";
std::string ReturnsToBeNonnullUsr = "c:@F@returnsToBeNonnull#*I#";
const llvm::DenseMap<int, std::vector<testing::Matcher<const Evidence&>>>
ExpectedNewResultsPerRound = {
{0,
{evidence(
paramSlot(0), Evidence::UNCHECKED_DEREFERENCE,
AllOf(functionNamed("target"),
// Double-check that target's usr is as expected before
// we use it to create SlotFingerprints.
ResultOf([](Symbol S) { return S.usr(); }, TargetUsr)))}},
{1,
{evidence(
paramSlot(0), Evidence::NONNULL_ARGUMENT,
AllOf(functionNamed("returnsToBeNonnull"),
// Double-check that returnsToBeNonnull's usr is as
// expected before we use it to create SlotFingerprints.
ResultOf([](Symbol S) { return S.usr(); },
ReturnsToBeNonnullUsr)))}},
{2,
{
// No new evidence from target's definition in this round, but in
// a full-TU analysis, this would be the round where we decide
// returnsToBeNonnull returns Nonnull, based on the now-Nonnull
// argument that is the only return value.
}},
{3,
{evidence(SLOT_RETURN_TYPE, Evidence::NONNULL_RETURN,
functionNamed("target"))}}};
// Assert first round results because they don't rely on previous inference
// propagation at all and in this case are test setup and preconditions.
auto FirstRoundResults = collectFromTargetFuncDefinition(Src);
ASSERT_THAT(FirstRoundResults,
IsSupersetOf(ExpectedNewResultsPerRound.at(0)));
for (const auto& E : ExpectedNewResultsPerRound.at(1)) {
ASSERT_THAT(FirstRoundResults, Not(Contains(E)));
}
auto SecondRoundResults = collectFromTargetFuncDefinition(
Src, {.Nonnull = {fingerprint(TargetUsr, paramSlot(0))}});
EXPECT_THAT(SecondRoundResults,
AllOf(IsSupersetOf(ExpectedNewResultsPerRound.at(0)),
IsSupersetOf(ExpectedNewResultsPerRound.at(1))));
for (const auto& E : ExpectedNewResultsPerRound.at(2)) {
ASSERT_THAT(SecondRoundResults, Not(Contains(E)));
}
auto ThirdRoundResults = collectFromTargetFuncDefinition(
Src, {.Nonnull = {fingerprint(TargetUsr, paramSlot(0)),
fingerprint(ReturnsToBeNonnullUsr, paramSlot(0))}});
EXPECT_THAT(ThirdRoundResults,
AllOf(IsSupersetOf(ExpectedNewResultsPerRound.at(0)),
IsSupersetOf(ExpectedNewResultsPerRound.at(1)),
IsSupersetOf(ExpectedNewResultsPerRound.at(2))));
for (const auto& E : ExpectedNewResultsPerRound.at(3)) {
ASSERT_THAT(ThirdRoundResults, Not(Contains(E)));
}
auto FourthRoundResults = collectFromTargetFuncDefinition(
Src, {.Nonnull = {
fingerprint(TargetUsr, paramSlot(0)),
fingerprint(ReturnsToBeNonnullUsr, paramSlot(0)),
// As noted in the Evidence matcher list above, we don't infer
// the return type of returnsToBeNonnull from only collecting
// evidence from target's definition, but for the sake of this
// test, let's pretend we collected evidence from the entire TU.
fingerprint(ReturnsToBeNonnullUsr, SLOT_RETURN_TYPE)}});
EXPECT_THAT(FourthRoundResults,
AllOf(IsSupersetOf(ExpectedNewResultsPerRound.at(0)),
IsSupersetOf(ExpectedNewResultsPerRound.at(1)),
IsSupersetOf(ExpectedNewResultsPerRound.at(2)),
IsSupersetOf(ExpectedNewResultsPerRound.at(3))));
}
TEST(CollectEvidenceFromDefinitionTest,
PreviousInferencesOfNonTargetParameterNullabilitiesPropagate) {
static constexpr llvm::StringRef Src = R"cc(
void takesToBeNonnull(int* a) {
// Not read when collecting evidence only from Target, but corresponding
// inference is explicitly input below.
*a;
}
void target(int* q) { takesToBeNonnull(q); }
)cc";
std::string TakesToBeNonnullUsr = "c:@F@takesToBeNonnull#*I#";
// Pretend that in a first round of inferring for all functions, we made this
// inference about takesToBeNonnull's first parameter.
// This test confirms that we use that information when collecting from
// target's definition.
EXPECT_THAT(
collectFromTargetFuncDefinition(
Src, {.Nonnull = {fingerprint(TakesToBeNonnullUsr, paramSlot(0))}}),
Contains(evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDefinitionTest, Pragma) {
static constexpr llvm::StringRef Src = R"cc(
#pragma nullability file_default nonnull
int* target(NullabilityUnknown<int*> p) {
return p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL)));
}
TEST(CollectEvidenceFromDefinitionTest, PragmaLocalTopLevelPointer) {
static constexpr llvm::StringRef Src = R"cc(
#pragma nullability file_default nonnull
void target(NullabilityUnknown<int*> p) {
int* local_top_level_pointer = p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL)));
}
// Just confirming that the test setup to run both FrontendActions is working.
TEST(CollectEvidenceFromDefinitionTest, PragmaAndMacroReplace) {
llvm::Twine Src = CheckMacroDefinitions + R"cc(
#pragma nullability file_default nonnull
int* target(NullabilityUnknown<int*> p) {
CHECK(p);
return p;
}
)cc";
EXPECT_THAT(collectFromTargetFuncDefinition(Src.str()),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ASSIGNED_TO_NONNULL),
evidence(paramSlot(0), Evidence::ABORT_IF_NULL)));
}
TEST(CollectEvidenceFromDefinitionTest, SolverLimitReached) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* p, int* q) {
*p;
*q;
}
)cc";
NullabilityPragmas Pragmas;
clang::TestAST AST(getAugmentedTestInputs(Src, Pragmas));
std::vector<Evidence> Results;
USRCache UsrCache;
EXPECT_THAT_ERROR(
collectEvidenceFromDefinition(
*cast<FunctionDecl>(
dataflow::test::findValueDecl(AST.context(), "target")),
evidenceEmitter([&](const Evidence& E) { Results.push_back(E); },
UsrCache, AST.context()),
UsrCache, Pragmas, /*PreviousInferences=*/{},
// Enough iterations to collect one piece of evidence but not both.
[]() {
return std::make_unique<dataflow::WatchedLiteralsSolver>(
/*MaxSATIterations=*/100);
}),
llvm::FailedWithMessage("SAT solver reached iteration limit"));
EXPECT_THAT(Results, SizeIs(1));
}
TEST(CollectEvidenceFromDeclarationTest, GlobalVariable) {
llvm::StringLiteral Src = R"cc(
Nullable<int *> target;
)cc";
EXPECT_THAT(collectFromTargetDecl(Src),
ElementsAre(evidence(Slot(0), Evidence::ANNOTATED_NULLABLE,
globalVarNamed("target"))));
}
TEST(SmartPointerCollectEvidenceFromDeclarationTest, GlobalVariable) {
llvm::StringLiteral Src = R"cc(
#include <memory>
Nullable<std::unique_ptr<int>> target;
)cc";
EXPECT_THAT(collectFromTargetDecl(Src),
ElementsAre(evidence(Slot(0), Evidence::ANNOTATED_NULLABLE,
globalVarNamed("target"))));
}
TEST(CollectEvidenceFromDeclarationTest, StaticMemberVariable) {
llvm::StringLiteral Src = R"cc(
struct S {
static Nonnull<int*> target;
};
)cc";
EXPECT_THAT(collectFromTargetDecl(Src),
ElementsAre(evidence(Slot(0), Evidence::ANNOTATED_NONNULL,
staticFieldNamed("S::target"))));
}
TEST(CollectEvidenceFromDeclarationTest, Field) {
llvm::StringLiteral Src = R"cc(
struct S {
Nonnull<int*> target;
};
)cc";
EXPECT_THAT(collectFromTargetDecl(Src),
ElementsAre(evidence(Slot(0), Evidence::ANNOTATED_NONNULL,
fieldNamed("S::target"))));
}
TEST(SmartPointerCollectEvidenceFromDeclarationTest, Field) {
llvm::StringLiteral Src = R"cc(
#include <memory>
struct S {
Nonnull<std::unique_ptr<int>> target;
};
)cc";
EXPECT_THAT(collectFromTargetDecl(Src),
ElementsAre(evidence(Slot(0), Evidence::ANNOTATED_NONNULL,
fieldNamed("S::target"))));
}
TEST(CollectEvidenceFromDeclarationTest, FunctionDeclReturnType) {
llvm::StringLiteral Src = R"cc(
Nonnull<int *> target();
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
ElementsAre(evidence(SLOT_RETURN_TYPE, Evidence::ANNOTATED_NONNULL,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDeclarationTest, FunctionDeclParams) {
llvm::StringLiteral Src = R"cc(
void target(Nullable<int*>, int*, Nonnull<int*>);
)cc";
EXPECT_THAT(collectFromTargetDecl(Src),
ElementsAre(evidence(paramSlot(0), Evidence::ANNOTATED_NULLABLE),
evidence(paramSlot(2), Evidence::ANNOTATED_NONNULL)));
}
TEST(CollectEvidenceFromDeclarationTest, FunctionDeclNonTopLevel) {
llvm::StringLiteral Src = R"cc(
Nonnull<int*>** target(Nullable<int*>*);
)cc";
EXPECT_THAT(collectFromTargetDecl(Src), IsEmpty());
}
TEST(SmartPointerCollectEvidenceFromDeclarationTest, FunctionDecl) {
llvm::StringLiteral Src = R"cc(
#include <memory>
Nullable<std::unique_ptr<int>> target(Nonnull<std::unique_ptr<int>>,
Nullable<std::unique_ptr<int>>);
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
ElementsAre(evidence(SLOT_RETURN_TYPE, Evidence::ANNOTATED_NULLABLE),
evidence(paramSlot(0), Evidence::ANNOTATED_NONNULL),
evidence(paramSlot(1), Evidence::ANNOTATED_NULLABLE)));
}
TEST(CollectEvidenceFromDeclarationTest, FunctionTemplateIgnored) {
// We used to inspect the type of `target` and crash.
llvm::StringLiteral Src = R"cc(
template <class A>
struct S {
template <class B>
static void target(const S<B>&) {}
};
)cc";
EXPECT_THAT(collectFromTargetDecl(Src), IsEmpty());
}
TEST(CollectEvidenceFromDeclarationTest, DefaultArgumentNullptrLiteral) {
static constexpr llvm::StringRef Src = R"cc(
void target(int* = nullptr);
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDeclarationTest, DefaultArgumentZeroLiteral) {
static constexpr llvm::StringRef Src =
R"cc(
void target(int* = 0);
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDeclarationTest, DefaultArgumentAnnotatedVariable) {
static constexpr llvm::StringRef Src = R"cc(
Nonnull<int*> q;
void target(int* = q);
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::NONNULL_ARGUMENT,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDeclarationTest,
DefaultArgumentCallingAnnotatedFunction) {
static constexpr llvm::StringRef Src = R"cc(
Nullable<int*> getDefault();
void target(int* = getDefault());
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDeclarationTest,
DefaultArgumentUnannotatedNonLiteralExpressionsUnknown) {
static constexpr llvm::StringRef Src = R"cc(
int* getDefault();
int* q = nullptr;
int i = 1;
void target(int* = getDefault(), int* = q, int* = &i);
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::UNKNOWN_ARGUMENT,
functionNamed("target")),
evidence(paramSlot(1), Evidence::UNKNOWN_ARGUMENT,
functionNamed("target")),
evidence(paramSlot(2), Evidence::UNKNOWN_ARGUMENT,
functionNamed("target"))));
}
TEST(SmartPointerCollectEvidenceFromDeclarationTest,
DefaultArgumentNullptrLiteral) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
void target(std::unique_ptr<int> = nullptr);
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("target"))));
}
TEST(SmartPointerCollectEvidenceFromDeclarationTest,
DefaultArgumentMakeUniqueTooComplex) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
void target(std::unique_ptr<int> = std::make_unique<int>(1));
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::UNKNOWN_ARGUMENT,
functionNamed("target"))));
}
TEST(SmartPointerCollectEvidenceFromDeclarationTest,
DefaultArgumentReferenceTypesNullptrLiteral) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
void target(const std::unique_ptr<int>& pl = nullptr,
std::unique_ptr<int>&& pr = nullptr);
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
UnorderedElementsAre(evidence(paramSlot(0), Evidence::NULLABLE_ARGUMENT,
functionNamed("target")),
evidence(paramSlot(1), Evidence::NULLABLE_ARGUMENT,
functionNamed("target"))));
}
TEST(CollectEvidenceFromDeclarationTest, NonnullAttributeOnFunction) {
static constexpr llvm::StringRef Src = R"cc(
int* target(int* p, int** q, int*& r, bool b) __attribute__((nonnull));
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
// attribute applies to top-level non-reference raw pointer
// parameter types only, not return type or other params.
ElementsAre(evidence(paramSlot(0), Evidence::GCC_NONNULL_ATTRIBUTE),
evidence(paramSlot(1), Evidence::GCC_NONNULL_ATTRIBUTE)));
}
TEST(CollectEvidenceFromDeclarationTest, NonnullAttributeOnFunctionWithArgs) {
static constexpr llvm::StringRef Src = R"cc(
int* target(int* p, int** q, int*& r, bool b, int* not_indicated)
__attribute__((nonnull(1, 2, 3, 4)));
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
// attribute applies to the indicated and eligible parameters only.
ElementsAre(evidence(paramSlot(0), Evidence::GCC_NONNULL_ATTRIBUTE),
evidence(paramSlot(1), Evidence::GCC_NONNULL_ATTRIBUTE)));
}
TEST(CollectEvidenceFromDeclarationTest, NonnullAttributeOnMethodWithArgs) {
static constexpr llvm::StringRef Src = R"cc(
struct T {
// Index 1 on a non-static method is for the implicit `this` parameter.
int* target(int* p, int* not_indicated) __attribute__((nonnull(2)));
};
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
ElementsAre(evidence(paramSlot(0), Evidence::GCC_NONNULL_ATTRIBUTE)));
}
TEST(CollectEvidenceFromDeclarationTest,
NonnullAttributeOnStaticMethodWithArgs) {
static constexpr llvm::StringRef Src = R"cc(
struct T {
// no implicit `this` parameter for static methods.
static int* target(int* p, int* q) __attribute__((nonnull(2)));
};
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
ElementsAre(evidence(paramSlot(1), Evidence::GCC_NONNULL_ATTRIBUTE)));
}
TEST(CollectEvidenceFromDeclarationTest, NonnullAttributeOnParam) {
static constexpr llvm::StringRef Src = R"cc(
int* target(int* p __attribute__((nonnull())), int* not_indicated);
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
ElementsAre(evidence(paramSlot(0), Evidence::GCC_NONNULL_ATTRIBUTE)));
}
TEST(SmartPointerCollectEvidenceFromDeclarationTest,
NonnullAttributeOnFunction) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
void target(std::unique_ptr<int> p, std::unique_ptr<int>* q,
std::unique_ptr<int*> r) __attribute__((nonnull));
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
// attribute applies to top-level non-reference *raw* pointer
// parameter types only.
ElementsAre(evidence(paramSlot(1), Evidence::GCC_NONNULL_ATTRIBUTE)));
}
TEST(CollectEvidenceFromDeclarationTest, ReturnsNonnullAttribute) {
static constexpr llvm::StringRef Src = R"cc(
int** target() __attribute__((returns_nonnull));
)cc";
EXPECT_THAT(
collectFromTargetDecl(Src),
// Affects the top-level pointer.
ElementsAre(evidence(SLOT_RETURN_TYPE, Evidence::GCC_NONNULL_ATTRIBUTE)));
}
TEST(CollectEvidenceFromDeclarationTest, ReturnsNonnullAttributeReference) {
static constexpr llvm::StringRef Src = R"cc(
int*& target() __attribute__((returns_nonnull));
)cc";
// No effect on reference types.
EXPECT_THAT(collectFromTargetDecl(Src), IsEmpty());
}
TEST(SmartPointerCollectEvidenceFromDeclarationTest, ReturnsNonnullAttribute) {
static constexpr llvm::StringRef Src = R"cc(
#include <memory>
std::unique_ptr<int> target() __attribute__((returns_nonnull));
)cc";
// No effect on smart pointers.
EXPECT_THAT(collectFromTargetDecl(Src), IsEmpty());
}
TEST(CollectEvidenceFromDeclarationTest, Pragma) {
static constexpr llvm::StringRef Src = R"cc(
#pragma nullability file_default nonnull
void target(int* p);
)cc";
EXPECT_THAT(collectFromTargetDecl(Src),
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::ANNOTATED_NONNULL)));
}
MATCHER_P(declNamed, Name, "") {
std::string Actual;
llvm::raw_string_ostream OS(Actual);
if (auto* ND = dyn_cast<NamedDecl>(arg))
ND->getNameForDiagnostic(
OS, arg->getDeclContext()->getParentASTContext().getPrintingPolicy(),
/*Qualified=*/true);
return ::testing::ExplainMatchResult(Name, Actual, result_listener);
}
TEST(EvidenceSitesTest, Functions) {
TestAST AST(R"cc(
void foo();
void bar();
void bar() {}
void baz() {}
struct S {
S() {}
void member();
};
void S::member() {}
)cc");
auto Sites = EvidenceSites::discover(AST.context());
EXPECT_THAT(
Sites.Declarations,
UnorderedElementsAre(declNamed("foo"), declNamed("bar"), declNamed("bar"),
declNamed("baz"), declNamed("S::S"),
declNamed("S::member"), declNamed("S::member")));
EXPECT_THAT(Sites.Definitions,
UnorderedElementsAre(declNamed("bar"), declNamed("baz"),
declNamed("S::S"), declNamed("S::member")));
}
TEST(EvidenceSitesTest, Lambdas) {
TestAST AST(R"cc(
auto Lambda = []() {};
)cc");
auto Sites = EvidenceSites::discover(AST.context());
EXPECT_THAT(Sites.Declarations,
UnorderedElementsAre(declNamed("(anonymous class)::operator()")));
EXPECT_THAT(Sites.Definitions,
UnorderedElementsAre(declNamed("(anonymous class)::operator()"),
declNamed("Lambda")));
}
TEST(EvidenceSitesTest, GlobalVariables) {
NullabilityPragmas Pragmas;
TestAST AST = getAugmentedTestInputs(
R"cc(
#include <memory>
int* x = true ? nullptr : nullptr;
int* y;
int a;
int b = *y;
std::unique_ptr<int> p;
std::unique_ptr<int> q = nullptr;
)cc",
Pragmas);
auto Sites = EvidenceSites::discover(AST.context());
EXPECT_THAT(Sites.Declarations,
UnorderedElementsAre(declNamed("x"), declNamed("y"),
declNamed("p"), declNamed("q")));
EXPECT_THAT(
Sites.Definitions,
UnorderedElementsAre(
declNamed("x"), declNamed("b"),
// unique_ptr p has an initializer because of default construction.
declNamed("p"), declNamed("q")));
}
TEST(EvidenceSitesTest, StaticMemberVariables) {
TestAST AST(R"cc(
struct S {
inline static int* a = nullptr;
static int* b;
static int* c;
};
int* S::c = nullptr;
)cc");
auto Sites = EvidenceSites::discover(AST.context());
EXPECT_THAT(
Sites.Declarations,
UnorderedElementsAre(
declNamed("S::a"), declNamed("S::b"),
// one for in-class declaration and one for out-of-class definition
declNamed("S::c"), declNamed("S::c")));
EXPECT_THAT(Sites.Definitions,
UnorderedElementsAre(declNamed("S::a"), declNamed("S::c")));
}
TEST(EvidenceSitesTest, NonStaticMemberVariables) {
NullabilityPragmas Pragmas;
TestAST AST = getAugmentedTestInputs(
R"cc(
#include <memory>
struct S {
int* a = nullptr;
int* b;
std::unique_ptr<int> p = nullptr;
std::unique_ptr<int> q;
};
)cc",
Pragmas);
auto Sites = EvidenceSites::discover(AST.context());
EXPECT_THAT(Sites.Declarations,
UnorderedElementsAre(declNamed("S::a"), declNamed("S::b"),
declNamed("S::p"), declNamed("S::q")));
EXPECT_THAT(Sites.Definitions, IsEmpty());
}
TEST(EvidenceSitesTest, LocalVariablesNotIncluded) {
TestAST AST(R"cc(
void foo() {
int* p = nullptr;
static int* q = nullptr;
}
)cc");
auto Sites = EvidenceSites::discover(AST.context());
EXPECT_THAT(Sites.Declarations, UnorderedElementsAre(declNamed("foo")));
EXPECT_THAT(Sites.Definitions, UnorderedElementsAre(declNamed("foo")));
}
TEST(EvidenceSitesTest, Templates) {
TestAST AST(R"cc(
template <int I>
int f() {
return I;
}
template <>
int f<1>() {
return 1;
}
struct S {
template <int I>
int f() {
return I;
}
};
template <int I>
struct T {
int f() { return I; }
};
template <int I>
int v = 1;
int Unused = f<0>() + f<1>() + S{}.f<0>() + T<0>{}.f() + v<0>;
)cc");
auto Sites = EvidenceSites::discover(AST.context());
// Relevant declarations are the written ones that are not templates.
EXPECT_THAT(Sites.Declarations, UnorderedElementsAre(declNamed("f<1>")));
// Instantiations are relevant definitions, as is the global variable Unused.
EXPECT_THAT(Sites.Definitions,
UnorderedElementsAre(declNamed("f<0>"), declNamed("f<1>"),
declNamed("S::f<0>"), declNamed("T<0>::f"),
declNamed("v<0>"), declNamed("Unused")));
for (auto* Def : Sites.Definitions) {
std::string Actual;
llvm::raw_string_ostream OS(Actual);
if (auto* ND = dyn_cast<NamedDecl>(Def))
ND->getNameForDiagnostic(
OS, Def->getDeclContext()->getParentASTContext().getPrintingPolicy(),
/*Qualified=*/true);
llvm::errs() << "Actual: " << Actual << "\n";
}
}
TEST(EvidenceEmitterTest, NotInferenceTarget) {
TestAST AST(R"cc(
template <int I>
int target() {
return I;
})cc");
const auto* TargetDecl =
dataflow::test::findValueDecl(AST.context(), "target");
ASSERT_NE(TargetDecl, nullptr);
USRCache USRCache;
EXPECT_DEATH(
evidenceEmitter([](const Evidence& E) {}, USRCache, AST.context())(
*TargetDecl, Slot{}, Evidence::ANNOTATED_UNKNOWN,
TargetDecl->getLocation()),
"not an inference target");
}
} // namespace
} // namespace clang::tidy::nullability