Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ca992f2

Browse files
authored
Merge pull request #5005 from tamasvajk/feature/follow-line
C#: Follow line directives when getting element location
2 parents 1fd2449 + 543f591 commit ca992f2

86 files changed

Lines changed: 14969 additions & 7025 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
lgtm,codescanning
2+
* The `PreprocessorDirective` class and its base classes have been added to support
3+
preprocessor directives, such as `#if`, `#define`, `#undef`, `#line`, `#region`,
4+
`#warning`, `#error`, `#pragma warning`, `#pragma checksum` and `#nullable`. Furthermore,
5+
`#line` directives are now taken into account when querying the location of any
6+
code construct. Files referenced in preprocessor directives are also included in the
7+
extraction sources. This change is expected to lead to better error reporting locations
8+
in generated code, such as generated code from `.cshtml` files in ASP.NET Core.

csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,12 @@ private static bool FileIsUpToDate(string src, string dest)
215215
/// <summary>
216216
/// Extracts compilation-wide entities, such as compilations and compiler diagnostics.
217217
/// </summary>
218-
public void AnalyseCompilation(string cwd, string[] args)
218+
public void AnalyseCompilation()
219219
{
220-
extractionTasks.Add(() => DoAnalyseCompilation(cwd, args));
220+
extractionTasks.Add(() => DoAnalyseCompilation());
221221
}
222222

223-
224-
225-
private void DoAnalyseCompilation(string cwd, string[] args)
223+
private void DoAnalyseCompilation()
226224
{
227225
try
228226
{
@@ -234,7 +232,7 @@ private void DoAnalyseCompilation(string cwd, string[] args)
234232
compilationTrapFile = trapWriter; // Dispose later
235233
var cx = extractor.CreateContext(compilation.Clone(), trapWriter, new AssemblyScope(assembly, assemblyPath), AddAssemblyTrapPrefix);
236234

237-
compilationEntity = new Entities.Compilation(cx, cwd, args);
235+
compilationEntity = Entities.Compilation.Create(cx);
238236
}
239237
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
240238
{
@@ -374,9 +372,15 @@ private void DoExtractTree(SyntaxTree tree)
374372
if (!upToDate)
375373
{
376374
var cx = extractor.CreateContext(compilation.Clone(), trapWriter, new SourceScope(tree), AddAssemblyTrapPrefix);
377-
Populators.CompilationUnit.Extract(cx, tree.GetRoot());
375+
// Ensure that the file itself is populated in case the source file is totally empty
376+
var root = tree.GetRoot();
377+
Extraction.Entities.File.Create(cx, root.SyntaxTree.FilePath);
378+
379+
var csNode = (CSharpSyntaxNode)root;
380+
csNode.Accept(new CompilationUnitVisitor(cx));
381+
csNode.Accept(new DirectiveVisitor(cx));
378382
cx.PopulateAll();
379-
cx.ExtractComments(cx.CommentGenerator);
383+
CommentPopulator.ExtractCommentBlocks(cx, cx.CommentGenerator);
380384
cx.PopulateAll();
381385
}
382386
}
@@ -389,7 +393,7 @@ private void DoExtractTree(SyntaxTree tree)
389393
}
390394
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
391395
{
392-
extractor.Message(new Message("Unhandled exception processing syntax tree", tree.FilePath, null, ex.StackTrace));
396+
extractor.Message(new Message($"Unhandled exception processing syntax tree. {ex.Message}", tree.FilePath, null, ex.StackTrace));
393397
}
394398
}
395399

