]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/avoids_cpp_reserved.c
78ba6ca8b87fbecdbd7a23db1593d424f537ae22
[ccan] / tools / ccanlint / tests / avoids_cpp_reserved.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <limits.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <err.h>
13 #include <string.h>
14 #include <ctype.h>
15
16 static const char *can_build(struct manifest *m)
17 {
18         if (safe_mode)
19                 return "Safe mode enabled";
20         return NULL;
21 }
22
23 static struct ccan_file *main_header(struct manifest *m)
24 {
25         struct ccan_file *f;
26
27         list_for_each(&m->h_files, f, list) {
28                 if (strstarts(f->name, m->basename)
29                     && strlen(f->name) == strlen(m->basename) + 2)
30                         return f;
31         }
32         /* Should not happen: we depend on main_header_compiles */
33         abort();
34 }
35
36 static void check_headers_no_cpp(struct manifest *m,
37                                  unsigned int *timeleft, struct score *score)
38 {
39         char *contents;
40         char *tmpsrc, *tmpobj, *cmdout;
41         int fd;
42         struct ccan_file *mainh = main_header(m);
43
44         tmpsrc = temp_file(m, "-included.c", mainh->fullname);
45         tmpobj = temp_file(m, ".o", tmpsrc);
46
47         /* We don't fail you for this. */
48         score->pass = true;
49         fd = open(tmpsrc, O_WRONLY | O_CREAT | O_EXCL, 0600);
50         if (fd < 0)
51                 err(1, "Creating temporary file %s", tmpsrc);
52
53         contents = tal_fmt(tmpsrc,
54                    "#define alignas #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
55                    "#define class #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
56                    "#define constexpr #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
57                    "#define const_cast #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
58                    "#define decltype #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
59                    "#define delete #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
60                    "#define dynamic_cast #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
61                    "#define explicit #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
62                    "#define false #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
63                    "#define friend #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
64                    "#define mutable #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
65                    "#define namespace #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
66                    "#define new #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
67                    "#define nullptr #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
68                    "#define operator #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
69                    "#define public #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
70                    "#define private #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
71                    "#define protected #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
72                    "#define reinterpret_cast #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
73                    "#define static_assert #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
74                    "#define static_cast #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
75                    "#define template #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
76                    "#define this #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
77                    "#define thread_local #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
78                    "#define throw #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
79                    "#define true #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
80                    "#define try #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
81                    "#define typeid #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
82                    "#define typename #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
83                    "#define using #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
84                    "#define virtual #DONT_USE_CPLUSPLUS_RESERVED_NAMES\n"
85                    "#include <ccan/%s/%s.h>\n",
86                                    m->modname, m->basename);
87         if (write(fd, contents, strlen(contents)) != strlen(contents))
88                 err(1, "writing to temporary file %s", tmpsrc);
89         close(fd);
90
91         if (compile_object(score, tmpsrc, ccan_dir, compiler, cflags,
92                            tmpobj, &cmdout)) {
93                 score->score = score->total;
94         } else {
95                 score->error = tal_fmt(score,
96                                        "Main header file with C++ names:\n%s",
97                                        cmdout);
98         }
99 }
100
101 struct ccanlint avoids_cpp_reserved = {
102         .key = "avoids_cpp_reserved",
103         .name = "Modules main header compiles without C++ reserved words",
104         .check = check_headers_no_cpp,
105         .can_run = can_build,
106         .needs = "main_header_compiles"
107 };
108
109 REGISTER_TEST(avoids_cpp_reserved);