Don't override nullability for params that already have annotations.
PiperOrigin-RevId: 548801024
Change-Id: If34247f979ae959eff8a623ca0379ddf948184a1
diff --git a/nullability/inference/collect_evidence_test.cc b/nullability/inference/collect_evidence_test.cc
index ccf82ee..1fa7375 100644
--- a/nullability/inference/collect_evidence_test.cc
+++ b/nullability/inference/collect_evidence_test.cc
@@ -42,10 +42,23 @@
("@" + llvm::StringRef(Name) + "#").str());
}
+clang::TestInputs getInputsWithAnnotationDefinitions(llvm::StringRef Source) {
+ clang::TestInputs Inputs = Source;
+ Inputs.ExtraFiles["nullability.h"] = R"cc(
+ template <typename T>
+ using Nullable [[clang::annotate("Nullable")]] = T;
+ template <typename T>
+ using Nonnull [[clang::annotate("Nonnull")]] = T;
+ )cc";
+ Inputs.ExtraArgs.push_back("-include");
+ Inputs.ExtraArgs.push_back("nullability.h");
+ return Inputs;
+}
+
std::vector<Evidence> collectEvidenceFromTargetFunction(
llvm::StringRef Source) {
std::vector<Evidence> Results;
- clang::TestAST AST(Source);
+ clang::TestAST AST(getInputsWithAnnotationDefinitions(Source));
auto Err = collectEvidenceFromImplementation(
cast<FunctionDecl>(
*dataflow::test::findValueDecl(AST.context(), "target")),
@@ -56,16 +69,7 @@
std::vector<Evidence> collectEvidenceFromTargetDecl(llvm::StringRef Source) {
std::vector<Evidence> Results;
- clang::TestInputs Inputs = Source;
- Inputs.ExtraFiles["nullability.h"] = R"cc(
- template <typename T>
- using Nullable [[clang::annotate("Nullable")]] = T;
- template <typename T>
- using Nonnull [[clang::annotate("Nonnull")]] = T;
- )cc";
- Inputs.ExtraArgs.push_back("-include");
- Inputs.ExtraArgs.push_back("nullability.h");
- clang::TestAST AST(Inputs);
+ clang::TestAST AST(getInputsWithAnnotationDefinitions(Source));
collectEvidenceFromTargetDeclaration(
*dataflow::test::findValueDecl(AST.context(), "target"),
evidenceEmitter([&](const Evidence& E) { Results.push_back(E); }));
@@ -108,6 +112,14 @@
UnorderedElementsAre(
evidence(paramSlot(0), Evidence::UNCHECKED_DEREFERENCE)));
}
+TEST(InferAnnotationsTest, DerefOfNonnull) {
+ static constexpr llvm::StringRef Src = R"cc(
+ void target(Nonnull<int *> p) {
+ *p;
+ }
+ )cc";
+ EXPECT_THAT(collectEvidenceFromTargetFunction(Src), IsEmpty());
+}
TEST(InferAnnotationsTest, DereferenceBeforeAssignment) {
static constexpr llvm::StringRef Src = R"cc(