blob: aa57c02acf3e28faedf2d23cfc395affc8db6099 [file] [log] [blame]
Sam McCall1af66fd2023-07-17 09:57:52 -07001// Part of the Crubit project, under the Apache License v2.0 with LLVM
2// Exceptions. See /LICENSE for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5// Combines evidence of the nullability of a symbol into inference conclusions.
6//
7// This evidence was gathered from local analysis across the codebase in a
8// "map" phase, merging this evidence is conceptually the "reduce" phase.
9//
10// To allow this to be distributed efficiently for commonly-used symbols,
11// merging is performed incrementally by repeatedly combining partial results.
12
13#ifndef CRUBIT_NULLABILITY_INFERENCE_MERGE_H_
14#define CRUBIT_NULLABILITY_INFERENCE_MERGE_H_
15
16#include <vector>
17
18#include "nullability/inference/inference.proto.h"
19#include "llvm/ADT/ArrayRef.h"
20
21namespace clang::tidy::nullability {
22
23// Build a Partial representing a single piece of evidence.
24Partial partialFromEvidence(const Evidence &);
25// Update LHS to include the evidence from RHS.
26// The two must describe the same symbol.
27// The merging of partials is commutative and associative.
28void mergePartials(Partial &LHS, const Partial &RHS);
29// Form nullability conclusions from a set of evidence.
30Inference finalize(const Partial &);
31
32struct InferResult {
Googlere4a1bc12024-04-01 07:08:31 -070033 Nullability Nullability;
Sam McCall1af66fd2023-07-17 09:57:52 -070034 bool Conflict = false;
Googler8c90fda2023-10-02 06:34:34 -070035 bool Trivial = false;
Sam McCall1af66fd2023-07-17 09:57:52 -070036};
37// Final inference decisions, based on event counts.
38// TODO: once this interface sticks, move to a dedicated file.
39InferResult infer(llvm::ArrayRef<unsigned> EventCounts);
40
41// Combines local evidence about symbol nullability to form a global conclusion.
42// All evidence must for be the same symbol, and there must be some.
43//
44// This signature fundamentally limits the scalability of merging: we must see
45// all the evidence for a symbol at once.
46inline Inference mergeEvidence(llvm::ArrayRef<Evidence> Ev) {
47 Partial P = partialFromEvidence(Ev.front());
48 for (const auto &E : Ev.drop_front())
49 mergePartials(P, partialFromEvidence(E));
50 return finalize(P);
51}
52
53} // namespace clang::tidy::nullability
54
55#endif