OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart2js.test.memory_compiler; | 5 library dart2js.test.memory_compiler; |
6 | 6 |
7 import 'memory_source_file_helper.dart'; | 7 import 'memory_source_file_helper.dart'; |
8 | 8 |
9 import 'package:compiler/src/compile_time_constants.dart'; | |
10 | 9 |
11 import 'package:compiler/src/dart2jslib.dart' | 10 import 'package:compiler/src/null_compiler_output.dart' |
12 show NullSink; | 11 show NullCompilerOutput; |
13 | 12 |
14 import 'package:compiler/compiler.dart' show | 13 import 'package:compiler/compiler.dart' show |
15 CompilerOutputProvider, | 14 DiagnosticHandler; |
| 15 |
| 16 import 'package:compiler/compiler_new.dart' show |
| 17 CompilerDiagnostics, |
| 18 CompilerOutput, |
16 Diagnostic, | 19 Diagnostic, |
17 DiagnosticHandler, | |
18 PackagesDiscoveryProvider; | 20 PackagesDiscoveryProvider; |
19 | 21 |
20 import 'dart:async'; | 22 import 'dart:async'; |
21 | 23 |
22 import 'package:compiler/src/mirrors/source_mirrors.dart'; | 24 import 'package:compiler/src/mirrors/source_mirrors.dart'; |
23 import 'package:compiler/src/mirrors/analyze.dart'; | 25 import 'package:compiler/src/mirrors/analyze.dart'; |
24 | 26 |
25 import 'package:compiler/src/library_loader.dart' | 27 import 'package:compiler/src/library_loader.dart' |
26 show LoadedLibraries; | 28 show LoadedLibraries; |
27 | 29 |
| 30 import 'package:compiler/src/old_to_new_api.dart'; |
| 31 |
28 export 'output_collector.dart'; | 32 export 'output_collector.dart'; |
29 | 33 |
30 class DiagnosticMessage { | 34 class DiagnosticMessage { |
31 final Uri uri; | 35 final Uri uri; |
32 final int begin; | 36 final int begin; |
33 final int end; | 37 final int end; |
34 final String message; | 38 final String message; |
35 final Diagnostic kind; | 39 final Diagnostic kind; |
36 | 40 |
37 DiagnosticMessage(this.uri, this.begin, this.end, this.message, this.kind); | 41 DiagnosticMessage(this.uri, this.begin, this.end, this.message, this.kind); |
38 | 42 |
39 String toString() => '$uri:$begin:$end:$message:$kind'; | 43 String toString() => '$uri:$begin:$end:$message:$kind'; |
40 } | 44 } |
41 | 45 |
42 class DiagnosticCollector { | 46 class DiagnosticCollector implements CompilerDiagnostics { |
43 List<DiagnosticMessage> messages = <DiagnosticMessage>[]; | 47 List<DiagnosticMessage> messages = <DiagnosticMessage>[]; |
44 | 48 |
45 void call(Uri uri, int begin, int end, String message, | 49 void call(Uri uri, int begin, int end, String message, Diagnostic kind) { |
46 Diagnostic kind) { | 50 reportDiagnostic(uri, begin, end, message, kind); |
| 51 } |
| 52 |
| 53 @override |
| 54 void reportDiagnostic(Uri uri, int begin, int end, String message, |
| 55 Diagnostic kind) { |
47 messages.add(new DiagnosticMessage(uri, begin, end, message, kind)); | 56 messages.add(new DiagnosticMessage(uri, begin, end, message, kind)); |
48 } | 57 } |
49 | 58 |
50 Iterable<DiagnosticMessage> filterMessagesByKind(Diagnostic kind) { | 59 Iterable<DiagnosticMessage> filterMessagesByKind(Diagnostic kind) { |
51 return messages.where( | 60 return messages.where( |
52 (DiagnosticMessage message) => message.kind == kind); | 61 (DiagnosticMessage message) => message.kind == kind); |
53 } | 62 } |
54 | 63 |
55 Iterable<DiagnosticMessage> get errors { | 64 Iterable<DiagnosticMessage> get errors { |
56 return filterMessagesByKind(Diagnostic.ERROR); | 65 return filterMessagesByKind(Diagnostic.ERROR); |
57 } | 66 } |
58 | 67 |
59 Iterable<DiagnosticMessage> get warnings { | 68 Iterable<DiagnosticMessage> get warnings { |
60 return filterMessagesByKind(Diagnostic.WARNING); | 69 return filterMessagesByKind(Diagnostic.WARNING); |
61 } | 70 } |
62 | 71 |
63 Iterable<DiagnosticMessage> get hints { | 72 Iterable<DiagnosticMessage> get hints { |
64 return filterMessagesByKind(Diagnostic.HINT); | 73 return filterMessagesByKind(Diagnostic.HINT); |
65 } | 74 } |
66 | 75 |
67 Iterable<DiagnosticMessage> get infos { | 76 Iterable<DiagnosticMessage> get infos { |
68 return filterMessagesByKind(Diagnostic.INFO); | 77 return filterMessagesByKind(Diagnostic.INFO); |
69 } | 78 } |
| 79 |
| 80 /// `true` if non-verbose messages has been collected. |
| 81 bool get hasRegularMessages { |
| 82 return messages.any((m) => m.kind != Diagnostic.VERBOSE_INFO); |
| 83 } |
70 } | 84 } |
71 | 85 |
72 DiagnosticHandler createDiagnosticHandler(DiagnosticHandler diagnosticHandler, | 86 class MultiDiagnostics implements CompilerDiagnostics { |
73 SourceFileProvider provider, | 87 final List<CompilerDiagnostics> diagnosticsList; |
74 bool showDiagnostics) { | 88 |
75 var handler = diagnosticHandler; | 89 const MultiDiagnostics([this.diagnosticsList = const []]); |
| 90 |
| 91 @override |
| 92 void reportDiagnostic(Uri uri, int begin, int end, |
| 93 String message, Diagnostic kind) { |
| 94 for (CompilerDiagnostics diagnostics in diagnosticsList) { |
| 95 diagnostics.reportDiagnostic(uri, begin, end, message, kind); |
| 96 } |
| 97 } |
| 98 } |
| 99 |
| 100 CompilerDiagnostics createCompilerDiagnostics( |
| 101 CompilerDiagnostics diagnostics, |
| 102 SourceFileProvider provider, |
| 103 bool showDiagnostics) { |
| 104 CompilerDiagnostics handler = diagnostics; |
76 if (showDiagnostics) { | 105 if (showDiagnostics) { |
77 if (diagnosticHandler == null) { | 106 if (diagnostics == null) { |
78 handler = new FormattingDiagnosticHandler(provider); | 107 handler = new FormattingDiagnosticHandler(provider); |
79 } else { | 108 } else { |
80 var formattingHandler = new FormattingDiagnosticHandler(provider); | 109 var formattingHandler = new FormattingDiagnosticHandler(provider); |
81 handler = (Uri uri, int begin, int end, String message, Diagnostic kind) { | 110 handler = new MultiDiagnostics([diagnostics, formattingHandler]); |
82 diagnosticHandler(uri, begin, end, message, kind); | |
83 formattingHandler(uri, begin, end, message, kind); | |
84 }; | |
85 } | 111 } |
86 } else if (diagnosticHandler == null) { | 112 } else if (diagnostics == null) { |
87 handler = (Uri uri, int begin, int end, String message, Diagnostic kind) {}; | 113 handler = new MultiDiagnostics(); |
88 } | 114 } |
89 return handler; | 115 return handler; |
90 } | 116 } |
91 | 117 |
92 Expando<MemorySourceFileProvider> expando = | 118 Expando<MemorySourceFileProvider> expando = |
93 new Expando<MemorySourceFileProvider>(); | 119 new Expando<MemorySourceFileProvider>(); |
94 | 120 |
95 Compiler compilerFor( | 121 Compiler compilerFor( |
96 Map<String, String> memorySourceFiles, | 122 Map<String, String> memorySourceFiles, |
97 {DiagnosticHandler diagnosticHandler, | 123 {CompilerDiagnostics diagnosticHandler, |
98 CompilerOutputProvider outputProvider, | 124 CompilerOutput outputProvider, |
99 List<String> options: const [], | 125 List<String> options: const [], |
100 Compiler cachedCompiler, | 126 Compiler cachedCompiler, |
101 bool showDiagnostics: true, | 127 bool showDiagnostics: true, |
102 Uri packageRoot, | 128 Uri packageRoot, |
103 Uri packageConfig, | 129 Uri packageConfig, |
104 PackagesDiscoveryProvider packagesDiscoveryProvider}) { | 130 PackagesDiscoveryProvider packagesDiscoveryProvider}) { |
105 Uri libraryRoot = Uri.base.resolve('sdk/'); | 131 Uri libraryRoot = Uri.base.resolve('sdk/'); |
106 if (packageRoot == null && | 132 if (packageRoot == null && |
107 packageConfig == null && | 133 packageConfig == null && |
108 packagesDiscoveryProvider == null) { | 134 packagesDiscoveryProvider == null) { |
109 packageRoot = Uri.base.resolveUri(new Uri.file('${Platform.packageRoot}/')); | 135 packageRoot = Uri.base.resolveUri(new Uri.file('${Platform.packageRoot}/')); |
110 } | 136 } |
111 | 137 |
112 MemorySourceFileProvider provider; | 138 MemorySourceFileProvider provider; |
113 var readStringFromUri; | |
114 if (cachedCompiler == null) { | 139 if (cachedCompiler == null) { |
115 provider = new MemorySourceFileProvider(memorySourceFiles); | 140 provider = new MemorySourceFileProvider(memorySourceFiles); |
116 readStringFromUri = provider.readStringFromUri; | |
117 // Saving the provider in case we need it later for a cached compiler. | 141 // Saving the provider in case we need it later for a cached compiler. |
118 expando[readStringFromUri] = provider; | 142 expando[provider] = provider; |
119 } else { | 143 } else { |
120 // When using a cached compiler, it has read a number of files from disk | 144 // When using a cached compiler, it has read a number of files from disk |
121 // already (and will not attempt to read them again due to caching). These | 145 // already (and will not attempt to read them again due to caching). These |
122 // files must be available to the new diagnostic handler. | 146 // files must be available to the new diagnostic handler. |
123 provider = expando[cachedCompiler.provider]; | 147 provider = expando[cachedCompiler.provider]; |
124 readStringFromUri = cachedCompiler.provider; | |
125 provider.memorySourceFiles = memorySourceFiles; | 148 provider.memorySourceFiles = memorySourceFiles; |
126 } | 149 } |
127 var handler = | 150 diagnosticHandler = |
128 createDiagnosticHandler(diagnosticHandler, provider, showDiagnostics); | 151 createCompilerDiagnostics(diagnosticHandler, provider, showDiagnostics); |
129 | 152 |
130 EventSink<String> noOutputProvider(String name, String extension) { | |
131 if (name != '') throw 'Attempt to output file "$name.$extension"'; | |
132 return new NullSink('$name.$extension'); | |
133 } | |
134 if (outputProvider == null) { | 153 if (outputProvider == null) { |
135 outputProvider = noOutputProvider; | 154 outputProvider = const NullCompilerOutput(); |
136 } | 155 } |
137 | 156 |
138 Compiler compiler = new Compiler( | 157 Compiler compiler = new Compiler( |
139 readStringFromUri, | 158 provider, |
140 outputProvider, | 159 outputProvider, |
141 handler, | 160 diagnosticHandler, |
142 libraryRoot, | 161 libraryRoot, |
143 packageRoot, | 162 packageRoot, |
144 options, | 163 options, |
145 {}, | 164 {}, |
146 packageConfig, | 165 packageConfig, |
147 packagesDiscoveryProvider); | 166 packagesDiscoveryProvider); |
148 | 167 |
149 if (cachedCompiler != null) { | 168 if (cachedCompiler != null) { |
150 compiler.coreLibrary = | 169 compiler.coreLibrary = |
151 cachedCompiler.libraryLoader.lookupLibrary(Uri.parse('dart:core')); | 170 cachedCompiler.libraryLoader.lookupLibrary(Uri.parse('dart:core')); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 @override | 248 @override |
230 void forEachLibrary(f) {} | 249 void forEachLibrary(f) {} |
231 | 250 |
232 @override | 251 @override |
233 getLibrary(Uri uri) => copiedLibraries[uri]; | 252 getLibrary(Uri uri) => copiedLibraries[uri]; |
234 | 253 |
235 @override | 254 @override |
236 Uri get rootUri => null; | 255 Uri get rootUri => null; |
237 } | 256 } |
238 | 257 |
| 258 |
| 259 DiagnosticHandler createDiagnosticHandler(DiagnosticHandler diagnosticHandler, |
| 260 SourceFileProvider provider, |
| 261 bool showDiagnostics) { |
| 262 var handler = diagnosticHandler; |
| 263 if (showDiagnostics) { |
| 264 if (diagnosticHandler == null) { |
| 265 handler = new FormattingDiagnosticHandler(provider); |
| 266 } else { |
| 267 var formattingHandler = new FormattingDiagnosticHandler(provider); |
| 268 handler = (Uri uri, int begin, int end, String message, Diagnostic kind) { |
| 269 diagnosticHandler(uri, begin, end, message, kind); |
| 270 formattingHandler(uri, begin, end, message, kind); |
| 271 }; |
| 272 } |
| 273 } else if (diagnosticHandler == null) { |
| 274 handler = (Uri uri, int begin, int end, String message, Diagnostic kind) {}; |
| 275 } |
| 276 return handler; |
| 277 } |
| 278 |
239 Future<MirrorSystem> mirrorSystemFor(Map<String,String> memorySourceFiles, | 279 Future<MirrorSystem> mirrorSystemFor(Map<String,String> memorySourceFiles, |
240 {DiagnosticHandler diagnosticHandler, | 280 {DiagnosticHandler diagnosticHandler, |
241 List<String> options: const [], | 281 List<String> options: const [], |
242 bool showDiagnostics: true}) { | 282 bool showDiagnostics: true}) { |
243 Uri libraryRoot = Uri.base.resolve('sdk/'); | 283 Uri libraryRoot = Uri.base.resolve('sdk/'); |
244 Uri packageRoot = Uri.base.resolveUri( | 284 Uri packageRoot = Uri.base.resolveUri( |
245 new Uri.file('${Platform.packageRoot}/')); | 285 new Uri.file('${Platform.packageRoot}/')); |
246 | 286 |
247 var provider = new MemorySourceFileProvider(memorySourceFiles); | 287 var provider = new MemorySourceFileProvider(memorySourceFiles); |
248 var handler = | 288 var handler = |
249 createDiagnosticHandler(diagnosticHandler, provider, showDiagnostics); | 289 createDiagnosticHandler(diagnosticHandler, provider, showDiagnostics); |
250 | 290 |
251 List<Uri> libraries = <Uri>[]; | 291 List<Uri> libraries = <Uri>[]; |
252 memorySourceFiles.forEach((String path, _) { | 292 memorySourceFiles.forEach((String path, _) { |
253 libraries.add(new Uri(scheme: 'memory', path: path)); | 293 libraries.add(new Uri(scheme: 'memory', path: path)); |
254 }); | 294 }); |
255 | 295 |
256 return analyze(libraries, libraryRoot, packageRoot, | 296 return analyze(libraries, libraryRoot, packageRoot, |
257 provider, handler, options); | 297 provider, handler, options); |
258 } | 298 } |
OLD | NEW |