2 // The LLVM Compiler Infrastructure
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
11 * Created by Blaine Garst on 9/16/08.
22 bool readfile(char *buffer
, const char *from
) {
23 int fd
= open(from
, 0);
24 if (fd
< 0) return false;
25 int count
= read(fd
, buffer
, 512);
26 if (count
< 0) return false;
27 buffer
[count
] = 0; // zap newline
31 // basic idea, take compiler args, run compiler, and verify that expected failure matches any existing one
33 int main(int argc
, char *argv
[]) {
34 if (argc
== 1) return 0;
35 char *copy
[argc
+1]; // make a copy
36 // find and strip off -e "errorfile"
37 char *errorfile
= NULL
;
38 int counter
= 0, i
= 0;
39 for (i
= 1; i
< argc
; ++i
) { // skip 0 arg which is "fail"
40 if (!strncmp(argv
[i
], "-e", 2)) {
41 errorfile
= argv
[++i
];
44 copy
[counter
++] = argv
[i
];
52 sprintf(buffer
, "/tmp/errorfile_%d", getpid());
54 int fd
= creat(buffer
, 0777);
56 fprintf(stderr
, "didn't open custom error file %s as 1, got %d\n", buffer
, fd
);
61 int result
= execv(copy
[0], copy
);
65 printf("fork failed\n");
69 pid_t deadchild
= wait(&status
);
70 if (deadchild
!= child
) {
71 printf("wait got %d instead of %d\n", deadchild
, child
);
74 if (WEXITSTATUS(status
) == 0) {
75 printf("compiler exited normally, not good under these circumstances\n");
78 //printf("exit status of child %d was %d\n", child, WEXITSTATUS(status));
79 sprintf(buffer
, "/tmp/errorfile_%d", child
);
81 //printf("ignoring error file: %s\n", errorfile);
84 bool gotErrorFile
= readfile(desired
, errorfile
);
85 bool gotOutput
= readfile(got
, buffer
);
86 if (!gotErrorFile
&& gotOutput
) {
87 printf("didn't read errorfile %s, it should have something from\n*****\n%s\n*****\nin it.\n",
91 else if (gotErrorFile
&& gotOutput
) {
92 char *where
= strstr(got
, desired
);
94 printf("didn't find contents of %s in %s\n", errorfile
, buffer
);
99 printf("errorfile %s and output %s inconsistent\n", errorfile
, buffer
);