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