P4C
The P4 Compiler
Loading...
Searching...
No Matches
error_helper.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#ifndef LIB_ERROR_HELPER_H_
17#define LIB_ERROR_HELPER_H_
18
19#include <type_traits>
20
21#include <boost/format.hpp>
22
23#include "lib/error_message.h"
24#include "lib/source_file.h"
25#include "lib/stringify.h"
26
27namespace P4 {
28namespace priv {
29
30// All these methods return std::string because this is the native format of boost::format
31// Position is printed at the beginning.
32static inline ErrorMessage error_helper(boost::format &f, ErrorMessage out) {
33 out.message = boost::str(f);
34 return out;
35}
36
37template <class... Args>
38auto error_helper(boost::format &f, ErrorMessage out, const char *t, Args &&...args) {
39 return error_helper(f % t, out, std::forward<Args>(args)...);
40}
41
42template <typename T, class... Args>
43auto error_helper(boost::format &f, ErrorMessage out, const T &t,
44 Args &&...args) -> std::enable_if_t<Util::has_toString_v<T>, ErrorMessage>;
45
46template <typename T, class... Args>
47auto error_helper(boost::format &f, ErrorMessage out, const T &t, Args &&...args)
48 -> std::enable_if_t<!Util::has_toString_v<T> && !std::is_pointer_v<T>, ErrorMessage>;
49
50template <typename T, class... Args>
51auto error_helper(boost::format &f, ErrorMessage out, const T *t, Args &&...args) {
52 // Contrary to bug_helper we do not want to show raw pointers to users in
53 // ordinary error messages. Therefore we explicitly delegate to
54 // reference-arg implementation here.
55 return error_helper(f, out, *t, std::forward<Args>(args)...);
56}
57
58template <class... Args>
59ErrorMessage error_helper(boost::format &f, ErrorMessage out, const Util::SourceInfo &info,
60 Args &&...args) {
61 if (info.isValid()) out.locations.push_back(info);
62 return error_helper(f % "", std::move(out), std::forward<Args>(args)...);
63}
64
65template <typename T>
66void maybeAddSourceInfo(ErrorMessage &out, const T &t) {
67 if constexpr (Util::has_SourceInfo_v<T>) {
68 auto info = t.getSourceInfo();
69 if (info.isValid()) out.locations.push_back(info);
70 }
71}
72
73template <typename T, class... Args>
74auto error_helper(boost::format &f, ErrorMessage out, const T &t, Args &&...args)
75 -> std::enable_if_t<!Util::has_toString_v<T> && !std::is_pointer_v<T>, ErrorMessage> {
76 maybeAddSourceInfo(out, t);
77 return error_helper(f % t, std::move(out), std::forward<Args>(args)...);
78}
79
80template <typename T, class... Args>
81auto error_helper(boost::format &f, ErrorMessage out, const T &t,
82 Args &&...args) -> std::enable_if_t<Util::has_toString_v<T>, ErrorMessage> {
83 maybeAddSourceInfo(out, t);
84 return error_helper(f % t.toString(), std::move(out), std::forward<Args>(args)...);
85}
86
87} // namespace priv
88
89// Most direct invocations of error_helper usually only reduce arguments
90template <class... Args>
91ErrorMessage error_helper(boost::format &f, Args &&...args) {
92 ErrorMessage msg;
93 return priv::error_helper(f, msg, std::forward<Args>(args)...);
94}
95
96// Invoked from ErrorReporter
97template <class... Args>
98ErrorMessage error_helper(boost::format &f, ErrorMessage msg, Args &&...args) {
99 return priv::error_helper(f, std::move(msg), std::forward<Args>(args)...);
100}
101
102// This overload exists for backwards compatibility
103template <class... Args>
104ErrorMessage error_helper(boost::format &f, const std::string &prefix, const Util::SourceInfo &info,
105 const std::string &suffix, Args &&...args) {
106 return priv::error_helper(f, ErrorMessage(prefix, info, suffix), std::forward<Args>(args)...);
107}
108
109} // namespace P4
110
111#endif /* LIB_ERROR_HELPER_H_ */
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
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 error.h:148
std::vector< Util::SourceInfo > locations
Particular formatted message.
Definition error_message.h:44
std::string message
Typically error/warning type from catalog.
Definition error_message.h:43