Class: Pronto::Cppcheck

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

Overview

Main class for extracting cppcheck complains

Constant Summary collapse

C_OR_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) ⇒ Cppcheck

Returns a new instance of Cppcheck.



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

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

Instance Method Details

#c_or_cpp?(file) ⇒ Boolean

Returns:

  • (Boolean)


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

def c_or_cpp?(file)
  C_OR_CPP_FILE_EXTENSIONS.any? { |extension| file.end_with? ".#{extension}" }
end

#executableObject



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

def executable
  'cppcheck'
end

#filesObject



20
21
22
23
24
25
26
27
28
# File 'lib/pronto/cppcheck.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/cppcheck.rb', line 71

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

#git_repo_pathObject



117
118
119
120
# File 'lib/pronto/cppcheck.rb', line 117

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

#messages(complains) ⇒ Object



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

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/cppcheck.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
93
# File 'lib/pronto/cppcheck.rb', line 81

def parse_output_line(line)
  splits = line.strip.split(':')
  message = splits[4..].join(':').strip
  message = "cppcheck: #{message}"

  {
    file_path: splits[0],
    line_number: splits[1].to_i,
    column_number: splits[2].to_i,
    message:,
    level: violation_level(splits[3])
  }
end

#patch_line_for_offence(path, lineno) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pronto/cppcheck.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/cppcheck.rb', line 42

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

#run_cppcheckObject

rubocop:disable Metrics/MethodLength



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

def run_cppcheck # 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_CPPCHECK_OPTS', nil)
    if files_to_lint.empty?
      []
    else
      cmd = "#{executable} --template='{file}:{line}:{column}:{severity}:{id}:{message}' --quiet #{extra} #{files_to_lint}"
      _stdout, stderr, _status = Open3.capture3(cmd)
      return [] if stderr.nil?

      parse_output stderr
    end
  end
end

#violation_level(severity) ⇒ Object



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

def violation_level(severity)
  # TODO
  if severity == 'error'
    'error'
  else
    'warning'
  end
end