From 548918e5246f8b93d75018db707988feb1d35fa5 Mon Sep 17 00:00:00 2001 From: Andrzej Kotulski Date: Mon, 21 Nov 2016 03:43:40 -0800 Subject: [PATCH] [tests] extend compilation database test with changed-files-index options Summary: Add test for compilation database and --changed-files-index option Fix one bug that the test uncovered Reviewed By: dulmarod Differential Revision: D4198502 fbshipit-source-id: 9039c65 --- .../integration/CaptureCompilationDatabase.ml | 2 +- .../build_systems/clang_compilation_db/Makefile | 2 +- .../clang_compilation_db/issues.exp | 3 ++- .../clang_compilation_database/CMakeLists.txt | 2 +- .../clang_compilation_database/hello.cpp | 16 +++++++++++++++- .../clang_compilation_database/index.txt | 2 ++ .../clang_compilation_database/lib1.cpp | 11 +++++++++++ .../clang_compilation_database/lib1.h | 9 +++++++++ .../clang_compilation_database/lib2.cpp | 11 +++++++++++ .../clang_compilation_database/lib2.h | 9 +++++++++ 10 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 infer/tests/build_systems/codetoanalyze/clang_compilation_database/index.txt create mode 100644 infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib1.cpp create mode 100644 infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib1.h create mode 100644 infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib2.cpp create mode 100644 infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib2.h diff --git a/infer/src/integration/CaptureCompilationDatabase.ml b/infer/src/integration/CaptureCompilationDatabase.ml index 3ebac6bb5..f06c02ed2 100644 --- a/infer/src/integration/CaptureCompilationDatabase.ml +++ b/infer/src/integration/CaptureCompilationDatabase.ml @@ -42,7 +42,7 @@ let read_files_to_compile () = | Some lines -> IList.fold_left (fun changed_files line -> - let file = replace_header_file_with_source_file (DB.abs_source_file_from_path line) in + let file = replace_header_file_with_source_file (DB.source_file_from_string line) in DB.SourceFileSet.add file changed_files) changed_files lines diff --git a/infer/tests/build_systems/clang_compilation_db/Makefile b/infer/tests/build_systems/clang_compilation_db/Makefile index f456c4afe..e9ef92a81 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 = --report-custom-error --developer-mode --project-root $(CMAKE_DIR) --changed-files-index $(CMAKE_DIR)/index.txt 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 0781f943a..8c6294f0b 100644 --- a/infer/tests/build_systems/clang_compilation_db/issues.exp +++ b/infer/tests/build_systems/clang_compilation_db/issues.exp @@ -1 +1,2 @@ -hello.cpp, test, 2, NULL_DEREFERENCE, [start of procedure test()] +hello.cpp, test0, 2, NULL_DEREFERENCE, [start of procedure test0()] +hello.cpp, test1, 2, NULL_DEREFERENCE, [start of procedure test1(),start of procedure deref1()] 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 b5b5f0157..d2ef0fa9e 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) +add_library (Hello hello.cpp lib1.cpp lib2.cpp) diff --git a/infer/tests/build_systems/codetoanalyze/clang_compilation_database/hello.cpp b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/hello.cpp index ec86ac272..8a940d9c4 100644 --- a/infer/tests/build_systems/codetoanalyze/clang_compilation_database/hello.cpp +++ b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/hello.cpp @@ -7,7 +7,21 @@ * of patent rights can be found in the PATENTS file in the same directory. */ -void test() { +#include "lib1.h" +#include "lib2.h" + +void test0() { int* s = nullptr; // requires -std=c++11 flag *s = 42; } + +int test1() { + int* s = nullptr; + return deref1(s); // should be nullderef +} + +void test2() { + int* s = nullptr; + return deref2(s); // should be nullderef, but will be skipped + // because of --changed-files-index option +} diff --git a/infer/tests/build_systems/codetoanalyze/clang_compilation_database/index.txt b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/index.txt new file mode 100644 index 000000000..d19540906 --- /dev/null +++ b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/index.txt @@ -0,0 +1,2 @@ +hello.cpp +lib1.cpp diff --git a/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib1.cpp b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib1.cpp new file mode 100644 index 000000000..3550a2845 --- /dev/null +++ b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib1.cpp @@ -0,0 +1,11 @@ +/* + * 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 "lib1.h" + +int deref1(int* p) { return *p; } diff --git a/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib1.h b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib1.h new file mode 100644 index 000000000..784418184 --- /dev/null +++ b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib1.h @@ -0,0 +1,9 @@ +/* + * 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 deref1(int* p); diff --git a/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib2.cpp b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib2.cpp new file mode 100644 index 000000000..5c627a2ac --- /dev/null +++ b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib2.cpp @@ -0,0 +1,11 @@ +/* + * 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 "lib2.h" + +int deref2(int* p) { return *p; } diff --git a/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib2.h b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib2.h new file mode 100644 index 000000000..537b5b63f --- /dev/null +++ b/infer/tests/build_systems/codetoanalyze/clang_compilation_database/lib2.h @@ -0,0 +1,9 @@ +/* + * 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 deref2(int* p);