2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10 * Created by Blaine Garst on 9/16/08.
21 bool readfile(char *buffer
, const char *from
) {
22 int fd
= open(from
, 0);
23 if (fd
< 0) return false;
24 int count
= read(fd
, buffer
, 512);
25 if (count
< 0) return false;
26 buffer
[count
] = 0; // zap newline
30 // basic idea, take compiler args, run compiler, and verify that expected failure matches any existing one
32 int main(int argc
, char *argv
[]) {
33 if (argc
== 1) return 0;
34 char *copy
[argc
+1]; // make a copy
35 // find and strip off -e "errorfile"
36 char *errorfile
= NULL
;
37 int counter
= 0, i
= 0;
38 for (i
= 1; i
< argc
; ++i
) { // skip 0 arg which is "fail"
39 if (!strncmp(argv
[i
], "-e", 2)) {
40 errorfile
= argv
[++i
];
43 copy
[counter
++] = argv
[i
];
51 sprintf(buffer
, "/tmp/errorfile_%d", getpid());
53 int fd
= creat(buffer
, 0777);
55 fprintf(stderr
, "didn't open custom error file %s as 1, got %d\n", buffer
, fd
);
60 int result
= execv(copy
[0], copy
);
64 printf("fork failed\n");
68 pid_t deadchild
= wait(&status
);
69 if (deadchild
!= child
) {
70 printf("wait got %d instead of %d\n", deadchild
, child
);
73 if (WEXITSTATUS(status
) == 0) {
74 printf("compiler exited normally, not good under these circumstances\n");
77 //printf("exit status of child %d was %d\n", child, WEXITSTATUS(status));
78 sprintf(buffer
, "/tmp/errorfile_%d", child
);
80 //printf("ignoring error file: %s\n", errorfile);
83 bool gotErrorFile
= readfile(desired
, errorfile
);
84 bool gotOutput
= readfile(got
, buffer
);
85 if (!gotErrorFile
&& gotOutput
) {
86 printf("didn't read errorfile %s, it should have something from\n*****\n%s\n*****\nin it.\n",
90 else if (gotErrorFile
&& gotOutput
) {
91 char *where
= strstr(got
, desired
);
93 printf("didn't find contents of %s in %s\n", errorfile
, buffer
);
98 printf("errorfile %s and output %s inconsistent\n", errorfile
, buffer
);