Module: Roebe::Warning::Processor

Defined in:
lib/roebe/toplevel_methods/warning.rb

Overview

Roebe::Warning::Processor

Constant Summary collapse

IGNORE_MAP =
#

Roebe::Warning::Processor::IGNORE_MAP

Map of symbols to regexps for warning messages to ignore.

#
{
  ambiguous_slash: /: warning: ambiguous first argument; put parentheses or a space even after `\/' operator\n\z/,
  arg_prefix: /: warning: `[&\*]' interpreted as argument prefix\n\z/,
  bignum: /: warning: constant ::Bignum is deprecated\n\z/,
  fixnum: /: warning: constant ::Fixnum is deprecated\n\z/,
  method_redefined: /: warning: method redefined; discarding old .+\n\z|: warning: previous definition of .+ was here\n\z/,
  missing_gvar: /: warning: global variable `\$.+' not initialized\n\z/,
  missing_ivar: /: warning: instance variable @.+ not initialized\n\z/,
  not_reached: /: warning: statement not reached\n\z/,
  shadow: /: warning: shadowing outer local variable - \w+\n\z/,
  unused_var: /: warning: assigned but unused variable - \w+\n\z/,
  useless_operator: /: warning: possibly useless use of [><!=]+ in void context\n\z/,
  keyword_separation: /: warning: (?:Using the last argument (?:for `.+' )?as keyword parameters is deprecated; maybe \*\* should be added to the call|Passing the keyword argument (?:for `.+' )?as the last hash parameter is deprecated|Splitting the last argument (?:for `.+' )?into positional and keyword parameters is deprecated|The called method (?:`.+' )?is defined here)\n\z/,
  safe: /: warning: (?:rb_safe_level_2_warning|rb_safe_level|rb_set_safe_level_force|rb_set_safe_level|rb_secure|rb_insecure_operation|rb_check_safe_obj|\$SAFE) will (?:be removed|become a normal global variable) in Ruby 3\.0\n\z/,
  taint: /: warning: (?:rb_error_untrusted|rb_check_trusted|Pathname#taint|Pathname#untaint|rb_env_path_tainted|Object#tainted\?|Object#taint|Object#untaint|Object#untrusted\?|Object#untrust|Object#trust|rb_obj_infect|rb_tainted_str_new|rb_tainted_str_new_cstr) is deprecated and will be removed in Ruby 3\.2\.\n\z/,
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extend_itObject

#

Roebe::Warning::Processor.extend_it

#


176
177
178
179
180
181
182
# File 'lib/roebe/toplevel_methods/warning.rb', line 176

def self.extend_it
  @ignore = []
  @process = []
  @dedup = false
  @monitor = Monitor.new
  extend ::Roebe::Warning::Processor
end

Instance Method Details

#clearObject

Clear all current ignored warnings, warning processors, and duplicate check cache. Also disables deduplicating warnings if that is currently enabled.



39
40
41
42
43
44
45
# File 'lib/roebe/toplevel_methods/warning.rb', line 39

def clear
  synchronize {
    @ignore.clear
    @process.clear
    @dedup = false
  }
end

#dedupObject

dedup

Deduplicate warnings, supress warning messages if the same warning message has already occurred. Note that this can lead to unbounded memory use if unique warnings are generated.



52
53
54
# File 'lib/roebe/toplevel_methods/warning.rb', line 52

def dedup
  @dedup = {}
end

#freezeObject

freeze



57
58
59
60
61
# File 'lib/roebe/toplevel_methods/warning.rb', line 57

def freeze
  @ignore.freeze
  @process.freeze
  super
end

#ignore(regexp, path = '') ⇒ Object

Ignore any warning messages matching the given regexp, if they start with the given path. The regexp can also be one of the following symbols (or an array including them), which will use an appropriate regexp for the given warning:

:arg_prefix

Ignore warnings when using * or & as an argument prefix

:ambiguous_slash

Ignore warnings for things like method /regexp/

:bignum

Ignore warnings when referencing the ::Bignum constant.

:fixnum

Ignore warnings when referencing the ::Fixnum constant.

:keyword_separation

Ignore warnings related to keyword argument separation.

:method_redefined

Ignore warnings when defining a method in a class/module where a method of the same name was already defined in that class/module.

:missing_gvar

Ignore warnings for accesses to global variables that have not yet been initialized

:missing_ivar

Ignore warnings for accesses to instance variables that have not yet been initialized

:not_reached

Ignore statement not reached warnings.

:safe

Ignore warnings related to $SAFE and related C-API functions.

:shadow

Ignore warnings related to shadowing outer local variables.

:taint

Ignore warnings related to taint and related methods and C-API functions.

:unused_var

Ignore warnings for unused variables.

:useless_operator

Ignore warnings when using operators such as == and > when the result is not used.

Examples:

# Ignore all uninitialized instance variable warnings
Warning.ignore(/instance variable @\w+ not initialized/)

# Ignore all uninitialized instance variable warnings in current file
Warning.ignore(/instance variable @\w+ not initialized/, __FILE__)

# Ignore all uninitialized instance variable warnings in current file
Warning.ignore(:missing_ivar, __FILE__)

# Ignore all uninitialized instance variable and method redefined warnings in current file
Warning.ignore([:missing_ivar, :method_redefined],  __FILE__)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/roebe/toplevel_methods/warning.rb', line 100

def ignore(regexp, path = '')
  case regexp
  when Regexp
    # already regexp
  when Symbol
    regexp = IGNORE_MAP.fetch(regexp)
  when Array
    regexp = Regexp.union(regexp.map{|re| IGNORE_MAP.fetch(re)})
  else
    raise TypeError, "first argument to Warning.ignore should be Regexp, Symbol, or Array of Symbols, got #{regexp.inspect}"
  end

  synchronize { 
    @ignore << [path, regexp]
  }
  nil
end

#process(path = '', &block) ⇒ Object

process

Handle all warnings starting with the given path, instead of the default behavior of printing them to $stderr. Examples:

# Write warning to LOGGER at level warning
Warning.process do |warning|
  LOGGER.warning(warning)
end

# Write warnings in the current file to LOGGER at level error level
Warning.process(__FILE__) do |warning|
  LOGGER.error(warning)
end


132
133
134
135
136
137
138
139
# File 'lib/roebe/toplevel_methods/warning.rb', line 132

def process(path = '', &block)
  synchronize do
    @process << [path, block]
    @process.sort_by!(&:first)
    @process.reverse!
  end
  nil
end

#warn(str) ⇒ Object

Handle ignored warnings and warning processors. If the warning is not ignored, is not a duplicate warning (if checking for duplicates) and there is no warning processor setup for the warning string, then use the default behavior of writing to $stderr.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/roebe/toplevel_methods/warning.rb', line 145

def warn(str)
  synchronize{@ignore.dup}.each do |path, regexp|
    if str.start_with?(path) && str =~ regexp
      return
    end
  end

  if @dedup
    if synchronize{@dedup[str]}
      return
    end
    synchronize{@dedup[str] = true}
  end

  synchronize { @process.dup }.each { |path, block|
    if str.start_with?(path)
      block.call(str)
      return
    end
  }
    super
end