module AdLint::Cpp::TextLineNormalizer

Public Class Methods

complete_macro_reference?(pp_toks, pp_ctxt) click to toggle source
# File lib/adlint/cpp/eval.rb, line 984
def complete_macro_reference?(pp_toks, pp_ctxt)
  idx = 0
  while tok = pp_toks[idx]
    idx += 1
    macro = pp_ctxt.macro_table.lookup(tok.value)
    if macro && macro.function_like?
      next if not_calling_function_like_macro?(pp_toks, idx)
    else
      next
    end

    # NOTE: It's not completed when a new-line appears after the macro
    #       name.
    return false unless pp_toks.drop(idx).any? { |t| t.value == "(" }

    paren_cnt = 0
    while tok = pp_toks[idx]
      case tok.value
      when "("
        paren_cnt += 1
      when ")"
        paren_cnt -= 1
        break if paren_cnt == 0
      end
      idx += 1
    end

    return false if paren_cnt > 0
  end
  true
end
normalize(text_line, pp_ctxt) click to toggle source
# File lib/adlint/cpp/eval.rb, line 957
def normalize(text_line, pp_ctxt)
  tab_width = pp_ctxt.traits.of_project.coding_style.tab_width
  pp_toks = []
  unless pp_ctxt.deferred_text_lines.empty?
    pp_ctxt.deferred_text_lines.each do |deferred_line|
      lexer = TextLineToPPTokensLexer.new(deferred_line, tab_width)
      pp_toks += lexer.execute.to_a
    end
  end

  lexer = TextLineToPPTokensLexer.new(text_line, tab_width)
  pp_toks += lexer.execute.to_a

  fun_like_macro_referred = pp_toks.any? { |tok|
    (macro = pp_ctxt.macro_table.lookup(tok.value)) ?
      macro.function_like? : false
  }

  if fun_like_macro_referred
    return nil unless complete_macro_reference?(pp_toks, pp_ctxt)
  end

  pp_ctxt.macro_table.replace(pp_toks)
  pp_toks
end
not_calling_function_like_macro?(pp_toks, idx) click to toggle source
# File lib/adlint/cpp/eval.rb, line 1017
def not_calling_function_like_macro?(pp_toks, idx)
  while pp_tok = pp_toks[idx]
    case
    when pp_tok.value == "("
      return false
    when pp_tok.type == :NEW_LINE
      idx += 1
    else
      return true
    end
  end
  false
end

Private Instance Methods

complete_macro_reference?(pp_toks, pp_ctxt) click to toggle source
# File lib/adlint/cpp/eval.rb, line 984
def complete_macro_reference?(pp_toks, pp_ctxt)
  idx = 0
  while tok = pp_toks[idx]
    idx += 1
    macro = pp_ctxt.macro_table.lookup(tok.value)
    if macro && macro.function_like?
      next if not_calling_function_like_macro?(pp_toks, idx)
    else
      next
    end

    # NOTE: It's not completed when a new-line appears after the macro
    #       name.
    return false unless pp_toks.drop(idx).any? { |t| t.value == "(" }

    paren_cnt = 0
    while tok = pp_toks[idx]
      case tok.value
      when "("
        paren_cnt += 1
      when ")"
        paren_cnt -= 1
        break if paren_cnt == 0
      end
      idx += 1
    end

    return false if paren_cnt > 0
  end
  true
end
normalize(text_line, pp_ctxt) click to toggle source
# File lib/adlint/cpp/eval.rb, line 957
def normalize(text_line, pp_ctxt)
  tab_width = pp_ctxt.traits.of_project.coding_style.tab_width
  pp_toks = []
  unless pp_ctxt.deferred_text_lines.empty?
    pp_ctxt.deferred_text_lines.each do |deferred_line|
      lexer = TextLineToPPTokensLexer.new(deferred_line, tab_width)
      pp_toks += lexer.execute.to_a
    end
  end

  lexer = TextLineToPPTokensLexer.new(text_line, tab_width)
  pp_toks += lexer.execute.to_a

  fun_like_macro_referred = pp_toks.any? { |tok|
    (macro = pp_ctxt.macro_table.lookup(tok.value)) ?
      macro.function_like? : false
  }

  if fun_like_macro_referred
    return nil unless complete_macro_reference?(pp_toks, pp_ctxt)
  end

  pp_ctxt.macro_table.replace(pp_toks)
  pp_toks
end
not_calling_function_like_macro?(pp_toks, idx) click to toggle source
# File lib/adlint/cpp/eval.rb, line 1017
def not_calling_function_like_macro?(pp_toks, idx)
  while pp_tok = pp_toks[idx]
    case
    when pp_tok.value == "("
      return false
    when pp_tok.type == :NEW_LINE
      idx += 1
    else
      return true
    end
  end
  false
end