Skip to content

Commit

Permalink
[nullability] Fix false positives for functions with forward declarat…
Browse files Browse the repository at this point in the history
…ions.

Since [this
patch](b4120db),
we erroneously used the `ParmVarDecl`s of the forward declaration when creating
initial values for the parameters; the body uses the `ParmVarDecl`s of the
definition, so it would not see these initial values.

PiperOrigin-RevId: 598560058
Change-Id: I28c0659e431a7bdf9154969bd3709fb466b7c76d
  • Loading branch information
martinboehme authored and Copybara-Service committed Jan 15, 2024
1 parent c8c5185 commit 4b01ed0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion nullability/pointer_nullability_diagnosis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,10 @@ diagnosePointerNullability(const FunctionDecl *Func) {
for (const ParmVarDecl *Parm : Func->parameters())
checkParmVarDeclWithPointerDefaultArg(Ctx, *Parm, Diags);

if (!Func->hasBody()) return Diags;
// If `Func` has multiple declarations in the current TU, make sure we use the
// one that defines the body of the function. (We retrieve this using the
// output parameter of `hasBody()`.)
if (!Func->hasBody(Func)) return Diags;

auto Diagnoser = pointerNullabilityDiagnoser();
const std::int64_t MaxSATIterations = 2'000'000;
Expand Down
15 changes: 15 additions & 0 deletions nullability/test/basic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,20 @@ TEST(PointerNullabilityTest, ArraySubscript) {
)cc"));
}

TEST(PointerNullabilityTest, ForwardDeclaration) {
// Check that we handle a function with a forward declaration correctly. This
// is a regression test for a bug where we erroneously used the `ParmVarDecl`s
// of the first declaration when creating initial values for the parameters;
// the body uses the `ParmVarDecl`s of the second declaration, so it would not
// see these initial values.
EXPECT_TRUE(checkDiagnostics(R"cc(
void target(int* _Nullable p);
void target(int* _Nullable p) {
if (p == nullptr) return;
*p;
}
)cc"));
}

} // namespace
} // namespace clang::tidy::nullability

0 comments on commit 4b01ed0

Please sign in to comment.