Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(423)

Side by Side Diff: pkg/dev_compiler/lib/src/compiler/compiler.dart

Issue 2598593003: support --options flag and other analysis options flags in DDC (Closed)
Patch Set: add missing tests Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 import 'dart:collection' show HashSet, Queue; 5 import 'dart:collection' show HashSet, Queue;
6 import 'dart:convert' show BASE64, JSON, UTF8; 6 import 'dart:convert' show BASE64, JSON, UTF8;
7 import 'dart:io' show File; 7 import 'dart:io' show File;
8 import 'package:analyzer/dart/element/element.dart' show LibraryElement; 8 import 'package:analyzer/dart/element/element.dart' show LibraryElement;
9 import 'package:analyzer/analyzer.dart' 9 import 'package:analyzer/analyzer.dart'
10 show AnalysisError, CompilationUnit, ErrorSeverity; 10 show AnalysisError, CompilationUnit, ErrorSeverity;
11 import 'package:analyzer/file_system/file_system.dart' show ResourceProvider; 11 import 'package:analyzer/file_system/file_system.dart' show ResourceProvider;
12 import 'package:analyzer/file_system/physical_file_system.dart'
13 show PhysicalResourceProvider;
14 import 'package:analyzer/src/context/builder.dart' show ContextBuilder;
15 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl;
12 import 'package:analyzer/src/generated/engine.dart' 16 import 'package:analyzer/src/generated/engine.dart'
13 show AnalysisContext, AnalysisEngine; 17 show AnalysisContext, AnalysisEngine, AnalysisOptionsImpl;
14 import 'package:analyzer/src/generated/source.dart' show DartUriResolver; 18 import 'package:analyzer/src/generated/sdk.dart' show DartSdkManager;
19 import 'package:analyzer/src/generated/source.dart'
20 show ContentCache, DartUriResolver;
15 import 'package:analyzer/src/generated/source_io.dart' 21 import 'package:analyzer/src/generated/source_io.dart'
16 show Source, SourceKind, UriResolver; 22 show Source, SourceKind, UriResolver;
17 import 'package:analyzer/src/summary/package_bundle_reader.dart' 23 import 'package:analyzer/src/summary/package_bundle_reader.dart'
18 show InSummarySource, InputPackagesResultProvider, SummaryDataStore; 24 show InSummarySource, InputPackagesResultProvider, SummaryDataStore;
19 import 'package:analyzer/src/error/codes.dart' show StaticTypeWarningCode; 25 import 'package:analyzer/src/error/codes.dart' show StaticTypeWarningCode;
20 import 'package:args/args.dart' show ArgParser, ArgResults; 26 import 'package:args/args.dart' show ArgParser, ArgResults;
21 import 'package:args/src/usage_exception.dart' show UsageException; 27 import 'package:args/src/usage_exception.dart' show UsageException;
22 import 'package:func/func.dart' show Func1; 28 import 'package:func/func.dart' show Func1;
23 import 'package:path/path.dart' as path; 29 import 'package:path/path.dart' as path;
24 import 'package:source_maps/source_maps.dart'; 30 import 'package:source_maps/source_maps.dart';
25 31
26 import '../analyzer/context.dart' 32 import '../analyzer/context.dart' show AnalyzerOptions, createSourceFactory;
27 show
28 AnalyzerOptions,
29 createAnalysisContext,
30 createSdkPathResolver,
31 createSourceFactory;
32 import '../js_ast/js_ast.dart' as JS; 33 import '../js_ast/js_ast.dart' as JS;
33 import 'code_generator.dart' show CodeGenerator; 34 import 'code_generator.dart' show CodeGenerator;
34 import 'error_helpers.dart' show errorSeverity, formatError, sortErrors; 35 import 'error_helpers.dart' show errorSeverity, formatError, sortErrors;
35 import 'extension_types.dart' show ExtensionTypeSet; 36 import 'extension_types.dart' show ExtensionTypeSet;
36 import 'js_names.dart' as JS; 37 import 'js_names.dart' as JS;
37 import 'module_builder.dart' show transformModuleFormat, ModuleFormat; 38 import 'module_builder.dart' show transformModuleFormat, ModuleFormat;
38 import 'source_map_printer.dart' show SourceMapPrintingContext; 39 import 'source_map_printer.dart' show SourceMapPrintingContext;
39 40
40 /// Compiles a set of Dart files into a single JavaScript module. 41 /// Compiles a set of Dart files into a single JavaScript module.
41 /// 42 ///
(...skipping 10 matching lines...) Expand all
52 /// 53 ///
53 /// For all other files, it is up to the [AnalysisContext] to decide whether or 54 /// For all other files, it is up to the [AnalysisContext] to decide whether or
54 /// not any caching is performed. By default an analysis context will assume 55 /// not any caching is performed. By default an analysis context will assume
55 /// sources are immutable for the life of the context, and cache information 56 /// sources are immutable for the life of the context, and cache information
56 /// about them. 57 /// about them.
57 class ModuleCompiler { 58 class ModuleCompiler {
58 final AnalysisContext context; 59 final AnalysisContext context;
59 final SummaryDataStore summaryData; 60 final SummaryDataStore summaryData;
60 final ExtensionTypeSet _extensionTypes; 61 final ExtensionTypeSet _extensionTypes;
61 62
62 ModuleCompiler.withContext(AnalysisContext context, this.summaryData) 63 ModuleCompiler._(AnalysisContext context, this.summaryData)
63 : context = context, 64 : context = context,
64 _extensionTypes = new ExtensionTypeSet(context) { 65 _extensionTypes = new ExtensionTypeSet(context);
65 if (!context.analysisOptions.strongMode) {
66 throw new ArgumentError('AnalysisContext must be strong mode');
67 }
68 if (!context.sourceFactory.dartSdk.context.analysisOptions.strongMode) {
69 throw new ArgumentError('AnalysisContext must have strong mode SDK');
70 }
71 }
72 66
73 factory ModuleCompiler(AnalyzerOptions options, 67 factory ModuleCompiler(String projRoot, AnalyzerOptions options,
74 {DartUriResolver sdkResolver, 68 {ResourceProvider resourceProvider, List<UriResolver> fileResolvers}) {
75 ResourceProvider resourceProvider,
76 List<UriResolver> fileResolvers}) {
77 AnalysisEngine.instance.processRequiredPlugins(); 69 AnalysisEngine.instance.processRequiredPlugins();
78 70
79 sdkResolver ??= 71 resourceProvider ??= PhysicalResourceProvider.INSTANCE;
80 createSdkPathResolver(options.dartSdkSummaryPath, options.dartSdkPath); 72
73 var contextBuilder = new ContextBuilder(resourceProvider,
74 new DartSdkManager(options.dartSdkPath, true), new ContentCache(),
75 options: options.contextBuilderOptions);
76
77 var analysisOptions = contextBuilder.getAnalysisOptions(projRoot);
vsm 2017/01/03 17:56:28 Hmm, looks like this is the library-root being pas
danrubel 2017/01/03 18:51:48 Correct. It will start looking in the projRoot dir
78 (analysisOptions as AnalysisOptionsImpl)
79 ..strongMode = true
vsm 2017/01/03 17:56:28 Why do we need to set this here and in the context
danrubel 2017/01/03 18:51:48 Hmmm... good point. Removed. And I moved trackCac
80 ..trackCacheDependencies = false;
81 var sdk = contextBuilder.findSdk(null, analysisOptions);
82
83 var sdkResolver = new DartUriResolver(sdk);
81 84
82 // Read the summaries. 85 // Read the summaries.
83 var summaryData = 86 var summaryData =
84 new SummaryDataStore(options.summaryPaths, recordDependencyInfo: true); 87 new SummaryDataStore(options.summaryPaths, recordDependencyInfo: true);
85 88
86 var srcFactory = createSourceFactory(options, 89 var srcFactory = createSourceFactory(options,
87 sdkResolver: sdkResolver, 90 sdkResolver: sdkResolver,
88 fileResolvers: fileResolvers, 91 fileResolvers: fileResolvers,
89 summaryData: summaryData, 92 summaryData: summaryData,
90 resourceProvider: resourceProvider); 93 resourceProvider: resourceProvider);
91 94
92 var context = createAnalysisContext(); 95 var context =
96 AnalysisEngine.instance.createAnalysisContext() as AnalysisContextImpl;
97 context.analysisOptions = analysisOptions;
93 context.sourceFactory = srcFactory; 98 context.sourceFactory = srcFactory;
94 context.typeProvider = sdkResolver.dartSdk.context.typeProvider; 99 context.typeProvider = sdkResolver.dartSdk.context.typeProvider;
95 context.resultProvider = 100 context.resultProvider =
96 new InputPackagesResultProvider(context, summaryData); 101 new InputPackagesResultProvider(context, summaryData);
97 options.declaredVariables.forEach(context.declaredVariables.define); 102 options.declaredVariables.forEach(context.declaredVariables.define);
98 context.declaredVariables.define('dart.isVM', 'false'); 103 context.declaredVariables.define('dart.isVM', 'false');
99 104
100 // TODO(vsm): Should this be hardcoded? 105 // TODO(vsm): Should this be hardcoded?
101 context.declaredVariables.define('dart.library.html', 'true'); 106 context.declaredVariables.define('dart.library.html', 'true');
102 context.declaredVariables.define('dart.library.io', 'false'); 107 context.declaredVariables.define('dart.library.io', 'false');
103 108
104 return new ModuleCompiler.withContext(context, summaryData); 109 if (!context.analysisOptions.strongMode) {
110 throw new ArgumentError('AnalysisContext must be strong mode');
111 }
112 if (!context.sourceFactory.dartSdk.context.analysisOptions.strongMode) {
113 throw new ArgumentError('AnalysisContext must have strong mode SDK');
114 }
115
116 return new ModuleCompiler._(context, summaryData);
105 } 117 }
106 118
107 bool _isFatalError(AnalysisError e, CompilerOptions options) { 119 bool _isFatalError(AnalysisError e, CompilerOptions options) {
108 if (errorSeverity(context, e) != ErrorSeverity.ERROR) return false; 120 if (errorSeverity(context, e) != ErrorSeverity.ERROR) return false;
109 121
110 // These errors are not fatal in the REPL compile mode as we 122 // These errors are not fatal in the REPL compile mode as we
111 // allow access to private members across library boundaries 123 // allow access to private members across library boundaries
112 // and those accesses will show up as undefined members unless 124 // and those accesses will show up as undefined members unless
113 // additional analyzer changes are made to support them. 125 // additional analyzer changes are made to support them.
114 // TODO(jacobr): consider checking that the identifier name 126 // TODO(jacobr): consider checking that the identifier name
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 // Fall back to a relative path. 570 // Fall back to a relative path.
559 return path.toUri(path.relative(path.fromUri(uri), from: dir)).toString(); 571 return path.toUri(path.relative(path.fromUri(uri), from: dir)).toString();
560 } 572 }
561 573
562 for (int i = 0; i < list.length; i++) { 574 for (int i = 0; i < list.length; i++) {
563 list[i] = transformUri(list[i]); 575 list[i] = transformUri(list[i]);
564 } 576 }
565 map['file'] = transformUri(map['file']); 577 map['file'] = transformUri(map['file']);
566 return map; 578 return map;
567 } 579 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698