clang 23.0.0git
WPASuite.h
Go to the documentation of this file.
1//===- WPASuite.h -----------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// The value returned by AnalysisDriver::run(). Bundles the EntityIdTable
10// with the analysis results keyed by AnalysisName.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_CORE_WHOLEPROGRAMANALYSIS_WPASUITE_H
15#define LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_CORE_WHOLEPROGRAMANALYSIS_WPASUITE_H
16
22#include "llvm/Support/Error.h"
23#include <map>
24#include <memory>
25
26namespace clang::ssaf {
27
28class AnalysisDriver;
30class TestFixture;
31
32/// Bundles the EntityIdTable (moved from the LUSummary) and the analysis
33/// results produced by one AnalysisDriver::run() call, keyed by AnalysisName.
34///
35/// This is the natural unit of persistence: entity names and analysis results
36/// are self-contained in one object.
37class WPASuite {
38 friend class AnalysisDriver;
39 friend class SerializationFormat;
40 friend class TestFixture;
41
42 EntityIdTable IdTable;
43 std::map<AnalysisName, std::unique_ptr<AnalysisResult>> Data;
44
45 WPASuite() = default;
46
47public:
48 /// Returns the EntityIdTable that maps EntityId values to their symbolic
49 /// names.
50 const EntityIdTable &idTable() const { return IdTable; }
51
52 /// Returns true if a result for \p ResultT is present.
53 template <typename ResultT> [[nodiscard]] bool contains() const {
54 static_assert(std::is_base_of_v<AnalysisResult, ResultT>,
55 "ResultT must derive from AnalysisResult");
56 static_assert(HasAnalysisName_v<ResultT>,
57 "ResultT must have a static analysisName() method");
58
59 return contains(ResultT::analysisName());
60 }
61
62 /// Returns true if a result for \p Name is present.
63 [[nodiscard]] bool contains(AnalysisName Name) const {
64 return Data.find(Name) != Data.end();
65 }
66
67 /// Returns a const reference to the result for \p ResultT, or an error if
68 /// absent.
69 template <typename ResultT>
70 [[nodiscard]] llvm::Expected<const ResultT &> get() const {
71 static_assert(std::is_base_of_v<AnalysisResult, ResultT>,
72 "ResultT must derive from AnalysisResult");
73 static_assert(HasAnalysisName_v<ResultT>,
74 "ResultT must have a static analysisName() method");
75
76 auto Result = get(ResultT::analysisName());
77 if (!Result) {
78 return Result.takeError();
79 }
80 return static_cast<const ResultT &>(*Result);
81 }
82
83 /// Returns a const reference to the result for \p Name, or an error if
84 /// absent.
86 get(AnalysisName Name) const {
87 auto It = Data.find(Name);
88 if (It == Data.end()) {
89 return ErrorBuilder::create(std::errc::invalid_argument,
90 "no result for '{0}' in WPASuite", Name)
91 .build();
92 }
93 return *It->second;
94 }
95};
96
97} // namespace clang::ssaf
98
99#endif // LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_CORE_WHOLEPROGRAMANALYSIS_WPASUITE_H
Orchestrates whole-program analysis over an LUSummary.
Uniquely identifies a whole-program analysis and the AnalysisResult it produces.
Manages entity name interning and provides efficient EntityId handles.
static ErrorBuilder create(std::error_code EC, const char *Fmt, Args &&...ArgVals)
Create an ErrorBuilder with an error code and formatted message.
llvm::Error build() const
Build and return the final error.
Abstract base class for serialization formats.
const EntityIdTable & idTable() const
Returns the EntityIdTable that maps EntityId values to their symbolic names.
Definition WPASuite.h:50
bool contains() const
Returns true if a result for ResultT is present.
Definition WPASuite.h:53
friend class SerializationFormat
Definition WPASuite.h:39
llvm::Expected< const AnalysisResult & > get(AnalysisName Name) const
Returns a const reference to the result for Name, or an error if absent.
Definition WPASuite.h:86
llvm::Expected< const ResultT & > get() const
Returns a const reference to the result for ResultT, or an error if absent.
Definition WPASuite.h:70
friend class TestFixture
Definition WPASuite.h:40
bool contains(AnalysisName Name) const
Returns true if a result for Name is present.
Definition WPASuite.h:63
friend class AnalysisDriver
Definition WPASuite.h:38
constexpr bool HasAnalysisName_v
@ Result
The result type of a method or function.
Definition TypeBase.h:905