From 43f2ce0739378bb54e2c7eaaa2906cc3457ce489 Mon Sep 17 00:00:00 2001 From: Tim Keith Date: Mon, 16 Jul 2018 16:24:14 -0700 Subject: [PATCH] [flang] Add tests for .mod file writing The source files contain the expected contents of generated .mod files. `test_modfile.sh` compiles the source file and verifies that the correct .mod files are generated. Original-commit: flang-compiler/f18@a23f53c1a7d6f1dba157c1bc87ebc046d3b818e6 Reviewed-on: https://github.com/flang-compiler/f18/pull/126 Tree-same-pre-rewrite: false --- flang/test/semantics/CMakeLists.txt | 11 ++++++ flang/test/semantics/modfile01.f90 | 36 +++++++++++++++++++ flang/test/semantics/modfile02.f90 | 34 ++++++++++++++++++ flang/test/semantics/modfile03.f90 | 57 ++++++++++++++++++++++++++++++ flang/test/semantics/test_modfile.sh | 67 ++++++++++++++++++++++++++++++++++++ 5 files changed, 205 insertions(+) create mode 100644 flang/test/semantics/modfile01.f90 create mode 100644 flang/test/semantics/modfile02.f90 create mode 100644 flang/test/semantics/modfile03.f90 create mode 100755 flang/test/semantics/test_modfile.sh diff --git a/flang/test/semantics/CMakeLists.txt b/flang/test/semantics/CMakeLists.txt index f19d964..3ab490d 100644 --- a/flang/test/semantics/CMakeLists.txt +++ b/flang/test/semantics/CMakeLists.txt @@ -55,6 +55,13 @@ set(SYMBOL_TESTS symbol01.f90 ) +# These test files have expected .mod file contents in the source +set(MODFILE_TESTS + modfile01.f90 + modfile02.f90 + modfile03.f90 +) + foreach(test ${ERROR_TESTS}) add_test(NAME ${test} COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test_errors.sh ${test}) endforeach() @@ -62,3 +69,7 @@ endforeach() foreach(test ${SYMBOL_TESTS}) add_test(NAME ${test} COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test_symbols.sh ${test}) endforeach() + +foreach(test ${MODFILE_TESTS}) + add_test(NAME ${test} COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test_modfile.sh ${test}) +endforeach() diff --git a/flang/test/semantics/modfile01.f90 b/flang/test/semantics/modfile01.f90 new file mode 100644 index 0000000..a16b811 --- /dev/null +++ b/flang/test/semantics/modfile01.f90 @@ -0,0 +1,36 @@ +! Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. +! +! Licensed under the Apache License, Version 2.0 (the "License"); +! you may not use this file except in compliance with the License. +! You may obtain a copy of the License at +! +! http://www.apache.org/licenses/LICENSE-2.0 +! +! Unless required by applicable law or agreed to in writing, software +! distributed under the License is distributed on an "AS IS" BASIS, +! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +! See the License for the specific language governing permissions and +! limitations under the License. + +! Check correct modfile generation for type with private component. +module m + integer :: i + integer, private :: j + type :: t + integer :: i + integer, private :: j + end type + type, private :: u + end type + type(t) :: x +end + +!Expect: m.mod +!module m +!integer::i +!type::t +!integer::i +!integer,private::j +!end type +!type(t)::x +!end diff --git a/flang/test/semantics/modfile02.f90 b/flang/test/semantics/modfile02.f90 new file mode 100644 index 0000000..1841cae --- /dev/null +++ b/flang/test/semantics/modfile02.f90 @@ -0,0 +1,34 @@ +! Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. +! +! Licensed under the Apache License, Version 2.0 (the "License"); +! you may not use this file except in compliance with the License. +! You may obtain a copy of the License at +! +! http://www.apache.org/licenses/LICENSE-2.0 +! +! Unless required by applicable law or agreed to in writing, software +! distributed under the License is distributed on an "AS IS" BASIS, +! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +! See the License for the specific language governing permissions and +! limitations under the License. + +! Check modfile generation for private type in public API. + +module m + type, private :: t1 + integer :: i + end type + type, private :: t2 + integer :: i + end type + type(t1) :: x1 + type(t2), private :: x2 +end + +!Expect: m.mod +!module m +!type,private::t1 +!integer::i +!end type +!type(t1)::x1 +!end diff --git a/flang/test/semantics/modfile03.f90 b/flang/test/semantics/modfile03.f90 new file mode 100644 index 0000000..fb743d0 --- /dev/null +++ b/flang/test/semantics/modfile03.f90 @@ -0,0 +1,57 @@ +! Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. +! +! Licensed under the Apache License, Version 2.0 (the "License"); +! you may not use this file except in compliance with the License. +! You may obtain a copy of the License at +! +! http://www.apache.org/licenses/LICENSE-2.0 +! +! Unless required by applicable law or agreed to in writing, software +! distributed under the License is distributed on an "AS IS" BASIS, +! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +! See the License for the specific language governing permissions and +! limitations under the License. + +! Check modfile generation with use-association. + +module m1 + integer :: x1 + integer, private :: x2 +end + +module m2 + use m1 + integer :: y1 +end + +module m3 + use m2, z1 => x1 +end + +module m4 + use m1 + use m2 +end + +!Expect: m1.mod +!module m1 +!integer::x1 +!end + +!Expect: m2.mod +!module m2 +!use m1,only:x1 +!integer::y1 +!end + +!Expect: m3.mod +!module m3 +!use m2,only:y1 +!use m2,only:z1=>x1 +!end + +!Expect: m4.mod +!module m4 +!use m1,only:x1 +!use m2,only:y1 +!end diff --git a/flang/test/semantics/test_modfile.sh b/flang/test/semantics/test_modfile.sh new file mode 100755 index 0000000..cc0d7a9 --- /dev/null +++ b/flang/test/semantics/test_modfile.sh @@ -0,0 +1,67 @@ +#!/usr/bin/bash +# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Compile a source file and compare generated .mod files against expected. + +set -e +PATH=/usr/bin +srcdir=$(dirname $0) +CMD="${F18:-../../../tools/f18/f18} -fdebug-resolve-names -fparse-only" + +if [[ $# != 1 ]]; then + echo "Usage: $0 " + exit 1 +fi +src=$srcdir/$1 +[[ ! -f $src ]] && echo "File not found: $src" && exit 1 + +if [[ $KEEP ]]; then + temp=. +else + temp=$(mktemp --directory --tmpdir=.) + trap "rm -rf $temp" EXIT +fi + +( cd $temp && $CMD $src ) + +actual=$temp/actual.mod +expect=$temp/expect.mod +actual_files=$temp/actual_files +diffs=$temp/diffs + +( cd $temp && ls -1 *.mod ) > $actual_files +expected_files=$(sed -n 's/^!Expect: \(.*\)/\1/p' $src) +extra_files=$(echo "$expected_files" | comm -23 $actual_files -) +if [[ ! -z "$extra_files" ]]; then + echo "Unexpected .mod files produced:" $extra_files + echo FAIL + exit 1 +fi +for mod in $expected_files; do + if [[ ! -f $temp/$mod ]]; then + echo "Compilation did not produce expected mod file: $mod" + echo FAIL + exit 1 + fi + sed '/^!mod\$/d' $temp/$mod > $actual + sed '1,/^!Expect: '"$mod"'/d' $src | sed -e '/^$/,$d' -e 's/^!//' > $expect + if ! diff -U999999 $actual $expect > $diffs; then + echo "Module file $mod differs from expected:" + sed '1,2d' $diffs + echo FAIL + exit 1 + fi +done +echo PASS -- 2.7.4