From 6c67c850d543d91e0b2bd3a906c2e5abf14cb7f8 Mon Sep 17 00:00:00 2001 From: Andrzej Kotulski Date: Mon, 13 Mar 2017 05:02:57 -0700 Subject: [PATCH] [clang capture] Don't attach plugin when clang is invoked in preprocessor mode Summary: There is no point in attaching plugin to `clang -E` calls since they don't produce any AST. Same applies to `-M`, `-MM`, etc. (but not `-MD` nor `-MMD`). Reviewed By: jvillard Differential Revision: D4681618 fbshipit-source-id: 7a76add --- .gitignore | 3 ++- Makefile | 11 +++++++---- infer/src/clang/ClangCommand.re | 11 +++++++---- .../build_systems/clang_with_E_flag/Makefile | 17 +++++++++++++++++ .../build_systems/clang_with_E_flag/issues.exp | 0 .../build_systems/clang_with_MD_flag/Makefile | 17 +++++++++++++++++ .../build_systems/clang_with_MD_flag/issues.exp | 1 + .../build_systems/clang_with_M_flag/Makefile | 17 +++++++++++++++++ .../build_systems/clang_with_M_flag/issues.exp | 0 9 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 infer/tests/build_systems/clang_with_E_flag/Makefile create mode 100644 infer/tests/build_systems/clang_with_E_flag/issues.exp create mode 100644 infer/tests/build_systems/clang_with_MD_flag/Makefile create mode 100644 infer/tests/build_systems/clang_with_MD_flag/issues.exp create mode 100644 infer/tests/build_systems/clang_with_M_flag/Makefile create mode 100644 infer/tests/build_systems/clang_with_M_flag/issues.exp diff --git a/.gitignore b/.gitignore index 22589ba02..5b148e8b9 100644 --- a/.gitignore +++ b/.gitignore @@ -31,6 +31,7 @@ duplicates.txt /infer/tests/build_systems/codetoanalyze/path with spaces/ /infer/tests/build_systems/clang_compilation_db_escaped/compile_commands.json /infer/tests/build_systems/clang_compilation_db_relpath/compile_commands.json +/infer/tests/build_systems/clang_with_MD_flag/hello.d /infer/tests/build_systems/codetoanalyze/xcodebuild/simple_app/app_built /infer/tests/build_systems/codetoanalyze/xcodebuild/simple_app/build/ infer/tests/build_systems/differential_*/**/*.class @@ -139,4 +140,4 @@ infer/src/.project /.yarn/* # generated by Maven -/infer/annotations/target \ No newline at end of file +/infer/annotations/target diff --git a/Makefile b/Makefile index 382aba104..8213e6346 100644 --- a/Makefile +++ b/Makefile @@ -21,11 +21,14 @@ BUILD_SYSTEMS_TESTS += \ clang_multiple_files \ clang_translation \ clang_unknown_ext \ + clang_with_E_flag \ + clang_with_M_flag \ + clang_with_MD_flag \ delete_results_dir \ - differential_resolve_infer_eradicate_conflict \ - differential_skip_anonymous_class_renamings \ - differential_skip_duplicated_types_on_filenames \ - differential_skip_duplicated_types_on_filenames_with_renamings \ + differential_resolve_infer_eradicate_conflict \ + differential_skip_anonymous_class_renamings \ + differential_skip_duplicated_types_on_filenames \ + differential_skip_duplicated_types_on_filenames_with_renamings \ fail_on_issue \ j1 \ linters \ diff --git a/infer/src/clang/ClangCommand.re b/infer/src/clang/ClangCommand.re index a5244ee19..dfb9593be 100644 --- a/infer/src/clang/ClangCommand.re +++ b/infer/src/clang/ClangCommand.re @@ -53,16 +53,19 @@ let value_of_option {orig_argv} => value_of_argv_option orig_argv; let has_flag {orig_argv} flag => List.exists f::(String.equal flag) orig_argv; -let can_attach_ast_exporter cmd => - has_flag cmd "-cc1" && ( +let can_attach_ast_exporter cmd => { + let is_supported_language cmd => switch (value_of_option cmd "-x") { | None => Logging.stderr "malformed -cc1 command has no \"-x\" flag!"; false | Some lang when String.is_prefix prefix::"assembler" lang => false | Some _ => true - } - ); + }; + /* -Eonly is -cc1 flag that gets produced by 'clang -M -### ...' */ + let is_preprocessor_only cmd => has_flag cmd "-E" || has_flag cmd "-Eonly"; + has_flag cmd "-cc1" && is_supported_language cmd && not (is_preprocessor_only cmd) +}; let argv_cons a b => [a, ...b]; diff --git a/infer/tests/build_systems/clang_with_E_flag/Makefile b/infer/tests/build_systems/clang_with_E_flag/Makefile new file mode 100644 index 000000000..e513898de --- /dev/null +++ b/infer/tests/build_systems/clang_with_E_flag/Makefile @@ -0,0 +1,17 @@ +# Copyright (c) 2017 - 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 = ../.. + +ANALYZER = infer +CLANG_OPTIONS = -E -c +INFER_OPTIONS = --report-custom-error --developer-mode --project-root ../codetoanalyze --no-failures-allowed +INFERPRINT_OPTIONS = --issues-tests + +SOURCES = ../codetoanalyze/hello.c + +include $(TESTS_DIR)/clang.make diff --git a/infer/tests/build_systems/clang_with_E_flag/issues.exp b/infer/tests/build_systems/clang_with_E_flag/issues.exp new file mode 100644 index 000000000..e69de29bb diff --git a/infer/tests/build_systems/clang_with_MD_flag/Makefile b/infer/tests/build_systems/clang_with_MD_flag/Makefile new file mode 100644 index 000000000..0dcfcb1a3 --- /dev/null +++ b/infer/tests/build_systems/clang_with_MD_flag/Makefile @@ -0,0 +1,17 @@ +# Copyright (c) 2017 - 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 = ../.. + +ANALYZER = infer +CLANG_OPTIONS = -MD -c +INFER_OPTIONS = --report-custom-error --developer-mode --project-root ../codetoanalyze --no-failures-allowed +INFERPRINT_OPTIONS = --issues-tests + +SOURCES = ../codetoanalyze/hello.c + +include $(TESTS_DIR)/clang.make diff --git a/infer/tests/build_systems/clang_with_MD_flag/issues.exp b/infer/tests/build_systems/clang_with_MD_flag/issues.exp new file mode 100644 index 000000000..d03e4fd25 --- /dev/null +++ b/infer/tests/build_systems/clang_with_MD_flag/issues.exp @@ -0,0 +1 @@ +hello.c, test, 2, NULL_DEREFERENCE, [start of procedure test()] diff --git a/infer/tests/build_systems/clang_with_M_flag/Makefile b/infer/tests/build_systems/clang_with_M_flag/Makefile new file mode 100644 index 000000000..5adb826bc --- /dev/null +++ b/infer/tests/build_systems/clang_with_M_flag/Makefile @@ -0,0 +1,17 @@ +# Copyright (c) 2017 - 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 = ../.. + +ANALYZER = infer +CLANG_OPTIONS = -M -c +INFER_OPTIONS = --report-custom-error --developer-mode --project-root ../codetoanalyze --no-failures-allowed +INFERPRINT_OPTIONS = --issues-tests + +SOURCES = ../codetoanalyze/hello.c + +include $(TESTS_DIR)/clang.make diff --git a/infer/tests/build_systems/clang_with_M_flag/issues.exp b/infer/tests/build_systems/clang_with_M_flag/issues.exp new file mode 100644 index 000000000..e69de29bb