Update the way llvm2cpp tests are done:
[oota-llvm.git] / test / lib / llvm2cpp.exp
1 # This file defines a tcl proc to assist with testing the llvm2cpp. There are
2 # no llvm2cpp specific test cases. Instead, it utilizes all the existing test
3 # cases and makes sure llvm2cpp can run them. The basic idea is that we find
4 # all the LLVM Assembly (*.ll) files, run llvm2cpp on them to generate a C++
5 # program, compile those programs, run them and see if what they produce matches
6 # the original input to llvm2cpp.
7
8 proc llvm2cpp-test { files } {
9   global subdir llvmtoolsdir llvmlibsdir objdir srcdir objroot srcroot 
10   set timeout 30
11   set path [file join $objdir $subdir]
12   set llvm2cpp [file join $llvmtoolsdir llvm2cpp ]
13   set llvmas [file join $llvmtoolsdir llvm-as ]
14   set llvmdis [file join $llvmtoolsdir llvm-dis ]
15   set llvmupgrade [ file join $llvmtoolsdir llvm-upgrade ]
16
17   #Make Output Directory if it does not exist already
18   if { [file exists path] } {
19       cd $path
20   } else {
21       file mkdir $path
22       cd $path
23   }
24   
25   file mkdir Output
26
27   foreach test $files {
28       
29     set filename [file tail $test]
30     set generated [file join Output $filename.cpp]
31     set executable [file join Output $filename.exe]
32     set output [file join Output $filename.gen]
33     set assembly [file join Output $filename.asm]
34     set testname [file rootname $filename]
35     set bytecode [file join Output $filename.bc]
36
37     # Note that the stderr for llvm-as, etc. must be redirected to /dev/null 
38     # because otherwise exec will see the msgs and return 1 even though they 
39     # are only warnings. If real errors are generated on stderr then llvm-as 
40     # will return a non-zero retval anyway so we're good.
41
42     # Scan the test file to see if there's an XFAIL file. If so, don't run it
43     set retval [ catch { 
44       exec -keepnewline grep XFAIL $test 2>/dev/null } msg ]
45     if { $retval == 0 } {
46       continue;
47     }
48
49     # Scan the test file to see if there's a line with "lvm-upgrade" in it. 
50     # If so, run llvm-upgrade first or else llvm-as will fail on it.
51     set retval [ catch { 
52       exec -keepnewline grep llvm-upgrade $test 2>/dev/null } msg ]
53
54     if { $retval == 0 } {
55       # In this case we must run llvm-upgrade before llvm-as
56       set pipeline llvm-upgrade|llvm-as|llvm-dis
57       set retval [ catch { 
58         exec -keepnewline $llvmupgrade < $test -o - | $llvmas | $llvmdis -f -o $assembly 2>/dev/null } msg ]
59     } else {
60       # llvm-upgrade not necessary, just llvm-as/llvm-dis
61       set pipeline llvm-as|llvm-dis
62       set retval [ catch { 
63         exec -keepnewline $llvmas < $test -o - | $llvmdis -f -o $assembly 2>/dev/null } msg ]
64     }
65
66     if { $retval != 0 } {
67       fail "$test: $pipeline returned $retval\n$msg"
68       continue 
69     }
70
71     # Build bytecode for llvm2cpp input
72     set retval [ catch { 
73       exec -keepnewline $llvmas < $assembly > $bytecode 2>/dev/null } msg ]
74
75     if { $retval != 0 } {
76       fail "$test: llvm-as returned $retval\n$msg"
77       continue 
78     }
79
80     set retval [ catch { 
81       exec -keepnewline $llvm2cpp -f -o $generated < $bytecode 2>/dev/null } msg]
82
83     if { $retval != 0 } {
84       fail "$test: llvm2cpp returned $retval\n$msg"
85       continue
86     }
87
88     set retval [ catch { 
89       exec -keepnewline gcc -g -D__STDC_LIMIT_MACROS -o $executable $generated -I$srcroot/include -I$objroot/include -L$llvmlibsdir -lLLVMCore -lLLVMSupport -lLLVMbzip2 -lLLVMSystem -lstdc++ } msg ] 
90     if { $retval != 0 } {
91       fail "$test: gcc returned $retval\n$msg"
92       continue
93     }
94
95     set retval [ catch { exec -keepnewline $executable > $output } msg ]
96     if { $retval != 0 } {
97       set execname [file tail $executable]
98       fail "$test: $execname returned $retval:\n$msg"
99       continue
100     } 
101
102     set retval [ catch { 
103       exec -keepnewline diff $assembly $output } msg ]
104
105     if { $retval != 0 } {
106       fail "$test: diff returned $retval:\n$msg"
107       continue
108     }
109     pass "$test"
110   }
111 }
112
113