1 # Copyright 2003 Dave Abrahams
2 # Copyright 2004 Vladimir Prus
3 # Distributed under the Boost Software License, Version 1.0.
4 # (See accompanying file LICENSE_1_0.txt or copy at
5 # http://www.boost.org/LICENSE_1_0.txt)
7 # Print a stack backtrace leading to this rule's caller. Each argument
8 # represents a line of output to be printed after the first line of the
11 rule backtrace ( skip-frames prefix messages * : * )
13 local frame-skips = 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77 81 ;
14 local drop-elements = $(frame-skips[$(skip-frames)]) ;
15 if ! ( $(skip-frames) in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 )
17 ECHO "warning: backtrace doesn't support skipping $(skip-frames) "
18 "frames; using 1 instead." ;
22 local args = $(.args) ;
23 if $(.user-modules-only)
25 local bt = [ nearest-user-location ] ;
28 ECHO $(prefix) at $(bt) ;
30 for local n in $(args)
34 ECHO $(prefix) $($(n)) ;
40 # Get the whole backtrace, then drop the initial quadruples
41 # corresponding to the frames that must be skipped.
42 local bt = [ BACKTRACE ] ;
43 bt = $(bt[$(drop-elements)-]) ;
47 local m = [ MATCH ^(.+)\\.$ : $(bt[3]) ] ;
48 ECHO $(bt[1]):$(bt[2]): "in" $(bt[4]) "from module" $(m) ;
50 # The first time through, print each argument on a separate line.
51 for local n in $(args)
55 ECHO $(prefix) $($(n)) ;
58 args = ; # Kill args so that this never happens again.
60 # Move on to the next quadruple.
66 .args ?= messages 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ;
68 .last-error-$(.args) ?= ;
73 # This is not really an exception-handling mechanism, but it does allow us to
74 # perform some error-checking on our error-checking. Errors are suppressed after
75 # a try, and the first one is recorded. Use catch to check that the error
76 # message matched expectations.
78 # Begin looking for error messages.
83 .last-error-$(.args) = ;
87 # Stop looking for error messages; generate an error if an argument of messages
88 # is not found in the corresponding argument in the error call.
90 rule catch ( messages * : * )
92 .disabled = $(.disabled[2-]) ; # Pop the stack.
96 if ! $(.last-error-$(.args))-is-defined
98 error-skip-frames 3 expected an error, but none occurred ;
102 for local n in $(.args)
104 if ! $($(n)) in $(.last-error-$(n))
106 local v = [ sequence.join $($(n)) : " " ] ;
108 local joined = [ sequence.join $(.last-error-$(n)) : " " ] ;
110 .last-error-$(.args) = ;
111 error-skip-frames 3 expected \"$(v)\" in argument $(n) of error
112 : got \"$(joined)\" instead ;
119 rule error-skip-frames ( skip-frames messages * : * )
123 backtrace $(skip-frames) error: $(messages) : $(2) : $(3) : $(4) : $(5)
124 : $(6) : $(7) : $(8) : $(9) : $(10) : $(11) : $(12) : $(13) : $(14)
125 : $(15) : $(16) : $(17) : $(18) : $(19) ;
128 else if ! $(.last-error-$(.args))
130 for local n in $(.args)
132 # Add an extra empty string so that we always have something in the
134 .last-error-$(n) = $($(n)) "" ;
139 if --no-error-backtrace in [ modules.peek : ARGV ]
141 .no-error-backtrace = true ;
145 # Print an error message with a stack backtrace and exit.
147 rule error ( messages * : * )
149 if $(.no-error-backtrace)
151 local first-printed ;
152 # Print each argument on a separate line.
153 for local n in $(.args)
155 if $($(n))-is-defined
157 if ! $(first-printed)
159 ECHO error: $($(n)) ;
160 first-printed = true ;
172 error-skip-frames 3 $(1) : $(2) : $(3) : $(4) : $(5) : $(6) : $(7) :
173 $(8) : $(9) : $(10) : $(11) : $(12) : $(13) : $(14) : $(15) : $(16)
174 : $(17) : $(18) : $(19) ;
179 # Same as 'error', but the generated backtrace will include only user files.
181 rule user-error ( messages * : * )
183 .user-modules-only = 1 ;
184 error-skip-frames 3 $(1) : $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) :
185 $(9) : $(10) : $(11) : $(12) : $(13) : $(14) : $(15) : $(16) : $(17) :
190 # Print a warning message with a stack backtrace and exit.
194 backtrace 2 warning: $(1) : $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) :
195 $(9) : $(10) : $(11) : $(12) : $(13) : $(14) : $(15) : $(16) : $(17) :
200 # Convert an arbitrary argument list into a list with ":" separators and quoted
201 # elements representing the same information. This is mostly useful for
202 # formatting descriptions of arguments with which a rule was called when
203 # reporting an error.
208 local remaining = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ;
209 while $($(remaining))
211 local n = $(remaining[1]) ;
212 remaining = $(remaining[2-]) ;
218 result += \"$($(n))\" ;
224 # Return the file:line for the nearest entry in backtrace which correspond to a
227 rule nearest-user-location ( )
229 local bt = [ BACKTRACE ] ;
232 while $(bt) && ! $(result)
234 local m = [ MATCH ^(.+)\\.$ : $(bt[3]) ] ;
235 local user-modules = ([Jj]amroot(.jam|.v2|)|([Jj]amfile(.jam|.v2|)|user-config.jam|site-config.jam|project-config.jam|project-root.jam) ;
237 if [ MATCH $(user-modules) : $(bt[1]:D=) ]
239 result = $(bt[1]):$(bt[2]) ;
247 # If optimized rule is available in Jam, use it.
248 if NEAREST_USER_LOCATION in [ RULENAMES ]
250 rule nearest-user-location ( )
252 local r = [ NEAREST_USER_LOCATION ] ;
253 return $(r[1]):$(r[2]) ;
260 # Show that we can correctly catch an expected error.
263 error an error occurred : somewhere ;
265 catch an error occurred : somewhere ;
267 # Show that unexpected errors generate real errors.
272 error an error occurred : somewhere ;
274 catch an error occurred : nowhere ;
276 catch expected \"nowhere\" in argument 2 ;
278 # Show that not catching an error where one was expected is an error.
286 catch expected an error, but none occurred ;