Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library dart2js.analyze_helpers.test; | |
| 6 | |
| 7 import 'dart:io'; | |
| 8 | |
| 9 import 'package:async_helper/async_helper.dart'; | |
| 10 import 'package:compiler/compiler_new.dart' show | |
| 11 Diagnostic; | |
| 12 import 'package:compiler/src/apiimpl.dart' show | |
| 13 Compiler; | |
| 14 import 'package:compiler/src/constants/expressions.dart' show | |
| 15 ConstructedConstantExpression; | |
| 16 import 'package:compiler/src/dart_types.dart' show | |
| 17 InterfaceType; | |
| 18 import 'package:compiler/src/diagnostics/source_span.dart' show | |
| 19 SourceSpan; | |
| 20 import 'package:compiler/src/elements/elements.dart'; | |
| 21 import 'package:compiler/src/filenames.dart' show | |
| 22 nativeToUriPath; | |
| 23 import 'package:compiler/src/resolution/semantic_visitor.dart'; | |
| 24 import 'package:compiler/src/resolution/tree_elements.dart' show | |
| 25 TreeElements; | |
| 26 import 'package:compiler/src/source_file_provider.dart' show | |
| 27 FormattingDiagnosticHandler; | |
| 28 import 'package:compiler/src/tree/tree.dart'; | |
| 29 import 'package:compiler/src/universe/universe.dart' show | |
| 30 CallStructure; | |
| 31 import 'package:expect/expect.dart'; | |
| 32 | |
| 33 import 'memory_compiler.dart'; | |
| 34 | |
| 35 main(List<String> arguments) { | |
| 36 bool verbose = arguments.contains('-v'); | |
| 37 | |
| 38 List<String> options = <String>[ | |
| 39 '--analyze-only', | |
| 40 '--analyze-main', | |
| 41 '--categories=Client,Server']; | |
| 42 if (verbose) { | |
| 43 options.add('--verbose'); | |
| 44 } | |
| 45 asyncTest(() async { | |
| 46 Compiler compiler = compilerFor( | |
| 47 options: options, showDiagnostics: verbose); | |
| 48 FormattingDiagnosticHandler diagnostics = | |
| 49 new FormattingDiagnosticHandler(compiler.provider); | |
| 50 HelperAnalyzer analyzer = new HelperAnalyzer(diagnostics); | |
| 51 Directory dir = | |
| 52 new Directory.fromUri(Uri.base.resolve('pkg/compiler/lib/')); | |
| 53 for (FileSystemEntity entity in dir.listSync(recursive: true)) { | |
| 54 if (entity is File && entity.path.endsWith('.dart')) { | |
| 55 Uri file = Uri.base.resolve(nativeToUriPath(entity.path)); | |
| 56 if (verbose) { | |
| 57 print('---- analyzing $file ----'); | |
| 58 } | |
| 59 LibraryElement library = await compiler.analyzeUri(file); | |
| 60 if (library != null) { | |
| 61 library.forEachLocalMember((Element element) { | |
| 62 if (element is ClassElement) { | |
| 63 element.forEachLocalMember((AstElement member) { | |
| 64 analyzer.analyze(member.resolvedAst); | |
| 65 }); | |
| 66 } else if (element is MemberElement) { | |
| 67 analyzer.analyze(element.resolvedAst); | |
| 68 } | |
| 69 }); | |
| 70 } | |
| 71 } | |
| 72 } | |
| 73 Expect.isTrue(analyzer.errors.isEmpty, "Errors found."); | |
| 74 }); | |
| 75 } | |
| 76 | |
| 77 class HelperAnalyzer extends TraversalVisitor { | |
| 78 final FormattingDiagnosticHandler diagnostics; | |
| 79 List<SourceSpan> errors = <SourceSpan>[]; | |
| 80 | |
| 81 ResolvedAst resolvedAst; | |
| 82 | |
| 83 @override | |
| 84 TreeElements get elements => resolvedAst.elements; | |
| 85 | |
| 86 AnalyzableElement get analyzedElement => resolvedAst.element; | |
| 87 | |
| 88 HelperAnalyzer(this.diagnostics) : super(null); | |
| 89 | |
| 90 @override | |
| 91 void apply(Node node, [_]) { | |
| 92 node.accept(this); | |
| 93 } | |
| 94 | |
| 95 void analyze(ResolvedAst resolvedAst) { | |
| 96 if (resolvedAst.node == null) { | |
| 97 // Skip synthesized members. | |
| 98 return; | |
| 99 } | |
| 100 this.resolvedAst = resolvedAst; | |
| 101 //print('analyze:$resolvedAst'); | |
|
karlklose
2015/08/28 11:38:26
Remove or put into verbose block.
Johnni Winther
2015/08/28 11:45:51
Done.
| |
| 102 apply(resolvedAst.node); | |
| 103 this.resolvedAst = null; | |
| 104 } | |
| 105 | |
| 106 bool isHelper(Element element) { | |
| 107 Uri uri = element.library.canonicalUri; | |
| 108 return uri.path.endsWith('src/helpers/helpers.dart'); | |
| 109 } | |
| 110 | |
| 111 void checkAccess(Node node, MemberElement element) { | |
| 112 if (isHelper(element) && !isHelper(analyzedElement)) { | |
| 113 Uri uri = analyzedElement.implementation.sourcePosition.uri; | |
| 114 SourceSpan span = new SourceSpan.fromNode(uri, node); | |
| 115 diagnostics.report(null, span.uri, span.begin, span.end, | |
| 116 "Helper used in production code.", | |
| 117 Diagnostic.ERROR); | |
| 118 errors.add(span); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 @override | |
| 123 void visitTopLevelFieldInvoke( | |
| 124 Send node, | |
| 125 FieldElement field, | |
| 126 NodeList arguments, | |
| 127 CallStructure callStructure, | |
| 128 _) { | |
| 129 checkAccess(node, field); | |
| 130 apply(arguments); | |
| 131 } | |
| 132 | |
| 133 @override | |
| 134 void visitTopLevelGetterInvoke( | |
| 135 Send node, | |
| 136 GetterElement getter, | |
| 137 NodeList arguments, | |
| 138 CallStructure callStructure, | |
| 139 _) { | |
| 140 checkAccess(node, getter); | |
| 141 apply(arguments); | |
| 142 } | |
| 143 | |
| 144 @override | |
| 145 void visitTopLevelFunctionInvoke( | |
| 146 Send node, | |
| 147 MethodElement method, | |
| 148 NodeList arguments, | |
| 149 CallStructure callStructure, | |
| 150 _) { | |
| 151 checkAccess(node, method); | |
| 152 apply(arguments); | |
| 153 } | |
| 154 | |
| 155 @override | |
| 156 void visitTopLevelFieldGet( | |
| 157 Send node, | |
| 158 FieldElement field, | |
| 159 _) { | |
| 160 checkAccess(node, field); | |
| 161 } | |
| 162 | |
| 163 @override | |
| 164 void visitTopLevelGetterGet( | |
| 165 Send node, | |
| 166 GetterElement getter, | |
| 167 _) { | |
| 168 checkAccess(node, getter); | |
| 169 } | |
| 170 | |
| 171 @override | |
| 172 void visitTopLevelFunctionGet( | |
| 173 Send node, | |
| 174 MethodElement method, | |
| 175 _) { | |
| 176 checkAccess(node, method); | |
| 177 } | |
| 178 | |
| 179 @override | |
| 180 void visitGenerativeConstructorInvoke( | |
| 181 NewExpression node, | |
| 182 ConstructorElement constructor, | |
| 183 InterfaceType type, | |
| 184 NodeList arguments, | |
| 185 CallStructure callStructure, | |
| 186 _) { | |
| 187 checkAccess(node, constructor); | |
| 188 apply(arguments); | |
| 189 } | |
| 190 | |
| 191 @override | |
| 192 void visitRedirectingGenerativeConstructorInvoke( | |
| 193 NewExpression node, | |
| 194 ConstructorElement constructor, | |
| 195 InterfaceType type, | |
| 196 NodeList arguments, | |
| 197 CallStructure callStructure, | |
| 198 _) { | |
| 199 checkAccess(node, constructor); | |
| 200 apply(arguments); | |
| 201 } | |
| 202 | |
| 203 @override | |
| 204 void visitFactoryConstructorInvoke( | |
| 205 NewExpression node, | |
| 206 ConstructorElement constructor, | |
| 207 InterfaceType type, | |
| 208 NodeList arguments, | |
| 209 CallStructure callStructure, | |
| 210 _) { | |
| 211 checkAccess(node, constructor); | |
| 212 apply(arguments); | |
| 213 } | |
| 214 | |
| 215 @override | |
| 216 void visitRedirectingFactoryConstructorInvoke( | |
| 217 NewExpression node, | |
| 218 ConstructorElement constructor, | |
| 219 InterfaceType type, | |
| 220 ConstructorElement effectiveTarget, | |
| 221 InterfaceType effectiveTargetType, | |
| 222 NodeList arguments, | |
| 223 CallStructure callStructure, | |
| 224 _) { | |
| 225 checkAccess(node, constructor); | |
| 226 apply(arguments); | |
| 227 } | |
| 228 | |
| 229 @override | |
| 230 void visitConstConstructorInvoke( | |
| 231 NewExpression node, | |
| 232 ConstructedConstantExpression constant, | |
| 233 _) { | |
| 234 checkAccess(node, constant.target); | |
| 235 } | |
| 236 } | |
| OLD | NEW |