|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.CodeAnalysis.CSharp; |
| 3 | +using Semmle.Extraction.CommentProcessing; |
| 4 | +using Semmle.Extraction.CSharp.Entities; |
| 5 | +using System; |
| 6 | + |
| 7 | +namespace Semmle.Extraction.CSharp.Populators |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Populators for trivias. |
| 11 | + /// </summary> |
| 12 | + public static class TriviaPopulator |
| 13 | + { |
| 14 | + public static void ExtractCommentBlocks(Context cx, ICommentGenerator gen) |
| 15 | + { |
| 16 | + cx.Try(null, null, () => |
| 17 | + { |
| 18 | + gen.GenerateBindings((entity, duplicationGuardKey, block, binding) => |
| 19 | + { |
| 20 | + var commentBlock = Entities.CommentBlock.Create(cx, block); |
| 21 | + Action a = () => |
| 22 | + { |
| 23 | + commentBlock.BindTo(entity, binding); |
| 24 | + }; |
| 25 | + // When the duplication guard key exists, it means that the entity is guarded against |
| 26 | + // trap duplication (<see cref = "Context.BindComments(IEntity, Location)" />). |
| 27 | + // We must therefore also guard comment construction. |
| 28 | + if (duplicationGuardKey != null) |
| 29 | + cx.WithDuplicationGuard(duplicationGuardKey, a); |
| 30 | + else |
| 31 | + a(); |
| 32 | + }); |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + public static void ExtractTrivia(Context cx, SyntaxTrivia trivia) |
| 37 | + { |
| 38 | + switch (trivia.Kind()) |
| 39 | + { |
| 40 | + case SyntaxKind.SingleLineDocumentationCommentTrivia: |
| 41 | + /* |
| 42 | + This is actually a multi-line comment consisting of /// lines. |
| 43 | + So split it up. |
| 44 | + */ |
| 45 | + |
| 46 | + var text = trivia.ToFullString(); |
| 47 | + |
| 48 | + var split = text.Split('\n'); |
| 49 | + var currentLocation = trivia.GetLocation().SourceSpan.Start - 3; |
| 50 | + |
| 51 | + for (var line = 0; line < split.Length - 1; ++line) |
| 52 | + { |
| 53 | + var fullLine = split[line]; |
| 54 | + var nextLineLocation = currentLocation + fullLine.Length + 1; |
| 55 | + fullLine = fullLine.TrimEnd('\r'); |
| 56 | + var trimmedLine = fullLine; |
| 57 | + |
| 58 | + var leadingSpaces = trimmedLine.IndexOf('/'); |
| 59 | + if (leadingSpaces != -1) |
| 60 | + { |
| 61 | + fullLine = fullLine.Substring(leadingSpaces); |
| 62 | + currentLocation += leadingSpaces; |
| 63 | + trimmedLine = trimmedLine.Substring(leadingSpaces + 3); // Remove leading spaces and the "///" |
| 64 | + trimmedLine = trimmedLine.Trim(); |
| 65 | + |
| 66 | + var span = Microsoft.CodeAnalysis.Text.TextSpan.FromBounds(currentLocation, currentLocation + fullLine.Length); |
| 67 | + var location = Microsoft.CodeAnalysis.Location.Create(trivia.SyntaxTree, span); |
| 68 | + var commentType = CommentLineType.XmlDoc; |
| 69 | + cx.CommentGenerator.AddComment(CommentLine.Create(cx, location, commentType, trimmedLine, fullLine)); |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + cx.ModelError("Unexpected comment format"); |
| 74 | + } |
| 75 | + currentLocation = nextLineLocation; |
| 76 | + } |
| 77 | + break; |
| 78 | + |
| 79 | + case SyntaxKind.SingleLineCommentTrivia: |
| 80 | + { |
| 81 | + var contents = trivia.ToString().Substring(2); |
| 82 | + var commentType = CommentLineType.Singleline; |
| 83 | + if (contents.Length > 0 && contents[0] == '/') |
| 84 | + { |
| 85 | + commentType = CommentLineType.XmlDoc; |
| 86 | + contents = contents.Substring(1); // An XML comment. |
| 87 | + } |
| 88 | + cx.CommentGenerator.AddComment(CommentLine.Create(cx, trivia.GetLocation(), commentType, contents.Trim(), trivia.ToFullString())); |
| 89 | + } |
| 90 | + break; |
| 91 | + case SyntaxKind.MultiLineDocumentationCommentTrivia: |
| 92 | + case SyntaxKind.MultiLineCommentTrivia: |
| 93 | + /* We receive a single SyntaxTrivia for a multiline block spanning several lines. |
| 94 | + So we split it into separate lines |
| 95 | + */ |
| 96 | + text = trivia.ToFullString(); |
| 97 | + |
| 98 | + split = text.Split('\n'); |
| 99 | + currentLocation = trivia.GetLocation().SourceSpan.Start; |
| 100 | + |
| 101 | + for (var line = 0; line < split.Length; ++line) |
| 102 | + { |
| 103 | + var fullLine = split[line]; |
| 104 | + var nextLineLocation = currentLocation + fullLine.Length + 1; |
| 105 | + fullLine = fullLine.TrimEnd('\r'); |
| 106 | + var trimmedLine = fullLine; |
| 107 | + if (line == 0) |
| 108 | + trimmedLine = trimmedLine.Substring(2); |
| 109 | + if (line == split.Length - 1) |
| 110 | + trimmedLine = trimmedLine.Substring(0, trimmedLine.Length - 2); |
| 111 | + trimmedLine = trimmedLine.Trim(); |
| 112 | + |
| 113 | + var span = Microsoft.CodeAnalysis.Text.TextSpan.FromBounds(currentLocation, currentLocation + fullLine.Length); |
| 114 | + var location = Microsoft.CodeAnalysis.Location.Create(trivia.SyntaxTree, span); |
| 115 | + var commentType = line == 0 ? CommentLineType.Multiline : CommentLineType.MultilineContinuation; |
| 116 | + cx.CommentGenerator.AddComment(CommentLine.Create(cx, location, commentType, trimmedLine, fullLine)); |
| 117 | + currentLocation = nextLineLocation; |
| 118 | + } |
| 119 | + break; |
| 120 | + // Strangely, these are reported as SingleLineCommentTrivia. |
| 121 | + case SyntaxKind.DocumentationCommentExteriorTrivia: |
| 122 | + cx.ModelError($"Unhandled comment type {trivia.Kind()} for {trivia}"); |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments