Skip to content

Commit 19f1d2b

Browse files
authored
Merge pull request github#13290 from tamasvajk/feature/source-generators
C#: Extract source files generated by source generators
2 parents 16bc584 + 138bfad commit 19f1d2b

File tree

9 files changed

+56
-5
lines changed

9 files changed

+56
-5
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,17 @@ private static ExitCode AnalyseTracing(
381381
references => ResolveReferences(compilerArguments, analyser, canonicalPathCache, references),
382382
(analyser, syntaxTrees) =>
383383
{
384+
var paths = compilerArguments.SourceFiles
385+
.Select(src => src.Path)
386+
.ToList();
387+
388+
if (compilerArguments.GeneratedFilesOutputDirectory is not null)
389+
{
390+
paths.AddRange(Directory.GetFiles(compilerArguments.GeneratedFilesOutputDirectory, "*.cs", SearchOption.AllDirectories));
391+
}
392+
384393
return ReadSyntaxTrees(
385-
compilerArguments.SourceFiles.Select(src => canonicalPathCache.GetCanonicalPath(src.Path)),
394+
paths.Select(canonicalPathCache.GetCanonicalPath),
386395
analyser,
387396
compilerArguments.ParseOptions,
388397
compilerArguments.Encoding,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
| Program.cs:0:0:0:0 | Program.cs |
2+
| obj/Debug/net7.0/.NETCoreApp,Version=v7.0.AssemblyAttributes.cs:0:0:0:0 | obj/Debug/net7.0/.NETCoreApp,Version=v7.0.AssemblyAttributes.cs |
3+
| obj/Debug/net7.0/cshtml.AssemblyInfo.cs:0:0:0:0 | obj/Debug/net7.0/cshtml.AssemblyInfo.cs |
4+
| obj/Debug/net7.0/cshtml.GlobalUsings.g.cs:0:0:0:0 | obj/Debug/net7.0/cshtml.GlobalUsings.g.cs |
5+
| obj/Debug/net7.0/cshtml.RazorAssemblyInfo.cs:0:0:0:0 | obj/Debug/net7.0/cshtml.RazorAssemblyInfo.cs |
6+
| obj/Debug/net7.0/generated/Microsoft.NET.Sdk.Razor.SourceGenerators/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/Views_Home_Index_cshtml.g.cs:0:0:0:0 | obj/Debug/net7.0/generated/Microsoft.NET.Sdk.Razor.SourceGenerators/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/Views_Home_Index_cshtml.g.cs |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import csharp
2+
3+
from File f
4+
where f.fromSource()
5+
select f
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var dummy = "dummy";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<div class="text-center">
6+
<h1 class="display-4">Welcome</h1>
7+
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
8+
</div>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<Target Name="DeleteBinObjFolders" BeforeTargets="Clean">
11+
<RemoveDir Directories=".\bin" />
12+
<RemoveDir Directories=".\obj" />
13+
</Target>
14+
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from create_database_utils import *
2+
3+
run_codeql_database_create(['dotnet build'], lang="csharp", extra_args=["--extractor-option=cil=false"])
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: majorAnalysis
3+
---
4+
* The extractor has been changed to run after the traced compiler call. This allows inspecting compiler generated files, such as the output of source generators. With this change, `.cshtml` files and their generated `.cshtml.g.cs` counterparts are extracted on dotnet 6 and above.

csharp/tools/tracing-config.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function RegisterExtractorPack(id)
6363
end
6464
end
6565
if match then
66-
local injections = { '-p:UseSharedCompilation=false' }
66+
local injections = { '-p:UseSharedCompilation=false', '-p:EmitCompilerGeneratedFiles=true' }
6767
if dotnetRunNeedsSeparator then
6868
table.insert(injections, '--')
6969
end
@@ -118,7 +118,8 @@ function RegisterExtractorPack(id)
118118
compilerArguments,
119119
nil, {
120120
'/p:UseSharedCompilation=false',
121-
'/p:MvcBuildViews=true'
121+
'/p:MvcBuildViews=true',
122+
'/p:EmitCompilerGeneratedFiles=true',
122123
})
123124

124125
}
@@ -154,7 +155,7 @@ function RegisterExtractorPack(id)
154155

155156
if seenCompilerCall then
156157
return {
157-
order = ORDER_BEFORE,
158+
order = ORDER_AFTER,
158159
invocation = {
159160
path = AbsolutifyExtractorPath(id, extractor),
160161
arguments = {
@@ -194,7 +195,7 @@ function RegisterExtractorPack(id)
194195

195196
if seenCompilerCall then
196197
return {
197-
order = ORDER_BEFORE,
198+
order = ORDER_AFTER,
198199
invocation = {
199200
path = AbsolutifyExtractorPath(id, extractor),
200201
arguments = {

0 commit comments

Comments
 (0)