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

Side by Side Diff: lib/strong_mode.dart

Issue 1230903002: fixes #6, refactor to use AnalysisError/Listener throughout dev_compiler (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 5 months 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
« no previous file with comments | « lib/src/utils.dart ('k') | test/codegen/expect/temps.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 /// Types needed to implement "strong" checking in the Dart analyzer. 5 /// Types needed to implement "strong" checking in the Dart analyzer.
6 /// This is intended to be used by analyzer_cli and analysis_server packages. 6 /// This is intended to be used by analyzer_cli and analysis_server packages.
7 library dev_compiler.strong_mode; 7 library dev_compiler.strong_mode;
8 8
9 import 'package:analyzer/src/generated/engine.dart' 9 import 'package:analyzer/src/generated/engine.dart'
10 show AnalysisContextImpl, AnalysisErrorInfo, AnalysisErrorInfoImpl; 10 show AnalysisContextImpl, AnalysisErrorInfo, AnalysisErrorInfoImpl;
11 import 'package:analyzer/src/generated/error.dart' 11 import 'package:analyzer/src/generated/error.dart'
12 show 12 show
13 AnalysisError, 13 AnalysisError,
14 AnalysisErrorListener,
15 CompileTimeErrorCode,
14 ErrorCode, 16 ErrorCode,
15 CompileTimeErrorCode, 17 ErrorSeverity,
16 StaticTypeWarningCode, 18 HintCode,
17 HintCode; 19 StaticTypeWarningCode;
18 import 'package:analyzer/src/generated/source.dart' show Source; 20 import 'package:analyzer/src/generated/source.dart' show Source;
19 import 'package:args/args.dart'; 21 import 'package:args/args.dart';
20 import 'package:logging/logging.dart' show Level;
21 22
22 import 'src/checker/checker.dart' show CodeChecker; 23 import 'src/checker/checker.dart' show CodeChecker;
23 import 'src/checker/resolver.dart' show LibraryResolverWithInference; 24 import 'src/checker/resolver.dart' show LibraryResolverWithInference;
24 import 'src/checker/rules.dart' show RestrictedRules; 25 import 'src/checker/rules.dart' show RestrictedRules;
25 import 'src/report.dart' show CheckerReporter, Message;
26 26
27 /// A type checker for Dart code that operates under stronger rules, and has 27 /// A type checker for Dart code that operates under stronger rules, and has
28 /// the ability to do local type inference in some situations. 28 /// the ability to do local type inference in some situations.
29 class StrongChecker { 29 class StrongChecker {
30 final AnalysisContextImpl _context; 30 final AnalysisContextImpl _context;
31 final CodeChecker _checker; 31 final CodeChecker _checker;
32 final _ErrorReporter _reporter; 32 final _ErrorCollector _reporter;
33 final StrongModeOptions _options;
34 33
35 StrongChecker._(this._context, this._options, this._checker, this._reporter); 34 StrongChecker._(this._context, this._checker, this._reporter);
36 35
37 factory StrongChecker( 36 factory StrongChecker(
38 AnalysisContextImpl context, StrongModeOptions options) { 37 AnalysisContextImpl context, StrongModeOptions options) {
39 // TODO(jmesserly): is there a cleaner way to plug this in? 38 // TODO(jmesserly): is there a cleaner way to plug this in?
40 if (context.libraryResolverFactory != null) { 39 if (context.libraryResolverFactory != null) {
41 throw new ArgumentError.value(context, 'context', 40 throw new ArgumentError.value(context, 'context',
42 'Analysis context must not have libraryResolverFactory already set.'); 41 'Analysis context must not have libraryResolverFactory already set.');
43 } 42 }
44 context.libraryResolverFactory = 43 context.libraryResolverFactory =
45 (c) => new LibraryResolverWithInference(c, options); 44 (c) => new LibraryResolverWithInference(c, options);
46 45
47 var rules = new RestrictedRules(context.typeProvider, options: options); 46 var rules = new RestrictedRules(context.typeProvider, options: options);
48 var reporter = new _ErrorReporter(); 47 var reporter = new _ErrorCollector(options.hints);
49 var checker = new CodeChecker(rules, reporter, options); 48 var checker = new CodeChecker(rules, reporter, options);
50 return new StrongChecker._(context, options, checker, reporter); 49 return new StrongChecker._(context, checker, reporter);
51 } 50 }
52 51
53 /// Computes and returns DDC errors for the [source]. 52 /// Computes and returns DDC errors for the [source].
54 AnalysisErrorInfo computeErrors(Source source) { 53 AnalysisErrorInfo computeErrors(Source source) {
55 var errors = new List<AnalysisError>(); 54 var errors = new List<AnalysisError>();
56 55 _reporter.errors = errors;
57 // TODO(jmesserly): change DDC to emit ErrorCodes directly.
58 _reporter._log = (Message msg) {
59 // Skip hints unless requested.
60 if (msg.level < Level.WARNING && !_options.hints) return;
61
62 var errorCodeFactory = _levelToErrorCode[msg.level];
63 var category = '${msg.runtimeType}';
64 var errorCode = errorCodeFactory(category, msg.message);
65 var len = msg.end - msg.begin;
66 errors.add(new AnalysisError(source, msg.begin, len, errorCode));
67 };
68 56
69 for (Source librarySource in _context.getLibrariesContaining(source)) { 57 for (Source librarySource in _context.getLibrariesContaining(source)) {
70 var resolved = _context.resolveCompilationUnit2(source, librarySource); 58 var resolved = _context.resolveCompilationUnit2(source, librarySource);
71 _checker.visitCompilationUnit(resolved); 59 _checker.visitCompilationUnit(resolved);
72 } 60 }
73 _reporter._log = null; 61 _reporter.errors = null;
62
74 return new AnalysisErrorInfoImpl(errors, _context.getLineInfo(source)); 63 return new AnalysisErrorInfoImpl(errors, _context.getLineInfo(source));
75 } 64 }
76 } 65 }
77 66
78 /// Maps a DDC log level to an analyzer ErrorCode subclass. 67 class _ErrorCollector implements AnalysisErrorListener {
79 final _levelToErrorCode = <Level, _ErrorCodeFactory>{ 68 List<AnalysisError> errors;
80 Level.SEVERE: (n, m) => new CompileTimeErrorCode(n, m), 69 final bool hints;
81 Level.WARNING: (n, m) => new StaticTypeWarningCode(n, m), 70 _ErrorCollector(this.hints);
82 Level.INFO: (n, m) => new HintCode(n, m)
83 };
84 71
85 class _ErrorReporter implements CheckerReporter { 72 void onError(AnalysisError error) {
86 _CheckerReporterLog _log; 73 // Unless DDC hints are requested, filter them out.
87 void log(Message message) => _log(message); 74 var HINT = ErrorSeverity.INFO.ordinal;
75 if (hints || error.errorCode.errorSeverity.ordinal > HINT) {
76 errors.add(error);
77 }
78 }
88 } 79 }
89 80
90 class StrongModeOptions { 81 class StrongModeOptions {
91 82
92 /// Whether to infer return types and field types from overridden members. 83 /// Whether to infer return types and field types from overridden members.
93 final bool inferFromOverrides; 84 final bool inferFromOverrides;
94 static const inferFromOverridesDefault = true; 85 static const inferFromOverridesDefault = true;
95 86
96 /// Whether to infer types for consts and fields by looking at initializers on 87 /// Whether to infer types for consts and fields by looking at initializers on
97 /// the RHS. For example, in a constant declaration like: 88 /// the RHS. For example, in a constant declaration like:
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 return inferFromOverrides == s.inferFromOverrides && 180 return inferFromOverrides == s.inferFromOverrides &&
190 inferTransitively == s.inferTransitively && 181 inferTransitively == s.inferTransitively &&
191 onlyInferConstsAndFinalFields == s.onlyInferConstsAndFinalFields && 182 onlyInferConstsAndFinalFields == s.onlyInferConstsAndFinalFields &&
192 inferDownwards == s.inferDownwards && 183 inferDownwards == s.inferDownwards &&
193 relaxedCasts == s.relaxedCasts && 184 relaxedCasts == s.relaxedCasts &&
194 nonnullableTypes.length == s.nonnullableTypes.length && 185 nonnullableTypes.length == s.nonnullableTypes.length &&
195 new Set.from(nonnullableTypes).containsAll(s.nonnullableTypes); 186 new Set.from(nonnullableTypes).containsAll(s.nonnullableTypes);
196 } 187 }
197 } 188 }
198 189
199 typedef void _CheckerReporterLog(Message message);
200 typedef ErrorCode _ErrorCodeFactory(String name, String message);
201
202 List<String> _optionsToList(String option, 190 List<String> _optionsToList(String option,
203 {List<String> defaultValue: const <String>[]}) { 191 {List<String> defaultValue: const <String>[]}) {
204 if (option == null) { 192 if (option == null) {
205 return defaultValue; 193 return defaultValue;
206 } else if (option.isEmpty) { 194 } else if (option.isEmpty) {
207 return <String>[]; 195 return <String>[];
208 } else { 196 } else {
209 return option.split(','); 197 return option.split(',');
210 } 198 }
211 } 199 }
OLDNEW
« no previous file with comments | « lib/src/utils.dart ('k') | test/codegen/expect/temps.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698