csharp/extractor/Semmle.Extraction.CSharp/Entities/CommentLine.cs

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -21,97 +21,6 @@ private CommentLine(Context cx, Microsoft.CodeAnalysis.Location loc, CommentLine
2121
public string Text { get { return symbol.Item2; } }
2222
public string RawText { get; private set; }
2323

24-
public static void Extract(Context cx, SyntaxTrivia trivia)
25-
{
26-
switch (trivia.Kind())
27-
{
28-
case SyntaxKind.SingleLineDocumentationCommentTrivia:
29-
/*
30-
This is actually a multi-line comment consisting of /// lines.
31-
So split it up.
32-
*/
33-
34-
var text = trivia.ToFullString();
35-
36-
var split = text.Split('\n');
37-
var currentLocation = trivia.GetLocation().SourceSpan.Start - 3;
38-
39-
for (var line = 0; line < split.Length - 1; ++line)
40-
{
41-
var fullLine = split[line];
42-
var nextLineLocation = currentLocation + fullLine.Length + 1;
43-
fullLine = fullLine.TrimEnd('\r');
44-
var trimmedLine = fullLine;
45-
46-
var leadingSpaces = trimmedLine.IndexOf('/');
47-
if (leadingSpaces != -1)
48-
{
49-
fullLine = fullLine.Substring(leadingSpaces);
50-
currentLocation += leadingSpaces;
51-
trimmedLine = trimmedLine.Substring(leadingSpaces + 3); // Remove leading spaces and the "///"
52-
trimmedLine = trimmedLine.Trim();
53-
54-
var span = Microsoft.CodeAnalysis.Text.TextSpan.FromBounds(currentLocation, currentLocation + fullLine.Length);
55-
var location = Microsoft.CodeAnalysis.Location.Create(trivia.SyntaxTree, span);
56-
var commentType = CommentLineType.XmlDoc;
57-
cx.CommentGenerator.AddComment(Create(cx, location, commentType, trimmedLine, fullLine));
58-
}
59-
else
60-
{
61-
cx.ModelError("Unexpected comment format");
62-
}
63-
currentLocation = nextLineLocation;
64-
}
65-
break;
66-
67-
case SyntaxKind.SingleLineCommentTrivia:
68-
{
69-
var contents = trivia.ToString().Substring(2);
70-
var commentType = CommentLineType.Singleline;
71-
if (contents.Length > 0 && contents[0] == '/')
72-
{
73-
commentType = CommentLineType.XmlDoc;
74-
contents = contents.Substring(1); // An XML comment.
75-
}
76-
cx.CommentGenerator.AddComment(Create(cx, trivia.GetLocation(), commentType, contents.Trim(), trivia.ToFullString()));
77-
}
78-
break;
79-
case SyntaxKind.MultiLineDocumentationCommentTrivia:
80-
case SyntaxKind.MultiLineCommentTrivia:
81-
/* We receive a single SyntaxTrivia for a multiline block spanning several lines.
82-
So we split it into separate lines
83-
*/
84-
text = trivia.ToFullString();
85-
86-
split = text.Split('\n');
87-
currentLocation = trivia.GetLocation().SourceSpan.Start;
88-
89-
for (var line = 0; line < split.Length; ++line)
90-
{
91-
var fullLine = split[line];
92-
var nextLineLocation = currentLocation + fullLine.Length + 1;
93-
fullLine = fullLine.TrimEnd('\r');
94-
var trimmedLine = fullLine;
95-
if (line == 0)
96-
trimmedLine = trimmedLine.Substring(2);
97-
if (line == split.Length - 1)
98-
trimmedLine = trimmedLine.Substring(0, trimmedLine.Length - 2);
99-
trimmedLine = trimmedLine.Trim();
100-
101-
var span = Microsoft.CodeAnalysis.Text.TextSpan.FromBounds(currentLocation, currentLocation + fullLine.Length);
102-
var location = Microsoft.CodeAnalysis.Location.Create(trivia.SyntaxTree, span);
103-
var commentType = line == 0 ? CommentLineType.Multiline : CommentLineType.MultilineContinuation;
104-
cx.CommentGenerator.AddComment(Create(cx, location, commentType, trimmedLine, fullLine));
105-
currentLocation = nextLineLocation;
106-
}
107-
break;
108-
// Strangely, these are reported as SingleLineCommentTrivia.
109-
case SyntaxKind.DocumentationCommentExteriorTrivia:
110-
cx.ModelError($"Unhandled comment type {trivia.Kind()} for {trivia}");
111-
break;
112-
}
113-
}
114-
11524
private Extraction.Entities.Location location;
11625

11726
public override void Populate(TextWriter trapFile)
@@ -131,7 +40,7 @@ public override void WriteId(TextWriter trapFile)
13140
trapFile.Write(";commentline");
13241
}
13342

134-
private static CommentLine Create(Context cx, Microsoft.CodeAnalysis.Location loc, CommentLineType type, string text, string raw)
43+
internal static CommentLine Create(Context cx, Microsoft.CodeAnalysis.Location loc, CommentLineType type, string text, string raw)
13544
{
13645
var init = (loc, type, text, raw);
13746
return CommentLineFactory.Instance.CreateEntity(cx, init, init);

csharp/extractor/Semmle.Extraction.CSharp/Entities/Compilation.cs

Lines changed: 0 additions & 125 deletions
This file was deleted.

0 commit comments

Comments
 (0)