1- # Copyright 2011 David Malcolm <dmalcolm@redhat.com>
2- # Copyright 2011 Red Hat, Inc.
1+ # Copyright 2011, 2016 David Malcolm <dmalcolm@redhat.com>
2+ # Copyright 2011, 2016 Red Hat, Inc.
33#
44# This is free software: you can redistribute it and/or modify it
55# under the terms of the GNU General Public License as published by
@@ -58,6 +58,13 @@ class FormatStringWarning(CExtensionWarning):
5858 def __init__ (self , fmt_string ):
5959 self .fmt_string = fmt_string
6060
61+ def emit_as_warning (self , loc ):
62+ if gcc .warning (loc , str (self )):
63+ sys .stderr .write (self .extra_info ())
64+
65+ def extra_info (self ):
66+ return ""
67+
6168class UnknownFormatChar (FormatStringWarning ):
6269 def __init__ (self , fmt_string , ch ):
6370 FormatStringWarning .__init__ (self , fmt_string )
@@ -137,12 +144,14 @@ def __init__(self, funcname, fmt, varargs):
137144 self .varargs = varargs
138145
139146 def __str__ (self ):
140- result = ('%s in call to %s with format string "%s"\n '
141- ' expected %i extra arguments:\n '
142- % (self ._get_desc_prefix (),
143- self .funcname ,
144- self .fmt .fmt_string ,
145- self .fmt .num_expected ()))
147+ return ('%s in call to %s with format string "%s"'
148+ % (self ._get_desc_prefix (),
149+ self .funcname ,
150+ self .fmt .fmt_string ))
151+
152+ def extra_info (self ):
153+ result = (' expected %i extra arguments:\n '
154+ % self .fmt .num_expected ())
146155 for (arg , exp_type ) in self .fmt .iter_exp_types ():
147156 result += ' %s\n ' % describe_type (exp_type )
148157 if len (self .varargs ) == 0 :
@@ -380,8 +389,8 @@ def check_callsite(stmt, parser, funcname, format_idx, varargs_idx, with_size_t)
380389 try :
381390 fmt = parser .from_string (fmt_string , with_size_t )
382391 except FormatStringWarning :
383- exc = sys .exc_info ()[1 ]
384- gcc . warning (stmt .loc , str ( exc ) )
392+ err = sys .exc_info ()[1 ]
393+ err . emit_as_warning (stmt .loc )
385394 return
386395 log ('fmt: %r' , fmt .args )
387396
@@ -392,11 +401,11 @@ def check_callsite(stmt, parser, funcname, format_idx, varargs_idx, with_size_t)
392401 varargs = stmt .args [varargs_idx :]
393402 # log('varargs: %r', varargs)
394403 if len (varargs ) < len (exp_types ):
395- gcc . warning ( loc , str ( NotEnoughVars (funcname , fmt , varargs )) )
404+ NotEnoughVars (funcname , fmt , varargs ). emit_as_warning ( loc )
396405 return
397406
398407 if len (varargs ) > len (exp_types ):
399- gcc . warning ( loc , str ( TooManyVars (funcname , fmt , varargs )) )
408+ TooManyVars (funcname , fmt , varargs ). emit_as_warning ( loc )
400409 return
401410
402411 for index , ((exp_arg , exp_type ), vararg ) in enumerate (zip (exp_types , varargs )):
@@ -408,9 +417,7 @@ def check_callsite(stmt, parser, funcname, format_idx, varargs_idx, with_size_t)
408417 loc = vararg .location
409418 else :
410419 loc = stmt .loc
411- gcc .warning (loc ,
412- str (err ))
413- sys .stderr .write (err .extra_info ())
420+ err .emit_as_warning (loc )
414421
415422 def maybe_check_callsite (stmt ):
416423 if stmt .fndecl :
0 commit comments