Skip to content

Commit df75ee9

Browse files
authored
Ensure VB parser is disposed (#76636)
Noticed then when I was trying out adding a pooled object to the parser and that the dispose wasn't always being called to release the object back to the pool. I'm still investigating whether I want to add the pooled data to the parser, but minimially, the existing dispose method should be called.
1 parent 095410a commit df75ee9

File tree

4 files changed

+62
-59
lines changed

4 files changed

+62
-59
lines changed

src/Compilers/VisualBasic/Portable/Scanner/Directives.vb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
5252
Me.GetNextTokenInState(ScannerState.VB)
5353

5454
Else
55-
Dim parser As New Parser(Me)
56-
57-
directiveTrivia = parser.ParseConditionalCompilationStatement()
58-
directiveTrivia = parser.ConsumeStatementTerminatorAfterDirective(directiveTrivia)
55+
Using parser = New Parser(Me)
56+
directiveTrivia = parser.ParseConditionalCompilationStatement()
57+
directiveTrivia = parser.ConsumeStatementTerminatorAfterDirective(directiveTrivia)
58+
End Using
5959
End If
6060

6161
Debug.Assert(directiveTrivia.FullWidth > 0, "should at least get #")

src/Compilers/VisualBasic/Portable/Scanner/XmlDocComments.vb

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -68,62 +68,62 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
6868
Me.MoveToNextSyntaxNodeInTrivia()
6969

7070
Else
71-
Dim parser As New Parser(Me)
72-
73-
Me.IsScanningXmlDoc = True
74-
Me._isStartingFirstXmlDocLine = True
75-
76-
' NOTE: Documentation comment syntax trivia must have at least one child xml node, because
77-
' all the ['''] trivia are created as leading trivia for appropriate tokens.
78-
' This means that we have to create at least one XmlText having trailing
79-
' EOL to represent an empty documentation comment: ['''<eol>]
80-
'
81-
' The problem with this approach is that in presence of some errors (like
82-
' not closed XML tags) we create missing tokens needed to represent the nodes
83-
' *after* that last <eol> of the doc comment trivia, that means all the locations
84-
' of created diagnostics will land on the first character of the next line
85-
' after documentation comment
86-
'
87-
' To workaround this we parse XML nodes in two phases:
88-
' - in the first phase we detect the last DocCommentLineBreak and create
89-
' end-of-xml token instead; this should force all diagnostics to be
90-
' reported on the next token location;
91-
' - in the second phase we continue parsing XML nodes but don't create
92-
' end-of-xml token which should just result in parsing one single node
93-
' of XmlText type containing EOL;
94-
' Then we merge the results and create resulting DocumentationCommentTrivia
95-
96-
' The first phase
97-
Me._endOfXmlInsteadOfLastDocCommentLineBreak = True
98-
Dim nodes = parser.ParseXmlContent(ScannerState.Content)
99-
100-
' The second phase
101-
Me._endOfXmlInsteadOfLastDocCommentLineBreak = False
102-
If nodes.Count = 0 AndAlso parser.CurrentToken.Kind = SyntaxKind.EndOfXmlToken Then
103-
' This must be an empty documentation comment, we need to reset scanner so
104-
' that the doc comment exterior trivia ([''']) lands on the final XmlNode
105-
106-
ResetLineBufferOffset()
107-
restorePoint.RestoreTokens(includeLookAhead:=False)
71+
Using parser = New Parser(Me)
72+
Me.IsScanningXmlDoc = True
10873
Me._isStartingFirstXmlDocLine = True
109-
End If
11074

