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;
29
30/// Bundles the EntityIdTable (moved from the LUSummary) and the analysis
31/// results produced by one AnalysisDriver::run() call, keyed by AnalysisName.
32///
33/// This is the natural unit of persistence: entity names and analysis results
34/// are self-contained in one object.
35class WPASuite {
36 friend class AnalysisDriver;
37
38 EntityIdTable IdTable;
39 std::map<AnalysisName, std::unique_ptr<AnalysisResult>> Data;
40
41 WPASuite() = default;
42
43public:
44 /// Returns the EntityIdTable that maps EntityId values to their symbolic
45 /// names.
46 const EntityIdTable &idTable() const { return IdTable; }
47
48 /// Returns true if a result for \p ResultT is present.
49 template <typename ResultT> [[nodiscard]] bool contains() const {
50 static_assert(std::is_base_of_v<AnalysisResult, ResultT>,
51 "ResultT must derive from AnalysisResult");
52 static_assert(HasAnalysisName_v<ResultT>,
53 "ResultT must have a static analysisName() method");
54
55 return contains(ResultT::analysisName());
56 }
57
58 /// Returns true if a result for \p Name is present.
59 [[nodiscard]] bool contains(AnalysisName Name) const {
60 return Data.find(Name) != Data.end();
61 }
62
63 /// Returns a const reference to the result for \p ResultT, or an error if
64 /// absent.
65 template <typename ResultT>
66 [[nodiscard]] llvm::Expected<const ResultT &> get() const {
67 static_assert(std::is_base_of_v<AnalysisResult, ResultT>,
68 "ResultT must derive from AnalysisResult");
69 static_assert(HasAnalysisName_v<ResultT>,
70 "ResultT must have a static analysisName() method");
71
72 auto Result = get(ResultT::analysisName());
73 if (!Result) {
74 return Result.takeError();
75 }
76 return static_cast<const ResultT &>(*Result);
77 }
78
79 /// Returns a const reference to the result for \p Name, or an error if
80 /// absent.
82 get(AnalysisName Name) const {
83 auto It = Data.find(Name);
84 if (It == Data.end()) {
85 return ErrorBuilder::create(std::errc::invalid_argument,
86 "no result for '{0}' in WPASuite", Name)
87 .build();
88 }
89 return *It->second;
90 }
91};
92
93} // namespace clang::ssaf
94
95#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.
const EntityIdTable & idTable() const
Returns the EntityIdTable that maps EntityId values to their symbolic names.
Definition WPASuite.h:46
bool contains() const
Returns true if a result for ResultT is present.
Definition WPASuite.h:49
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:82
llvm::Expected< const ResultT & > get() const
Returns a const reference to the result for ResultT, or an error if absent.
Definition WPASuite.h:66
bool contains(AnalysisName Name) const
Returns true if a result for Name is present.
Definition WPASuite.h:59
friend class AnalysisDriver
Definition WPASuite.h:36
constexpr bool HasAnalysisName_v
@ Result
The result type of a method or function.
Definition TypeBase.h:905