[python] restore project_root option and use it to find source files

Summary:
This makes our python code work (instead of crashing) when the source file
should be found not from the current directory (or absolute path), eg with
`infer --project-root ..  -- clang -c hello.c`.

Reviewed By: jeremydubreil

Differential Revision: D4130802

fbshipit-source-id: 001f72d
master
Jules Villard 8 years ago committed by Facebook Github Bot
parent 2f5d132734
commit 2cd0151e30

@ -13,7 +13,7 @@ ifeq ($(IS_FACEBOOK_TREE),yes)
include $(ROOT_DIR)/facebook//Makefile.env include $(ROOT_DIR)/facebook//Makefile.env
endif endif
BUILD_SYSTEMS_TESTS = ant BUILD_SYSTEMS_TESTS = ant project_root_rel
DIRECT_TESTS= DIRECT_TESTS=
ifeq ($(BUILD_C_ANALYZERS),yes) ifeq ($(BUILD_C_ANALYZERS),yes)

@ -93,6 +93,10 @@ base_group.add_argument('--pmd-xml',
infer_parser = argparse.ArgumentParser(parents=[base_parser]) infer_parser = argparse.ArgumentParser(parents=[base_parser])
infer_group = infer_parser.add_argument_group('backend arguments') infer_group = infer_parser.add_argument_group('backend arguments')
infer_group.add_argument('-pr', '--project-root',
dest='project_root',
help='Location of the project root '
'(default is current directory)')
infer_group.add_argument('-j', '--multicore', metavar='n', type=int, infer_group.add_argument('-j', '--multicore', metavar='n', type=int,
default=multiprocessing.cpu_count(), default=multiprocessing.cpu_count(),
dest='multicore', help='Set the number of cores to ' dest='multicore', help='Set the number of cores to '
@ -352,7 +356,7 @@ class AnalyzerWrapper(object):
if self.args.pmd_xml: if self.args.pmd_xml:
xml_out = os.path.join(self.args.infer_out, xml_out = os.path.join(self.args.infer_out,
config.PMD_XML_FILENAME) config.PMD_XML_FILENAME)
issues.print_and_save_errors(json_report, issues.print_and_save_errors(self.args.project_root, json_report,
bugs_out, xml_out) bugs_out, xml_out)
def analyze_and_report(self): def analyze_and_report(self):

@ -391,7 +391,8 @@ def collect_results(args, start_time, targets):
if args.pmd_xml: if args.pmd_xml:
xml_out = os.path.join(args.infer_out, xml_out = os.path.join(args.infer_out,
config.PMD_XML_FILENAME) config.PMD_XML_FILENAME)
issues.print_and_save_errors(json_report, bugs_out, xml_out) issues.print_and_save_errors(args.project_root, json_report,
bugs_out, xml_out)
stats['int']['total_time'] = int(round(utils.elapsed_time(start_time))) stats['int']['total_time'] = int(round(utils.elapsed_time(start_time)))

@ -207,7 +207,8 @@ class BuckAnalyzer:
if self.args.pmd_xml: if self.args.pmd_xml:
xml_out = os.path.join( xml_out = os.path.join(
self.args.infer_out, config.PMD_XML_FILENAME) self.args.infer_out, config.PMD_XML_FILENAME)
issues.print_and_save_errors(merged_reports_path, bugs_out, xml_out) issues.print_and_save_errors(self.args.project_root,
merged_reports_path, bugs_out, xml_out)
return os.EX_OK return os.EX_OK
def capture_with_compilation_database(self): def capture_with_compilation_database(self):

@ -93,7 +93,8 @@ def text_of_report(report):
) )
def _text_of_report_list(reports, formatter=colorize.TERMINAL_FORMATTER): def _text_of_report_list(project_root, reports,
formatter=colorize.TERMINAL_FORMATTER):
text_errors_list = [] text_errors_list = []
error_types_count = {} error_types_count = {}
for report in reports: for report in reports:
@ -103,7 +104,7 @@ def _text_of_report_list(reports, formatter=colorize.TERMINAL_FORMATTER):
source_context = '' source_context = ''
if formatter == colorize.TERMINAL_FORMATTER: if formatter == colorize.TERMINAL_FORMATTER:
source_context = source.build_source_context( source_context = source.build_source_context(
filename, os.path.join(project_root, filename),
formatter, formatter,
line, line,
) )
@ -166,18 +167,18 @@ def _text_of_report_list(reports, formatter=colorize.TERMINAL_FORMATTER):
return msg return msg
def _is_user_visible(report): def _is_user_visible(project_root, report):
filename = report[JSON_INDEX_FILENAME] filename = report[JSON_INDEX_FILENAME]
kind = report[JSON_INDEX_KIND] kind = report[JSON_INDEX_KIND]
return (os.path.isfile(filename) and return (os.path.isfile(os.path.join(project_root, filename)) and
kind in [ISSUE_KIND_ERROR, ISSUE_KIND_WARNING, ISSUE_KIND_ADVICE]) kind in [ISSUE_KIND_ERROR, ISSUE_KIND_WARNING, ISSUE_KIND_ADVICE])
def print_and_save_errors(json_report, bugs_out, xml_out): def print_and_save_errors(project_root, json_report, bugs_out, xml_out):
errors = utils.load_json_from_path(json_report) errors = utils.load_json_from_path(json_report)
errors = filter(_is_user_visible, errors) errors = [e for e in errors if _is_user_visible(project_root, e)]
utils.stdout('\n' + _text_of_report_list(errors)) utils.stdout('\n' + _text_of_report_list(project_root, errors))
plain_out = _text_of_report_list(errors, plain_out = _text_of_report_list(project_root, errors,
formatter=colorize.PLAIN_FORMATTER) formatter=colorize.PLAIN_FORMATTER)
with codecs.open(bugs_out, 'w', with codecs.open(bugs_out, 'w',
encoding=config.CODESET, errors='replace') as file_out: encoding=config.CODESET, errors='replace') as file_out:

@ -129,6 +129,7 @@ let capture build_cmd = function
"-l" :: (string_of_float Config.load_average) :: "-l" :: (string_of_float Config.load_average) ::
(if not Config.pmd_xml then [] else (if not Config.pmd_xml then [] else
["--pmd-xml"]) @ ["--pmd-xml"]) @
["--project-root"; Config.project_root] @
(if not Config.reactive_mode then [] else (if not Config.reactive_mode then [] else
["--reactive"]) @ ["--reactive"]) @
"--out" :: Config.results_dir :: "--out" :: Config.results_dir ::

@ -0,0 +1,16 @@
# Copyright (c) 2016 - present Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
TESTS_DIR = ../..
include $(TESTS_DIR)/clang.make
ANALYZER = infer
CLANG_OPTIONS = -c
INFER_OPTIONS = --report-custom-error --developer-mode --project-root ../../../tests
SOURCES = \
../codetoanalyze/hello.c \

@ -0,0 +1 @@
build_systems/codetoanalyze/hello.c, test, 2, NULL_DEREFERENCE
Loading…
Cancel
Save