Thu, 03 Feb 2011 09:36:28 +0000
5017953: spurious cascaded diagnostics when name not found
Summary: when an operator is applied to one or more erroneous operands, spurious diagnostics are generated
Reviewed-by: jjg
duke@1 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. |
duke@1 | 8 | * |
duke@1 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 13 | * accompanied this code). |
duke@1 | 14 | * |
duke@1 | 15 | * You should have received a copy of the GNU General Public License version |
duke@1 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 18 | * |
ohair@554 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 20 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 21 | * questions. |
duke@1 | 22 | */ |
duke@1 | 23 | |
duke@1 | 24 | import java.io.*; |
duke@1 | 25 | import java.util.*; |
duke@1 | 26 | import javax.lang.model.util.*; |
duke@1 | 27 | import javax.tools.*; |
duke@1 | 28 | import com.sun.tools.javac.api.*; |
duke@1 | 29 | import com.sun.source.tree.*; |
duke@1 | 30 | import com.sun.source.util.*; |
duke@1 | 31 | import com.sun.tools.javac.tree.JCTree; |
duke@1 | 32 | import com.sun.tools.javac.tree.JCTree.*; |
duke@1 | 33 | import com.sun.tools.javac.util.Position; |
duke@1 | 34 | |
duke@1 | 35 | /* |
duke@1 | 36 | * Abstract class to help check the scopes in a parsed source file. |
duke@1 | 37 | * -- parse source file |
duke@1 | 38 | * -- scan trees looking for string literals |
duke@1 | 39 | * -- check the scope at that point against the string, using |
duke@1 | 40 | * boolean check(Scope s, String ref) |
duke@1 | 41 | */ |
duke@1 | 42 | abstract class Checker { |
duke@1 | 43 | // parse the source file and call check(scope, string) for each string literal found |
duke@1 | 44 | void check(String... fileNames) throws IOException { |
duke@1 | 45 | File testSrc = new File(System.getProperty("test.src")); |
duke@1 | 46 | |
duke@1 | 47 | DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() { |
duke@1 | 48 | public void report(Diagnostic d) { |
duke@1 | 49 | System.err.println(d); |
duke@1 | 50 | if (d.getKind() == Diagnostic.Kind.ERROR) |
duke@1 | 51 | errors = true; |
duke@1 | 52 | new Exception().printStackTrace(); |
duke@1 | 53 | } |
duke@1 | 54 | }; |
duke@1 | 55 | |
duke@1 | 56 | JavacTool tool = JavacTool.create(); |
duke@1 | 57 | StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null); |
duke@1 | 58 | Iterable<? extends JavaFileObject> files = |
duke@1 | 59 | fm.getJavaFileObjectsFromFiles(getFiles(testSrc, fileNames)); |
duke@1 | 60 | task = tool.getTask(null, fm, dl, null, null, files); |
duke@1 | 61 | Iterable<? extends CompilationUnitTree> units = task.parse(); |
duke@1 | 62 | |
duke@1 | 63 | if (errors) |
duke@1 | 64 | throw new AssertionError("errors occurred creating trees"); |
duke@1 | 65 | |
duke@1 | 66 | ScopeScanner s = new ScopeScanner(); |
duke@1 | 67 | for (CompilationUnitTree unit: units) { |
duke@1 | 68 | TreePath p = new TreePath(unit); |
duke@1 | 69 | s.scan(p, getTrees()); |
duke@1 | 70 | } |
duke@1 | 71 | task = null; |
duke@1 | 72 | |
duke@1 | 73 | if (errors) |
duke@1 | 74 | throw new AssertionError("errors occurred checking scopes"); |
duke@1 | 75 | } |
duke@1 | 76 | |
duke@1 | 77 | // default impl: split ref at ";" and call checkLocal(scope, ref_segment) on scope and its enclosing scopes |
duke@1 | 78 | protected boolean check(Scope s, String ref) { |
duke@1 | 79 | // System.err.println("check scope: " + s); |
duke@1 | 80 | // System.err.println("check ref: " + ref); |
duke@1 | 81 | if (s == null && (ref == null || ref.trim().length() == 0)) |
duke@1 | 82 | return true; |
duke@1 | 83 | |
duke@1 | 84 | if (s == null) { |
duke@1 | 85 | error(s, ref, "scope missing"); |
duke@1 | 86 | return false; |
duke@1 | 87 | } |
duke@1 | 88 | |
duke@1 | 89 | if (ref == null) { |
duke@1 | 90 | error(s, ref, "scope unexpected"); |
duke@1 | 91 | return false; |
duke@1 | 92 | } |
duke@1 | 93 | |
duke@1 | 94 | String local; |
duke@1 | 95 | String encl; |
duke@1 | 96 | int semi = ref.indexOf(';'); |
duke@1 | 97 | if (semi == -1) { |
duke@1 | 98 | local = ref; |
duke@1 | 99 | encl = null; |
duke@1 | 100 | } else { |
duke@1 | 101 | local = ref.substring(0, semi); |
duke@1 | 102 | encl = ref.substring(semi + 1); |
duke@1 | 103 | } |
duke@1 | 104 | |
duke@1 | 105 | return checkLocal(s, local.trim()) |
duke@1 | 106 | & check(s.getEnclosingScope(), encl); |
duke@1 | 107 | } |
duke@1 | 108 | |
duke@1 | 109 | // override if using default check(Scope,String) |
duke@1 | 110 | boolean checkLocal(Scope s, String ref) { |
duke@1 | 111 | throw new IllegalStateException(); |
duke@1 | 112 | } |
duke@1 | 113 | |
duke@1 | 114 | void error(Scope s, String ref, String msg) { |
duke@1 | 115 | System.err.println("Error: " + msg); |
duke@1 | 116 | System.err.println("Scope: " + (s == null ? null : asList(s.getLocalElements()))); |
duke@1 | 117 | System.err.println("Expect: " + ref); |
duke@1 | 118 | System.err.println("javac: " + (s == null ? null : ((JavacScope) s).getEnv())); |
duke@1 | 119 | errors = true; |
duke@1 | 120 | } |
duke@1 | 121 | |
duke@1 | 122 | protected Elements getElements() { |
duke@1 | 123 | return task.getElements(); |
duke@1 | 124 | } |
duke@1 | 125 | |
duke@1 | 126 | protected Trees getTrees() { |
duke@1 | 127 | return Trees.instance(task); |
duke@1 | 128 | } |
duke@1 | 129 | |
duke@1 | 130 | boolean errors = false; |
duke@1 | 131 | protected JavacTask task; |
duke@1 | 132 | |
duke@1 | 133 | // scan a parse tree, and for every string literal found, call check(scope, string) with |
duke@1 | 134 | // the string value at the scope at that point |
duke@1 | 135 | class ScopeScanner extends TreePathScanner<Boolean,Trees> { |
duke@1 | 136 | public Boolean visitLiteral(LiteralTree tree, Trees trees) { |
duke@1 | 137 | TreePath path = getCurrentPath(); |
duke@1 | 138 | CompilationUnitTree unit = path.getCompilationUnit(); |
duke@1 | 139 | Position.LineMap lineMap = ((JCCompilationUnit)unit).lineMap; |
duke@1 | 140 | // long line = lineMap.getLineNumber(((JCTree)tree).pos/*trees.getSourcePositions().getStartPosition(tree)*/); |
duke@1 | 141 | // System.err.println(line + ": " + abbrev(tree)); |
duke@1 | 142 | Scope s = trees.getScope(path); |
duke@1 | 143 | if (tree.getKind() == Tree.Kind.STRING_LITERAL) |
duke@1 | 144 | check(s, tree.getValue().toString().trim()); |
duke@1 | 145 | return null; |
duke@1 | 146 | } |
duke@1 | 147 | |
duke@1 | 148 | private String abbrev(Tree tree) { |
duke@1 | 149 | int max = 48; |
duke@1 | 150 | String s = tree.toString().replaceAll("[ \n]+", " "); |
duke@1 | 151 | return (s.length() < max ? s : s.substring(0, max-3) + "..."); |
duke@1 | 152 | } |
duke@1 | 153 | } |
duke@1 | 154 | |
duke@1 | 155 | // prefix filenames with a directory |
duke@1 | 156 | static Iterable<File> getFiles(File dir, String... names) { |
duke@1 | 157 | List<File> files = new ArrayList<File>(names.length); |
duke@1 | 158 | for (String name: names) |
duke@1 | 159 | files.add(new File(dir, name)); |
duke@1 | 160 | return files; |
duke@1 | 161 | } |
duke@1 | 162 | |
duke@1 | 163 | static private <T> List<T> asList(Iterable<T> iter) { |
duke@1 | 164 | List<T> l = new ArrayList<T>(); |
duke@1 | 165 | for (T t: iter) |
duke@1 | 166 | l.add(t); |
duke@1 | 167 | return l; |
duke@1 | 168 | } |
duke@1 | 169 | } |