blob: 0fb51916fff6efe3a00b5d23af6bfce7f5f3ecff (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2022, Ideas On Board
*
* IPA module
*/
#pragma once
#include <list>
#include <memory>
#include <string>
#include <vector>
#include <libcamera/base/log.h>
#include <libcamera/base/utils.h>
#include "libcamera/internal/yaml_parser.h"
#include "algorithm.h"
namespace libcamera {
LOG_DECLARE_CATEGORY(IPAModuleAlgo)
namespace ipa {
template<typename _Context, typename _FrameContext, typename _Config,
typename _Params, typename _Stats>
class Module : public Loggable
{
public:
using Context = _Context;
using FrameContext = _FrameContext;
using Config = _Config;
using Params = _Params;
using Stats = _Stats;
virtual ~Module() {}
const std::list<std::unique_ptr<Algorithm<Module>>> &algorithms() const
{
return algorithms_;
}
int createAlgorithms(Context &context, const YamlObject &algorithms)
{
const auto &list = algorithms.asList();
for (const auto &[i, algo] : utils::enumerate(list)) {
if (!algo.isDictionary()) {
LOG(IPAModuleAlgo, Error)
<< "Invalid YAML syntax for algorithm " << i;
algorithms_.clear();
return -EINVAL;
}
int ret = createAlgorithm(context, algo);
if (ret) {
algorithms_.clear();
return ret;
}
}
return 0;
}
static void registerAlgorithm(AlgorithmFactoryBase<Module> *factory)
{
factories().push_back(factory);
}
private:
int createAlgorithm(Context &context, const YamlObject &data)
{
const auto &[name, algoData] = *data.asDict().begin();
std::unique_ptr<Algorithm<Module>> algo = createAlgorithm(name);
if (!algo) {
LOG(IPAModuleAlgo, Error)
<< "Algorithm '" << name << "' not found";
return -EINVAL;
}
int ret = algo->init(context, algoData);
if (ret) {
LOG(IPAModuleAlgo, Error)
<< "Algorithm '" << name << "' failed to initialize";
return ret;
}
LOG(IPAModuleAlgo, Debug)
<< "Instantiated algorithm '" << name << "'";
algorithms_.push_back(std::move(algo));
return 0;
}
static std::unique_ptr<Algorithm<Module>> createAlgorithm(const std::string &name)
{
for (const AlgorithmFactoryBase<Module> *factory : factories()) {
if (factory->name() == name)
return factory->create();
}
return nullptr;
}
static std::vector<AlgorithmFactoryBase<Module> *> &factories()
{
/*
* The static factories map is defined inside the function to ensure
* it gets initialized on first use, without any dependency on
* link order.
*/
static std::vector<AlgorithmFactoryBase<Module> *> factories;
return factories;
}
std::list<std::unique_ptr<Algorithm<Module>>> algorithms_;
};
} /* namespace ipa */
} /* namespace libcamera */
|
91'>491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
#!/usr/bin/python3
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2018, Google Inc.
#
# Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
#
# checkstyle.py - A patch style checker script based on clang-format
#
# TODO:
#
# - Support other formatting tools and checkers (cppcheck, cpplint, kwstyle, ...)
# - Split large hunks to minimize context noise
# - Improve style issues counting
#
import argparse
import difflib
import fnmatch
import os.path
import re
import shutil
import subprocess
import sys
dependencies = {
'clang-format': True,
'git': True,
}
# ------------------------------------------------------------------------------
# Colour terminal handling
#
class Colours:
Default = 0
Black = 0
Red = 31
Green = 32
Yellow = 33
Blue = 34
Magenta = 35
Cyan = 36
LightGrey = 37
DarkGrey = 90
LightRed = 91
LightGreen = 92
Lightyellow = 93
LightBlue = 94
LightMagenta = 95
LightCyan = 96
White = 97
@staticmethod
def fg(colour):
if sys.stdout.isatty():
return '\033[%um' % colour
else:
return ''
@staticmethod
def bg(colour):
if sys.stdout.isatty():
return '\033[%um' % (colour + 10)
else:
return ''
@staticmethod
def reset():
if sys.stdout.isatty():
return '\033[0m'
else:
return ''
# ------------------------------------------------------------------------------
# Diff parsing, handling and printing
#
class DiffHunkSide(object):
"""A side of a diff hunk, recording line numbers"""
def __init__(self, start):
self.start = start
self.touched = []
self.untouched = []
def __len__(self):
return len(self.touched) + len(self.untouched)
class DiffHunk(object):
diff_header_regex = re.compile(r'@@ -([0-9]+),?([0-9]+)? \+([0-9]+),?([0-9]+)? @@')
def __init__(self, line):
match = DiffHunk.diff_header_regex.match(line)
if not match:
raise RuntimeError("Malformed diff hunk header '%s'" % line)
self.__from_line = int(match.group(1))
self.__to_line = int(match.group(3))
self.__from = DiffHunkSide(self.__from_line)
self.__to = DiffHunkSide(self.__to_line)
self.lines = []
def __repr__(self):
s = '%s@@ -%u,%u +%u,%u @@\n' % \
(Colours.fg(Colours.Cyan),
self.__from.start, len(self.__from),
self.__to.start, len(self.__to))
for line in self.lines:
if line[0] == '-':
s += Colours.fg(Colours.Red)
elif line[0] == '+':
s += Colours.fg(Colours.Green)
if line[0] == '-':
spaces = 0
for i in range(len(line)):
if line[-i-1].isspace():
spaces += 1
else:
break
spaces = len(line) - spaces
line = line[0:spaces] + Colours.bg(Colours.Red) + line[spaces:]
s += line
s += Colours.reset()
s += '\n'
return s[:-1]
def append(self, line):
if line[0] == ' ':
self.__from.untouched.append(self.__from_line)
self.__from_line += 1
self.__to.untouched.append(self.__to_line)
self.__to_line += 1
elif line[0] == '-':
self.__from.touched.append(self.__from_line)
self.__from_line += 1
elif line[0] == '+':
self.__to.touched.append(self.__to_line)
self.__to_line += 1
self.lines.append(line.rstrip('\n'))
def intersects(self, lines):
for line in lines:
if line in self.__from.touched:
return True
return False
def side(self, side):
if side == 'from':
return self.__from
else:
return self.__to
def parse_diff(diff):
hunks = []
hunk = None
for line in diff:
if line.startswith('@@'):
if hunk:
hunks.append(hunk)
hunk = DiffHunk(line)
elif hunk is not None:
hunk.append(line)
if hunk:
hunks.append(hunk)
return hunks
# ------------------------------------------------------------------------------
# Commit, Staged Changes & Amendments
#
class CommitFile:
def __init__(self, name):
info = name.split()
self.__status = info[0][0]
# For renamed files, store the new name
if self.__status == 'R':
self.__filename = info[2]
else:
self.__filename = info[1]
@property
def filename(self):
return self.__filename
@property
def status(self):
return self.__status
class Commit:
def __init__(self, commit):
self.commit = commit
self._parse()
def _parse(self):
# Get the commit title and list of files.
ret = subprocess.run(['git', 'show', '--pretty=oneline', '--name-status',
self.commit],
stdout=subprocess.PIPE).stdout.decode('utf-8')
files = ret.splitlines()
self._files = [CommitFile(f) for f in files[1:]]
self._title = files[0]
def files(self, filter='AMR'):
return [f.filename for f in self._files if f.status in filter]
@property
def title(self):
return self._title
def get_diff(self, top_level, filename):
diff = subprocess.run(['git', 'diff', '%s~..%s' % (self.commit, self.commit),
'--', '%s/%s' % (top_level, filename)],
stdout=subprocess.PIPE).stdout.decode('utf-8')
return parse_diff(diff.splitlines(True))
def get_file(self, filename):
return subprocess.run(['git', 'show', '%s:%s' % (self.commit, filename)],
stdout=subprocess.PIPE).stdout.decode('utf-8')
class StagedChanges(Commit):
def __init__(self):
Commit.__init__(self, '')
def _parse(self):
ret = subprocess.run(['git', 'diff', '--staged', '--name-status'],
stdout=subprocess.PIPE).stdout.decode('utf-8')
self._title = 'Staged changes'
self._files = [CommitFile(f) for f in ret.splitlines()]
def get_diff(self, top_level, filename):
diff = subprocess.run(['git', 'diff', '--staged', '--',
'%s/%s' % (top_level, filename)],
stdout=subprocess.PIPE).stdout.decode('utf-8')
return parse_diff(diff.splitlines(True))
class Amendment(StagedChanges):
def __init__(self):
StagedChanges.__init__(self)
def _parse(self):
# Create a title using HEAD commit
ret = subprocess.run(['git', 'show', '--pretty=oneline', '--no-patch'],
stdout=subprocess.PIPE).stdout.decode('utf-8')
self._title = 'Amendment of ' + ret.strip()
# Extract the list of modified files
ret = subprocess.run(['git', 'diff', '--staged', '--name-status', 'HEAD~'],
stdout=subprocess.PIPE).stdout.decode('utf-8')
self._files = [CommitFile(f) for f in ret.splitlines()]
def get_diff(self, top_level, filename):
diff = subprocess.run(['git', 'diff', '--staged', 'HEAD~', '--',
'%s/%s' % (top_level, filename)],
stdout=subprocess.PIPE).stdout.decode('utf-8')
return parse_diff(diff.splitlines(True))
# ------------------------------------------------------------------------------
# Helpers
#
class ClassRegistry(type):
def __new__(cls, clsname, bases, attrs):
newclass = super().__new__(cls, clsname, bases, attrs)
if bases:
bases[0].subclasses.append(newclass)
bases[0].subclasses.sort(key=lambda x: getattr(x, 'priority', 0),
reverse=True)
return newclass
# ------------------------------------------------------------------------------
# Commit Checkers
#
class CommitChecker(metaclass=ClassRegistry):
subclasses = []
def __init__(self):
pass
#
# Class methods
#
@classmethod
def checkers(cls):
for checker in cls.subclasses:
yield checker
class CommitIssue(object):
def __init__(self, msg):
self.msg = msg
class HeaderAddChecker(CommitChecker):
@classmethod
def check(cls, commit, top_level):
issues = []
meson_files = [f for f in commit.files('M')
if os.path.basename(f) == 'meson.build']
for filename in commit.files('AR'):
if not filename.startswith('include/libcamera/') or \
not filename.endswith('.h'):
continue
meson = os.path.dirname(filename) + '/meson.build'
header = os.path.basename(filename)
issue = CommitIssue('Header %s added without corresponding update to %s' %
(filename, meson))
if meson not in meson_files:
issues.append(issue)
continue
diff = commit.get_diff(top_level, meson)
found = False
for hunk in diff:
for line in hunk.lines:
if line[0] != '+':
continue
if line.find("'%s'" % header) != -1:
found = True
break
if found:
break
if not found:
issues.append(issue)
return issues
# ------------------------------------------------------------------------------
# Style Checkers
#
class StyleChecker(metaclass=ClassRegistry):
subclasses = []
def __init__(self):
pass
#
# Class methods
#
@classmethod
def checkers(cls, filename):
for checker in cls.subclasses:
if checker.supports(filename):
yield checker
@classmethod
def supports(cls, filename):
for pattern in cls.patterns:
if fnmatch.fnmatch(os.path.basename(filename), pattern):
return True
return False
@classmethod
def all_patterns(cls):
patterns = set()
for checker in cls.subclasses:
patterns.update(checker.patterns)
return patterns
class StyleIssue(object):
def __init__(self, line_number, line, msg):
self.line_number = line_number
self.line = line
self.msg = msg
class IncludeChecker(StyleChecker):
patterns = ('*.cpp', '*.h')
headers = ('assert', 'ctype', 'errno', 'fenv', 'float', 'inttypes',
'limits', 'locale', 'setjmp', 'signal', 'stdarg', 'stddef',
'stdint', 'stdio', 'stdlib', 'string', 'time', 'uchar', 'wchar',
'wctype')
include_regex = re.compile('^#include <c([a-z]*)>')
def __init__(self, content):
super().__init__()
self.__content = content
def check(self, line_numbers):
issues = []
for line_number in line_numbers:
line = self.__content[line_number - 1]
match = IncludeChecker.include_regex.match(line)
if not match:
continue
header = match.group(1)
if header not in IncludeChecker.headers:
continue
issues.append(StyleIssue(line_number, line,
'C compatibility header <%s.h> is preferred' % header))
return issues
class LogCategoryChecker(StyleChecker):
log_regex = re.compile('\\bLOG\((Debug|Info|Warning|Error|Fatal)\)')
patterns = ('*.cpp',)
def __init__(self, content):
super().__init__()
self.__content = content
def check(self, line_numbers):
issues = []
for line_number in line_numbers:
line = self.__content[line_number-1]
if not LogCategoryChecker.log_regex.search(line):
continue
issues.append(StyleIssue(line_number, line, 'LOG() should use categories'))
return issues
class MesonChecker(StyleChecker):
patterns = ('meson.build',)
def __init__(self, content):
super().__init__()
self.__content = content
def check(self, line_numbers):
issues = []
for line_number in line_numbers:
line = self.__content[line_number-1]
if line.find('\t') != -1:
issues.append(StyleIssue(line_number, line, 'meson.build should use spaces for indentation'))
return issues
class Pep8Checker(StyleChecker):
patterns = ('*.py',)
results_regex = re.compile('stdin:([0-9]+):([0-9]+)(.*)')
def __init__(self, content):
super().__init__()
self.__content = content
def check(self, line_numbers):
issues = []
data = ''.join(self.__content).encode('utf-8')
try:
ret = subprocess.run(['pycodestyle', '--ignore=E501', '-'],
input=data, stdout=subprocess.PIPE)
except FileNotFoundError:
issues.append(StyleIssue(0, None, 'Please install pycodestyle to validate python additions'))
return issues
results = ret.stdout.decode('utf-8').splitlines()
for item in results:
search = re.search(Pep8Checker.results_regex, item)
line_number = int(search.group(1))
position = int(search.group(2))
msg = search.group(3)
if line_number in line_numbers:
line = self.__content[line_number - 1]
issues.append(StyleIssue(line_number, line, msg))
return issues
class ShellChecker(StyleChecker):
patterns = ('*.sh',)
results_line_regex = re.compile('In - line ([0-9]+):')
def __init__(self, content):
super().__init__()
self.__content = content
def check(self, line_numbers):
issues = []
data = ''.join(self.__content).encode('utf-8')
try:
ret = subprocess.run(['shellcheck', '-Cnever', '-'],
input=data, stdout=subprocess.PIPE)
except FileNotFoundError:
issues.append(StyleIssue(0, None, 'Please install shellcheck to validate shell script additions'))
return issues
results = ret.stdout.decode('utf-8').splitlines()
for nr, item in enumerate(results):
search = re.search(ShellChecker.results_line_regex, item)
if search is None:
continue
line_number = int(search.group(1))
line = results[nr + 1]
msg = results[nr + 2]
# Determined, but not yet used
position = msg.find('^') + 1
if line_number in line_numbers:
issues.append(StyleIssue(line_number, line, msg))
return issues
# ------------------------------------------------------------------------------
# Formatters
#
class Formatter(metaclass=ClassRegistry):
subclasses = []
def __init__(self):
pass
#
# Class methods
#
@classmethod
def formatters(cls, filename):
for formatter in cls.subclasses:
if formatter.supports(filename):
yield formatter
@classmethod
def supports(cls, filename):
for pattern in cls.patterns:
if fnmatch.fnmatch(os.path.basename(filename), pattern):
return True
return False
@classmethod
def all_patterns(cls):
patterns = set()
for formatter in cls.subclasses:
patterns.update(formatter.patterns)
return patterns
class CLangFormatter(Formatter):
patterns = ('*.c', '*.cpp', '*.h')
priority = -1
@classmethod
def format(cls, filename, data):
ret = subprocess.run(['clang-format', '-style=file',
'-assume-filename=' + filename],
input=data.encode('utf-8'), stdout=subprocess.PIPE)
return ret.stdout.decode('utf-8')
class DoxygenFormatter(Formatter):
patterns = ('*.c', '*.cpp')
return_regex = re.compile(' +\\* +\\\\return +[a-z]')
@classmethod
def format(cls, filename, data):
lines = []
in_doxygen = False
for line in data.split('\n'):
if line.find('/**') != -1:
in_doxygen = True
if not in_doxygen:
lines.append(line)
continue
line = cls.return_regex.sub(lambda m: m.group(0)[:-1] + m.group(0)[-1].upper(), line)
if line.find('*/') != -1:
in_doxygen = False
lines.append(line)
return '\n'.join(lines)
class DPointerFormatter(Formatter):
# Ensure consistent naming of variables related to the d-pointer design
# pattern.
patterns = ('*.cpp', '*.h')
# The clang formatter runs first, we can thus rely on appropriate coding
# style.
declare_regex = re.compile(r'^(\t*)(const )?([a-zA-Z0-9_]+) \*( ?const )?([a-zA-Z0-9_]+) = (LIBCAMERA_[DO]_PTR)\(([a-zA-Z0-9_]+)\);$')
@classmethod
def format(cls, filename, data):
lines = []
for line in data.split('\n'):
match = cls.declare_regex.match(line)
if match:
indent = match.group(1) or ''
const = match.group(2) or ''
macro = match.group(6)
klass = match.group(7)
if macro == 'LIBCAMERA_D_PTR':
var = 'Private *const d'
else:
var = f'{klass} *const o'
line = f'{indent}{const}{var} = {macro}({klass});'
lines.append(line)
return '\n'.join(lines)
class IncludeOrderFormatter(Formatter):
patterns = ('*.cpp', '*.h')
include_regex = re.compile('^#include (["<])([^">]*)([">])')
@classmethod
def format(cls, filename, data):
lines = []
includes = []
# Parse blocks of #include statements, and output them as a sorted list
# when we reach a non #include statement.
for line in data.split('\n'):
match = IncludeOrderFormatter.include_regex.match(line)
if match:
# If the current line is an #include statement, add it to the
# includes group and continue to the next line.
open_token = match.group(1)
file_name = match.group(2)
close_token = match.group(3)
# Ensure the "..." include style for internal headers and the
# <...> style for all other libcamera headers.
if (file_name.startswith('libcamera/internal')):
open_token = '"'
close_token = '"'
elif (file_name.startswith('libcamera/')):
open_token = '<'
close_token = '>'
line = f'#include {open_token}{file_name}{close_token}'
includes.append((line, file_name))
continue
# The current line is not an #include statement, output the sorted
# stashed includes first, and then the current line.
if len(includes):
includes.sort(key=lambda i: i[1])
for include in includes:
lines.append(include[0])
includes = []
lines.append(line)
# In the unlikely case the file ends with an #include statement, make
# sure we output the stashed includes.
if len(includes):
includes.sort(key=lambda i: i[1])
for include in includes:
lines.append(include[0])
includes = []
return '\n'.join(lines)
class StripTrailingSpaceFormatter(Formatter):
patterns = ('*.c', '*.cpp', '*.h', '*.py', 'meson.build')
@classmethod
def format(cls, filename, data):
lines = data.split('\n')
for i in range(len(lines)):
lines[i] = lines[i].rstrip() + '\n'
return ''.join(lines)
# ------------------------------------------------------------------------------
# Style checking
#
def check_file(top_level, commit, filename):
# Extract the line numbers touched by the commit.
commit_diff = commit.get_diff(top_level, filename)
lines = []
for hunk in commit_diff:
lines.extend(hunk.side('to').touched)
# Skip commits that don't add any line.
if len(lines) == 0:
return 0
# Format the file after the commit with all formatters and compute the diff
# between the unformatted and formatted contents.
after = commit.get_file(filename)
formatted = after
for formatter in Formatter.formatters(filename):
formatted = formatter.format(filename, formatted)
after = after.splitlines(True)
formatted = formatted.splitlines(True)
diff = difflib.unified_diff(after, formatted)
# Split the diff in hunks, recording line number ranges for each hunk, and
# filter out hunks that are not touched by the commit.
formatted_diff = parse_diff(diff)
formatted_diff = [hunk for hunk in formatted_diff if hunk.intersects(lines)]
# Check for code issues not related to formatting.
issues = []
for checker in StyleChecker.checkers(filename):
checker = checker(after)
for hunk in commit_diff:
issues += checker.check(hunk.side('to').touched)
# Print the detected issues.
if len(issues) == 0 and len(formatted_diff) == 0:
return 0
print('%s---' % Colours.fg(Colours.Red), filename)
print('%s+++' % Colours.fg(Colours.Green), filename)
if len(formatted_diff):