1 // go-diagnostics.cc -- Go error/warning diagnostics utilities.
3 // Copyright 2016 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 #include "go-diagnostics.h"
12 return std::string(xstrerror(errno
));
15 // Rewrite a format string to expand any extensions not
16 // supported by sprintf(). See comments in go-diagnostics.h
17 // for list of supported format specifiers.
20 expand_format(const char* fmt
)
23 for (const char* c
= fmt
; *c
; ++c
)
35 // malformed format string
45 ss
<< mformat_value();
50 ss
<< go_open_quote();
55 ss
<< go_close_quote();
60 ss
<< go_open_quote();
64 ss
<< mformat_value();
70 ss
<< go_close_quote();
82 // Expand message format specifiers, using a combination of
83 // expand_format above to handle extensions (ex: %m, %q) and vasprintf()
84 // to handle regular printf-style formatting. A pragma is being used here to
85 // suppress this warning:
87 // warning: function ‘std::__cxx11::string expand_message(const char*, __va_list_tag*)’ might be a candidate for ‘gnu_printf’ format attribute [-Wsuggest-attribute=format]
89 // What appears to be happening here is that the checker is deciding that
90 // because of the call to vasprintf() (which has attribute gnu_printf), the
91 // calling function must need to have attribute gnu_printf as well, even
92 // though there is already an attribute declaration for it.
95 expand_message(const char* fmt
, va_list ap
) GO_ATTRIBUTE_GCC_DIAG(1,0);
97 #pragma GCC diagnostic push
98 #pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
101 expand_message(const char* fmt
, va_list ap
)
104 std::string expanded_fmt
= expand_format(fmt
);
105 int nwr
= vasprintf(&mbuf
, expanded_fmt
.c_str(), ap
);
108 // memory allocation failed
109 go_be_error_at(Linemap::unknown_location(),
110 "memory allocation failed in vasprintf");
113 std::string rval
= std::string(mbuf
);
118 #pragma GCC diagnostic pop
120 static const char* cached_open_quote
= NULL
;
121 static const char* cached_close_quote
= NULL
;
126 if (cached_open_quote
== NULL
)
127 go_be_get_quotechars(&cached_open_quote
, &cached_close_quote
);
128 return cached_open_quote
;
134 if (cached_close_quote
== NULL
)
135 go_be_get_quotechars(&cached_open_quote
, &cached_close_quote
);
136 return cached_close_quote
;
140 go_error_at(const Location location
, const char* fmt
, ...)
145 go_be_error_at(location
, expand_message(fmt
, ap
));
150 go_warning_at(const Location location
, int opt
, const char* fmt
, ...)
155 go_be_warning_at(location
, opt
, expand_message(fmt
, ap
));
160 go_fatal_error(const Location location
, const char* fmt
, ...)
165 go_be_fatal_error(location
, expand_message(fmt
, ap
));
170 go_inform(const Location location
, const char* fmt
, ...)
175 go_be_inform(location
, expand_message(fmt
, ap
));