blob: 63b11e867446e2ffdc33760d882986f4875e7367 (
plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
# Copyright 2003 Dave Abrahams
# Copyright 2004 Vladimir Prus
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
# Print a stack backtrace leading to this rule's caller. Each argument
# represents a line of output to be printed after the first line of the
# backtrace.
#
rule backtrace ( skip-frames prefix messages * : * )
{
local frame-skips = 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77 81 ;
local drop-elements = $(frame-skips[$(skip-frames)]) ;
if ! ( $(skip-frames) in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 )
{
ECHO "warning: backtrace doesn't support skipping $(skip-frames) frames;"
"using 1 instead." ;
drop-elements = 5 ;
}
local args = $(.args) ;
if $(.user-modules-only)
{
local bt = [ nearest-user-location ] ;
ECHO "$(prefix) at $(bt) " ;
for local n in $(args)
{
if $($(n))-is-not-empty
{
ECHO $(prefix) $($(n)) ;
}
}
}
else
{
# Get the whole backtrace, then drop the initial quadruples
# corresponding to the frames that must be skipped.
local bt = [ BACKTRACE ] ;
bt = $(bt[$(drop-elements)-]) ;
while $(bt)
{
local m = [ MATCH ^(.+)\\.$ : $(bt[3]) ] ;
ECHO $(bt[1]):$(bt[2]): "in" $(bt[4]) "from module" $(m) ;
# The first time through, print each argument on a separate line.
for local n in $(args)
{
if $($(n))-is-not-empty
{
ECHO $(prefix) $($(n)) ;
}
}
args = ; # Kill args so that this never happens again.
# Move on to the next quadruple.
bt = $(bt[5-]) ;
}
}
}
.args ?= messages 2 3 4 5 6 7 8 9 ;
.disabled ?= ;
.last-error-$(.args) ?= ;
# try-catch --
#
# This is not really an exception-handling mechanism, but it does allow us to
# perform some error-checking on our error-checking. Errors are suppressed after
# a try, and the first one is recorded. Use catch to check that the error
# message matched expectations.
# Begin looking for error messages.
#
rule try ( )
{
.disabled += true ;
.last-error-$(.args) = ;
}
# Stop looking for error messages; generate an error if an argument of messages
# is not found in the corresponding argument in the error call.
#
rule catch ( messages * : * )
{
.disabled = $(.disabled[2-]) ; # Pop the stack.
import sequence ;
if ! $(.last-error-$(.args))-is-not-empty
{
error-skip-frames 3 expected an error, but none occurred ;
}
else
{
for local n in $(.args)
{
if ! $($(n)) in $(.last-error-$(n))
{
local v = [ sequence.join $($(n)) : " " ] ;
v ?= "" ;
local joined = [ sequence.join $(.last-error-$(n)) : " " ] ;
.last-error-$(.args) = ;
error-skip-frames 3 expected \"$(v)\" in argument $(n) of error
: got \"$(joined)\" instead ;
}
}
}
}
rule error-skip-frames ( skip-frames messages * : * )
{
if ! $(.disabled)
{
backtrace $(skip-frames) error: $(messages) : $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) ;
EXIT ;
}
else if ! $(.last-error-$(.args))
{
for local n in $(.args)
{
# Add an extra empty string so that we always have
# something in the event of an error
.last-error-$(n) = $($(n)) "" ;
}
}
}
if --no-error-backtrace in [ modules.peek : ARGV ]
{
.no-error-backtrace = true ;
}
# Print an error message with a stack backtrace and exit.
#
rule error ( messages * : * )
{
if $(.no-error-backtrace)
{
# Print each argument on a separate line.
for local n in $(.args)
{
if $($(n))-is-not-empty
{
if ! $(first-printed)
{
ECHO error: $($(n)) ;
first-printed = true ;
}
else
{
ECHO $($(n)) ;
}
}
}
EXIT ;
}
else
{
error-skip-frames 3 $(1) : $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) ;
}
}
# Same as 'error', but the generated backtrace will include only user files.
#
rule user-error ( messages * : * )
{
.user-modules-only = 1 ;
error-skip-frames 3 $(1) : $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) ;
}
# Print a warning message with a stack backtrace and exit.
#
rule warning
{
backtrace 2 warning: $(1) : $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) ;
}
# Convert an arbitrary argument list into a list with ":" separators and quoted
# elements representing the same information. This is mostly useful for
# formatting descriptions of arguments with which a rule was called when
# reporting an error.
#
rule lol->list ( * )
{
local result ;
local remaining = 1 2 3 4 5 6 7 8 9 ;
while $($(remaining))
{
local n = $(remaining[1]) ;
remaining = $(remaining[2-]) ;
if $(n) != 1
{
result += ":" ;
}
result += \"$($(n))\" ;
}
return $(result) ;
}
# Return the file:line for the nearest entry in backtrace which correspond to a
# user module.
#
rule nearest-user-location ( )
{
local bt = [ BACKTRACE ] ;
local result ;
while $(bt) && ! $(result)
{
local m = [ MATCH ^(.+)\\.$ : $(bt[3]) ] ;
local user-modules = ([Jj]amroot(.jam|.v2|)|([Jj]amfile(.jam|.v2|)|user-config.jam|site-config.jam|project-root.jam) ;
if [ MATCH $(user-modules) : $(bt[1]:D=) ]
{
result = $(bt[1]):$(bt[2]) ;
}
bt = $(bt[5-]) ;
}
return $(result) ;
}
# If optimized rule is available in Jam, use it.
if NEAREST_USER_LOCATION in [ RULENAMES ]
{
rule nearest-user-location ( )
{
local r = [ NEAREST_USER_LOCATION ] ;
return $(r[1]):$(r[2]) ;
}
}
rule __test__ ( )
{
# Show that we can correctly catch an expected error.
try ;
{
error an error occurred : somewhere ;
}
catch an error occurred : somewhere ;
# Show that unexpected errors generate real errors.
try ;
{
try ;
{
error an error occurred : somewhere ;
}
catch an error occurred : nowhere ;
}
catch expected \"nowhere\" in argument 2 ;
# Show that not catching an error where one was expected is an error.
try ;
{
try ;
{
}
catch ;
}
catch expected an error, but none occurred ;
}
|