Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions nullability/inference/collect_evidence.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3266,7 +3266,7 @@ static void saveCFGScopeVirtualMethodIndex(
CFGVirtualMethodSummary);
}

llvm::Expected<CFGSummary> summarizeDefinition(
llvm::Expected<std::optional<CFGSummary>> summarizeDefinition(
const Decl& Definition, USRCache& USRCache,
const NullabilityPragmas& Pragmas,
const VirtualMethodIndex& VirtualMethodsInTU,
Expand All @@ -3290,7 +3290,7 @@ llvm::Expected<CFGSummary> summarizeDefinition(
// functions.
if (!TargetFunc && !isInferenceTarget(Definition) &&
!hasAnyInferenceTargets(ReferencedDecls))
return Summary;
return std::nullopt;

ASTContext &Ctx = Definition.getASTContext();
llvm::Expected<dataflow::AdornedCFG> ACFG =
Expand Down Expand Up @@ -3340,10 +3340,10 @@ llvm::Expected<CFGSummary> summarizeDefinition(
.moveInto(Results))
return Error;

// When `Results` are empty, the post-analysis callbacks should not have been
// called, so `Summary` should be empty. But, to be sure, explicitly return a
// fresh empty summary.
if (Results.empty()) return CFGSummary();
// When `Results` are empty, there's nothing to summarize and the
// post-analysis callbacks should not have been called, so `Summary` should be
// empty. Instead of returning an empty summary, don't return one.
if (Results.empty()) return std::nullopt;

if (std::optional<dataflow::DataflowAnalysisState<PointerNullabilityLattice>>
&ExitBlockResult = Results[ACFG->getCFG().getExit().getBlockID()]) {
Expand Down Expand Up @@ -3374,6 +3374,7 @@ llvm::Expected<CFGSummary> summarizeDefinition(
"SAT solver reached iteration limit");
}

if (Summary.behavior_summaries_size() <= 0) return std::nullopt;
return Summary;
}

Expand Down
7 changes: 4 additions & 3 deletions nullability/inference/collect_evidence.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <algorithm>
#include <memory>
#include <optional>
#include <ostream>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -193,9 +194,9 @@ llvm::Error collectEvidenceFromDefinition(
const SolverFactory &MakeSolver = makeDefaultSolverForInference);

// Summarizes Nullability-relevant behaviors in and context for `Definition`.
// If the resulting summary has no `behavior_summaries`, the analysis succeeded,
// but there's no relevant content.
llvm::Expected<CFGSummary> summarizeDefinition(
// If std::nullopt is returned, the analysis succeeded, but there's no relevant
// content.
llvm::Expected<std::optional<CFGSummary>> summarizeDefinition(
const Decl& Definition, USRCache& USRCache,
const NullabilityPragmas& Pragmas,
const VirtualMethodIndex& VirtualMethodsInTU,
Expand Down
35 changes: 21 additions & 14 deletions nullability/inference/collect_evidence_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <cassert>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
Expand Down Expand Up @@ -299,18 +300,21 @@ TEST(SummarizeDefinitionTest, Deref) {
definition_location: "input.cc:2:10"
)pb";

llvm::Expected<CFGSummary> Summary = summarizeTargetFuncDefinition(Src);
ASSERT_THAT_EXPECTED(Summary, llvm::Succeeded());
llvm::Expected<std::optional<CFGSummary>> SummaryResult =
summarizeTargetFuncDefinition(Src);
ASSERT_THAT_EXPECTED(SummaryResult, llvm::Succeeded());
ASSERT_TRUE(SummaryResult->has_value());
CFGSummary& Summary = **SummaryResult;
// Given our reliance on particular atoms, we verify that the atom maps are
// not empty. It is difficult to meaningfully connect the input code to more
// detailed aspects of these maps.
EXPECT_THAT(Summary->logical_context().atom_defs(), Not(IsEmpty()));
Summary->mutable_logical_context()->clear_atom_defs();
EXPECT_THAT(Summary.logical_context().atom_defs(), Not(IsEmpty()));
Summary.mutable_logical_context()->clear_atom_defs();

EXPECT_THAT(Summary->logical_context().atom_deps(), Not(IsEmpty()));
Summary->mutable_logical_context()->clear_atom_deps();
EXPECT_THAT(Summary.logical_context().atom_deps(), Not(IsEmpty()));
Summary.mutable_logical_context()->clear_atom_deps();

EXPECT_THAT(*Summary, EqualsProto(Proto));
EXPECT_THAT(Summary, EqualsProto(Proto));
}

TEST(SummarizeDefinitionTest, NullableArgPassed) {
Expand Down Expand Up @@ -340,18 +344,21 @@ TEST(SummarizeDefinitionTest, NullableArgPassed) {
definition_location: "input.cc:3:10"
)pb";

llvm::Expected<CFGSummary> Summary = summarizeTargetFuncDefinition(Src);
ASSERT_THAT_EXPECTED(Summary, llvm::Succeeded());
llvm::Expected<std::optional<CFGSummary>> SummaryResult =
summarizeTargetFuncDefinition(Src);
ASSERT_THAT_EXPECTED(SummaryResult, llvm::Succeeded());
ASSERT_TRUE(SummaryResult->has_value());
CFGSummary& Summary = **SummaryResult;
// Given our reliance on particular atoms, we verify that the atom maps are
// not empty. It is difficult to meaningfully connect the input code to more
// detailed aspects of these maps.
EXPECT_THAT(Summary->logical_context().atom_defs(), Not(IsEmpty()));
Summary->mutable_logical_context()->clear_atom_defs();
EXPECT_THAT(Summary.logical_context().atom_defs(), Not(IsEmpty()));
Summary.mutable_logical_context()->clear_atom_defs();

EXPECT_THAT(Summary->logical_context().atom_deps(), Not(IsEmpty()));
Summary->mutable_logical_context()->clear_atom_deps();
EXPECT_THAT(Summary.logical_context().atom_deps(), Not(IsEmpty()));
Summary.mutable_logical_context()->clear_atom_deps();

EXPECT_THAT(*Summary, EqualsProto(Proto));
EXPECT_THAT(Summary, EqualsProto(Proto));
}

class CollectEvidenceFromDefinitionTest
Expand Down
16 changes: 10 additions & 6 deletions nullability/inference/collect_evidence_test_utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "nullability/inference/collect_evidence_test_utilities.h"

#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -73,7 +74,7 @@ std::string printToString(DefinitionCollectionMode Mode) {
llvm_unreachable("Unknown CollectionMode");
}

static llvm::Expected<CFGSummary> summarizeDefinitionNamed(
static llvm::Expected<std::optional<CFGSummary>> summarizeDefinitionNamed(
llvm::StringRef TargetName, llvm::StringRef Source) {
USRCache UsrCache;
NullabilityPragmas Pragmas;
Expand All @@ -84,7 +85,7 @@ static llvm::Expected<CFGSummary> summarizeDefinitionNamed(
getVirtualMethodIndex(AST.context(), UsrCache));
}

llvm::Expected<CFGSummary> summarizeTargetFuncDefinition(
llvm::Expected<std::optional<CFGSummary>> summarizeTargetFuncDefinition(
llvm::StringRef Source) {
return summarizeDefinitionNamed("target", Source);
}
Expand All @@ -98,17 +99,20 @@ collectFromDefinitionViaSummaryWithErrors(
USRCache UsrCache;
std::vector<Evidence> Results;
VirtualMethodIndex TUScopeVMI = getVirtualMethodIndex(ASTCtx, UsrCache);
llvm::Expected<CFGSummary> Summary = summarizeDefinition(
llvm::Expected<std::optional<CFGSummary>> SummaryResult = summarizeDefinition(
Definition, UsrCache, Pragmas, TUScopeVMI, MakeSolver);
if (!Summary) return {Summary.takeError(), Results};
if (!SummaryResult) return {SummaryResult.takeError(), Results};
if (!SummaryResult->has_value())
return {llvm::Error::success(), std::vector<Evidence>{}};
CFGSummary& Summary = **SummaryResult;

auto VMIForPropagation = std::make_shared<VirtualMethodIndex>(
loadVirtualMethodsIndex(Summary->virtual_method_index()));
loadVirtualMethodsIndex(Summary.virtual_method_index()));
// All overrides from anywhere in the TU are relevant for propagating
// evidence, so we use the entire TU-scoped collection for this direction.
VMIForPropagation->Overrides = std::move(TUScopeVMI.Overrides);
return {collectEvidenceFromSummary(
*Summary,
Summary,
evidenceEmitterWithPropagation(
[&Results](const Evidence& E) { Results.push_back(E); },
VMIForPropagation),
Expand Down
3 changes: 2 additions & 1 deletion nullability/inference/collect_evidence_test_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef THIRD_PARTY_CRUBIT_NULLABILITY_INFERENCE_COLLECT_EVIDENCE_TEST_UTILITIES_H_
#define THIRD_PARTY_CRUBIT_NULLABILITY_INFERENCE_COLLECT_EVIDENCE_TEST_UTILITIES_H_

#include <optional>
#include <string>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -105,7 +106,7 @@ std::string printToString(DefinitionCollectionMode Mode);

/// Summarizes the definition in `Source` with the name "target" (a generic name
/// using the LLVM-style capitalization for function names).
llvm::Expected<CFGSummary> summarizeTargetFuncDefinition(
llvm::Expected<std::optional<CFGSummary>> summarizeTargetFuncDefinition(
llvm::StringRef Source);

//// Returns both an error and a vector to represent partial computations --
Expand Down
7 changes: 5 additions & 2 deletions nullability/inference/infer_tu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "nullability/inference/infer_tu.h"

#include <memory>
#include <optional>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -114,9 +115,11 @@ class InferenceManager {
for (const auto* Impl : Sites.Definitions) {
if (Filter && !Filter(*Impl)) continue;

if (llvm::Expected<CFGSummary> Summary =
if (llvm::Expected<std::optional<CFGSummary>> Summary =
summarizeDefinition(*Impl, USRCache, Pragmas, *VMI)) {
Result.Summaries.push_back(*std::move(Summary));
if (Summary->has_value()) {
Result.Summaries.push_back(**std::move(Summary));
}
} else {
llvm::errs() << "Error summarizing definition: " << Summary.takeError()
<< "\n";
Expand Down