3 ###########################################################################
5 # Copyright 2013 BMW Car IT GmbH
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 ###########################################################################
21 import sys, re, string
22 from common_modules.common import *
24 def check_tabs_no_spaces(filename, file_lines):
26 Checks if any tab character "\t" exists in the file
29 for i in range(len(file_lines)):
30 if file_lines[i].count("\t") > 0:
31 log_warning(filename, i + 1, "tab character (\\t) found, use 4 spaces instead", file_lines[i].strip(" "))
33 def check_correct_space_count(filename, file_lines):
35 Checks that the number of spaces at the beginning of every line is divislbe by four
38 in_multiline_comment = False
39 for i in range(len(file_lines)):
40 if in_multiline_comment:
41 #if end of muli-line comment
42 if file_lines[i].count("*/") > 0:
43 #just make sure there is no other new multi-line comment starting on the same line (after the current mult-line comment is closed)
44 in_multiline_comment = file_lines[i].count("/*") > 0 and file_lines[i].index("/*") > file_lines[i].index("*/")
46 in_multiline_comment = file_lines[i].count("/*") > 0
48 #regex searches for the first character that is not a space character
49 found_match = re.search("(?! )", file_lines[i])
51 space_count = found_match.start()
52 if space_count % 4 != 0:
53 log_warning(filename, i + 1, "number of spaces at beginning of line must be divisible by 4")
56 def check_no_spacing_line_end(filename, file_lines):
58 Checks that lines do not end with unnecessary white space characters
61 for i in range(len(file_lines)):
62 if re.search(" $", file_lines[i]):
63 log_warning(filename, i + 1, "unneeded space(s) at end of line")
65 def check_tabbing_and_spacing(filename, file_lines):
67 Calls other functions that check general issues about tabbing and spacing
70 check_tabs_no_spaces(filename, file_lines)
71 check_correct_space_count(filename, file_lines)
72 check_no_spacing_line_end(filename, file_lines)
75 if __name__ == "__main__":
76 targets = sys.argv[1:]
77 targets = get_all_files(targets)
81 \t**** No input provided ****
82 \tTakes a list of files/directories as input and performs specific style checking on all files/directories.
84 \tGives warnings if a line contains unneeded spaces at end of line, contains tab characters (\\t)
85 \tor if spaces at line beginning are not divisible by 4.
90 if t[-2:] == ".h" or t[-4:] == ".cpp" or t[-2] == ".c":
91 _, _, file_lines, _ = read_file(t)
92 check_tabbing_and_spacing(t, file_lines)