Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions check_api/src/main/java/com/google/errorprone/VisitorState.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ public VisitorState withPath(TreePath path) {
}

private VisitorState withNoPathForMemoization() {
if (path == null) {
return this;
}
return new VisitorState(context, null, suppressedState, sharedState);
}

Expand Down Expand Up @@ -624,32 +627,34 @@ private Cache(Supplier<T> impl) {

@Override
public synchronized T get(VisitorState state) {
/*
* Don't let callers rely on the TreePath: The Cache is shared across the whole compilation,
* not just the current VisitorState's TreePath's CompilationUnit.
*/
state = state.withNoPathForMemoization();

/* javac is single-threaded, so in principle we don't really need to lock.
But in practice it's cheap enough to be worth getting peace of mind that this is
always correct. */
T value = cache.get();
if (value == null) {
value = impl.get(state);
value = compute(state);
if (value != null) {
cache = new SoftReference<>(value);
provenance = state.sharedState.javacInvocationInstance;
}
} else {
JavacInvocationInstance current = state.sharedState.javacInvocationInstance;
if (provenance != current) {
value = impl.get(state);
value = compute(state);
cache = new SoftReference<>(value);
provenance = current;
}
}
return value;
}

private T compute(VisitorState state) {
/*
* Don't let callers rely on the TreePath: The Cache is shared across the whole compilation,
* not just the current VisitorState's TreePath's CompilationUnit.
*/
return impl.get(state.withNoPathForMemoization());
}
}

/**
Expand Down