| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp.regtests; |
| 18 |
|
| 19 |
import java.io.BufferedReader; |
| 20 |
import java.io.IOException; |
| 21 |
import java.io.InputStreamReader; |
| 22 |
import java.util.logging.Level; |
| 23 |
|
| 24 |
import com.google.javascript.jscomp.CompilationLevel; |
| 25 |
import com.google.javascript.jscomp.Compiler; |
| 26 |
import com.google.javascript.jscomp.CompilerOptions; |
| 27 |
import com.google.javascript.jscomp.SourceFile; |
| 28 |
import com.google.javascript.jscomp.Result; |
| 29 |
import com.google.javascript.jscomp.WarningLevel; |
| 30 |
|
| 31 |
public class CompileEachLineOfProgramOutput { |
| 32 |
private static final SourceFile extern = |
| 33 |
SourceFile.fromCode("externs.js", ""); |
| 34 |
private static final CompilerOptions options = |
| 35 |
new CompilerOptions(); |
| 36 |
|
| 37 |
static { |
| 38 |
CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel( |
| 39 |
options); |
| 40 |
WarningLevel.QUIET.setOptionsForWarningLevel(options); |
| 41 |
Compiler.setLoggingLevel(Level.OFF); |
| 42 |
} |
| 43 |
|
| 44 |
public static void main(String[] args) throws IOException { |
| 45 |
if (args.length == 0){ |
| 46 |
usage(); |
| 47 |
} |
| 48 |
Runtime r = Runtime.getRuntime(); |
| 49 |
Process p = null; |
| 50 |
try { |
| 51 |
p = r.exec(args); |
| 52 |
} catch (IOException e) { |
| 53 |
if (args[0].equals("generatejs")) { |
| 54 |
|
| 55 |
System.out.println("generatejs not found, required for generating " + |
| 56 |
"fuzz test cases"); |
| 57 |
System.out.println("See: http://github.com/rictic/generatejs"); |
| 58 |
System.exit(2); |
| 59 |
} else { |
| 60 |
throw e; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
BufferedReader br = new BufferedReader( |
| 65 |
new InputStreamReader(p.getInputStream())); |
| 66 |
int programsCompiled = 0, compilerErrors = 0; |
| 67 |
for (String program = br.readLine(); program != null; program = |
| 68 |
br.readLine()) { |
| 69 |
try { |
| 70 |
compile(program, programsCompiled); |
| 71 |
} catch(Exception e) { |
| 72 |
System.out.println("Compiler error on program #" + |
| 73 |
programsCompiled + ":"); |
| 74 |
System.out.println(program); |
| 75 |
System.out.println("Details:"); |
| 76 |
e.printStackTrace(System.out); |
| 77 |
System.out.println("\n\n\n"); |
| 78 |
compilerErrors++; |
| 79 |
} |
| 80 |
|
| 81 |
programsCompiled++; |
| 82 |
} |
| 83 |
|
| 84 |
if (compilerErrors == 0){ |
| 85 |
System.out.println(programsCompiled + |
| 86 |
" programs compiled without error"); |
| 87 |
System.exit(0); |
| 88 |
} else { |
| 89 |
System.out.println("==========FAILURE==========="); |
| 90 |
System.out.println(compilerErrors + |
| 91 |
" programs caused an error within the compiler out of " + |
| 92 |
programsCompiled + " tested."); |
| 93 |
System.exit(1); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public static Result compile(String program, int num) { |
| 98 |
SourceFile input = SourceFile.fromCode(""+num, program); |
| 99 |
Compiler compiler = new Compiler(); |
| 100 |
Result result = compiler.compile(extern, input, options); |
| 101 |
return result; |
| 102 |
} |
| 103 |
|
| 104 |
private static void usage() { |
| 105 |
System.out.println( |
| 106 |
"Usage: pass in a program to execute (with arguments)"); |
| 107 |
System.out.println( |
| 108 |
"The program is expected to produce JS programs to stdout, " + |
| 109 |
"one per line"); |
| 110 |
} |
| 111 |
} |