blob: ed168c7bdee37b371b8466a1644e1614de152544 [file] [log] [blame]
tzik0137dc5d42015-12-17 05:29:221# Copyright (c) 2011 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# This file is meant to be included into an target to create a unittest that
6# invokes a set of no-compile tests. A no-compile test is a test that asserts
7# a particular construct will not compile.
8#
9# Also see:
10# http://dev.chromium.org/developers/testing/no-compile-tests
11#
12# To use this, create a gyp target with the following form:
13#
14# import("//build/nocompile.gni")
15# nocompile_test("my_module_nc_unittests") {
16# sources = [
17# 'nc_testset_1.nc',
18# 'nc_testset_2.nc',
19# ]
20# }
21#
22# The .nc files are C++ files that contain code we wish to assert will not
23# compile. Each individual test case in the file should be put in its own
24# #ifdef section. The expected output should be appended with a C++-style
25# comment that has a python list of regular expressions. This will likely
26# be greater than 80-characters. Giving a solid expected output test is
27# important so that random compile failures do not cause the test to pass.
28#
29# Example .nc file:
30#
Lei Zhangf84e27282018-04-27 02:24:0031# #if defined(TEST_NEEDS_SEMICOLON) // [r"expected ',' or ';' at end of input"]
tzik0137dc5d42015-12-17 05:29:2232#
33# int a = 1
34#
Lei Zhangf84e27282018-04-27 02:24:0035# #elif defined(TEST_NEEDS_CAST) // [r"invalid conversion from 'void*' to 'char*'"]
tzik0137dc5d42015-12-17 05:29:2236#
37# void* a = NULL;
38# char* b = a;
39#
40# #endif
41#
Lei Zhangf84e27282018-04-27 02:24:0042# If we needed disable TEST_NEEDS_SEMICOLON, then change the define to:
tzik0137dc5d42015-12-17 05:29:2243#
Lei Zhangf84e27282018-04-27 02:24:0044# DISABLE_TEST_NEEDS_SEMICOLON
45# TEST_NEEDS_CAST
tzik0137dc5d42015-12-17 05:29:2246#
47# The lines above are parsed by a regexp so avoid getting creative with the
48# formatting or ifdef logic; it will likely just not work.
49#
50# Implementation notes:
51# The .nc files are actually processed by a python script which executes the
52# compiler and generates a .cc file that is empty on success, or will have a
53# series of #error lines on failure, and a set of trivially passing gunit
54# TEST() functions on success. This allows us to fail at the compile step when
55# something goes wrong, and know during the unittest run that the test was at
56# least processed when things go right.
57
Hans Wennborg8261b1682019-01-14 13:40:1658import("//build/config/clang/clang.gni")
Nico Weber8c6f2962019-08-01 15:30:2259import("//build/toolchain/toolchain.gni")
tzik0137dc5d42015-12-17 05:29:2260import("//testing/test.gni")
61
62declare_args() {
wychen4f7b28c2017-01-04 08:04:2963 # TODO(crbug.com/105388): make sure no-compile test is not flaky.
wychen09ac3a22017-02-27 20:19:2664 enable_nocompile_tests =
65 (is_linux || is_mac || is_ios) && is_clang && host_cpu == target_cpu
tzik0137dc5d42015-12-17 05:29:2266}
67
68if (enable_nocompile_tests) {
tzikcffd16b2017-08-01 00:23:3269 import("//build/config/c++/c++.gni")
Hans Wennborg8261b1682019-01-14 13:40:1670 import("//build/config/sysroot.gni")
tzik0137dc5d42015-12-17 05:29:2271 template("nocompile_test") {
72 nocompile_target = target_name + "_run_nocompile"
73
74 action_foreach(nocompile_target) {
Eric Secklerfb2a899d2018-09-10 15:25:3075 testonly = true
tzik0137dc5d42015-12-17 05:29:2276 script = "//tools/nocompile_driver.py"
77 sources = invoker.sources
Eric Secklerfb2a899d2018-09-10 15:25:3078 deps = invoker.deps
79 if (defined(invoker.public_deps)) {
80 public_deps = invoker.public_deps
81 }
tzikb5f8f4b2016-02-16 02:34:4582
83 result_path = "$target_gen_dir/{{source_name_part}}_nc.cc"
84 depfile = "${result_path}.d"
Nico Weber4782d73682020-01-14 04:52:1785 outputs = [ result_path ]
tzik0137dc5d42015-12-17 05:29:2286 args = [
Hans Wennborg8261b1682019-01-14 13:40:1687 rebase_path("$clang_base_path/bin/clang++", root_build_dir),
tzik0137dc5d42015-12-17 05:29:2288 "4", # number of compilers to invoke in parallel.
89 "{{source}}",
brettwbb57bea12017-01-18 22:11:0590 rebase_path(result_path, root_build_dir),
Trent Apted71169bb2017-07-28 00:12:1091 "--",
tzikcffd16b2017-08-01 00:23:3292 "-nostdinc++",
Tom Anderson04125932017-11-28 03:13:1393 "-isystem" + rebase_path("$libcxx_prefix/include", root_build_dir),
94 "-isystem" + rebase_path("$libcxxabi_prefix/include", root_build_dir),
tzikcffd16b2017-08-01 00:23:3295 "-std=c++14",
Trent Apted71169bb2017-07-28 00:12:1096 "-Wall",
97 "-Werror",
98 "-Wfatal-errors",
Victor Costan63cd2c2d2018-02-16 02:29:0799 "-Wthread-safety",
Trent Apted71169bb2017-07-28 00:12:10100 "-I" + rebase_path("//", root_build_dir),
Eric Seckler03d0d422018-08-29 20:06:36101 "-I" + rebase_path(root_gen_dir, root_build_dir),
Hans Wennborg18026ae2019-08-22 19:07:46102
Jamie Madill90cdc252019-08-15 23:12:47103 # TODO(https://crbug.com/989932): Track build/config/compiler/BUILD.gn
Hans Wennborg18026ae2019-08-22 19:07:46104 "-Wno-implicit-int-float-conversion",
105 ]
Trent Apted71169bb2017-07-28 00:12:10106 if (sysroot != "") {
107 args += [
108 "--sysroot",
109 rebase_path(sysroot, root_build_dir),
110 ]
111 }
tzik0137dc5d42015-12-17 05:29:22112 }
113
114 test(target_name) {
115 deps = invoker.deps + [ ":$nocompile_target" ]
116 sources = get_target_outputs(":$nocompile_target")
117 }
118 }
119}