1 # 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.
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.
10 # http://dev.chromium.org/developers/testing/no-compile-tests
12 # To use this, create a gyp target with the following form:
14 # import("//build/nocompile.gni")
15 # nocompile_test("my_module_nc_unittests") {
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.
31 # #if defined(TEST_NEEDS_SEMICOLON) // [r"expected ',' or ';' at end of input"]
35 # #elif defined(TEST_NEEDS_CAST) // [r"invalid conversion from 'void*' to 'char*'"]
42 # If we needed disable TEST_NEEDS_SEMICOLON, then change the define to:
44 # DISABLE_TEST_NEEDS_SEMICOLON
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.
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.
58 import("//build/config/clang/clang.gni")
59 import("//testing/test.gni")
62 # TODO(crbug.com/105388): make sure no-compile test is not flaky.
63 enable_nocompile_tests =
64 (is_linux || is_mac || is_ios) && is_clang && host_cpu == target_cpu
67 if (enable_nocompile_tests) {
68 import("//build/config/c++/c++.gni")
69 import("//build/config/sysroot.gni")
70 template("nocompile_test") {
71 nocompile_target = target_name + "_run_nocompile"
73 action_foreach(nocompile_target) {
75 script = "//tools/nocompile_driver.py"
76 sources = invoker.sources
78 if (defined(invoker.public_deps)) {
79 public_deps = invoker.public_deps
82 result_path = "$target_gen_dir/{{source_name_part}}_nc.cc"
83 depfile = "${result_path}.d"
88 rebase_path("$clang_base_path/bin/clang++", root_build_dir),
89 "4", # number of compilers to invoke in parallel.
91 rebase_path(result_path, root_build_dir),
94 "-isystem" + rebase_path("$libcxx_prefix/include", root_build_dir),
95 "-isystem" + rebase_path("$libcxxabi_prefix/include", root_build_dir),
101 "-I" + rebase_path("//", root_build_dir),
102 "-I" + rebase_path(root_gen_dir, root_build_dir),
107 rebase_path(sysroot, root_build_dir),
113 deps = invoker.deps + [ ":$nocompile_target" ]
114 sources = get_target_outputs(":$nocompile_target")