diff --git a/.gitignore b/.gitignore index ca4673b4e69..2bb2701f7a2 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ _build_infer *.exp.test* *.dot.test *.test.dot +*.test.sil *.test.txt duplicates.txt *.ast.sh diff --git a/Makefile b/Makefile index f3ce8ec0144..b46540a45df 100644 --- a/Makefile +++ b/Makefile @@ -174,6 +174,12 @@ endif endif # HAS_OBJC endif # BUILD_C_ANALYZERS +ifneq ($(BUILD_SWIFT_ANALYZERS),no) +DIRECT_TESTS += \ + swift_frontend \ + +endif + ifeq ($(BUILD_ERLANG_ANALYZERS),yes) ifneq ($(ERLC),no) ifneq ($(ESCRIPT),no) diff --git a/Makefile.config b/Makefile.config index 24a192ad7da..15373db84fa 100644 --- a/Makefile.config +++ b/Makefile.config @@ -107,6 +107,8 @@ CLANG_DEPS_NO_MODELS := $(INFER_BIN) CLANG_DEPS := $(CLANG_DEPS_NO_MODELS) $(MODELS_RESULTS_FILE) +SWIFT_DEPS := $(INFER_BIN) + XCODE_ISYSROOT_SUFFIX := Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk ifeq ($(wildcard $(XCODE_BASE)/$(XCODE_ISYSROOT_SUFFIX)),) XCODE_ISYSROOT_SUFFIX := SDKs/MacOSX.sdk diff --git a/infer/src/integration/Driver.ml b/infer/src/integration/Driver.ml index 1acc70ddc9e..6c164dbf5f5 100644 --- a/infer/src/integration/Driver.ml +++ b/infer/src/integration/Driver.ml @@ -36,7 +36,7 @@ type mode = | Python of {prog: string; args: string list} | PythonBytecode of {files: string list} | Rebar3 of {args: string list} - | Swiftc of {args: string list} + | Swiftc of {prog: string; args: string list} | Textual of {textualfiles: string list} | XcodeBuild of {prog: string; args: string list} | XcodeXcpretty of {prog: string; args: string list} @@ -94,8 +94,8 @@ let pp_mode fmt = function F.fprintf fmt "Python driver mode:@\nfiles = '%a'" Pp.cli_args files | Rebar3 {args} -> F.fprintf fmt "Rebar3 driver mode:@\nargs = %a" Pp.cli_args args - | Swiftc {args} -> - F.fprintf fmt "Swift driver mode:@\nargs = %a" Pp.cli_args args + | Swiftc {prog; args} -> + F.fprintf fmt "Swift driver mode:@\nprog = '%s' args = %a" prog Pp.cli_args args | Erlc {args} -> F.fprintf fmt "Erlc driver mode:@\nargs = %a" Pp.cli_args args | Hackc {prog; args} -> @@ -218,9 +218,9 @@ let capture ~changed_files mode = | Rebar3 {args} -> L.progress "Capturing in rebar3 mode...@." ; Erlang.capture ~command:"rebar3" ~args - | Swiftc {args} -> + | Swiftc {prog; args} -> L.progress "Capturing in swift mode...@." ; - Swift.capture ~command:"swiftc" ~args + Swift.capture ~command:prog ~args | Erlc {args} -> L.progress "Capturing in erlc mode...@." ; Erlang.capture ~command:"erlc" ~args @@ -560,7 +560,7 @@ let mode_of_build_command build_cmd (buck_mode : BuckMode.t option) = | BRebar3 -> Rebar3 {args} | BSwiftc -> - Swiftc {args} + Swiftc {prog; args} | BErlc -> Erlc {args} | BHackc -> diff --git a/infer/src/integration/Driver.mli b/infer/src/integration/Driver.mli index 8fab70f046a..6bffee096a8 100644 --- a/infer/src/integration/Driver.mli +++ b/infer/src/integration/Driver.mli @@ -35,7 +35,7 @@ type mode = | Python of {prog: string; args: string list} | PythonBytecode of {files: string list} | Rebar3 of {args: string list} - | Swiftc of {args: string list} + | Swiftc of {prog: string; args: string list} | Textual of {textualfiles: string list} | XcodeBuild of {prog: string; args: string list} | XcodeXcpretty of {prog: string; args: string list} diff --git a/infer/src/textual/TextualSil.ml b/infer/src/textual/TextualSil.ml index 0f3b4ae6c49..6056b2c5112 100644 --- a/infer/src/textual/TextualSil.ml +++ b/infer/src/textual/TextualSil.ml @@ -1310,10 +1310,10 @@ let module_to_sil lang module_ decls_env = let pp_copyright fmt = F.fprintf fmt "// \n" ; F.fprintf fmt "// Copyright (c) Facebook, Inc. and its affiliates.\n" ; - F.fprintf fmt "// \n" ; + F.fprintf fmt "//\n" ; F.fprintf fmt "// This source code is licensed under the MIT license found in the\n" ; F.fprintf fmt "// LICENSE file in the root directory of this source tree.\n" ; - F.fprintf fmt "//\n\n" + F.fprintf fmt "\n" let from_java ~filename tenv cfg = diff --git a/infer/tests/codetoanalyze/java/sil/issues.exp b/infer/tests/codetoanalyze/java/sil/issues.exp index efc58e169b5..86de1670647 100644 --- a/infer/tests/codetoanalyze/java/sil/issues.exp +++ b/infer/tests/codetoanalyze/java/sil/issues.exp @@ -1 +1 @@ -Array.sil, line 36, column 6: textual type error: expression n4 has type *int[][], while a subtype of *(*int[])[] was expected +Array.sil, line 35, column 6: textual type error: expression n4 has type *int[][], while a subtype of *(*int[])[] was expected diff --git a/infer/tests/codetoanalyze/swift/frontend/Makefile b/infer/tests/codetoanalyze/swift/frontend/Makefile new file mode 100644 index 00000000000..43c5562472a --- /dev/null +++ b/infer/tests/codetoanalyze/swift/frontend/Makefile @@ -0,0 +1,45 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +TESTS_DIR=../../.. + +SWIFT_OPTIONS = +INFER_OPTIONS = + +SOURCES = $(wildcard *.swift) + +CLEAN_EXTRA = $(foreach source,$(SOURCES),$(basename $(notdir $(source))).o) + +include $(TESTS_DIR)/base.make +include $(TESTS_DIR)/swift-base.make + +default: test + +%.test.sil: %.swift $(SWIFT_DEPS) $(HEADERS) + $(QUIET)$(call silent_on_success,Testing the infer/swift frontend in $(TEST_REL_DIR),\ + $(INFER_BIN) capture --frontend-tests --dump-textual --project-root $(TESTS_DIR) $(INFER_OPTIONS) -- \ + $(SWIFTC) $(SWIFT_OPTIONS) $<) + +capture: $(SOURCES:.swift=.test.sil) + +.PHONY: print +print: capture + +.PHONY: test +test: capture + $(QUIET)error=0; for file in $(SOURCES) ; do \ + diff -u "$$file.sil" "$$file.test.sil" || error=1 ; \ + done ; \ + if [ $$error = 1 ]; then exit 1; fi + +.PHONY: replace +replace: capture + $(QUIET)for file in $(SOURCES) ; do \ + mv $$file.test.sil $$file.sil ; \ + done + +.PHONY: clean +clean: + $(REMOVE_DIR) infer-out */*.test.sil */*/*.test.sil $(CLEAN_EXTRA) diff --git a/infer/tests/codetoanalyze/swift/frontend/hello.swift b/infer/tests/codetoanalyze/swift/frontend/hello.swift new file mode 100644 index 00000000000..da3441e9fed --- /dev/null +++ b/infer/tests/codetoanalyze/swift/frontend/hello.swift @@ -0,0 +1,4 @@ +func main() { + print("Hello, World!") +} + diff --git a/infer/tests/codetoanalyze/swift/frontend/hello.swift.sil b/infer/tests/codetoanalyze/swift/frontend/hello.swift.sil new file mode 100644 index 00000000000..1639e34a815 --- /dev/null +++ b/infer/tests/codetoanalyze/swift/frontend/hello.swift.sil @@ -0,0 +1,6 @@ +// +// Copyright (c) Facebook, Inc. and its affiliates. +// +// This source code is licensed under the MIT license found in the +// LICENSE file in the root directory of this source tree. + diff --git a/infer/tests/swift-base.make b/infer/tests/swift-base.make new file mode 100644 index 00000000000..d14858c4dc2 --- /dev/null +++ b/infer/tests/swift-base.make @@ -0,0 +1,7 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +%.o: %.swift + $(SWIFTC) $(SWIFT_OPTIONS) -o $@ $<