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