111-
nodes = parser.ParseRestOfDocCommentContent(nodes)
112-
Me.IsScanningXmlDoc = False
75+
' NOTE: Documentation comment syntax trivia must have at least one child xml node, because
76+
' all the ['''] trivia are created as leading trivia for appropriate tokens.
77+
' This means that we have to create at least one XmlText having trailing
78+
' EOL to represent an empty documentation comment: ['''<eol>]
79+
'
80+
' The problem with this approach is that in presence of some errors (like
81+
' not closed XML tags) we create missing tokens needed to represent the nodes
82+
' *after* that last <eol> of the doc comment trivia, that means all the locations
83+
' of created diagnostics will land on the first character of the next line
84+
' after documentation comment
85+
'
86+
' To workaround this we parse XML nodes in two phases:
87+
' - in the first phase we detect the last DocCommentLineBreak and create
88+
' end-of-xml token instead; this should force all diagnostics to be
89+
' reported on the next token location;
90+
' - in the second phase we continue parsing XML nodes but don't create
91+
' end-of-xml token which should just result in parsing one single node
92+
' of XmlText type containing EOL;
93+
' Then we merge the results and create resulting DocumentationCommentTrivia
94+
95+
' The first phase
96+
Me._endOfXmlInsteadOfLastDocCommentLineBreak = True
97+
Dim nodes = parser.ParseXmlContent(ScannerState.Content)
98+
99+
' The second phase
100+
Me._endOfXmlInsteadOfLastDocCommentLineBreak = False
101+
If nodes.Count = 0 AndAlso parser.CurrentToken.Kind = SyntaxKind.EndOfXmlToken Then
102+
' This must be an empty documentation comment, we need to reset scanner so
103+
' that the doc comment exterior trivia ([''']) lands on the final XmlNode
104+
105+
ResetLineBufferOffset()
106+
restorePoint.RestoreTokens(includeLookAhead:=False)
107+
Me._isStartingFirstXmlDocLine = True
108+
End If
109+
110+
nodes = parser.ParseRestOfDocCommentContent(nodes)
111+
Me.IsScanningXmlDoc = False
113112

114-
Debug.Assert(nodes.Any)
115-
Debug.Assert(nodes(0).FullWidth > 0, "should at least get {'''EoL} ")
113+
Debug.Assert(nodes.Any)
114+
Debug.Assert(nodes(0).FullWidth > 0, "should at least get {'''EoL} ")
116115

117-
' restore _currentToken and lookahead,
118-
' but keep offset and PP state
119-
ResetLineBufferOffset()
116+
' restore _currentToken and lookahead,
117+
' but keep offset and PP state
118+
ResetLineBufferOffset()
120119

121-
docCommentSyntax = SyntaxFactory.DocumentationCommentTrivia(nodes)
120+
docCommentSyntax = SyntaxFactory.DocumentationCommentTrivia(nodes)
122121

123-
If Me.Options.DocumentationMode < DocumentationMode.Diagnose Then
124-
' All diagnostics coming from documentation comment are ignored
125-
docCommentSyntax.ClearFlags(GreenNode.NodeFlags.ContainsDiagnostics)
126-
End If
122+
If Me.Options.DocumentationMode < DocumentationMode.Diagnose Then
123+
' All diagnostics coming from documentation comment are ignored
124+
docCommentSyntax.ClearFlags(GreenNode.NodeFlags.ContainsDiagnostics)
125+
End If
126+
End Using
127127
End If
128128

129129
' RESTORE lookahead state and current token if there were any

src/Compilers/VisualBasic/Portable/Syntax/SyntaxNodeFactories.vb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,17 +292,18 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
292292
Using scanner As New InternalSyntax.Scanner(MakeSourceText(text, 0), VisualBasicParseOptions.Default) ' NOTE: Default options should be enough
293293
scanner.ForceScanningXmlDocMode()
294294

295-
Dim parser = New InternalSyntax.Parser(scanner)
296-
parser.GetNextToken(InternalSyntax.ScannerState.Element)
295+
Using parser = New InternalSyntax.Parser(scanner)
296+
parser.GetNextToken(InternalSyntax.ScannerState.Element)
297297

298-
Dim xmlName = InternalSyntax.SyntaxFactory.XmlName(
298+
Dim xmlName = InternalSyntax.SyntaxFactory.XmlName(
299299
Nothing, InternalSyntax.SyntaxFactory.XmlNameToken(parentElementName, SyntaxKind.XmlNameToken, Nothing, Nothing))
300300

301-
Return DirectCast(
301+
Return DirectCast(
302302
parser.ParseXmlAttribute(
303303
requireLeadingWhitespace:=False,
304304
AllowNameAsExpression:=False,
305305
xmlElementName:=xmlName).CreateRed(Nothing, 0), BaseXmlAttributeSyntax)
306+
End Using
306307
End Using
307308
End Function
308309

src/Compilers/VisualBasic/Portable/Syntax/VisualBasicSyntaxTree.vb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
126126

127127
Dim node As InternalSyntax.CompilationUnitSyntax
128128
Using scanner
129-
node = New Parser(scanner).ParseCompilationUnit()
129+
Using parser = New Parser(scanner)
130+
node = parser.ParseCompilationUnit()
131+
End Using
130132
End Using
131133

132134
Dim root = DirectCast(node.CreateRed(Nothing, 0), CompilationUnitSyntax)

0 commit comments

Comments
 (0)