From e9931f6df2ebc1a283c33ec2295cec4f272b8288 Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Fri, 4 Apr 2025 16:55:20 -0500 Subject: [PATCH 1/4] Add new method Log.clear(). --- .../com/sun/tools/javac/api/JavacTaskPool.java | 12 +++++------- .../share/classes/com/sun/tools/javac/util/Log.java | 12 ++++++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java index 1ce4cf50ada8a..87bc64c4e1976 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -267,7 +267,7 @@ void clear() { if (ht.get(Log.logKey) instanceof ReusableLog) { //log already inited - not first round - ((ReusableLog)Log.instance(this)).clear(); + Log.instance(this).clear(); Enter.instance(this).newRound(); ((ReusableJavaCompiler)ReusableJavaCompiler.instance(this)).clear(); Types.instance(this).newRound(); @@ -395,11 +395,9 @@ static class ReusableLog extends Log { this.context = context; } - void clear() { - recorded.clear(); - sourceMap.clear(); - nerrors = 0; - nwarnings = 0; + @Override + public void clear() { + super.clear(); //Set a fake listener that will lazily lookup the context for the 'real' listener. Since //this field is never updated when a new task is created, we cannot simply reset the field //or keep old value. This is a hack to workaround the limitations in the current infrastructure. diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java index 56ee413ea0716..e9da2efea9c8a 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java @@ -689,6 +689,18 @@ public void report(JCDiagnostic diagnostic) { diagnosticHandler.report(diagnostic); } + /** + * Reset the state of this instance. + */ + public void clear() { + recorded.clear(); + sourceMap.clear(); + nerrors = 0; + nwarnings = 0; + nsuppressederrors = 0; + nsuppressedwarns = 0; + } + /** * Common diagnostic handling. * The diagnostic is counted, and depending on the options and how many diagnostics have been From a1108ac7266ad2288c3b649a3be7b69450fe5679 Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Fri, 4 Apr 2025 17:17:00 -0500 Subject: [PATCH 2/4] Invoke Log.useSource() before recursing into attribution. --- .../com/sun/tools/javac/api/JavacTrees.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java index ce719230455d3..3981f91dce17a 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -361,9 +361,14 @@ public TypeMirror getType(DocTreePath path) { new Log.DeferredDiagnosticHandler(log); try { Env env = getAttrContext(path.getTreePath()); - Type t = attr.attribType(dcReference.qualifierExpression, env); - if (t != null && !t.isErroneous()) { - return t; + JavaFileObject prevSource = log.useSource(env.toplevel.sourcefile); + try { + Type t = attr.attribType(dcReference.qualifierExpression, env); + if (t != null && !t.isErroneous()) { + return t; + } + } finally { + log.useSource(prevSource); } } catch (Abort e) { // may be thrown by Check.completionError in case of bad class file return null; @@ -390,6 +395,7 @@ private Symbol attributeDocReference(TreePath path, DCReference ref) { } Log.DeferredDiagnosticHandler deferredDiagnosticHandler = new Log.DeferredDiagnosticHandler(log); + JavaFileObject prevSource = log.useSource(env.toplevel.sourcefile); try { final TypeSymbol tsym; final Name memberName; @@ -511,6 +517,7 @@ private Symbol attributeDocReference(TreePath path, DCReference ref) { } catch (Abort e) { // may be thrown by Check.completionError in case of bad class file return null; } finally { + log.useSource(prevSource); log.popDiagnosticHandler(deferredDiagnosticHandler); } } From 23447164dfb9c4e470891d4d40d87de44448140e Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Mon, 7 Apr 2025 09:20:16 -0500 Subject: [PATCH 3/4] Revert accidentally included unrelated changes. --- .../com/sun/tools/javac/api/JavacTrees.java | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java index 3981f91dce17a..ce719230455d3 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -361,14 +361,9 @@ public TypeMirror getType(DocTreePath path) { new Log.DeferredDiagnosticHandler(log); try { Env env = getAttrContext(path.getTreePath()); - JavaFileObject prevSource = log.useSource(env.toplevel.sourcefile); - try { - Type t = attr.attribType(dcReference.qualifierExpression, env); - if (t != null && !t.isErroneous()) { - return t; - } - } finally { - log.useSource(prevSource); + Type t = attr.attribType(dcReference.qualifierExpression, env); + if (t != null && !t.isErroneous()) { + return t; } } catch (Abort e) { // may be thrown by Check.completionError in case of bad class file return null; @@ -395,7 +390,6 @@ private Symbol attributeDocReference(TreePath path, DCReference ref) { } Log.DeferredDiagnosticHandler deferredDiagnosticHandler = new Log.DeferredDiagnosticHandler(log); - JavaFileObject prevSource = log.useSource(env.toplevel.sourcefile); try { final TypeSymbol tsym; final Name memberName; @@ -517,7 +511,6 @@ private Symbol attributeDocReference(TreePath path, DCReference ref) { } catch (Abort e) { // may be thrown by Check.completionError in case of bad class file return null; } finally { - log.useSource(prevSource); log.popDiagnosticHandler(deferredDiagnosticHandler); } } From a9d584508d776496d2a851abfdd8f12ef1e80f65 Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Mon, 7 Apr 2025 09:22:30 -0500 Subject: [PATCH 4/4] Reset the DiagnosticHandler stack in clear(). --- .../share/classes/com/sun/tools/javac/util/Log.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java index e9da2efea9c8a..2167f47110477 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java @@ -699,6 +699,8 @@ public void clear() { nwarnings = 0; nsuppressederrors = 0; nsuppressedwarns = 0; + while (diagnosticHandler.prev != null) + popDiagnosticHandler(diagnosticHandler); } /**