Class: AdLint::Cpp::FunctionLikeMacro

Inherits:
Macro
  • Object
show all
Defined in:
lib/adlint/cpp/macro.rb

Direct Known Subclasses

PragmaOperator

Instance Attribute Summary collapse

Attributes inherited from Macro

#define_line

Instance Method Summary collapse

Methods inherited from Macro

#location, #name, #replacement_list

Methods included from LocationHolder

#analysis_target?

Constructor Details

#initialize(define_line) ⇒ FunctionLikeMacro

Returns a new instance of FunctionLikeMacro.



100
101
102
103
104
105
106
107
# File 'lib/adlint/cpp/macro.rb', line 100

def initialize(define_line)
  super
  if params = define_line.identifier_list
    @parameter_names = params.identifiers.map { |tok| tok.value }
  else
    @parameter_names = []
  end
end

Instance Attribute Details

#parameter_namesObject (readonly)

Returns the value of attribute parameter_names.



109
110
111
# File 'lib/adlint/cpp/macro.rb', line 109

def parameter_names
  @parameter_names
end

Instance Method Details

#expand(toks, macro_tbl, repl_ctxt) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/adlint/cpp/macro.rb', line 124

def expand(toks, macro_tbl, repl_ctxt)
  super

  args, * = parse_arguments(toks, 1)
  args = [] if @parameter_names.empty?
  args_hash =
    @parameter_names.zip(args).each_with_object({}) { |(param, arg), hash|
      hash[param] = arg
    }

  rslt_toks = expand_replacement_list(args_hash, toks.first.location,
                                      macro_tbl, repl_ctxt)
  macro_tbl.notify_function_like_macro_replacement(self, toks, args,
                                                   rslt_toks)
  rslt_toks
end

#function_like?Boolean

Returns:

  • (Boolean)


141
# File 'lib/adlint/cpp/macro.rb', line 141

def function_like?; true end

#replaceable_size(toks) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/adlint/cpp/macro.rb', line 111

def replaceable_size(toks)
  return 0 unless name.value == toks.first.value
  args, idx = parse_arguments(toks, 1)
  case
  when args && @parameter_names.empty?
    idx + 1
  when args && @parameter_names.size >= args.size
    idx + 1
  else
    0
  end
end