From 0479720c912be3cdd2d5fa9815ce4291f7613650 Mon Sep 17 00:00:00 2001 From: Andrzej Kotulski Date: Mon, 5 Dec 2016 05:14:08 -0800 Subject: [PATCH] [C++] Don't ignore header files passed in --changed-files-index Summary: Whenever header file is in changed-files-index, it should be captured and analyzed on demand. It was already being captured, but ondemand analysis wasn't triggered for code in header file. This diff does it. Use hacky header->source mapping to go from header to source (cluster) and then analyze everything in that cluster (inlucing code coming from header) Reviewed By: jberdine Differential Revision: D4265495 fbshipit-source-id: 61606f4 --- infer/src/base/DB.ml | 9 ++++++++- infer/src/base/DB.mli | 6 ++++-- .../src/integration/CaptureCompilationDatabase.ml | 8 +------- .../build_systems/clang_compilation_db/Makefile | 2 +- .../build_systems/clang_compilation_db/issues.exp | 2 ++ .../clang_compilation_database/CMakeLists.txt | 2 +- .../clang_compilation_database/index.txt | 1 + .../clang_compilation_database/lib3.cpp | 12 ++++++++++++ .../clang_compilation_database/lib3.h | 14 ++++++++++++++ 9 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib3.cpp create mode 100644 infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib3.h diff --git a/infer/src/base/DB.ml b/infer/src/base/DB.ml index e3633fab8..2f61734ec 100644 --- a/infer/src/base/DB.ml +++ b/infer/src/base/DB.ml @@ -158,7 +158,14 @@ let changed_source_files_set = Option.map_default read_file None Config.changed_files_index |> Option.map ( IList.fold_left - (fun changed_files line -> SourceFileSet.add (create_source_file line) changed_files) + (fun changed_files line -> + let source_file = create_source_file line in + let changed_files' = SourceFileSet.add source_file changed_files in + (* Add source corresponding to changed header if it exists *) + match source_file_of_header source_file with + | Some src -> SourceFileSet.add src changed_files' + | None -> changed_files' + ) SourceFileSet.empty ) diff --git a/infer/src/base/DB.mli b/infer/src/base/DB.mli index 69f7f7f1a..311bcc505 100644 --- a/infer/src/base/DB.mli +++ b/infer/src/base/DB.mli @@ -126,8 +126,10 @@ val source_file_is_under_project_root : source_file -> bool file exists. returns None otherwise *) val source_file_of_header : source_file -> source_file option -(** Set of files read from --changed-files-index file, None if option not specified *) -val changed_source_files_set : SourceFileSet.t option +(** Set of files read from --changed-files-index file, None if option not specified + NOTE: it may include extra source_files if --changed-files-index contains paths to + header files *) +val changed_source_files_set : SourceFileSet.t option (** {2 Source Dirs} *) diff --git a/infer/src/integration/CaptureCompilationDatabase.ml b/infer/src/integration/CaptureCompilationDatabase.ml index 0e76cdf23..54aec80b2 100644 --- a/infer/src/integration/CaptureCompilationDatabase.ml +++ b/infer/src/integration/CaptureCompilationDatabase.ml @@ -25,13 +25,7 @@ let should_capture_file_from_index () = Process.print_error_and_exit "Error reading the changed files index %s.\n%!" index | None -> function _ -> true) | Some files_set -> - function source_file -> - DB.SourceFileSet.mem source_file files_set || - (* as fallback try to capture corresponding source file for headers *) - Option.map_default - (fun src -> DB.SourceFileSet.mem src files_set) - false - (DB.source_file_of_header source_file) + function source_file -> DB.SourceFileSet.mem source_file files_set (** The buck targets are assumed to start with //, aliases are not supported. *) let check_args_for_targets args = diff --git a/infer/tests/build_systems/clang_compilation_db/Makefile b/infer/tests/build_systems/clang_compilation_db/Makefile index 943b7b5b5..393b053d1 100644 --- a/infer/tests/build_systems/clang_compilation_db/Makefile +++ b/infer/tests/build_systems/clang_compilation_db/Makefile @@ -12,7 +12,7 @@ CMAKE_BUILD_DIR = $(CMAKE_DIR)/_build ANALYZER = infer CLEAN_EXTRA = $(CMAKE_BUILD_DIR) -INFER_OPTIONS = --report-custom-error --developer-mode --project-root $(CMAKE_DIR) +INFER_OPTIONS = --cxx --report-custom-error --developer-mode --project-root $(CMAKE_DIR) SOURCES = $(CMAKE_DIR)/hello.cpp OBJECTS = $(CMAKE_BUILD_DIR)/compile_commands.json INFERPRINT_OPTIONS = --issues-tests diff --git a/infer/tests/build_systems/clang_compilation_db/issues.exp b/infer/tests/build_systems/clang_compilation_db/issues.exp index fbbf90b0e..048ebd3ed 100644 --- a/infer/tests/build_systems/clang_compilation_db/issues.exp +++ b/infer/tests/build_systems/clang_compilation_db/issues.exp @@ -1,5 +1,7 @@ hello.cpp, test0, 2, NULL_DEREFERENCE, [start of procedure test0()] hello.cpp, test1, 2, NULL_DEREFERENCE, [start of procedure test1(),start of procedure deref1()] +lib3.h, test, 1, NULL_DEREFERENCE, [start of procedure test(),start of procedure deref3()] hello.cpp, test0, 2, NULL_DEREFERENCE, [start of procedure test0()] hello.cpp, test1, 2, NULL_DEREFERENCE, [start of procedure test1(),start of procedure deref1()] hello.cpp, test2, 2, NULL_DEREFERENCE, [start of procedure test2(),start of procedure deref2()] +lib3.h, test, 1, NULL_DEREFERENCE, [start of procedure test(),start of procedure deref3()] diff --git a/infer/tests/build_systems/codetoanalyze/clang_compilation_database/CMakeLists.txt b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/CMakeLists.txt index d2ef0fa9e..d6e67c9e5 100644 --- a/infer/tests/build_systems/codetoanalyze/clang_compilation_database/CMakeLists.txt +++ b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/CMakeLists.txt @@ -1,4 +1,4 @@ cmake_minimum_required (VERSION 3.1) set(CMAKE_CXX_STANDARD 11) project (HELLO) -add_library (Hello hello.cpp lib1.cpp lib2.cpp) +add_library (Hello hello.cpp lib1.cpp lib2.cpp lib3.cpp) diff --git a/infer/tests/build_systems/codetoanalyze/clang_compilation_database/index.txt b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/index.txt index d19540906..57fcaafdf 100644 --- a/infer/tests/build_systems/codetoanalyze/clang_compilation_database/index.txt +++ b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/index.txt @@ -1,2 +1,3 @@ hello.cpp lib1.cpp +lib3.h diff --git a/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib3.cpp b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib3.cpp new file mode 100644 index 000000000..8bc920a1d --- /dev/null +++ b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib3.cpp @@ -0,0 +1,12 @@ +/* + * 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. + */ +#include "lib3.h" + +int deref3(int* p) { return *p; } + diff --git a/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib3.h b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib3.h new file mode 100644 index 000000000..f57a69f51 --- /dev/null +++ b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib3.h @@ -0,0 +1,14 @@ +/* + * 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. + */ + +int deref3(int* p); + +int test() { + return deref3(nullptr); +}