Skip to content

Commit

Permalink
Merge pull request #15542 from tamasvajk/feature/relative-line-pragma
Browse files Browse the repository at this point in the history
C#: Try resolve relative paths in line mappings
  • Loading branch information
tamasvajk authored Feb 9, 2024
2 parents 31cb308 + 1c7e6e7 commit d46028f
Show file tree
Hide file tree
Showing 33 changed files with 120 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.IO;
using Microsoft.CodeAnalysis;
using Semmle.Util.Logging;

namespace Semmle.Extraction.CSharp.Entities
{
Expand All @@ -25,7 +27,8 @@ public override void Populate(TextWriter trapFile)
var mapped = Symbol.GetMappedLineSpan();
if (mapped.HasMappedPath && mapped.IsValid)
{
var mappedLoc = Create(Context, Location.Create(mapped.Path, default, mapped.Span));
var path = TryAdjustRelativeMappedFilePath(mapped.Path, Position.Path, Context.Extractor.Logger);
var mappedLoc = Create(Context, Location.Create(path, default, mapped.Span));

trapFile.locations_mapped(this, mappedLoc);
}
Expand Down Expand Up @@ -61,5 +64,25 @@ private class SourceLocationFactory : CachedEntityFactory<Location, NonGenerated

public override NonGeneratedSourceLocation Create(Context cx, Location init) => new NonGeneratedSourceLocation(cx, init);
}

public static string TryAdjustRelativeMappedFilePath(string mappedToPath, string mappedFromPath, ILogger logger)
{
if (!Path.IsPathRooted(mappedToPath))
{
try
{
var fullPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(mappedFromPath)!, mappedToPath));
logger.LogDebug($"Found relative path in line mapping: '{mappedToPath}', interpreting it as '{fullPath}'");

mappedToPath = fullPath;
}
catch (Exception e)
{
logger.LogDebug($"Failed to compute absolute path for relative path in line mapping: '{mappedToPath}': {e}");
}
}

