class AdLint::Exam::CBuiltin::W0431

Private Instance Methods

do_execute(phase_ctxt) click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 8882
def do_execute(phase_ctxt)
  while tok = next_token
    case tok.type
    when "{"
      on_left_brace(tok)
    when "}"
      on_right_brace(tok)
    when "("
      on_left_paren(tok)
    when ")"
      on_right_paren(tok)
    end

    case tok.type
    when :IF, :FOR, :WHILE
      if last_line_no < tok.location.line_no
        on_beginning_of_line(tok)
      end
      skip_controlling_part
      unless tok = peek_token and tok.type == "{"
        skip_simple_substatement
      end
    when :ELSE
      if last_line_no < tok.location.line_no
        on_beginning_of_line(tok)
      end
      unless tok = peek_token and tok.type == :IF || tok.type == "{"
        skip_simple_substatement
      end
    when :DO
      if last_line_no < tok.location.line_no
        on_beginning_of_line(tok)
      end
      unless tok = peek_token and tok.type == "{"
        skip_simple_substatement
      end
    else
      if last_line_no < tok.location.line_no
        on_beginning_of_line(tok)
      end
    end
  end
end
do_prepare(phase_ctxt) click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 8866
def do_prepare(phase_ctxt)
  fpath = phase_ctxt[:sources].first.fpath
  @tokens = phase_ctxt[:cc1_tokens].select { |tok|
    tok.location.fpath == fpath
  }
  @index = 0
  @indent_level = 0
  @indent_widths = Hash.new(0)
  @paren_depth = 0
  @lst_token = nil
end
last_line_no() click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 8878
def last_line_no
  @lst_token ? @lst_token.location.line_no : 0
end
monitor() click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 9054
def monitor
  @phase_ctxt.monitor
end
next_token() click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 8973
def next_token
  return nil unless tok = peek_token
  @index += 1

  case tok.type
  when :CASE
    while tok = peek_token
      @index += 1
      break if tok.type == ":"
    end
  when :IDENTIFIER, :DEFAULT
    if nxt_tok = @tokens[@index] and nxt_tok.type == ":"
      tok = peek_token
      @index += 1
    end
  end

  tok
end
on_beginning_of_line(tok) click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 9026
def on_beginning_of_line(tok)
  return if @paren_depth > 0 || @lst_token.replaced?

  case tok.type
  when "{"
    if @indent_level == 0
      widths_idx = @indent_level
    else
      widths_idx = @indent_level - 1
    end
  when "}"
    if indent_style == INDENT_STYLE_GNU && @indent_level > 0
      widths_idx = @indent_level + 1
    else
      widths_idx = @indent_level
    end
  else
    widths_idx = @indent_level
  end

  expected_column_no = @indent_widths[widths_idx]
  if tok.location.appearance_column_no < expected_column_no
    W(tok.location) if tok.analysis_target?(traits)
  end

  @indent_widths[widths_idx] = tok.location.appearance_column_no
end
on_left_brace(tok) click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 9001
def on_left_brace(tok)
  if indent_style == INDENT_STYLE_GNU && @indent_level > 0
    @indent_level += 2
  else
    @indent_level += 1
  end
end
on_left_paren(tok) click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 9018
def on_left_paren(tok)
  @paren_depth += 1
end
on_right_brace(tok) click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 9009
def on_right_brace(tok)
  if indent_style == INDENT_STYLE_GNU
    @indent_level -= 2
    @indent_level = 0 if @indent_level < 0
  else
    @indent_level -= 1
  end
end
on_right_paren(tok) click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 9022
def on_right_paren(tok)
  @paren_depth -= 1
end
peek_token() click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 8993
def peek_token
  if tok = @tokens[@index]
    @lst_token = @tokens[[0, @index - 1].max]
    checkpoint(tok.location)
  end
  tok
end
skip_controlling_part() click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 8926
def skip_controlling_part
  paren_depth = 0
  while tok = next_token
    case tok.type
    when "("
      paren_depth += 1
    when ")"
      paren_depth -= 1
      break if paren_depth == 0
    end
  end
end
skip_simple_substatement() click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 8939
def skip_simple_substatement
  paren_depth = 0
  while tok = next_token
    case tok.type
    when "("
      paren_depth += 1
    when ")"
      paren_depth -= 1
    end

    case tok.type
    when :IF, :FOR, :WHILE
      skip_controlling_part
      unless tok = peek_token and tok.type == "{"
        skip_simple_substatement
        break
      end
    when :ELSE
      unless tok = peek_token and tok.type == :IF || tok.type == "{"
        skip_simple_substatement
        break
      end
    when :DO
      unless tok = peek_token and tok.type == "{"
        skip_simple_substatement
        skip_simple_substatement
        break
      end
    when ";"
      break if paren_depth == 0
    end
  end
end