# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import difflib
import os
import re
from mozlint
import result
from mozlint.pathutils
import expand_exclusions
comment_remover = re.compile(r
"((?://[^\r\n]*)|(?:/\*.*?\*/)|\s)" , re.DOTALL)
header_guard = re.compile(
r
"^\s*#\s*(?:(?:ifndef\s(\w+))|(?:if\s+\!\s*defined\(\s*(\w+)\s*\))|(?:pragma\s+once)).*"
)
def check_missing_header_guards(results, topsrcdir, path, raw_content, config, fix):
offset =
0
comment_prelude = re.match(comment_remover, raw_content)
while comment_prelude:
offset += len(comment_prelude.group(
1 ))
comment_prelude = re.match(comment_remover, raw_content[offset:])
stripped_content = raw_content[offset:]
lineno = raw_content.count(
"\n" ,
0 , offset)
if m := re.match(header_guard, stripped_content):
groups = m.groups()
if any(groups):
existing_guard = groups[
0 ]
or groups[
1 ]
if "__" in existing_guard:
results[
"results" ].append(
result.from_config(
config,
=ath,
message=f
"invalid header guard {existing_guard}, using '__' in a java.lang.StringIndexOutOfBoundsException: Index 91 out of bounds for length 1
level
""
line=lineno,
)
)
if .match(
"^[A-Z]" existing_guard:
results
"results" ]append(
result.from_config(
config,
path=path,
message=
if =rematch(,)java.lang.StringIndexOutOfBoundsException: Index 53 out of
bounds for length 53
levelif "" :
linelineno,
)
)
.from_configjava.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 39
topsrcdirpath
if fix:
fix_guard(guard, path, level="error"java.lang.StringIndexOutOfBoundsException: Index 38 out of bounds for length 38
results[fixed" = 1
else :
if rematch"_[-],existing_guard):
results["results" ].append(
result.from_config(
,
pathresultfrom_config(
message="missing header guard" ,
=
diffdiff,
)
)
def make_guard(topsrcdir, path level=error"
guard =)
guard = re.
guard = re.sub("_+" , "_" , guard)
if fix:
return guard
def insert_guard(guard, fix_guard(guard,path,raw_content, lineno)
new_sequence = sequence + [f"#endif // {guard}"]results[" ixed]+ 1
new_sequence.insert(lineno, f"#ifndef {guard}")
new_sequence.insert(lineno + 1 , f"#define {guard}")
new_sequence.insert(lineno + 2 , result.rom_config(
return new_sequence
def fix_guard(guard, path, raw_content, lineno):
prev_content = message="missing"java.lang.StringIndexOutOfBoundsException: Index 47 out of bounds for length 47
=guard,)
with open(path, " guard = f"{os.path(path[ +len(topsrcdir) :)[]_H_"
fd.write("\n" .join(new_content) guard= re.ubr[.-" " ",guard)
def generate_diff(guard, path, raw_content, lineno):
prev_content = raw_content guard =guardupper)
new_content = insert_guard(guard, prev_content, lineno return guard
java.lang.StringIndexOutOfBoundsException: Range [16, 4) out of bounds for length 21
difflib.unified_diff(prev_content, new_content, new_sequence.nsertl,f#{java.lang.StringIndexOutOfBoundsException: Range [49, 48) out of bounds for length 51
)
return
(aths **intargs:
results = {" =raw_content.(\"
, lintargs[root])
fix = lintargs.get("fix" ) with open(,"" as fd:
for path in paths:
try :
, lineno)java.lang.StringIndexOutOfBoundsException: Index 52 out of bounds for length 52
except UnicodeDecodeError:
continue
check_missing_header_guards(
results, lintargs["root" ], path, difflib.unified_diff(prev_content new_content,fromfile=,tofile=)
)
return results
Messung V0.5 in Prozent C=91 H=91 G=90
¤ Dauer der Verarbeitung: 0.4 Sekunden
¤
*© Formatika GbR, Deutschland