P4C
The P4 Compiler
Loading...
Searching...
No Matches
error_helper.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7#ifndef LIB_ERROR_HELPER_H_
8#define LIB_ERROR_HELPER_H_
9
10#include <type_traits>
11
12#include <boost/format.hpp>
13
14#include "lib/error_message.h"
15#include "lib/source_file.h"
16#include "lib/stringify.h"
17
18namespace P4 {
19namespace priv {
20
21// All these methods return std::string because this is the native format of boost::format
22// Position is printed at the beginning.
23static inline ErrorMessage error_helper(boost::format &f, ErrorMessage out) {
24 out.message = boost::str(f);
25 return out;
26}
27
28template <class... Args>
29auto error_helper(boost::format &f, ErrorMessage out, const char *t, Args &&...args) {
30 return error_helper(f % t, out, std::forward<Args>(args)...);
31}
32
33template <typename T, class... Args>
34auto error_helper(boost::format &f, ErrorMessage out, const T &t,
35 Args &&...args) -> std::enable_if_t<Util::has_toString_v<T>, ErrorMessage>;
36
37template <typename T, class... Args>
38auto error_helper(boost::format &f, ErrorMessage out, const T &t, Args &&...args)
39 -> std::enable_if_t<!Util::has_toString_v<T> && !std::is_pointer_v<T>, ErrorMessage>;
40
41template <typename T, class... Args>
42auto error_helper(boost::format &f, ErrorMessage out, const T *t, Args &&...args) {
43 // Contrary to bug_helper we do not want to show raw pointers to users in
44 // ordinary error messages. Therefore we explicitly delegate to
45 // reference-arg implementation here.
46 return error_helper(f, out, *t, std::forward<Args>(args)...);
47}
48
49template <class... Args>
50ErrorMessage error_helper(boost::format &f, ErrorMessage out, const Util::SourceInfo &info,
51 Args &&...args) {
52 if (info.isValid()) out.locations.push_back(info);
53 return error_helper(f % "", std::move(out), std::forward<Args>(args)...);
54}
55
56template <typename T>
57void maybeAddSourceInfo(ErrorMessage &out, const T &t) {
58 if constexpr (Util::has_SourceInfo_v<T>) {
59 auto info = t.getSourceInfo();
60 if (info.isValid()) out.locations.push_back(info);
61 }
62}
63
64template <typename T, class... Args>
65auto error_helper(boost::format &f, ErrorMessage out, const T &t, Args &&...args)
66 -> std::enable_if_t<!Util::has_toString_v<T> && !std::is_pointer_v<T>, ErrorMessage> {
67 maybeAddSourceInfo(out, t);
68 return error_helper(f % t, std::move(out), std::forward<Args>(args)...);
69}
70
71template <typename T, class... Args>
72auto error_helper(boost::format &f, ErrorMessage out, const T &t,
73 Args &&...args) -> std::enable_if_t<Util::has_toString_v<T>, ErrorMessage> {
74 maybeAddSourceInfo(out, t);
75 return error_helper(f % t.toString(), std::move(out), std::forward<Args>(args)...);
76}
77
78} // namespace priv
79
80// Most direct invocations of error_helper usually only reduce arguments
81template <class... Args>
82ErrorMessage error_helper(boost::format &f, Args &&...args) {
83 ErrorMessage msg;
84 return priv::error_helper(f, msg, std::forward<Args>(args)...);
85}
86
87// Invoked from ErrorReporter
88template <class... Args>
89ErrorMessage error_helper(boost::format &f, ErrorMessage msg, Args &&...args) {
90 return priv::error_helper(f, std::move(msg), std::forward<Args>(args)...);
91}
92
93// This overload exists for backwards compatibility
94template <class... Args>
95ErrorMessage error_helper(boost::format &f, const std::string &prefix, const Util::SourceInfo &info,
96 const std::string &suffix, Args &&...args) {
97 return priv::error_helper(f, ErrorMessage(prefix, info, suffix), std::forward<Args>(args)...);
98}
99
100} // namespace P4
101
102#endif /* LIB_ERROR_HELPER_H_ */
Definition source_file.h:132
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
void info(const int kind, const char *format, const T *node, Args &&...args)
Report info messages of type kind. Requires that the node argument have source info.
Definition lib/error.h:167
Definition error_message.h:29