1 /* |
1 /* |
2 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
73 import com.sun.source.util.JavacTask; |
73 import com.sun.source.util.JavacTask; |
74 import com.sun.tools.javac.api.JavacTool; |
74 import com.sun.tools.javac.api.JavacTool; |
75 import com.sun.tools.javac.code.Flags; |
75 import com.sun.tools.javac.code.Flags; |
76 import com.sun.tools.javac.tree.EndPosTable; |
76 import com.sun.tools.javac.tree.EndPosTable; |
77 import com.sun.tools.javac.tree.JCTree; |
77 import com.sun.tools.javac.tree.JCTree; |
|
78 import com.sun.tools.javac.tree.JCTree.JCAnnotatedType; |
78 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
79 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
79 import com.sun.tools.javac.tree.JCTree.JCNewClass; |
80 import com.sun.tools.javac.tree.JCTree.JCNewClass; |
80 import com.sun.tools.javac.tree.JCTree.JCVariableDecl; |
81 import com.sun.tools.javac.tree.JCTree.JCVariableDecl; |
81 import com.sun.tools.javac.tree.TreeInfo; |
82 import com.sun.tools.javac.tree.TreeInfo; |
82 import com.sun.tools.javac.tree.TreeScanner; |
83 import com.sun.tools.javac.tree.TreeScanner; |
98 |
99 |
99 /* |
100 /* |
100 * @test |
101 * @test |
101 * @bug 6919889 |
102 * @bug 6919889 |
102 * @summary assorted position errors in compiler syntax trees |
103 * @summary assorted position errors in compiler syntax trees |
103 * @run main TreePosTest -q -r -ef ./tools/javac/typeAnnotations -ef ./tools/javap/typeAnnotations -et ANNOTATED_TYPE . |
104 * OLD: -q -r -ef ./tools/javac/typeAnnotations -ef ./tools/javap/typeAnnotations -et ANNOTATED_TYPE . |
|
105 * @run main TreePosTest -q -r . |
104 */ |
106 */ |
105 public class TreePosTest { |
107 public class TreePosTest { |
106 /** |
108 /** |
107 * Main entry point. |
109 * Main entry point. |
108 * If test.src is set, program runs in jtreg mode, and will throw an Error |
110 * If test.src is set, program runs in jtreg mode, and will throw an Error |
365 // and because of inconsistent nesting of left and right of |
367 // and because of inconsistent nesting of left and right of |
366 // array declarations: |
368 // array declarations: |
367 // e.g. int[][] a = new int[2][]; |
369 // e.g. int[][] a = new int[2][]; |
368 check("encl.start <= start", encl, self, encl.start <= self.start); |
370 check("encl.start <= start", encl, self, encl.start <= self.start); |
369 check("start <= pos", encl, self, self.start <= self.pos); |
371 check("start <= pos", encl, self, self.start <= self.pos); |
370 if (!(self.tag == TYPEARRAY |
372 if (!( (self.tag == TYPEARRAY || |
|
373 isAnnotatedArray(self.tree)) |
371 && (encl.tag == VARDEF || |
374 && (encl.tag == VARDEF || |
372 encl.tag == METHODDEF || |
375 encl.tag == METHODDEF || |
373 encl.tag == TYPEARRAY))) { |
376 encl.tag == TYPEARRAY || |
|
377 isAnnotatedArray(encl.tree)) |
|
378 || |
|
379 encl.tag == ANNOTATED_TYPE && self.tag == SELECT |
|
380 )) { |
374 check("encl.pos <= start || end <= encl.pos", |
381 check("encl.pos <= start || end <= encl.pos", |
375 encl, self, encl.pos <= self.start || self.end <= encl.pos); |
382 encl, self, encl.pos <= self.start || self.end <= encl.pos); |
376 } |
383 } |
377 check("pos <= end", encl, self, self.pos <= self.end); |
384 check("pos <= end", encl, self, self.pos <= self.end); |
378 if (!(self.tag == TYPEARRAY && encl.tag == TYPEARRAY)) { |
385 if (!( (self.tag == TYPEARRAY || isAnnotatedArray(self.tree)) && |
|
386 (encl.tag == TYPEARRAY || isAnnotatedArray(encl.tree)) |
|
387 || |
|
388 encl.tag == MODIFIERS && self.tag == ANNOTATION |
|
389 ) ) { |
379 check("end <= encl.end", encl, self, self.end <= encl.end); |
390 check("end <= encl.end", encl, self, self.end <= encl.end); |
380 } |
391 } |
381 } |
392 } |
382 } |
393 } |
383 |
394 |
384 Info prevEncl = encl; |
395 Info prevEncl = encl; |
385 encl = self; |
396 encl = self; |
386 tree.accept(this); |
397 tree.accept(this); |
387 encl = prevEncl; |
398 encl = prevEncl; |
|
399 } |
|
400 |
|
401 private boolean isAnnotatedArray(JCTree tree) { |
|
402 return tree.hasTag(ANNOTATED_TYPE) && |
|
403 ((JCAnnotatedType)tree).underlyingType.hasTag(TYPEARRAY); |
388 } |
404 } |
389 |
405 |
390 @Override |
406 @Override |
391 public void visitVarDef(JCVariableDecl tree) { |
407 public void visitVarDef(JCVariableDecl tree) { |
392 // enum member declarations are desugared in the parser and have |
408 // enum member declarations are desugared in the parser and have |
425 if (viewer == null) |
441 if (viewer == null) |
426 viewer = new Viewer(); |
442 viewer = new Viewer(); |
427 viewer.addEntry(sourcefile, label, encl, self); |
443 viewer.addEntry(sourcefile, label, encl, self); |
428 } |
444 } |
429 |
445 |
430 String s = self.tree.toString(); |
446 String s = "encl: " + encl.tree.toString() + |
|
447 " this: " + self.tree.toString(); |
431 String msg = sourcefile.getName() + ": " + label + ": " + |
448 String msg = sourcefile.getName() + ": " + label + ": " + |
432 "encl:" + encl + " this:" + self + "\n" + |
449 "encl:" + encl + " this:" + self + "\n" + |
433 s.substring(0, Math.min(80, s.length())).replaceAll("[\r\n]+", " "); |
450 s.substring(0, Math.min(80, s.length())).replaceAll("[\r\n]+", " "); |
434 error(msg); |
451 error(msg); |
435 } |
452 } |