Module: AdLint::Cpp::TextLineNormalizer

Defined in:
lib/adlint/cpp/eval.rb

Class Method Summary collapse

Class Method Details

.complete_macro_reference?(pp_toks, pp_ctxt) ⇒ Boolean

Returns:

  • (Boolean)


984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
# 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[idx..-1].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) ⇒ Object



957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
# 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) ⇒ Boolean

Returns:

  • (Boolean)


1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
# 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