Sam McCall | 1af66fd | 2023-07-17 09:57:52 -0700 | [diff] [blame] | 1 | // 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 | |
| 21 | namespace clang::tidy::nullability { |
| 22 | |
| 23 | // Build a Partial representing a single piece of evidence. |
| 24 | Partial 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. |
| 28 | void mergePartials(Partial &LHS, const Partial &RHS); |
| 29 | // Form nullability conclusions from a set of evidence. |
| 30 | Inference finalize(const Partial &); |
| 31 | |
| 32 | struct InferResult { |
Googler | e4a1bc1 | 2024-04-01 07:08:31 -0700 | [diff] [blame] | 33 | Nullability Nullability; |
Sam McCall | 1af66fd | 2023-07-17 09:57:52 -0700 | [diff] [blame] | 34 | bool Conflict = false; |
Googler | 8c90fda | 2023-10-02 06:34:34 -0700 | [diff] [blame] | 35 | bool Trivial = false; |
Sam McCall | 1af66fd | 2023-07-17 09:57:52 -0700 | [diff] [blame] | 36 | }; |
| 37 | // Final inference decisions, based on event counts. |
| 38 | // TODO: once this interface sticks, move to a dedicated file. |
| 39 | InferResult 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. |
| 46 | inline 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 |