return mappedToPath;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ protected override void PopulatePreprocessor(TextWriter trapFile)
{
trapFile.directive_lines(this, kind);

if (!string.IsNullOrWhiteSpace(Symbol.File.ValueText))
var path = Symbol.File.ValueText;
if (!string.IsNullOrWhiteSpace(path))
{
var file = File.Create(Context, Symbol.File.ValueText);
path = NonGeneratedSourceLocation.TryAdjustRelativeMappedFilePath(path, Symbol.SyntaxTree.FilePath, Context.Extractor.Logger);
var file = File.Create(Context, path);
trapFile.directive_line_file(this, file);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ private PragmaChecksumDirective(Context cx, PragmaChecksumDirectiveTriviaSyntax

protected override void PopulatePreprocessor(TextWriter trapFile)
{
var file = File.Create(Context, Symbol.File.ValueText);
var path = NonGeneratedSourceLocation.TryAdjustRelativeMappedFilePath(Symbol.File.ValueText, Symbol.SyntaxTree.FilePath, Context.Extractor.Logger);
var file = File.Create(Context, path);
trapFile.pragma_checksums(this, file, Symbol.Guid.ToString(), Symbol.Bytes.ToString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Areas_TestArea_Views_Shared_Test18 : global::Microsoft.AspNetCore.M
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
#line 6 "Areas/TestArea/Views/Shared/Test18.cshtml"
#line 6 "../Areas/TestArea/Views/Shared/Test18.cshtml"
if (Model != null)
{

Expand All @@ -33,15 +33,15 @@ public class Areas_TestArea_Views_Shared_Test18 : global::Microsoft.AspNetCore.M
#nullable disable
WriteLiteral(" <h3>Hello \"");
#nullable restore
#line 8 "Areas/TestArea/Views/Shared/Test18.cshtml"
#line 8 "../Areas/TestArea/Views/Shared/Test18.cshtml"
Write(Html.Raw(Model.Name));

#line default
#line hidden
#nullable disable
WriteLiteral("\"</h3>\n");
#nullable restore
#line 9 "Areas/TestArea/Views/Shared/Test18.cshtml"
#line 9 "../Areas/TestArea/Views/Shared/Test18.cshtml"
}

#line default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Areas_TestArea_Views_Test4_Test17 : global::Microsoft.AspNetCore.Mv
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
#line 6 "Areas/TestArea/Views/Test4/Test17.cshtml"
#line 6 "../Areas/TestArea/Views/Test4/Test17.cshtml"
if (Model != null)
{

Expand All @@ -33,15 +33,15 @@ public class Areas_TestArea_Views_Test4_Test17 : global::Microsoft.AspNetCore.Mv
#nullable disable
WriteLiteral(" <h3>Hello \"");
#nullable restore
#line 8 "Areas/TestArea/Views/Test4/Test17.cshtml"
#line 8 "../Areas/TestArea/Views/Test4/Test17.cshtml"
Write(Html.Raw(Model.Name));

#line default
#line hidden
#nullable disable
WriteLiteral("\"</h3>\n");
#nullable restore
#line 9 "Areas/TestArea/Views/Test4/Test17.cshtml"
#line 9 "../Areas/TestArea/Views/Test4/Test17.cshtml"
}

#line default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class MyAreas_Test4_Test22 : global::Microsoft.AspNetCore.Mvc.Razor.Razor
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
#line 6 "MyAreas/Test4/Test22.cshtml"
#line 6 "../MyAreas/Test4/Test22.cshtml"
if (Model != null)
{

Expand All @@ -33,15 +33,15 @@ public class MyAreas_Test4_Test22 : global::Microsoft.AspNetCore.Mvc.Razor.Razor
#nullable disable
WriteLiteral(" <h3>Hello \"");
#nullable restore
#line 8 "MyAreas/Test4/Test22.cshtml"
#line 8 "../MyAreas/Test4/Test22.cshtml"
Write(Html.Raw(Model.Name));

#line default
#line hidden
#nullable disable
WriteLiteral("\"</h3>\n");
#nullable restore
#line 9 "MyAreas/Test4/Test22.cshtml"
#line 9 "../MyAreas/Test4/Test22.cshtml"
}

#line default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Pages_Shared_Test21 : global::Microsoft.AspNetCore.Mvc.Razor.RazorP
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
#line 6 "Pages/Shared/Test21.cshtml"
#line 6 "../Pages/Shared/Test21.cshtml"
if (Model != null)
{

Expand All @@ -33,15 +33,15 @@ public class Pages_Shared_Test21 : global::Microsoft.AspNetCore.Mvc.Razor.RazorP
#nullable disable
WriteLiteral(" <h3>Hello \"");
#nullable restore
#line 8 "Pages/Shared/Test21.cshtml"
#line 8 "../Pages/Shared/Test21.cshtml"
Write(Html.Raw(Model.Name));

#line default
#line hidden
#nullable disable
WriteLiteral("\"</h3>\n");
#nullable restore
#line 9 "Pages/Shared/Test21.cshtml"
#line 9 "../Pages/Shared/Test21.cshtml"
}

#line default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ using test;
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
#line 6 "$PATHSLASH"
#line 6 "../$PATHSLASH"
if (Model != null)
{

Expand All @@ -33,15 +33,15 @@ using test;
#nullable disable
WriteLiteral(" <h3>Hello \"");
#nullable restore
#line 8 "$PATHSLASH"
#line 8 "../$PATHSLASH"
Write(Html.Raw(Model.Name));

#line default
#line hidden
#nullable disable
WriteLiteral("\"</h3>\n");
#nullable restore
#line 9 "$PATHSLASH"
#line 9 "../$PATHSLASH"
}

#line default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Views_Custom2_Test16 : global::Microsoft.AspNetCore.Mvc.Razor.Razor
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
#line 6 "Views/Custom2/Test16.cshtml"
#line 6 "../Views/Custom2/Test16.cshtml"
if (Model != null)
{

Expand All @@ -33,15 +33,15 @@ public class Views_Custom2_Test16 : global::Microsoft.AspNetCore.Mvc.Razor.Razor
#nullable disable
WriteLiteral(" <h3>Hello \"");
#nullable restore
#line 8 "Views/Custom2/Test16.cshtml"
#line 8 "../Views/Custom2/Test16.cshtml"
Write(Html.Raw(Model.Name));

#line default
#line hidden
#nullable disable
WriteLiteral("\"</h3>\n");
#nullable restore
#line 9 "Views/Custom2/Test16.cshtml"
#line 9 "../Views/Custom2/Test16.cshtml"
}

#line default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Views_Custom_Test3_Test15 : global::Microsoft.AspNetCore.Mvc.Razor.
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
#line 6 "Views/Custom/Test3/Test15.cshtml"
#line 6 "../Views/Custom/Test3/Test15.cshtml"
if (Model != null)
{

Expand All @@ -33,15 +33,15 @@ public class Views_Custom_Test3_Test15 : global::Microsoft.AspNetCore.Mvc.Razor.
#nullable disable
WriteLiteral(" <h3>Hello \"");
#nullable restore
#line 8 "Views/Custom/Test3/Test15.cshtml"
#line 8 "../Views/Custom/Test3/Test15.cshtml"
Write(Html.Raw(Model.Name));

#line default
#line hidden
#nullable disable
WriteLiteral("\"</h3>\n");
#nullable restore
#line 9 "Views/Custom/Test3/Test15.cshtml"
#line 9 "../Views/Custom/Test3/Test15.cshtml"
}

#line default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Views_Other_Test13 : global::Microsoft.AspNetCore.Mvc.Razor.RazorPa
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
#line 6 "Views/Other/Test13.cshtml"
#line 6 "../Views/Other/Test13.cshtml"
if (Model != null)
{

Expand All @@ -33,15 +33,15 @@ public class Views_Other_Test13 : global::Microsoft.AspNetCore.Mvc.Razor.RazorPa
#nullable disable
WriteLiteral(" <h3>Hello \"");
#nullable restore
#line 8 "Views/Other/Test13.cshtml"
#line 8 "../Views/Other/Test13.cshtml"
Write(Html.Raw(Model.Name));

#line default
#line hidden
#nullable disable
WriteLiteral("\"</h3>\n");
#nullable restore
#line 9 "Views/Other/Test13.cshtml"
#line 9 "../Views/Other/Test13.cshtml"
}

#line default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Views_Other_Test5 : global::Microsoft.AspNetCore.Mvc.Razor.RazorPag
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
#line 6 "Views/Other/Test5.cshtml"
#line 6 "../Views/Other/Test5.cshtml"
if (Model != null)
{

Expand All @@ -33,15 +33,15 @@ public class Views_Other_Test5 : global::Microsoft.AspNetCore.Mvc.Razor.RazorPag
#nullable disable
WriteLiteral(" <h3>Hello \"");
#nullable restore
#line 8 "Views/Other/Test5.cshtml"
#line 8 "../Views/Other/Test5.cshtml"
Write(Html.Raw(Model.Name));

#line default
#line hidden
#nullable disable
WriteLiteral("\"</h3>\n");
#nullable restore
#line 9 "Views/Other/Test5.cshtml"
#line 9 "../Views/Other/Test5.cshtml"
}

#line default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Views_Other_Test6 : global::Microsoft.AspNetCore.Mvc.Razor.RazorPag
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
#line 6 "Views/Other/Test6.cshtml"
#line 6 "../Views/Other/Test6.cshtml"
if (Model != null)
{

Expand All @@ -33,15 +33,15 @@ public class Views_Other_Test6 : global::Microsoft.AspNetCore.Mvc.Razor.RazorPag
#nullable disable
WriteLiteral(" <h3>Hello \"");
#nullable restore
#line 8 "Views/Other/Test6.cshtml"
#line 8 "../Views/Other/Test6.cshtml"
Write(Html.Raw(Model.Name));

#line default
#line hidden
#nullable disable
WriteLiteral("\"</h3>\n");
#nullable restore
#line 9 "Views/Other/Test6.cshtml"
#line 9 "../Views/Other/Test6.cshtml"
}

#line default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Views_Other_Test8 : global::Microsoft.AspNetCore.Mvc.Razor.RazorPag
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
#line 6 "Views/Other/Test8.cshtml"
#line 6 "../Views/Other/Test8.cshtml"
if (Model != null)
{

Expand All @@ -33,15 +33,15 @@ public class Views_Other_Test8 : global::Microsoft.AspNetCore.Mvc.Razor.RazorPag
#nullable disable
WriteLiteral(" <h3>Hello \"");
#nullable restore
#line 8 "Views/Other/Test8.cshtml"
#line 8 "../Views/Other/Test8.cshtml"
Write(Html.Raw(Model.Name));

#line default
#line hidden
#nullable disable
WriteLiteral("\"</h3>\n");
#nullable restore
#line 9 "Views/Other/Test8.cshtml"
#line 9 "../Views/Other/Test8.cshtml"
}

#line default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Views_Other_Test9 : global::Microsoft.AspNetCore.Mvc.Razor.RazorPag
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
#line 6 "Views/Other/Test9.cshtml"
#line 6 "../Views/Other/Test9.cshtml"
if (Model != null)
{

Expand All @@ -33,15 +33,15 @@ public class Views_Other_Test9 : global::Microsoft.AspNetCore.Mvc.Razor.RazorPag
#nullable disable
WriteLiteral(" <h3>Hello \"");
#nullable restore
#line 8 "Views/Other/Test9.cshtml"
#line 8 "../Views/Other/Test9.cshtml"
Write(Html.Raw(Model.Name));

#line default
#line hidden
#nullable disable
WriteLiteral("\"</h3>\n");
#nullable restore
#line 9 "Views/Other/Test9.cshtml"
#line 9 "../Views/Other/Test9.cshtml"
}

#line default
Expand Down
Loading

0 comments on commit d46028f

Please sign in to comment.