Skip to content

Commit 8ae5616

Browse files
authored
Fix compilation with FMT_ENFORCE_COMPILE_STRING and FMT_WERROR (#3091)
1 parent 76705fc commit 8ae5616

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

src/os.cc

+28-16
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ void detail::format_windows_error(detail::buffer<char>& out, int error_code,
169169
if (msg) {
170170
utf16_to_utf8 utf8_message;
171171
if (utf8_message.convert(msg) == ERROR_SUCCESS) {
172-
fmt::format_to(buffer_appender<char>(out), "{}: {}", message, utf8_message);
172+
fmt::format_to(buffer_appender<char>(out), "{}: {}", message,
173+
utf8_message);
173174
return;
174175
}
175176
}
@@ -192,19 +193,22 @@ buffered_file::buffered_file(cstring_view filename, cstring_view mode) {
192193
FMT_RETRY_VAL(file_, FMT_SYSTEM(fopen(filename.c_str(), mode.c_str())),
193194
nullptr);
194195
if (!file_)
195-
FMT_THROW(system_error(errno, "cannot open file {}", filename.c_str()));
196+
FMT_THROW(system_error(errno, FMT_STRING("cannot open file {}"),
197+
filename.c_str()));
196198
}
197199

198200
void buffered_file::close() {
199201
if (!file_) return;
200202
int result = FMT_SYSTEM(fclose(file_));
201203
file_ = nullptr;
202-
if (result != 0) FMT_THROW(system_error(errno, "cannot close file"));
204+
if (result != 0)
205+
FMT_THROW(system_error(errno, FMT_STRING("cannot close file")));
203206
}
204207

205208
int buffered_file::descriptor() const {
206209
int fd = FMT_POSIX_CALL(fileno(file_));
207-
if (fd == -1) FMT_THROW(system_error(errno, "cannot get file descriptor"));
210+
if (fd == -1)
211+
FMT_THROW(system_error(errno, FMT_STRING("cannot get file descriptor")));
208212
return fd;
209213
}
210214

@@ -222,7 +226,8 @@ file::file(cstring_view path, int oflag) {
222226
FMT_RETRY(fd_, FMT_POSIX_CALL(open(path.c_str(), oflag, mode)));
223227
# endif
224228
if (fd_ == -1)
225-
FMT_THROW(system_error(errno, "cannot open file {}", path.c_str()));
229+
FMT_THROW(
230+
system_error(errno, FMT_STRING("cannot open file {}"), path.c_str()));
226231
}
227232

228233
file::~file() noexcept {
@@ -238,7 +243,8 @@ void file::close() {
238243
// See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
239244
int result = FMT_POSIX_CALL(close(fd_));
240245
fd_ = -1;
241-
if (result != 0) FMT_THROW(system_error(errno, "cannot close file"));
246+
if (result != 0)
247+
FMT_THROW(system_error(errno, FMT_STRING("cannot close file")));
242248
}
243249

244250
long long file::size() const {
@@ -260,7 +266,7 @@ long long file::size() const {
260266
using Stat = struct stat;
261267
Stat file_stat = Stat();
262268
if (FMT_POSIX_CALL(fstat(fd_, &file_stat)) == -1)
263-
FMT_THROW(system_error(errno, "cannot get file attributes"));
269+
FMT_THROW(system_error(errno, FMT_STRING("cannot get file attributes")));
264270
static_assert(sizeof(long long) >= sizeof(file_stat.st_size),
265271
"return type of file::size is not large enough");
266272
return file_stat.st_size;
@@ -270,14 +276,16 @@ long long file::size() const {
270276
std::size_t file::read(void* buffer, std::size_t count) {
271277
rwresult result = 0;
272278
FMT_RETRY(result, FMT_POSIX_CALL(read(fd_, buffer, convert_rwcount(count))));
273-
if (result < 0) FMT_THROW(system_error(errno, "cannot read from file"));
279+
if (result < 0)
280+
FMT_THROW(system_error(errno, FMT_STRING("cannot read from file")));
274281
return detail::to_unsigned(result);
275282
}
276283

277284
std::size_t file::write(const void* buffer, std::size_t count) {
278285
rwresult result = 0;
279286
FMT_RETRY(result, FMT_POSIX_CALL(write(fd_, buffer, convert_rwcount(count))));
280-
if (result < 0) FMT_THROW(system_error(errno, "cannot write to file"));
287+
if (result < 0)
288+
FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
281289
return detail::to_unsigned(result);
282290
}
283291

@@ -286,16 +294,18 @@ file file::dup(int fd) {
286294
// http://pubs.opengroup.org/onlinepubs/009695399/functions/dup.html
287295
int new_fd = FMT_POSIX_CALL(dup(fd));
288296
if (new_fd == -1)
289-
FMT_THROW(system_error(errno, "cannot duplicate file descriptor {}", fd));
297+
FMT_THROW(system_error(
298+
errno, FMT_STRING("cannot duplicate file descriptor {}"), fd));
290299
return file(new_fd);
291300
}
292301

293302
void file::dup2(int fd) {
294303
int result = 0;
295304
FMT_RETRY(result, FMT_POSIX_CALL(dup2(fd_, fd)));
296305
if (result == -1) {
297-
FMT_THROW(system_error(errno, "cannot duplicate file descriptor {} to {}",
298-
fd_, fd));
306+
FMT_THROW(system_error(
307+
errno, FMT_STRING("cannot duplicate file descriptor {} to {}"), fd_,
308+
fd));
299309
}
300310
}
301311

@@ -320,7 +330,8 @@ void file::pipe(file& read_end, file& write_end) {
320330
// http://pubs.opengroup.org/onlinepubs/009696799/functions/pipe.html
321331
int result = FMT_POSIX_CALL(pipe(fds));
322332
# endif
323-
if (result != 0) FMT_THROW(system_error(errno, "cannot create pipe"));
333+
if (result != 0)
334+
FMT_THROW(system_error(errno, FMT_STRING("cannot create pipe")));
324335
// The following assignments don't throw because read_fd and write_fd
325336
// are closed.
326337
read_end = file(fds[0]);
@@ -335,8 +346,8 @@ buffered_file file::fdopen(const char* mode) {
335346
FILE* f = FMT_POSIX_CALL(fdopen(fd_, mode));
336347
# endif
337348
if (!f)
338-
FMT_THROW(
339-
system_error(errno, "cannot associate stream with file descriptor"));
349+
FMT_THROW(system_error(
350+
errno, FMT_STRING("cannot associate stream with file descriptor")));
340351
buffered_file bf(f);
341352
fd_ = -1;
342353
return bf;
@@ -349,7 +360,8 @@ long getpagesize() {
349360
return si.dwPageSize;
350361
# else
351362
long size = FMT_POSIX_CALL(sysconf(_SC_PAGESIZE));
352-
if (size < 0) FMT_THROW(system_error(errno, "cannot get memory page size"));
363+
if (size < 0)
364+
FMT_THROW(system_error(errno, FMT_STRING("cannot get memory page size")));
353365
return size;
354366
# endif
355367
}

0 commit comments

Comments
 (0)