Class: Pronto::Cpplint

Inherits:
Runner
  • Object
show all
Defined in:
lib/pronto/cpplint.rb

Overview

Main class for extracting cpplint complains

Constant Summary collapse

CPP_FILE_EXTENSIONS =
%w[c c++ cc cu cuh icc h++ hpp hxx hh cxx cpp].freeze

Instance Method Summary collapse

Constructor Details

#initialize(patches, commit = nil) ⇒ Cpplint

Returns a new instance of Cpplint.



12
13
14
# File 'lib/pronto/cpplint.rb', line 12

def initialize(patches, commit = nil)
  super(patches, commit)
end

Instance Method Details

#cpp?(file) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/pronto/cpplint.rb', line 67

def cpp?(file)
  CPP_FILE_EXTENSIONS.select { |extension| file.end_with? ".#{extension}" }.any?
end

#executableObject



16
17
18
# File 'lib/pronto/cpplint.rb', line 16

def executable
  'cpplint'
end

#filesObject



20
21
22
23
24
25
26
27
28
# File 'lib/pronto/cpplint.rb', line 20

def files
  return [] if @patches.nil?

  @files ||= @patches
             .select { |patch| patch.additions.positive? }
             .map(&:new_file_full_path)
             .map(&:to_s)
             .compact
end

#filter_cpp_files(all_files) ⇒ Object



71
72
73
74
# File 'lib/pronto/cpplint.rb', line 71

def filter_cpp_files(all_files)
  all_files.select { |file| cpp? file.to_s }
           .map { |file| file.to_s.shellescape }
end

#git_repo_pathObject



116
117
118
119
# File 'lib/pronto/cpplint.rb', line 116

def git_repo_path
  @git_repo_path ||= Rugged::Repository.discover(File.expand_path(Dir.pwd))
                                       .workdir
end

#messages(complains) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/pronto/cpplint.rb', line 103

def messages(complains)
  complains.map do |msg|
    patch_line = patch_line_for_offence(msg[:file_path],
                                        msg[:line_number])
    next if patch_line.nil?

    description = msg[:message]
    path = patch_line.patch.delta.new_file[:path]
    Message.new(path, patch_line, msg[:level].to_sym,
                description, nil, self.class)
  end.compact
end

#parse_output(executable_output) ⇒ Object



76
77
78
79
# File 'lib/pronto/cpplint.rb', line 76

def parse_output(executable_output)
  lines = executable_output.split("\n")
  lines.map { |line| parse_output_line(line) }
end

#parse_output_line(line) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pronto/cpplint.rb', line 81

def parse_output_line(line)
  splits = line.strip.split(':')
  message = splits[2..].join(':').strip
  message = "cpplint: #{message}"
  {
    file_path: splits[0],
    line_number: splits[1].to_i,
    column_number: 0,
    message:,
    level: violation_level(message)
  }
end

#patch_line_for_offence(path, lineno) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pronto/cpplint.rb', line 30

def patch_line_for_offence(path, lineno)
  patch_node = @patches.find do |patch|
    patch.new_file_full_path.to_s == path
  end

  return if patch_node.nil?

  patch_node.added_lines.find do |patch_line|
    patch_line.new_lineno == lineno
  end
end

#runObject



42
43
44
45
46
47
48
# File 'lib/pronto/cpplint.rb', line 42

def run
  if files.any?
    messages(run_cpplint)
  else
    []
  end
end

#run_cpplintObject

rubocop:disable Metrics/MethodLength



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pronto/cpplint.rb', line 50

def run_cpplint # rubocop:disable Metrics/MethodLength
  Dir.chdir(git_repo_path) do
    cpp_files = filter_cpp_files(files)
    files_to_lint = cpp_files.join(' ')
    extra = ENV.fetch('PRONTO_CPPLINT_OPTS', nil)
    if files_to_lint.empty?
      []
    else
      cmd = "#{executable} #{extra} #{files_to_lint}"
      _stdout, stderr, _status = Open3.capture3(cmd)
      return [] if stderr.nil?

      parse_output stderr
    end
  end
end

#violation_level(message) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/pronto/cpplint.rb', line 94

def violation_level(message)
  # TODO
  if message.split[1].include? 'HIGH'
    'error'
  else
    'warning'
  end
end