# 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 concurrent.futures import functools import glob import itertools import json import logging import os import re import shutil import subprocess import sys import tempfile from types import SimpleNamespace
import mozpack.path as mozpath from mach.decorators import Command, CommandArgument, SubCommand from mach.main import Mach from mozversioncontrol import get_repository_object
from mozbuild import build_commands from mozbuild.controller.clobber import Clobberer from mozbuild.util import cpu_count
# FIXME: use itertools.batched when moving to python 3.12 def batched(iterable, n): for ndx in range(0, len(iterable), n): yield iterable[ndx : ndx + n]
def build_repo_relative_path(abs_path, repo_path): """Build path relative to repository root"""
if os.path.islink(abs_path):
abs_path = mozpath.realpath(abs_path)
return mozpath.relpath(abs_path, repo_path)
def prompt_bool(prompt, limit=5): """Prompts the user with prompt and requires a boolean value.""" from mach.util import strtobool
for _ in range(limit): try: return strtobool(input(prompt + "[Y/N]\n")) except ValueError:
print( "ERROR! Please enter a valid option! Please use any of the following:" " Y, N, True, False, 1, 0"
) returnFalse
class StaticAnalysisSubCommand(SubCommand): def __call__(self, func):
after = SubCommand.__call__(self, func)
args = [
CommandArgument( "--verbose", "-v", action="store_true", help="Print verbose output."
)
] for arg in args:
after = arg(after) return after
class StaticAnalysisMonitor: def __init__(self, srcdir, objdir, checks, total):
self._total = total
self._processed = 0
self._current = None
self._srcdir = srcdir
import copy
self._checks = copy.deepcopy(checks)
# Transform the configuration to support Regex for item in self._checks: if item["name"] == "-*": continue
item["name"] = item["name"].replace("*", ".*")
from mozbuild.compilation.warnings import WarningsCollector, WarningsDatabase
self._warnings_database = WarningsDatabase()
def on_warning(warning): # Output paths relative to repository root if the paths are under repo tree
warning["filename"] = build_repo_relative_path(
warning["filename"], self._srcdir
)
if line.find("clang-tidy") != -1:
filename = line.split(" ")[-1] if os.path.isfile(filename):
self._current = build_repo_relative_path(filename, self._srcdir)
self._processed = self._processed + 1 else:
self._current = None return (warning, False) if warning isnotNone:
def get_check_config(checker_name): # get the matcher from self._checks that is the 'name' field for item in self._checks: if item["name"] == checker_name: return item
# We are using a regex in order to also match 'mozilla-.* like checkers'
matcher = re.match(item["name"], checker_name) if matcher isnotNoneand matcher.group(0) == checker_name: return item
check_config = get_check_config(
warning["flag"].removesuffix(",-warnings-as-errors")
) if check_config isnotNone:
warning["reliability"] = check_config.get("reliability", "low")
warning["reason"] = check_config.get("reason")
warning["publish"] = check_config.get("publish", True) elif warning["flag"] == "clang-diagnostic-error": # For a "warning" that is flagged as "clang-diagnostic-error" # set it as "publish"
warning["publish"] = True return (warning, True)
# Utilities for running C++ static analysis checks and format.
# List of file extension to consider (should start with dot)
_format_include_extensions = (".cpp", ".c", ".cc", ".h", ".m", ".mm") # File contaning all paths to exclude from formatting
_format_ignore_file = ".clang-format-ignore"
@Command( "clang-tidy",
category="devenv",
description="Convenience alias for the static-analysis command",
) def clang_tidy(command_context): # If no arguments are provided, just print a help message. """Detailed documentation: https://firefox-source-docs.mozilla.org/code-quality/static-analysis/index.html """
mach = Mach(os.getcwd())
def populate_context(key=None): if key == "topdir": return command_context.topsrcdir
@Command( "static-analysis",
category="devenv",
description="Run C++ static analysis checks using clang-tidy",
) def static_analysis(command_context): # If no arguments are provided, just print a help message. """Detailed documentation: https://firefox-source-docs.mozilla.org/code-quality/static-analysis/index.html """
mach = Mach(os.getcwd())
def populate_context(key=None): if key == "topdir": return command_context.topsrcdir
@StaticAnalysisSubCommand( "static-analysis", "check", "Run the checks using the helper tool"
)
@CommandArgument( "patterns",
nargs="*",
default=["**"],
help="Source files to be analyzed (glob on path). " "Can be omitted, in which case the entire code base " "is analyzed. The source argument is ignored if " "there is anything fed through stdin, in which case " "the analysis is only performed on the files changed " "in the patch streamed through stdin. This is called " "the diff mode.",
)
@CommandArgument( "--checks", "-c",
default="-*",
metavar="checks",
help="Static analysis checks to enable. By default, this enables only " "checks that are published here: https://mzl.la/2DRHeTh, but can be any " "clang-tidy checks syntax.",
)
@CommandArgument( "--jobs", "-j",
default="0",
metavar="jobs",
type=int,
help="Number of concurrent jobs to run. Default is the number of CPUs.",
)
@CommandArgument( "--strip", "-p",
default="1",
metavar="NUM",
help="Strip NUM leading components from file names in diff mode.",
)
@CommandArgument( "--fix", "-f",
default=False,
action="store_true",
help="Try to autofix errors detected by clang-tidy checkers.",
)
@CommandArgument( "--header-filter", "-h-f",
default="",
metavar="header_filter",
help="Regular expression matching the names of the headers to " "output diagnostics from. Diagnostics from the main file " "of each translation unit are always displayed",
)
@CommandArgument( "--output", "-o", default=None, help="Write clang-tidy output in a file"
)
@CommandArgument( "--format",
default="text",
choices=("text", "json"),
help="Output format to write in a file",
)
@CommandArgument( "--outgoing",
default=False,
action="store_true",
help="Run static analysis checks on outgoing files from mercurial repository",
) def check(
command_context,
patterns,
jobs,
strip,
verbose,
checks,
fix,
header_filter,
output,
format,
outgoing,
): from mozbuild.controller.building import (
StaticAnalysisFooter,
StaticAnalysisOutputManager,
)
def to_source(filename):
basename, ext = os.path.splitext(filename) if ext != ".h": return filename for src_ext in [".cpp", ".c", ".mm", ".cc", ".cxx"]: if os.path.exists(basename + src_ext): return basename + src_ext return filename
sources = {}
header_sources = {}
compile_db_files = {f["file"] for f in compile_db} for candidate in abs_files:
associated_entry = to_source(candidate) if associated_entry in compile_db_files: if candidate != associated_entry:
header_sources[associated_entry] = candidate # Check we're not erasing an existing entry elif associated_entry notin sources:
sources[associated_entry] = None
with StaticAnalysisOutputManager(
command_context.log_manager, monitor, footer
) as output_manager:
rc = 0
arg_max = 512# The actual shell limit is way above for batch in batched(list(sources.items()), arg_max):
args = _get_clang_tidy_command(
command_context,
clang_paths,
compilation_commands_path,
checks=checks,
header_filter=header_filter,
sources=dict(batch),
jobs=jobs,
fix=fix,
warnings_as_errors=True,
verbose=verbose,
)
rc |= command_context.run_process(
args=args,
ensure_exit_code=False,
line_handler=output_manager.on_line,
cwd=cwd,
)
with concurrent.futures.ThreadPoolExecutor(
max_workers=jobs or cpu_count()
) as executor:
futures = [] # process header independently, because we need per-instance # header-filter for it to work for header_src, header_hdr in header_sources.items():
args = _get_clang_tidy_command(
command_context,
clang_paths,
compilation_commands_path,
checks=checks,
header_filter=header_filter,
sources={header_src: header_hdr},
jobs=1,
fix=fix,
warnings_as_errors=True,
verbose=verbose,
)
futures.append(
executor.submit(
command_context.run_process,
args=args,
ensure_exit_code=False,
line_handler=output_manager.on_line,
cwd=cwd,
)
) for future in concurrent.futures.as_completed(futures): # Wait for every task to finish
rc |= future.result()
# Write output file if output isnotNone:
output_manager.write(output, format)
ifnot sources andnot header_sources:
command_context.log(
logging.WARNING, "static-analysis",
{}, "There are no files eligible for analysis. Please note that 'header' files " "cannot be used for analysis since they do not consist compilation units.",
) return0
return rc
def get_abspath_files(command_context, files): return [mozpath.join(command_context.topsrcdir, f) for f in files]
def get_files_with_commands(command_context, compile_db, sources): """
Returns an array of dictionaries having file_path with build command """
compile_db = json.load(open(compile_db))
commands_list = []
for src in sources: # It must be a C/C++ file
_, ext = os.path.splitext(src)
if ext.lower() notin _format_include_extensions:
command_context.log(logging.INFO, "static-analysis", {}, f"Skipping {src}") continue
file_with_abspath = os.path.join(command_context.topsrcdir, src) for f in compile_db: # Found for a file that we are looking if file_with_abspath == f["file"]:
commands_list.append(f)
return commands_list
@functools.cache def get_clang_tidy_config(command_context): from mozbuild.code_analysis.utils import ClangTidyConfig
return ClangTidyConfig(command_context.topsrcdir)
def _get_required_version(command_context):
version = get_clang_tidy_config(command_context).version if version isNone:
command_context.log(
logging.ERROR, "static-analysis",
{}, "ERROR: Unable to find 'package_version' in config.yml",
) return version
if"MOZ_AUTOMATION"in os.environ: # Only show it in the CI
command_context.log(
logging.INFO, "static-analysis",
{},
f"{clang_paths._clang_tidy_path} Version = {version_info} ",
)
except subprocess.CalledProcessError as e:
command_context.log(
logging.ERROR, "static-analysis",
{}, "Error determining the version clang-tidy binary, please see the "
f"attached exception: \n{e.output}",
) return version_info
def _is_version_eligible(command_context, clang_paths, log_error=True):
version = _get_required_version(command_context) if version isNone: returnFalse
current_version = _get_current_version(command_context, clang_paths) if current_version isNone: returnFalse
version = "version " + version if version in current_version: returnTrue
if log_error:
command_context.log(
logging.ERROR, "static-analysis",
{},
f"ERROR: You're using an old or incorrect version ({_get_current_version(command_context, clang_paths)}) of clang-tidy binary. "
f"Please update to a more recent one (at least > {_get_required_version(command_context)}) " "by running: './mach bootstrap' ",
)
# Flag header-filter is passed in order to limit the diagnostic messages only # to the specified header files. When no value is specified the default value # is considered to be the source in order to limit the diagnostic message to # the source files or folders.
common_args += [ "-header-filter=%s"
% (
header_filter if header_filter else"|".join(v or k for k, v in sources.items())
)
]
# headers we do not want to scan if header_skiplist := get_clang_tidy_config(command_context).header_skiplist:
common_args += ["-exclude-header-filter={}".format("|".join(header_skiplist))]
# From our configuration file, config.yaml, we build the configuration list, for # the checkers that are used. These configuration options are used to better fit # the checkers to our code.
cfg = get_clang_tidy_config(command_context).checks_config if cfg:
common_args += [f"-config={json.dumps(cfg)}"]
if fix:
common_args += ["-fix"]
ifnot verbose:
common_args += ["-quiet"]
return (
[
command_context.virtualenv_manager.python_path,
clang_paths._run_clang_tidy_path, "-j",
str(jobs), "-p",
compilation_commands_path,
]
+ common_args # run-clang-tidy expects regexps, not paths, so we need to escape # backslashes.
+ [re.escape(os.path.normpath(s)) for s in sources]
)
@StaticAnalysisSubCommand( "static-analysis", "autotest", "Run the auto-test suite in order to determine that the analysis did not regress.",
)
@CommandArgument( "--dump-results", "-d",
default=False,
action="store_true",
help="Generate the baseline for the regression test. Based on" " this baseline we will test future results.",
)
@CommandArgument( "--intree-tool", "-i",
default=False,
action="store_true",
help="Use a pre-aquired in-tree clang-tidy package from the automation env." " This option is only valid on automation environments.",
)
@CommandArgument( "checker_names",
nargs="*",
default=[],
help="Checkers that are going to be auto-tested.",
) def autotest(
command_context,
verbose=False,
dump_results=False,
intree_tool=False,
checker_names=[],
): # If 'dump_results' is True than we just want to generate the issues files for each # checker in particulat and thus 'force_download' becomes 'False' since we want to # do this on a local trusted clang-tidy package.
command_context._set_log_level(verbose)
command_context.activate_virtualenv()
force_download = not dump_results
# Configure the tree or download clang-tidy package, depending on the option that we choose if intree_tool:
clang_paths = SimpleNamespace() if"MOZ_AUTOMATION"notin os.environ:
command_context.log(
logging.INFO, "static-analysis",
{}, "The `autotest` with `--intree-tool` can only be ran in automation.",
) return1 if"MOZ_FETCHES_DIR"notin os.environ:
command_context.log(
logging.INFO, "static-analysis",
{}, "`MOZ_FETCHES_DIR` is missing from the environment variables.",
) return1
# For each checker run it
platform, _ = command_context.platform
if platform notin get_clang_tidy_config(command_context).platforms:
command_context.log(
logging.ERROR, "static-analysis",
{},
f"ERROR: RUNNING: clang-tidy autotest for platform {platform} not supported.",
) return TOOLS_UNSUPORTED_PLATFORM
max_workers = cpu_count()
command_context.log(
logging.INFO, "static-analysis",
{},
f"RUNNING: clang-tidy autotest for platform {platform} with {max_workers} workers.",
)
# List all available checkers
cmd = [clang_paths._clang_tidy_path, "-list-checks", "-checks=*"]
clang_output = subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode( "utf-8"
)
available_checks = clang_output.split("\n")[1:]
clang_tidy_checks = [c.strip() for c in available_checks if c]
# Build the dummy compile_commands.json
compilation_commands_path = _create_temp_compilation_db(command_context)
checkers_test_batch = []
checkers_results = [] with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = [] for item in get_clang_tidy_config(command_context).checks_with_data: # Skip if any of the following statements is true: # 1. Checker attribute 'publish' is False.
not_published = not bool(item.get("publish", True)) # 2. Checker has restricted-platforms and current platform is not of them.
ignored_platform = ( "restricted-platforms"in item and platform notin item["restricted-platforms"]
) # 3. Checker name is mozilla-* or -*.
ignored_checker = item["name"] in ["mozilla-*", "-*"] # 4. List checker_names is passed and the current checker is not part of the # list or 'publish' is False
checker_not_in_list = checker_names and (
item["name"] notin checker_names or not_published
) if (
not_published or ignored_platform or ignored_checker or checker_not_in_list
): continue
checkers_test_batch.append(item["name"])
futures.append(
executor.submit(
_verify_checker,
command_context,
clang_paths,
compilation_commands_path,
dump_results,
clang_tidy_checks,
item,
checkers_results,
)
)
error_code = TOOLS_SUCCESS for future in concurrent.futures.as_completed(futures): # Wait for every task to finish
ret_val = future.result() if ret_val != TOOLS_SUCCESS: # We are interested only in one error and we don't break # the execution of for loop since we want to make sure that all # tasks finished.
error_code = ret_val
if error_code != TOOLS_SUCCESS:
command_context.log(
logging.INFO, "static-analysis",
{}, "FAIL: the following clang-tidy check(s) failed:",
) for failure in checkers_results:
checker_error = failure["checker-error"]
checker_name = failure["checker-name"]
info1 = failure["info1"]
info2 = failure["info2"]
info3 = failure["info3"]
message_to_log = "" if checker_error == TOOLS_CHECKER_NOT_FOUND:
message_to_log = ( "\tChecker "
f"{checker_name} not present in this clang-tidy version."
) elif checker_error == TOOLS_CHECKER_NO_TEST_FILE:
message_to_log = ( "\tChecker "
f"{checker_name} does not have a test file - {checker_name}.cpp"
) elif checker_error == TOOLS_CHECKER_RETURNED_NO_ISSUES:
message_to_log = (
f"\tChecker {checker_name} did not find any issues in its test file, "
f"clang-tidy output for the run is:\n{info1}"
) elif checker_error == TOOLS_CHECKER_RESULT_FILE_NOT_FOUND:
message_to_log = f"\tChecker {checker_name} does not have a result file - {checker_name}.json" elif checker_error == TOOLS_CHECKER_DIFF_FAILED:
message_to_log = (
f"\tChecker {checker_name}\nExpected: {info1}\n"
f"Got: {info2}\n" "clang-tidy output for the run is:\n"
f"{info3}"
)
print("\n" + message_to_log)
# Also delete the tmp folder
shutil.rmtree(compilation_commands_path) return error_code
# Run the analysis on all checkers at the same time only if we don't dump results. ifnot dump_results:
ret_val = _run_analysis_batch(
command_context,
clang_paths,
compilation_commands_path,
checkers_test_batch,
) if ret_val != TOOLS_SUCCESS:
shutil.rmtree(compilation_commands_path) return ret_val
command_context.log(
logging.INFO, "static-analysis", {}, "SUCCESS: clang-tidy all tests passed."
) # Also delete the tmp folder
shutil.rmtree(compilation_commands_path)
if issues isNone: return TOOLS_CHECKER_FAILED_FILE
failed_checks = []
failed_checks_baseline = [] for checker in items:
test_file_path_json = (
mozpath.join(clang_paths._clang_tidy_base_path, "test", checker) + ".json"
) # Read the pre-determined issues
baseline_issues = _get_autotest_stored_issues(test_file_path_json)
# We also stored the 'reliability' index so strip that from the baseline_issues
baseline_issues[:] = [
item for item in baseline_issues if"reliability"notin item
]
found = all([element_base in issues for element_base in baseline_issues])
if len(failed_checks) > 0:
command_context.log(
logging.ERROR, "static-analysis",
{}, "ERROR: The following check(s) failed for bulk analysis: "
+ " ".join(failed_checks),
)
for failed_check, baseline_issue in zip(failed_checks, failed_checks_baseline):
print(
f"\tChecker {failed_check} expect following results: \n\t\t{baseline_issue}"
)
print(
f"This is the output generated by clang-tidy for the bulk build:\n{clang_output}"
) return TOOLS_CHECKER_DIFF_FAILED
return TOOLS_SUCCESS
def _create_temp_compilation_db(command_context):
directory = tempfile.mkdtemp(prefix="cc") with open(mozpath.join(directory, "compile_commands.json"), "w") as file_handler:
compile_commands = []
director = mozpath.join(
command_context.topsrcdir, "tools", "clang-tidy", "test"
) for item in get_clang_tidy_config(command_context).checks: if item in ["-*", "mozilla-*"]: continue
file = item + ".cpp"
element = {}
element["directory"] = director
element["command"] = "cpp -std=c++17 " + file
element["file"] = mozpath.join(director, file)
compile_commands.append(element)
@StaticAnalysisSubCommand( "static-analysis", "install", "Install the static analysis helper tool"
)
@CommandArgument( "source",
nargs="?",
type=str,
help="Where to fetch a local archive containing the static-analysis and " "format helper tool." "It will be installed in ~/.mozbuild/clang-tools." "Can be omitted, in which case the latest clang-tools " "helper for the platform would be automatically detected and installed.",
)
@CommandArgument( "--skip-cache",
action="store_true",
help="Skip all local caches to force re-fetching the helper tool.",
default=False,
)
@CommandArgument( "--force",
action="store_true",
help="Force re-install even though the tool exists in mozbuild.",
default=False,
) def install(
command_context,
source=None,
skip_cache=False,
force=False,
verbose=False,
):
command_context._set_log_level(verbose)
rc, _ = get_clang_tools(
command_context,
force=force,
skip_cache=skip_cache,
source=source,
verbose=verbose,
) return rc
from mozbuild.artifact_commands import artifact_clear_cache
return artifact_clear_cache(command_context)
@StaticAnalysisSubCommand( "static-analysis", "print-checks", "Print a list of the static analysis checks performed by default",
) def print_checks(command_context, verbose=False):
command_context._set_log_level(verbose)
rc, clang_paths = get_clang_tools(command_context, verbose=verbose)
# Structured information in case a checker fails
checker_error = { "checker-name": check, "checker-error": "", "info1": "", "info2": "", "info3": "",
}
# Verify if this checker actually exists if check notin clang_tidy_checks:
checker_error["checker-error"] = TOOLS_CHECKER_NOT_FOUND
checkers_results.append(checker_error) return TOOLS_CHECKER_NOT_FOUND
# Verify if the test file exists for this checker ifnot os.path.exists(test_file_path_cpp):
checker_error["checker-error"] = TOOLS_CHECKER_NO_TEST_FILE
checkers_results.append(checker_error) return TOOLS_CHECKER_NO_TEST_FILE
# Verify to see if we got any issues, if not raise exception ifnot issues:
checker_error["checker-error"] = TOOLS_CHECKER_RETURNED_NO_ISSUES
checker_error["info1"] = clang_output
checkers_results.append(checker_error) return TOOLS_CHECKER_RETURNED_NO_ISSUES
# Also store the 'reliability' index for this checker
issues.append({"reliability": item["reliability"]})
if dump_results:
_build_autotest_result(test_file_path_json, json.dumps(issues)) else: ifnot os.path.exists(test_file_path_json): # Result file for test not found maybe regenerate it?
checker_error["checker-error"] = TOOLS_CHECKER_RESULT_FILE_NOT_FOUND
checkers_results.append(checker_error) return TOOLS_CHECKER_RESULT_FILE_NOT_FOUND
# Read the pre-determined issues
baseline_issues = _get_autotest_stored_issues(test_file_path_json)
# Compare the two lists if issues != baseline_issues:
checker_error["checker-error"] = TOOLS_CHECKER_DIFF_FAILED
checker_error["info1"] = baseline_issues
checker_error["info2"] = issues
checker_error["info3"] = clang_output
checkers_results.append(checker_error) return TOOLS_CHECKER_DIFF_FAILED
return TOOLS_SUCCESS
def _build_autotest_result(file, issues): with open(file, "w") as f:
f.write(issues)
def _get_autotest_stored_issues(file): with open(file) as f: return json.load(f)
# Limit clang output parsing to 'Enabled checks:'
end = re.search(r"^Enabled checks:\n", clang_output, re.MULTILINE) if end isnotNone:
clang_output = clang_output[: end.start() - 1]
platform, _ = command_context.platform
re_strip_colors = re.compile(r"\x1b\[[\d;]+m", re.MULTILINE)
filtered = re_strip_colors.sub("", clang_output) # Starting with clang 8, for the diagnostic messages we have multiple `LF CR` # in order to be compatible with msvc compiler format, and for this # we are not interested to match the end of line.
regex_string = r"(.+):(\d+):(\d+): (warning|error): ([^\[\]\n]+)(?: \[([\.\w-]+)\])"
# For non 'win' based platforms we also need the 'end of the line' regex if platform notin ("win64", "win32"):
regex_string += "?$"
try:
config = command_context.config_environment except Exception:
command_context.log(
logging.WARNING, "static-analysis",
{}, "Looks like configure has not run yet, running it now...",
)
if clobber.clobber_needed():
choice = prompt_bool( "Configuration has changed and Clobber is needed. " "Do you want to proceed?"
) ifnot choice:
command_context.log(
logging.ERROR, "static-analysis",
{}, "ERROR: Without Clobber we cannot continue execution!",
) return (1, None, None)
os.environ["AUTOCLOBBER"] = "1"
if ran_configure: # Configure may have created the compilation database if the # mozconfig enables building the CompileDB backend by default, # So we recurse to see if the file exists once again. return _build_compile_db(command_context, verbose=verbose)
if config:
print( "Looks like a clang compilation database has not been " "created yet, creating it now..."
)
rc = build_commands.build_backend(
command_context, ["StaticAnalysis"], verbose=verbose
) if rc != 0: return rc, compile_db, compilation_commands_path assert os.path.exists(compile_db) return0, compile_db, compilation_commands_path
# First install what we can through install manifests. # Then build the rest of the build dependencies by running the full # export target, because we can't do anything better. for target in ("pre-export", "export", "pre-compile"):
rc = command_context._run_make(
directory=command_context.topobjdir,
target=target,
line_handler=None,
print_directory=verbose,
log=verbose,
silent=not verbose,
num_jobs=jobs,
) if rc != 0: return rc return0
def _do_clang_tools_exist(clang_paths): return (
os.path.exists(clang_paths._clang_tidy_path) and os.path.exists(clang_paths._clang_apply_replacements) and os.path.exists(clang_paths._run_clang_tidy_path)
)
if (
_do_clang_tools_exist(clang_paths) and _is_version_eligible(command_context, clang_paths, log_error=False) andnot force
): return0, clang_paths
if os.path.isdir(clang_paths._clang_tools_path) and download_if_needed: # The directory exists, perhaps it's corrupted? Delete it # and start from scratch.
shutil.rmtree(clang_paths._clang_tools_path) return get_clang_tools(
command_context,
force=force,
skip_cache=skip_cache,
source=source,
verbose=verbose,
download_if_needed=download_if_needed,
)
# Create base directory where we store clang binary
os.mkdir(clang_paths._clang_tools_path)
if source: return _get_clang_tools_from_source(command_context, clang_paths, source)
ifnot download_if_needed: return0, clang_paths
from mozbuild.bootstrap import bootstrap_toolchain
clang_tidy = bootstrap_toolchain("clang-tools/clang-tidy") ifnot clang_tidy: raise Exception("clang-tidy not found")
def _is_ignored_path(command_context, ignored_dir_re, f): # path needs to be relative to the src root
root_dir = command_context.topsrcdir + os.sep if f.startswith(root_dir):
f = f[len(root_dir) :] # the ignored_dir_re regex uses / on all platforms return re.match(ignored_dir_re, f.replace(os.sep, "/"))
def _generate_path_list(command_context, paths, verbose=True):
path_to_third_party = os.path.join(command_context.topsrcdir, _format_ignore_file)
ignored_dir = [] with open(path_to_third_party) as fh: for line in fh: # Remove comments and empty lines if line.startswith("#") or len(line.strip()) == 0: continue # The regexp is to make sure we are managing relative paths
ignored_dir.append(r"^[\./]*" + line.rstrip())
# Generates the list of regexp
ignored_dir_re = "(%s)" % "|".join(ignored_dir)
extensions = _format_include_extensions
path_entries = {} for f, h in paths.items(): if _is_ignored_path(command_context, ignored_dir_re, f): # Early exit if we have provided an ignored directory if verbose:
print(f"static-analysis: Ignored third party code '{f}'") continue # Make sure that the file exists and it has a supported extension elif os.path.isfile(f) and f.endswith(extensions):
path_entries[f] = h
try: # Check when everything is fine
result = subprocess.run(
[
sys.executable, "mach", "static-analysis", "check", "js/src/builtin/RegExp.cpp",
],
check=False,
env=env,
cwd=command_context.topsrcdir,
) assert result.returncode == 0, "in-tree files should pass the linter"
# And when errors are emitted, both for headers and sources
faulty_srcs = ( "js/src/builtin/TestingFunctions.cpp", "js/src/builtin/TestingFunctions.h",
) for src in faulty_srcs: with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as fd: try:
fd.close()
# modernize-use-auto is an unsupported check, and it generates # plenty of warnings on js/src/builtin/TestingFunctions.cpp
failing_flag = "modernize-use-auto"
result = subprocess.run(
[
sys.executable, "mach", "static-analysis", "check",
f"--checks=-*,{failing_flag}",
f"--output={fd.name}", "--format=json", "js/src/builtin/TestingFunctions.cpp",
],
check=False,
env=env,
cwd=command_context.topsrcdir,
) with open(fd.name) as json_fd:
errors = json.load(json_fd) finally: # FIXME: use delete_on_close=False once we move to 3.12
os.remove(fd.name)
assert result.returncode != 0, f"{failing_flag} check should find warnings" assert len(errors["files"]) > 0, ( "warnings should be present in the log file"
)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.