diff --git a/.gitignore b/.gitignore index 842523651..111f15662 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,8 @@ duplicates.txt /infer/tests/build_systems/codetoanalyze/ndk-build/hello_app/obj/ /infer/tests/build_systems/codetoanalyze/utf8_*n_pwd /infer/tests/build_systems/codetoanalyze/mvn/**/target/ +/infer/tests/build_systems/codetoanalyze/path with spaces/ +/infer/tests/build_systems/clang_compilation_db_relpath/compile_commands.json # generated by oUnit /oUnit-all.cache diff --git a/Makefile b/Makefile index 3c34a5279..d2863da51 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,7 @@ ifeq ($(BUILD_C_ANALYZERS),yes) BUILD_SYSTEMS_TESTS += \ assembly \ ck_analytics ck_imports \ + clang_compilation_db_relpath \ clang_multiple_files \ clang_translation \ clang_unknown_ext \ diff --git a/infer/src/integration/CompilationDatabase.ml b/infer/src/integration/CompilationDatabase.ml index c762dbf5d..d2245e4d7 100644 --- a/infer/src/integration/CompilationDatabase.ml +++ b/infer/src/integration/CompilationDatabase.ml @@ -69,12 +69,13 @@ let decode_json_file (database : t) json_path = | None -> exit_format_error () in let command, args = parse_command_and_arguments cmd in let compilation_data = { dir; command; args;} in - let source_file = SourceFile.from_abs_path file in + let abs_file = if Filename.is_relative file then dir ^/ file else file in + let source_file = SourceFile.from_abs_path abs_file in database := SourceFile.Map.add source_file compilation_data !database | _ -> exit_format_error () in parse_json json -let from_json_files db_json_files = +let from_json_files db_json_files = let db = empty () in IList.iter (decode_json_file db) db_json_files; Logging.out "created database with %d entries@\n" (get_size db); diff --git a/infer/tests/build_systems/clang_compilation_db_relpath/Makefile b/infer/tests/build_systems/clang_compilation_db_relpath/Makefile new file mode 100644 index 000000000..399dd478f --- /dev/null +++ b/infer/tests/build_systems/clang_compilation_db_relpath/Makefile @@ -0,0 +1,34 @@ +# 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 = ../.. + +ANALYZER = infer +INFER_OPTIONS = --report-custom-error --developer-mode +SOURCES = \ + ../codetoanalyze/hello.c ../codetoanalyze/hello2.c ../codetoanalyze/hello3.c \ + ../codetoanalyze/path\ with\ spaces/hel\ lo.c +OBJECTS = $(SOURCES:.c=.o) +CLEAN_EXTRA = compile_commands.json ../codetoanalyze/path\ with\ spaces +INFERPRINT_OPTIONS = --issues-tests + +include $(TESTS_DIR)/base.make + +../codetoanalyze/path\ with\ spaces/hel\ lo.c: ../codetoanalyze/path_with_spaces/hel_lo.c +# make does not want to interpret "$(@D)" in the right way here... + $(MKDIR_P) ../codetoanalyze/path\ with\ spaces/ + @cp "$<" "$@" + @touch "$@" + +compile_commands.json: compile_commands.json.in + sed -e 's#%pwd%#$(CURDIR)#g' $< > $@ || $(REMOVE) $@ + +infer-out/report.json: compile_commands.json $(SOURCES) Makefile \ + ../codetoanalyze/path\ with\ spaces/hel\ lo.c + $(call silent_on_success,\ + $(INFER_BIN) -a $(ANALYZER) $(INFER_OPTIONS) --project-root $(TESTS_DIR) \ + --clang-compilation-db-files $<) diff --git a/infer/tests/build_systems/clang_compilation_db_relpath/compile_commands.json.in b/infer/tests/build_systems/clang_compilation_db_relpath/compile_commands.json.in new file mode 100644 index 000000000..4b97e1696 --- /dev/null +++ b/infer/tests/build_systems/clang_compilation_db_relpath/compile_commands.json.in @@ -0,0 +1,22 @@ +[ + { + "command" : "clang -c hello.c", + "directory" : "%pwd%/../codetoanalyze", + "file" : "hello.c" + }, + { + "directory" : "%pwd%/../codetoanalyze/path with spaces", + "file" : "hel lo.c", + "command" : "clang -c hel\\ lo.c" + }, + { + "directory" : "%pwd%/../codetoanalyze", + "file" : "hello2.c", + "command" : "clang -c hello2.c" + }, + { + "command" : "clang -c hello3.c", + "directory" : "%pwd%/../codetoanalyze", + "file" : "hello3.c" + } +] diff --git a/infer/tests/build_systems/clang_compilation_db_relpath/issues.exp b/infer/tests/build_systems/clang_compilation_db_relpath/issues.exp new file mode 100644 index 000000000..095ffd542 --- /dev/null +++ b/infer/tests/build_systems/clang_compilation_db_relpath/issues.exp @@ -0,0 +1,4 @@ +build_systems/codetoanalyze/hello.c, test, 2, NULL_DEREFERENCE, [start of procedure test()] +build_systems/codetoanalyze/hello2.c, test2, 2, NULL_DEREFERENCE, [start of procedure test2()] +build_systems/codetoanalyze/hello3.c, test3, 2, NULL_DEREFERENCE, [start of procedure test3()] +build_systems/codetoanalyze/path with spaces/hel lo.c, test_hel_lo, 2, NULL_DEREFERENCE, [start of procedure test_hel_lo()] diff --git a/infer/tests/build_systems/codetoanalyze/path_with_spaces/hel_lo.c b/infer/tests/build_systems/codetoanalyze/path_with_spaces/hel_lo.c new file mode 100644 index 000000000..40eda0ac3 --- /dev/null +++ b/infer/tests/build_systems/codetoanalyze/path_with_spaces/hel_lo.c @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2015 - 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 + +void test_hel_lo() { + int* s = NULL; + *s = 42; +}