File: gnu%3A%3Aformat.3attr

package info (click to toggle)
manpages 6.15-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 20,184 kB
  • sloc: sh: 575; python: 222; perl: 190; makefile: 29; lisp: 22
file content (154 lines) | stat: -rw-r--r-- 2,422 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
.\" Copyright, the authors of the Linux man-pages project
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH gnu::format 3attr 2025-07-19 "Linux man-pages (unreleased)"
.SH NAME
gnu::format \- specify the style of format string
.SH SYNOPSIS
.nf
.BI [[gnu::format( style ,\~ fmt-idx ,\~ first-idx )]]
.fi
.SH DESCRIPTION
This attribute can be applied to a function.
It specifies that a function parameter is a format string,
and specifies the style of the format string.
This allows checking the syntax of the format string,
as well as the types of the variadic arguments.
The
.I style
can be one of the following.
.TP
.B printf
.TQ
.B scanf
.TQ
.B strftime
.TQ
.B strfmon
.P
.I fmt-idx
is a 1-based index that
specifies the position of the format string within the parameter list.
.P
.I first-idx
is a 1-based index that
specifies the position of the first argument
that corresponds to the format string.
If the first argument is part of a
.I va_list
argument,
it should be specified as
.BR 0 .
.SH VERSIONS
.SS GNU syntax
.TP
.BI __attribute__((format( style ,\~ fmt-idx ,\~ first-idx )))
.SS Styles
On some targets, other styles are additionally supported.
.TP
MinGW
.TQ
Microsoft Windows
.RS
.TQ
.B ms_printf
.TQ
.B ms_scanf
.TQ
.B ms_strftime
.P
These correspond to the formats supported by the
.I msvcrt.dll
library.
.P
GCC-only.
.RE
.TP
Solaris
.RS
.TQ
.B cmn_err
.RE
.TP
Darwin
.RS
.TQ
.B CFString
.RE
.TP
OpenBSD
.RS
.TQ
.B kprintf
.TQ
.B syslog
.TQ
.B syslog
Clang-only.
.RE
.TP
FreeBSD
.RS
.TQ
freebsd_kprintf
Clang-only.
.RE
.P
In some languages, other styles are additionally supported.
.TP
Objective-C
.RS
.TQ
.B NSString
.RE
.SS Non-variadic functions
Clang accepts the attribute on non-variadic functions as an extension.
.SH STANDARDS
GNU.
.SH HISTORY
gcc,
g++,
clang 2.8,
clang++ 2.8.
.SH EXAMPLES
.in +4n
.EX
[[gnu::format(printf, 3, 0)]]
int
vstprintf(int size;
    char buf[restrict size], int size,
    const char *restrict fmt, va_list args)
{
	int len;
\&
	if (size == 0) {
		errno = EOVERFLOW;
		return -1;
	}
\&
	len = vsnprintf(buf, size, fmt, args);
	if (len >= size) {
		errno = E2BIG;
		return -1;
	}
\&
	return len;
}
\&
[[gnu::format(printf, 3, 4)]]
int
stprintf(int size;
    char buf[restrict size], int size,
    const char *restrict fmt, ...)
{
	int len;
	va_list args;
\&
	va_start(args, fmt);
	len = vstprintf(buf, size, fmt, args);
	va_end(args);
\&
	return len;
}
.EE