Skip to content

Commit 9f22955

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Privatize ParsedFileState, follow-on work
Work towards #60635 I set out to rename `ParsedFileState.errors` and was concerned it might be a breaking change, but it turned out that the class could be made private. As well as ParsedFileStateCache. But then `getParsed` would return a private type. That could be fixed by making `getParsed` private. Also the `parsedFileStateCache` getter seemed suspicious. Turned out it was only used outside the library in order to clear the cache. So I make the field private (at which point analyzer told me it should be final), and added a clearing method. Change-Id: Iaf30a9cc2a0cfb5c9168db243b7fb2f6c624d8e1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/433264 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 449ecbb commit 9f22955

File tree

3 files changed

+66
-62
lines changed

3 files changed

+66
-62
lines changed

pkg/analyzer/lib/src/dart/analysis/driver.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ class AnalysisDriver {
575575
}
576576

577577
void afterPerformWork() {
578-
_fsState.parsedFileStateCache.clear();
578+
_fsState.clearParsedFileStateCache();
579579
}
580580

581581
/// Return a [Future] that completes after pending file changes are applied,

pkg/analyzer/lib/src/dart/analysis/file_state.dart

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -590,30 +590,6 @@ class FileState {
590590
return other is FileState && other.uri == uri;
591591
}
592592

593-
/// Returns either new, or cached parsed result for this file.
594-
ParsedFileState getParsed({required OperationPerformanceImpl performance}) {
595-
var result = _fsState.parsedFileStateCache.get(this);
596-
if (result != null) {
597-
return result;
598-
}
599-
600-
var diagnosticListener = RecordingDiagnosticListener();
601-
var unit = parseCode(
602-
code: content,
603-
diagnosticListener: diagnosticListener,
604-
performance: performance,
605-
);
606-
607-
result = ParsedFileState(
608-
code: content,
609-
unit: unit,
610-
errors: diagnosticListener.diagnostics,
611-
);
612-
_fsState.parsedFileStateCache.put(this, result);
613-
614-
return result;
615-
}
616-
617593
/// Return a new parsed unresolved [CompilationUnit].
618594
CompilationUnitImpl parse({
619595
DiagnosticListener diagnosticListener = DiagnosticListener.NULL_LISTENER,
@@ -680,7 +656,7 @@ class FileState {
680656
var rawFileState = _fsState.fileContentStrategy.get(path);
681657
var contentChanged = _fileContent?.contentHash != rawFileState.contentHash;
682658
_fileContent = rawFileState;
683-
_fsState.parsedFileStateCache.remove(this);
659+
_fsState._parsedFileStateCache.remove(this);
684660

685661
// Prepare the unlinked bundle key.
686662
var previousUnlinkedKey = _unlinkedKey;
@@ -826,6 +802,30 @@ class FileState {
826802
return _fsState.getFileForUri(absoluteUri);
827803
}
828804

805+
/// Returns either new, or cached parsed result for this file.
806+
_ParsedFileState _getParsed({required OperationPerformanceImpl performance}) {
807+
var result = _fsState._parsedFileStateCache.get(this);
808+
if (result != null) {
809+
return result;
810+
}
811+
812+
var diagnosticListener = RecordingDiagnosticListener();
813+
var unit = parseCode(
814+
code: content,
815+
diagnosticListener: diagnosticListener,
816+
performance: performance,
817+
);
818+
819+
result = _ParsedFileState(
820+
code: content,
821+
unit: unit,
822+
diagnostics: diagnosticListener.diagnostics,
823+
);
824+
_fsState._parsedFileStateCache.put(this, result);
825+
826+
return result;
827+
}
828+
829829
/// Return the unlinked unit, freshly deserialized from bytes,
830830
/// previously deserialized from bytes, or new.
831831
AnalysisDriverUnlinkedUnit _getUnlinkedUnit(
@@ -855,7 +855,7 @@ class FileState {
855855
return result;
856856
}
857857

858-
var unit = getParsed(performance: performance).unit;
858+
var unit = _getParsed(performance: performance).unit;
859859

860860
return performance.run('compute', (performance) {
861861
var unlinkedUnit = performance.run('serializeAstUnlinked2', (
@@ -1284,7 +1284,7 @@ class FileSystemState {
12841284
/// in the process of a single analysis operation. But after that, even
12851285
/// if these results are still valid, they are often never used again. So,
12861286
/// currently we clear the cache after each operation.
1287-
ParsedFileStateCache parsedFileStateCache = ParsedFileStateCache();
1287+
final _ParsedFileStateCache _parsedFileStateCache = _ParsedFileStateCache();
12881288

12891289
FileSystemState(
12901290
this._byteStore,
@@ -1340,6 +1340,11 @@ class FileSystemState {
13401340
}
13411341
}
13421342

1343+
/// Clears the parsed file state cache.
1344+
void clearParsedFileStateCache() {
1345+
_parsedFileStateCache.clear();
1346+
}
1347+
13431348
/// Collected files that transitively reference a file with the [path].
13441349
/// These files are potentially affected by the change.
13451350
void collectAffected(String path, Set<FileState> affected) {
@@ -2105,39 +2110,6 @@ class LibraryResolutionResult {
21052110
LibraryResolutionResult({required this.requirements, required this.bytes});
21062111
}
21072112

2108-
class ParsedFileState {
2109-
final String code;
2110-
final CompilationUnitImpl unit;
2111-
// TODO(srawlins): Rename to `diagnostics`.
2112-
final List<Diagnostic> errors;
2113-
2114-
ParsedFileState({
2115-
required this.code,
2116-
required this.unit,
2117-
required this.errors,
2118-
});
2119-
}
2120-
2121-
class ParsedFileStateCache {
2122-
final Map<FileState, ParsedFileState> _map = Map.identity();
2123-
2124-
void clear() {
2125-
_map.clear();
2126-
}
2127-
2128-
ParsedFileState? get(FileState file) {
2129-
return _map[file];
2130-
}
2131-
2132-
void put(FileState file, ParsedFileState result) {
2133-
_map[file] = result;
2134-
}
2135-
2136-
void remove(FileState file) {
2137-
_map.remove(file);
2138-
}
2139-
}
2140-
21412113
/// The file has `part of` directive.
21422114
sealed class PartFileKind extends FileKind {
21432115
PartFileKind({required super.file});
@@ -2427,6 +2399,38 @@ class _LibraryNameToFiles {
24272399
}
24282400
}
24292401

2402+
class _ParsedFileState {
2403+
final String code;
2404+
final CompilationUnitImpl unit;
2405+
final List<Diagnostic> diagnostics;
2406+
2407+
_ParsedFileState({
2408+
required this.code,
2409+
required this.unit,
2410+
required this.diagnostics,
2411+
});
2412+
}
2413+
2414+
class _ParsedFileStateCache {
2415+
final Map<FileState, _ParsedFileState> _map = Map.identity();
2416+
2417+
void clear() {
2418+
_map.clear();
2419+
}
2420+
2421+
_ParsedFileState? get(FileState file) {
2422+
return _map[file];
2423+
}
2424+
2425+
void put(FileState file, _ParsedFileState result) {
2426+
_map[file] = result;
2427+
}
2428+
2429+
void remove(FileState file) {
2430+
_map.remove(file);
2431+
}
2432+
}
2433+
24302434
extension on List<DirectiveState> {
24312435
void disposeAll() {
24322436
for (var directive in this) {

pkg/analyzer/lib/src/dart/micro/resolve_file.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ class FileResolver {
679679
}
680680

681681
void _clearFileSystemStateParsedCache() {
682-
fsState?.parsedFileStateCache.clear();
682+
fsState?.clearParsedFileStateCache();
683683
}
684684

685685
/// Make sure that [fsState], [contextObjects], and [libraryContext] are

0 commit comments

Comments
 (0)