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

Side by Side Diff: pkg/analyzer/lib/src/generated/resolver.dart

Issue 704413005: Report HintCode.UNUSED_ELEMENT for classes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixes for review comments. Created 6 years, 1 month 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 engine.resolver; 5 library engine.resolver;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 import "dart:math" as math; 8 import "dart:math" as math;
9 9
10 import 'java_core.dart'; 10 import 'java_core.dart';
(...skipping 1891 matching lines...) Expand 10 before | Expand all | Expand 10 after
1902 _errorReporter.reportErrorForNode(HintCode.IS_NOT_DOUBLE, node); 1902 _errorReporter.reportErrorForNode(HintCode.IS_NOT_DOUBLE, node);
1903 } 1903 }
1904 return true; 1904 return true;
1905 } 1905 }
1906 } 1906 }
1907 return false; 1907 return false;
1908 } 1908 }
1909 } 1909 }
1910 1910
1911 /** 1911 /**
1912 * Instances of the class [UnusedLocalVariableVerifier] traverse an element 1912 * Instances of the class [UnusedElementVerifier] traverse an element
1913 * structure looking for cases of [HintCode.UNUSED_LOCAL_VARIABLE]. 1913 * structure looking for cases of [HintCode.UNUSED_ELEMENT] and
1914 * [HintCode.UNUSED_LOCAL_VARIABLE].
1914 */ 1915 */
1915 class UnusedLocalVariableVerifier extends RecursiveElementVisitor { 1916 class UnusedElementVerifier extends RecursiveElementVisitor {
1916 /** 1917 /**
1917 * The error reporter by which errors will be reported. 1918 * The error reporter by which errors will be reported.
1918 */ 1919 */
1919 final ErrorReporter _errorReporter; 1920 final ErrorReporter _errorReporter;
1920 1921
1921 /** 1922 /**
1922 * Create a new instance of the [UnusedLocalVariableVerifier]. 1923 * Create a new instance of the [UnusedElementVerifier].
1923 */ 1924 */
1924 UnusedLocalVariableVerifier(this._errorReporter); 1925 UnusedElementVerifier(this._errorReporter);
1926
1927 @override
1928 visitClassElement(ClassElement element) {
1929 if (element is ClassElementImpl && !element.isUsed) {
1930 _errorReporter.reportErrorForElement(
1931 HintCode.UNUSED_ELEMENT,
1932 element,
1933 [element.kind.displayName, element.displayName]);
1934 }
1935 element.visitChildren(this);
1936 }
1925 1937
1926 @override 1938 @override
1927 visitLocalVariableElement(LocalVariableElement element) { 1939 visitLocalVariableElement(LocalVariableElement element) {
1928 if (element is LocalVariableElementImpl && !element.isUsed) { 1940 if (element is LocalVariableElementImpl && !element.isUsed) {
1929 _errorReporter.reportErrorForElement( 1941 _errorReporter.reportErrorForElement(
1930 HintCode.UNUSED_LOCAL_VARIABLE, 1942 HintCode.UNUSED_LOCAL_VARIABLE,
1931 element, 1943 element,
1932 [element.displayName]); 1944 [element.displayName]);
1933 } 1945 }
1934 } 1946 }
(...skipping 3467 matching lines...) Expand 10 before | Expand all | Expand 10 after
5402 } finally { 5414 } finally {
5403 timeCounter.stop(); 5415 timeCounter.stop();
5404 } 5416 }
5405 } 5417 }
5406 5418
5407 void _generateForCompilationUnit(CompilationUnit unit, Source source) { 5419 void _generateForCompilationUnit(CompilationUnit unit, Source source) {
5408 ErrorReporter errorReporter = new ErrorReporter(_errorListener, source); 5420 ErrorReporter errorReporter = new ErrorReporter(_errorListener, source);
5409 unit.accept(_importsVerifier); 5421 unit.accept(_importsVerifier);
5410 // dead code analysis 5422 // dead code analysis
5411 unit.accept(new DeadCodeVerifier(errorReporter)); 5423 unit.accept(new DeadCodeVerifier(errorReporter));
5412 unit.element.accept(new UnusedLocalVariableVerifier(errorReporter)); 5424 unit.element.accept(new UnusedElementVerifier(errorReporter));
5413 // dart2js analysis 5425 // dart2js analysis
5414 if (_enableDart2JSHints) { 5426 if (_enableDart2JSHints) {
5415 unit.accept(new Dart2JSVerifier(errorReporter)); 5427 unit.accept(new Dart2JSVerifier(errorReporter));
5416 } 5428 }
5417 // Dart best practices 5429 // Dart best practices
5418 unit.accept(new BestPracticesVerifier(errorReporter)); 5430 unit.accept(new BestPracticesVerifier(errorReporter));
5419 unit.accept(new OverrideVerifier(_manager, errorReporter)); 5431 unit.accept(new OverrideVerifier(_manager, errorReporter));
5420 // Find to-do comments 5432 // Find to-do comments
5421 new ToDoFinder(errorReporter).findIn(unit); 5433 new ToDoFinder(errorReporter).findIn(unit);
5422 // pub analysis 5434 // pub analysis
(...skipping 7051 matching lines...) Expand 10 before | Expand all | Expand 10 after
12474 */ 12486 */
12475 final TypeProvider typeProvider; 12487 final TypeProvider typeProvider;
12476 12488
12477 /** 12489 /**
12478 * The scope used to resolve labels for `break` and `continue` statements, or 12490 * The scope used to resolve labels for `break` and `continue` statements, or
12479 * `null` if no labels have been defined in the current context. 12491 * `null` if no labels have been defined in the current context.
12480 */ 12492 */
12481 LabelScope _labelScope; 12493 LabelScope _labelScope;
12482 12494
12483 /** 12495 /**
12496 * The class containing the AST nodes being visited,
12497 * or `null` if we are not in the scope of a class.
12498 */
12499 ClassElement _enclosingClass;
12500
12501 /**
12484 * Initialize a newly created visitor to resolve the nodes in a compilation un it. 12502 * Initialize a newly created visitor to resolve the nodes in a compilation un it.
12485 * 12503 *
12486 * @param library the library containing the compilation unit being resolved 12504 * @param library the library containing the compilation unit being resolved
12487 * @param source the source representing the compilation unit being visited 12505 * @param source the source representing the compilation unit being visited
12488 * @param typeProvider the object used to access the types from the core libra ry 12506 * @param typeProvider the object used to access the types from the core libra ry
12489 */ 12507 */
12490 ScopedVisitor.con1(Library library, this.source, this.typeProvider) { 12508 ScopedVisitor.con1(Library library, this.source, this.typeProvider) {
12491 this._definingLibrary = library.libraryElement; 12509 this._definingLibrary = library.libraryElement;
12492 LibraryScope libraryScope = library.libraryScope; 12510 LibraryScope libraryScope = library.libraryScope;
12493 this._errorListener = libraryScope.errorListener; 12511 this._errorListener = libraryScope.errorListener;
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
12609 Object visitClassDeclaration(ClassDeclaration node) { 12627 Object visitClassDeclaration(ClassDeclaration node) {
12610 ClassElement classElement = node.element; 12628 ClassElement classElement = node.element;
12611 Scope outerScope = _nameScope; 12629 Scope outerScope = _nameScope;
12612 try { 12630 try {
12613 if (classElement == null) { 12631 if (classElement == null) {
12614 AnalysisEngine.instance.logger.logInformation( 12632 AnalysisEngine.instance.logger.logInformation(
12615 "Missing element for class declaration ${node.name.name} in ${defini ngLibrary.source.fullName}", 12633 "Missing element for class declaration ${node.name.name} in ${defini ngLibrary.source.fullName}",
12616 new CaughtException(new AnalysisException(), null)); 12634 new CaughtException(new AnalysisException(), null));
12617 super.visitClassDeclaration(node); 12635 super.visitClassDeclaration(node);
12618 } else { 12636 } else {
12619 _nameScope = new TypeParameterScope(_nameScope, classElement); 12637 ClassElement outerClass = _enclosingClass;
12620 visitClassDeclarationInScope(node); 12638 try {
12621 _nameScope = new ClassScope(_nameScope, classElement); 12639 _enclosingClass = node.element;
12622 visitClassMembersInScope(node); 12640 _nameScope = new TypeParameterScope(_nameScope, classElement);
12641 visitClassDeclarationInScope(node);
12642 _nameScope = new ClassScope(_nameScope, classElement);
12643 visitClassMembersInScope(node);
12644 } finally {
12645 _enclosingClass = outerClass;
12646 }
12623 } 12647 }
12624 } finally { 12648 } finally {
12625 _nameScope = outerScope; 12649 _nameScope = outerScope;
12626 } 12650 }
12627 return null; 12651 return null;
12628 } 12652 }
12629 12653
12630 @override 12654 @override
12631 Object visitClassTypeAlias(ClassTypeAlias node) { 12655 Object visitClassTypeAlias(ClassTypeAlias node) {
12632 Scope outerScope = _nameScope; 12656 Scope outerScope = _nameScope;
(...skipping 2450 matching lines...) Expand 10 before | Expand all | Expand 10 after
15083 types.add(type); 15107 types.add(type);
15084 } 15108 }
15085 } 15109 }
15086 return types; 15110 return types;
15087 } 15111 }
15088 15112
15089 void _setElement(Identifier typeName, Element element) { 15113 void _setElement(Identifier typeName, Element element) {
15090 if (element != null) { 15114 if (element != null) {
15091 if (typeName is SimpleIdentifier) { 15115 if (typeName is SimpleIdentifier) {
15092 typeName.staticElement = element; 15116 typeName.staticElement = element;
15117 _markTypeNameElementUsed(typeName, element);
15093 } else if (typeName is PrefixedIdentifier) { 15118 } else if (typeName is PrefixedIdentifier) {
15094 PrefixedIdentifier identifier = typeName; 15119 PrefixedIdentifier identifier = typeName;
15095 identifier.identifier.staticElement = element; 15120 identifier.identifier.staticElement = element;
15096 SimpleIdentifier prefix = identifier.prefix; 15121 SimpleIdentifier prefix = identifier.prefix;
15097 Element prefixElement = nameScope.lookup(prefix, definingLibrary); 15122 Element prefixElement = nameScope.lookup(prefix, definingLibrary);
15098 if (prefixElement != null) { 15123 if (prefixElement != null) {
15099 prefix.staticElement = prefixElement; 15124 prefix.staticElement = prefixElement;
15100 } 15125 }
15101 } 15126 }
15102 } 15127 }
15103 } 15128 }
15104 15129
15105 /** 15130 /**
15131 * Marks [element] as used in its defining library.
15132 */
15133 void _markTypeNameElementUsed(Identifier typeName, Element element) {
15134 if (identical(element, _enclosingClass)) {
15135 return;
15136 }
15137 // ignore places where the element is not actually used
15138 if (typeName.parent is TypeName) {
15139 AstNode parent2 = typeName.parent.parent;
15140 if (parent2 is IsExpression) {
15141 return;
15142 }
15143 if (parent2 is VariableDeclarationList) {
15144 return;
15145 }
15146 }
15147 // check if the element is a local top-level element
15148 if (element is ElementImpl &&
15149 element.enclosingElement is CompilationUnitElement &&
15150 identical(element.library, definingLibrary)) {
15151 element.markUsed();
15152 }
15153 }
15154
15155 /**
15106 * Given a parameter element, create a function type based on the given return type and parameter 15156 * Given a parameter element, create a function type based on the given return type and parameter
15107 * list and associate the created type with the element. 15157 * list and associate the created type with the element.
15108 * 15158 *
15109 * @param element the parameter element whose type is to be set 15159 * @param element the parameter element whose type is to be set
15110 * @param returnType the (possibly `null`) return type of the function 15160 * @param returnType the (possibly `null`) return type of the function
15111 * @param parameterList the list of parameters to the function 15161 * @param parameterList the list of parameters to the function
15112 */ 15162 */
15113 void _setFunctionTypedParameterType(ParameterElementImpl element, TypeName ret urnType, FormalParameterList parameterList) { 15163 void _setFunctionTypedParameterType(ParameterElementImpl element, TypeName ret urnType, FormalParameterList parameterList) {
15114 List<ParameterElement> parameters = _getElements(parameterList); 15164 List<ParameterElement> parameters = _getElements(parameterList);
15115 FunctionTypeAliasElementImpl aliasElement = new FunctionTypeAliasElementImpl .forNode(null); 15165 FunctionTypeAliasElementImpl aliasElement = new FunctionTypeAliasElementImpl .forNode(null);
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after
15527 /** 15577 /**
15528 * Return the tag that has the given identifier, or {@code null} if there is n o such tag (the 15578 * Return the tag that has the given identifier, or {@code null} if there is n o such tag (the
15529 * identifier is not defined). 15579 * identifier is not defined).
15530 * 15580 *
15531 * @return the tag that has the given identifier 15581 * @return the tag that has the given identifier
15532 */ 15582 */
15533 String getTagWithId(String identifier) { 15583 String getTagWithId(String identifier) {
15534 return idToTagMap[identifier]; 15584 return idToTagMap[identifier];
15535 } 15585 }
15536 } 15586 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/error.dart ('k') | pkg/analyzer/test/generated/resolver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698