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 analyze_helper; | 5 library analyze_helper; |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import '../../../sdk/lib/_internal/compiler/compiler.dart' as api; | 9 import '../../../sdk/lib/_internal/compiler/compiler.dart' as api; |
10 import '../../../sdk/lib/_internal/compiler/implementation/apiimpl.dart'; | 10 import '../../../sdk/lib/_internal/compiler/implementation/apiimpl.dart'; |
11 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart' | 11 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart' |
12 hide Compiler; | 12 hide Compiler; |
13 import '../../../sdk/lib/_internal/compiler/implementation/filenames.dart'; | 13 import '../../../sdk/lib/_internal/compiler/implementation/filenames.dart'; |
14 import '../../../sdk/lib/_internal/compiler/implementation/source_file_provider.
dart'; | 14 import '../../../sdk/lib/_internal/compiler/implementation/source_file_provider.
dart'; |
15 | 15 |
16 /** | 16 /** |
17 * Map of white-listed warnings and errors. | 17 * Map of whitelisted warnings and errors. |
18 * | 18 * |
19 * Only add a white-listing together with a bug report to dartbug.com and add | 19 * Only add a whitelisting together with a bug report to dartbug.com and add |
20 * the bug issue number as a comment on the white-listing. | 20 * the bug issue number as a comment on the whitelisting. |
21 * | 21 * |
22 * Use an identifiable suffix of the file uri as key. Use a fixed substring of | 22 * Use an identifiable suffix of the file uri as key. Use a fixed substring of |
23 * the error/warning message in the list of white-listings for each file. | 23 * the error/warning message in the list of whitelistings for each file. |
24 */ | 24 */ |
25 // TODO(johnniwinther): Support canonical URIs as keys and message kinds as | 25 // TODO(johnniwinther): Support canonical URIs as keys and message kinds as |
26 // values. | 26 // values. |
27 | 27 |
28 class CollectingDiagnosticHandler extends FormattingDiagnosticHandler { | 28 class CollectingDiagnosticHandler extends FormattingDiagnosticHandler { |
29 bool hasWarnings = false; | 29 bool hasWarnings = false; |
| 30 bool hasHint = false; |
30 bool hasErrors = false; | 31 bool hasErrors = false; |
31 | 32 |
32 Map<String, Map<String, int>> whiteListMap | 33 Map<String, Map<String, int>> whiteListMap |
33 = new Map<String, Map<String, int>>(); | 34 = new Map<String, Map<String, int>>(); |
34 | 35 |
35 CollectingDiagnosticHandler(Map<String, List<String>> whiteList, | 36 CollectingDiagnosticHandler(Map<String, List<String>> whiteList, |
36 SourceFileProvider provider) | 37 SourceFileProvider provider) |
37 : super(provider) { | 38 : super(provider) { |
38 whiteList.forEach((String file, List<String> messageParts) { | 39 whiteList.forEach((String file, List<String> messageParts) { |
39 var useMap = new Map<String,int>(); | 40 var useMap = new Map<String,int>(); |
40 for (String messagePart in messageParts) { | 41 for (String messagePart in messageParts) { |
41 useMap[messagePart] = 0; | 42 useMap[messagePart] = 0; |
42 } | 43 } |
43 whiteListMap[file] = useMap; | 44 whiteListMap[file] = useMap; |
44 }); | 45 }); |
45 } | 46 } |
46 | 47 |
47 void checkResults() { | 48 void checkResults() { |
48 Expect.isFalse(hasWarnings); | 49 Expect.isFalse(hasWarnings); |
| 50 Expect.isFalse(hasHint); |
49 Expect.isFalse(hasErrors); | 51 Expect.isFalse(hasErrors); |
50 Expect.isTrue(checkWhiteListUse()); | 52 Expect.isTrue(checkWhiteListUse()); |
51 reportWhiteListUse(); | 53 reportWhiteListUse(); |
52 } | 54 } |
53 | 55 |
54 bool checkWhiteListUse() { | 56 bool checkWhiteListUse() { |
55 bool allUsed = true; | 57 bool allUsed = true; |
56 for (String file in whiteListMap.keys) { | 58 for (String file in whiteListMap.keys) { |
57 for (String messagePart in whiteListMap[file].keys) { | 59 for (String messagePart in whiteListMap[file].keys) { |
58 if (whiteListMap[file][messagePart] == 0) { | 60 if (whiteListMap[file][messagePart] == 0) { |
59 print("White-listing '$messagePart' is unused in '$file'. " | 61 print("Whitelisting '$messagePart' is unused in '$file'. " |
60 "Remove the white-listing from the white list map."); | 62 "Remove the whitelisting from the whitelist map."); |
61 allUsed = false; | 63 allUsed = false; |
62 } | 64 } |
63 } | 65 } |
64 } | 66 } |
65 return allUsed; | 67 return allUsed; |
66 } | 68 } |
67 | 69 |
68 void reportWhiteListUse() { | 70 void reportWhiteListUse() { |
69 for (String file in whiteListMap.keys) { | 71 for (String file in whiteListMap.keys) { |
70 for (String messagePart in whiteListMap[file].keys) { | 72 for (String messagePart in whiteListMap[file].keys) { |
71 int useCount = whiteListMap[file][messagePart]; | 73 int useCount = whiteListMap[file][messagePart]; |
72 print("White-listed message '$messagePart' suppressed $useCount " | 74 print("Whitelisted message '$messagePart' suppressed $useCount " |
73 "time(s) in '$file'."); | 75 "time(s) in '$file'."); |
74 } | 76 } |
75 } | 77 } |
76 } | 78 } |
77 | 79 |
78 bool checkWhiteList(Uri uri, String message) { | 80 bool checkWhiteList(Uri uri, String message) { |
79 if (uri == null) { | 81 if (uri == null) { |
80 return false; | 82 return false; |
81 } | 83 } |
82 String path = uri.path; | 84 String path = uri.path; |
83 for (String file in whiteListMap.keys) { | 85 for (String file in whiteListMap.keys) { |
84 if (path.endsWith(file)) { | 86 if (path.endsWith(file)) { |
85 for (String messagePart in whiteListMap[file].keys) { | 87 for (String messagePart in whiteListMap[file].keys) { |
86 if (message.contains(messagePart)) { | 88 if (message.contains(messagePart)) { |
87 whiteListMap[file][messagePart]++; | 89 whiteListMap[file][messagePart]++; |
88 return true; | 90 return true; |
89 } | 91 } |
90 } | 92 } |
91 } | 93 } |
92 } | 94 } |
93 return false; | 95 return false; |
94 } | 96 } |
95 | 97 |
96 void diagnosticHandler(Uri uri, int begin, int end, String message, | 98 void diagnosticHandler(Uri uri, int begin, int end, String message, |
97 api.Diagnostic kind) { | 99 api.Diagnostic kind) { |
98 if (kind == api.Diagnostic.WARNING) { | 100 if (kind == api.Diagnostic.WARNING) { |
99 if (checkWhiteList(uri, message)) { | 101 if (checkWhiteList(uri, message)) { |
100 // Suppress white listed warnings. | 102 // Suppress whitelisted warnings. |
101 return; | 103 return; |
102 } | 104 } |
103 hasWarnings = true; | 105 hasWarnings = true; |
104 } | 106 } |
| 107 if (kind == api.Diagnostic.HINT) { |
| 108 if (checkWhiteList(uri, message)) { |
| 109 // Suppress whitelisted hints. |
| 110 return; |
| 111 } |
| 112 hasHint = true; |
| 113 } |
105 if (kind == api.Diagnostic.ERROR) { | 114 if (kind == api.Diagnostic.ERROR) { |
106 if (checkWhiteList(uri, message)) { | 115 if (checkWhiteList(uri, message)) { |
107 // Suppress white listed warnings. | 116 // Suppress whitelisted errors. |
108 return; | 117 return; |
109 } | 118 } |
110 hasErrors = true; | 119 hasErrors = true; |
111 } | 120 } |
112 super.diagnosticHandler(uri, begin, end, message, kind); | 121 super.diagnosticHandler(uri, begin, end, message, kind); |
113 } | 122 } |
114 } | 123 } |
115 | 124 |
116 void analyze(List<Uri> uriList, Map<String, List<String>> whiteList) { | 125 void analyze(List<Uri> uriList, Map<String, List<String>> whiteList) { |
117 var libraryRoot = currentDirectory.resolve('sdk/'); | 126 var libraryRoot = currentDirectory.resolve('sdk/'); |
118 var provider = new SourceFileProvider(); | 127 var provider = new SourceFileProvider(); |
119 var handler = new CollectingDiagnosticHandler(whiteList, provider); | 128 var handler = new CollectingDiagnosticHandler(whiteList, provider); |
120 var compiler = new Compiler( | 129 var compiler = new Compiler( |
121 provider.readStringFromUri, | 130 provider.readStringFromUri, |
122 null, | 131 null, |
123 handler.diagnosticHandler, | 132 handler.diagnosticHandler, |
124 libraryRoot, libraryRoot, | 133 libraryRoot, libraryRoot, |
125 <String>['--analyze-only', '--analyze-all', | 134 <String>['--analyze-only', '--analyze-all', |
126 '--categories=Client,Server']); | 135 '--categories=Client,Server']); |
127 compiler.librariesToAnalyzeWhenRun = uriList; | 136 compiler.librariesToAnalyzeWhenRun = uriList; |
128 compiler.run(null); | 137 compiler.run(null); |
129 handler.checkResults(); | 138 handler.checkResults(); |
130 } | 139 } |
OLD | NEW |