1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #if defined(PW_NEGATIVE_COMPILATION_TESTS_ENABLED) && \
17     PW_NEGATIVE_COMPILATION_TESTS_ENABLED == 1
18 
19 // Declares a compilation failure test. Must be used in a #if or #elif
20 // statement. The code in the section must not compile.
21 //
22 // Internally, this expands to a macro that enables or disables the code section
23 // for the compilation failure test. It would be possible to use a plain macro
24 // and defined(...) for this, but the function-like macro gives a cleaner
25 // interface and catches typos that would otherwise cause tests to silently be
26 // skipped.
27 #define PW_NC_TEST(test_case) PW_NC_TEST_EXECUTE_CASE_##test_case
28 
29 #else
30 
31 // If testing is disabled, always evaluate false to disable the test case.
32 #define PW_NC_TEST(test_case) 0 && PW_NC_TEST_EXECUTE_CASE_##test_case
33 
34 #endif  // PW_NEGATIVE_COMPILATION_TESTS_ENABLED
35 
36 // Checks that the compilation output matches the provided regex in a negative
37 // compilation test. The regex must be a simple string literal. In Python, the
38 // string is taken directly from the C++ source, interpreted as a regular string
39 // literal, and compiled as a regular expression.
40 //
41 // Do not expand the regex since it may include non-standard backslash escapes,
42 // such as "\(".
43 #define PW_NC_EXPECT(regex) \
44   static_assert(PW_NEGATIVE_COMPILATION_TESTS_ENABLED == 1)
45 
46 // Checks that the compilation output matches the regex in Clang compilers only.
47 #define PW_NC_EXPECT_CLANG(regex) \
48   static_assert(PW_NEGATIVE_COMPILATION_TESTS_ENABLED == 1)
49 
50 // Checks that the compilation output matches the regex in GCC compilers only.
51 #define PW_NC_EXPECT_GCC(regex) \
52   static_assert(PW_NEGATIVE_COMPILATION_TESTS_ENABLED == 1)
53