[clangdb] support relative paths in compilation databases

Summary: Some compilation databases give relatives paths for the `"file"` field. This is not ambiguous as there is also a `"dir"` field, so use that to make the path absolute when needed.

Reviewed By: dulmarod

Differential Revision: D4559145

fbshipit-source-id: be36a16
master
Jules Villard 8 years ago committed by Facebook Github Bot
parent f3c181f204
commit 08aad39050

2
.gitignore vendored

@ -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

@ -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 \

@ -69,7 +69,8 @@ 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

@ -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 $<)

@ -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"
}
]

@ -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()]

@ -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 <stdlib.h>
void test_hel_lo() {
int* s = NULL;
*s = 42;
}
Loading…
Cancel
Save