diff --git a/libcxx/test/libcxx/selftest/additional_compile_flags_if_supported/substitutes-in-compile-flags.sh.cpp b/libcxx/test/libcxx/selftest/additional_compile_flags_if_supported/substitutes-in-compile-flags.sh.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/selftest/additional_compile_flags_if_supported/substitutes-in-compile-flags.sh.cpp @@ -0,0 +1,18 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// This test greps for %t, which is expanded to a path with backslashes. When +// that is passed to grep, those backslashes must be escaped. We escape those +// within the pattern into a file and use this file with 'grep'. + +// Make sure that substitutions are performed inside additional compiler flags. + +// ADDITIONAL_COMPILE_FLAGS_IF_SUPPORTED: -I %t.1 +// ADDITIONAL_COMPILE_FLAGS_IF_SUPPORTED: -isystem %t.2 , -isysroot %t.3 +// RUN: echo "-I %t.1 -isystem %t.2 -isysroot %t.3" | sed "s/\\\/\\\\\\\/g" > %t.escaped.grep +// RUN: echo "%{compile_flags}" | grep -e -f %t.escaped.grep diff --git a/libcxx/test/libcxx/selftest/additional_compile_flags_if_supported/substitutes-in-run.sh.cpp b/libcxx/test/libcxx/selftest/additional_compile_flags_if_supported/substitutes-in-run.sh.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/selftest/additional_compile_flags_if_supported/substitutes-in-run.sh.cpp @@ -0,0 +1,15 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// Make sure that additional compiler flags are added to the %{compile_flags} +// substitution if supported. + +// ADDITIONAL_COMPILE_FLAGS_IF_SUPPORTED: -Wall +// ADDITIONAL_COMPILE_FLAGS_IF_SUPPORTED: -bar +// ADDITIONAL_COMPILE_FLAGS_IF_SUPPORTED: -baz, -Wextra +// RUN: echo "%{compile_flags}" | grep -e '-Wall -Wextra' diff --git a/libcxx/utils/libcxx/test/format.py b/libcxx/utils/libcxx/test/format.py --- a/libcxx/utils/libcxx/test/format.py +++ b/libcxx/utils/libcxx/test/format.py @@ -13,6 +13,7 @@ import re import shutil import subprocess +from libcxx.test.dsl import hasCompileFlag def _supportsVerify(config): """ @@ -73,6 +74,7 @@ # Parse the test file, including custom directives additionalCompileFlags = [] + additionalOptionalCompileFlags = [] fileDependencies = [] parsers = [ lit.TestRunner.IntegratedTestKeywordParser('FILE_DEPENDENCIES:', @@ -80,7 +82,10 @@ initial_value=fileDependencies), lit.TestRunner.IntegratedTestKeywordParser('ADDITIONAL_COMPILE_FLAGS:', lit.TestRunner.ParserKind.LIST, - initial_value=additionalCompileFlags) + initial_value=additionalCompileFlags), + lit.TestRunner.IntegratedTestKeywordParser('ADDITIONAL_COMPILE_FLAGS_IF_SUPPORTED:', + lit.TestRunner.ParserKind.LIST, + initial_value=additionalOptionalCompileFlags), ] scriptInTest = lit.TestRunner.parseIntegratedTestScript(test, additional_parsers=parsers, @@ -102,6 +107,17 @@ substitutions = [(s, x + ' ' + ' '.join(additionalCompileFlags)) if s == '%{compile_flags}' else (s, x) for (s, x) in substitutions] + # Add compile flags specified with ADDITIONAL_COMPILE_FLAGS_IF_SUPPORTED. + supported = [hasCompileFlag(test.config, f) + for f in additionalOptionalCompileFlags] + # We need to remove elements in reverse order to maintain indices unchanged. + supported.reverse() + for idx, sup in enumerate(supported): + if not sup: + del additionalOptionalCompileFlags[idx] + substitutions = [(s, x + ' ' + ' '.join(additionalOptionalCompileFlags)) if s == '%{compile_flags}' + else (s, x) for (s, x) in substitutions] + # Perform substitutions in the script itself. script = lit.TestRunner.applySubstitutions(script, substitutions, recursion_limit=test.config.recursiveExpansionLimit) @@ -178,6 +194,12 @@ allows adding special compilation flags without having to use a .sh.cpp test, which would be more powerful but perhaps overkill. + // ADDITIONAL_COMPILE_FLAGS_IF_SUPPORTED: flag1, flag2, flag3 + + This directive behaves like ADDITIONAL_COMPILE_FLAGS, but only if + the compiler supports the flag. Unsupported flags will be silently + ignored. + Additional provided substitutions and features ==============================================