16#ifndef LIB_ERROR_HELPER_H_
17#define LIB_ERROR_HELPER_H_
21#include <boost/format.hpp>
23#include "lib/cstring.h"
24#include "lib/error_message.h"
25#include "lib/source_file.h"
26#include "lib/stringify.h"
37template <
class... Args>
40template <
class... Args>
44template <
class... Args>
48template <
typename T,
class... Args>
49auto error_helper(boost::format &f,
ErrorMessage out,
const T &t, Args... args) ->
50 typename std::enable_if<Util::HasToString<T>::value &&
51 !std::is_base_of<Util::IHasSourceInfo, T>::value,
54template <
typename T,
class... Args>
55auto error_helper(boost::format &f,
ErrorMessage out,
const T *t, Args... args) ->
56 typename std::enable_if<Util::HasToString<T>::value &&
57 !std::is_base_of<Util::IHasSourceInfo, T>::value,
60template <
typename T,
class... Args>
61auto error_helper(boost::format &f,
ErrorMessage out,
const T &t, Args... args) ->
62 typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value,
ErrorMessage>::type;
64template <
typename T,
class... Args>
65auto error_helper(boost::format &f,
ErrorMessage out,
const T *t, Args... args) ->
66 typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value,
ErrorMessage>::type;
68template <
class... Args>
71template <
class... Args>
74template <
typename T,
class... Args>
75auto error_helper(boost::format &f,
ErrorMessage out,
const T &t, Args... args) ->
76 typename std::enable_if<std::is_arithmetic<T>::value,
ErrorMessage>::type;
80template <
class... Args>
82 return error_helper(f % t, out, std::forward<Args>(args)...);
85template <
class... Args>
87 return error_helper(f % t.c_str(), out, std::forward<Args>(args)...);
90template <
typename T,
class... Args>
91auto error_helper(boost::format &f,
ErrorMessage out,
const T &t, Args... args) ->
92 typename std::enable_if<Util::HasToString<T>::value &&
93 !std::is_base_of<Util::IHasSourceInfo, T>::value,
95 return error_helper(f % t.toString(), out, std::forward<Args>(args)...);
98template <
typename T,
class... Args>
99auto error_helper(boost::format &f,
ErrorMessage out,
const T *t, Args... args) ->
100 typename std::enable_if<Util::HasToString<T>::value &&
101 !std::is_base_of<Util::IHasSourceInfo, T>::value,
103 return error_helper(f % t->toString(), out, std::forward<Args>(args)...);
106template <
class... Args>
108 return error_helper(f % t, out, std::forward<Args>(args)...);
111template <
class... Args>
113 return error_helper(f % t, out, std::forward<Args>(args)...);
116template <
typename T,
class... Args>
117auto error_helper(boost::format &f,
ErrorMessage out,
const T &t, Args... args) ->
118 typename std::enable_if<std::is_arithmetic<T>::value,
ErrorMessage>::type {
119 return error_helper(f % t, out, std::forward<Args>(args)...);
122template <
class... Args>
125 if (info.isValid()) out.
locations.push_back(info);
126 return error_helper(f %
"", out, std::forward<Args>(args)...);
129template <
typename T,
class... Args>
130auto error_helper(boost::format &f,
ErrorMessage out,
const T *t, Args... args) ->
131 typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value,
ErrorMessage>::type {
132 auto info = t->getSourceInfo();
133 if (info.isValid()) out.
locations.push_back(info);
134 return error_helper(f % t->toString(), out, std::forward<Args>(args)...);
137template <
typename T,
class... Args>
138auto error_helper(boost::format &f,
ErrorMessage out,
const T &t, Args... args) ->
139 typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value,
ErrorMessage>::type {
140 auto info = t.getSourceInfo();
141 if (info.isValid()) out.
locations.push_back(info);
142 return error_helper(f % t.toString(), out, std::forward<Args>(args)...);
148template <
class... Args>
149ErrorMessage error_helper(boost::format &f, Args... args) {
151 return ::priv::error_helper(f, msg, std::forward<Args>(args)...);
155template <
class... Args>
157 return ::priv::error_helper(f, msg, std::forward<Args>(args)...);
161template <
class... Args>
163 const std::string &suffix, Args... args) {
165 return ::priv::error_helper(f, msg, std::forward<Args>(args)...);
170static inline std::string bug_helper(boost::format &f, std::string message, std::string position,
172 std::string text = boost::str(f);
173 std::string result = position;
174 if (!position.empty()) result +=
": ";
175 result += message + text +
"\n" + tail;
179template <
class... Args>
180std::string bug_helper(boost::format &f, std::string message, std::string position,
181 std::string tail,
const char *t, Args... args);
183template <
class... Args>
184std::string bug_helper(boost::format &f, std::string message, std::string position,
185 std::string tail,
const cstring &t, Args... args);
187template <
class... Args>
188std::string bug_helper(boost::format &f, std::string message, std::string position,
191template <
typename T,
class... Args>
192auto bug_helper(boost::format &f, std::string message, std::string position, std::string tail,
193 const T &t, Args... args) ->
194 typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value, std::string>::type;
196template <
typename T,
class... Args>
197auto bug_helper(boost::format &f, std::string message, std::string position, std::string tail,
198 const T *t, Args... args) ->
199 typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value, std::string>::type;
201template <
typename T,
class... Args>
202auto bug_helper(boost::format &f, std::string message, std::string position, std::string tail,
203 const T *t, Args... args) ->
204 typename std::enable_if<!std::is_base_of<Util::IHasSourceInfo, T>::value, std::string>::type;
206template <
class... Args>
207std::string bug_helper(boost::format &f, std::string message, std::string position,
208 std::string tail,
const big_int *t, Args... args);
210template <
class... Args>
211std::string bug_helper(boost::format &f, std::string message, std::string position,
212 std::string tail,
const big_int &t, Args... args);
214template <
typename T,
class... Args>
215auto bug_helper(boost::format &f, std::string message, std::string position, std::string tail,
216 const T &t, Args... args) ->
217 typename std::enable_if<!std::is_base_of<Util::IHasSourceInfo, T>::value, std::string>::type;
221template <
class... Args>
222std::string bug_helper(boost::format &f, std::string message, std::string position,
223 std::string tail,
const char *t, Args... args) {
224 return bug_helper(f % t, message, position, tail, std::forward<Args>(args)...);
227template <
class... Args>
228std::string bug_helper(boost::format &f, std::string message, std::string position,
229 std::string tail,
const cstring &t, Args... args) {
230 return bug_helper(f % t.c_str(), message, position, tail, std::forward<Args>(args)...);
233template <
typename T,
class... Args>
234auto bug_helper(boost::format &f, std::string message, std::string position, std::string tail,
235 const T *t, Args... args) ->
236 typename std::enable_if<!std::is_base_of<Util::IHasSourceInfo, T>::value, std::string>::type {
237 std::stringstream str;
239 return bug_helper(f % str.str(), message, position, tail, std::forward<Args>(args)...);
242template <
class... Args>
243std::string bug_helper(boost::format &f, std::string message, std::string position,
244 std::string tail,
const big_int *t, Args... args) {
245 return bug_helper(f % t, message, position, tail, std::forward<Args>(args)...);
248template <
class... Args>
249std::string bug_helper(boost::format &f, std::string message, std::string position,
250 std::string tail,
const big_int &t, Args... args) {
251 return bug_helper(f % t, message, position, tail, std::forward<Args>(args)...);
254template <
typename T,
class... Args>
255auto bug_helper(boost::format &f, std::string message, std::string position, std::string tail,
256 const T &t, Args... args) ->
257 typename std::enable_if<!std::is_base_of<Util::IHasSourceInfo, T>::value, std::string>::type {
258 return bug_helper(f % t, message, position, tail, std::forward<Args>(args)...);
261template <
class... Args>
262std::string bug_helper(boost::format &f, std::string message, std::string position,
264 cstring posString = info.toPositionString();
265 if (position.empty()) {
266 position = posString;
269 if (!posString.isNullOrEmpty()) {
273 return bug_helper(f %
"", message, position, tail + posString + info.toSourceFragment(),
274 std::forward<Args>(args)...);
277template <
typename T,
class... Args>
278auto bug_helper(boost::format &f, std::string message, std::string position, std::string tail,
279 const T *t, Args... args) ->
280 typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value, std::string>::type {
282 return bug_helper(f, message, position, tail, std::forward<Args>(args)...);
285 cstring posString = t->getSourceInfo().toPositionString();
286 if (position.empty()) {
287 position = posString;
290 if (!posString.isNullOrEmpty()) {
294 std::stringstream str;
296 return bug_helper(f % str.str(), message, position,
297 tail + posString + t->getSourceInfo().toSourceFragment(),
298 std::forward<Args>(args)...);
301template <
typename T,
class... Args>
302auto bug_helper(boost::format &f, std::string message, std::string position, std::string tail,
303 const T &t, Args... args) ->
304 typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value, std::string>::type {
305 cstring posString = t.getSourceInfo().toPositionString();
306 if (position.empty()) {
307 position = posString;
310 if (!posString.isNullOrEmpty()) {
314 std::stringstream str;
316 return bug_helper(f % str.str(), message, position,
317 tail + posString + t.getSourceInfo().toSourceFragment(),
318 std::forward<Args>(args)...);
Definition source_file.h:126
Definition error_message.h:36
std::vector< Util::SourceInfo > locations
Particular formatted message.
Definition error_message.h:42
std::string message
Typically error/warning type from catalog.
Definition error_message.h:41