Skip to content

Commit 295e022

Browse files
committed
Merge branch 'main' into improve-tainted-arithmetic
2 parents c444754 + 089e4e2 commit 295e022

File tree

268 files changed

+92765
-264
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

268 files changed

+92765
-264
lines changed

cpp/ql/test/library-tests/ir/ir/PrintAST.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5925,7 +5925,7 @@ ir.cpp:
59255925
# 705| getStmt(0): [ReturnStmt] return ...
59265926
# 705| getExpr(): [ConditionalExpr] ... ? ... : ...
59275927
# 705| Type = [UnknownType] unknown
5928-
# 705| ValueCategory = prvalue
5928+
# 705| ValueCategory = prvalue(load)
59295929
# 705| getCondition(): [LTExpr] ... < ...
59305930
# 705| Type = [UnknownType] unknown
59315931
# 705| ValueCategory = prvalue

csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ private void AnalyseProject(FileInfo project)
289289

290290
try
291291
{
292-
var csProj = new CsProjFile(project);
292+
var csProj = new Extraction.CSharp.CsProjFile(project);
293293

294294
foreach (var @ref in csProj.References)
295295
{

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ protected override void ExtractInitializers(TextWriter trapFile)
4747
var baseType = Symbol.ContainingType.BaseType;
4848
if (baseType is null)
4949
{
50-
Context.ModelError(Symbol, "Unable to resolve base type in implicit constructor initializer");
50+
if (Symbol.ContainingType.SpecialType != SpecialType.System_Object)
51+
{
52+
Context.ModelError(Symbol, "Unable to resolve base type in implicit constructor initializer");
53+
}
5154
return;
5255
}
5356

csharp/extractor/Semmle.Extraction.CSharp.Standalone/CsProjFile.cs renamed to csharp/extractor/Semmle.Extraction.CSharp/Extractor/CsProjFile.cs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
using System.Linq;
55
using System.Xml;
66

7-
namespace Semmle.BuildAnalyser
7+
namespace Semmle.Extraction.CSharp
88
{
99
/// <summary>
1010
/// Represents a .csproj file and reads information from it.
1111
/// </summary>
12-
internal class CsProjFile
12+
public class CsProjFile
1313
{
1414
private string Filename { get; }
1515

@@ -38,14 +38,14 @@ public CsProjFile(FileInfo filename)
3838
// unrecognised content or is the wrong version.
3939
// This currently always fails on Linux because
4040
// Microsoft.Build is not cross platform.
41-
(csFiles, references) = ReadMsBuildProject(filename);
41+
(csFiles, references, projectReferences) = ReadMsBuildProject(filename);
4242
}
4343
catch // lgtm[cs/catch-of-all-exceptions]
4444
{
4545
// There was some reason why the project couldn't be loaded.
4646
// Fall back to reading the Xml document directly.
4747
// This method however doesn't handle variable expansion.
48-
(csFiles, references) = ReadProjectFileAsXml(filename, Directory);
48+
(csFiles, references, projectReferences) = ReadProjectFileAsXml(filename, Directory);
4949
}
5050
}
5151

@@ -55,7 +55,7 @@ public CsProjFile(FileInfo filename)
5555
/// and there seems to be no way to make it succeed. Fails on Linux.
5656
/// </summary>
5757
/// <param name="filename">The file to read.</param>
58-
private static (string[] csFiles, string[] references) ReadMsBuildProject(FileInfo filename)
58+
private static (string[] csFiles, string[] references, string[] projectReferences) ReadMsBuildProject(FileInfo filename)
5959
{
6060
var msbuildProject = new Microsoft.Build.Execution.ProjectInstance(filename.FullName);
6161

@@ -64,13 +64,18 @@ private static (string[] csFiles, string[] references) ReadMsBuildProject(FileIn
6464
.Select(item => item.EvaluatedInclude)
6565
.ToArray();
6666

67+
var projectReferences = msbuildProject.Items
68+
.Where(item => item.ItemType == "ProjectReference")
69+
.Select(item => item.EvaluatedInclude)
70+
.ToArray();
71+
6772
var csFiles = msbuildProject.Items
6873
.Where(item => item.ItemType == "Compile")
6974
.Select(item => item.GetMetadataValue("FullPath"))
7075
.Where(fn => fn.EndsWith(".cs"))
7176
.ToArray();
7277

73-
return (csFiles, references);
78+
return (csFiles, references, projectReferences);
7479
}
7580

7681
/// <summary>
@@ -79,7 +84,7 @@ private static (string[] csFiles, string[] references) ReadMsBuildProject(FileIn
7984
/// fallback if ReadMsBuildProject() fails.
8085
/// </summary>
8186
/// <param name="fileName">The .csproj file.</param>
82-
private static (string[] csFiles, string[] references) ReadProjectFileAsXml(FileInfo fileName, string directoryName)
87+
private static (string[] csFiles, string[] references, string[] projectReferences) ReadProjectFileAsXml(FileInfo fileName, string directoryName)
8388
{
8489
var projFile = new XmlDocument();
8590
var mgr = new XmlNamespaceManager(projFile.NameTable);
@@ -109,8 +114,16 @@ private static (string[] csFiles, string[] references) ReadProjectFileAsXml(File
109114

110115
var additionalCsFiles = System.IO.Directory.GetFiles(directoryName, "*.cs", SearchOption.AllDirectories);
111116

117+
var projectReferences = root
118+
.SelectNodes("/Project/ItemGroup/ProjectReference/@Include", mgr)
119+
?.NodeList()
120+
.Select(node => node.Value)
121+
.Select(csproj => GetFullPath(csproj, projDir))
122+
.Where(s => s is not null)
123+
?? Enumerable.Empty<string>();
124+
112125
#nullable disable warnings
113-
return (explicitCsFiles.Concat(additionalCsFiles).ToArray(), Array.Empty<string>());
126+
return (explicitCsFiles.Concat(additionalCsFiles).ToArray(), Array.Empty<string>(), projectReferences.ToArray());
114127
#nullable restore warnings
115128
}
116129

@@ -135,7 +148,7 @@ private static (string[] csFiles, string[] references) ReadProjectFileAsXml(File
135148
.ToArray();
136149

137150
#nullable disable warnings
138-
return (csFiles, references);
151+
return (csFiles, references, Array.Empty<string>());
139152
#nullable restore warnings
140153
}
141154

@@ -150,13 +163,19 @@ private static (string[] csFiles, string[] references) ReadProjectFileAsXml(File
150163
}
151164

152165
private readonly string[] references;
166+
private readonly string[] projectReferences;
153167
private readonly string[] csFiles;
154168

155169
/// <summary>
156170
/// The list of references as a list of assembly IDs.
157171
/// </summary>
158172
public IEnumerable<string> References => references;
159173

174+
/// <summary>
175+
/// The list of project references in full path format.
176+
/// </summary>
177+
public IEnumerable<string> ProjectReferences => projectReferences;
178+
160179
/// <summary>
161180
/// The list of C# source files in full path format.
162181
/// </summary>

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ public static ExitCode Run(string[] args)
107107

108108
try
109109
{
110+
if (options.ProjectsToLoad.Any())
111+
{
112+
AddSourceFilesFromProjects(options.ProjectsToLoad, options.CompilerArguments, logger);
113+
}
114+
110115
var compilerVersion = new CompilerVersion(options);
111116

112117
if (compilerVersion.SkipExtraction)
@@ -146,6 +151,41 @@ public static ExitCode Run(string[] args)
146151
}
147152
}
148153

154+
private static void AddSourceFilesFromProjects(IEnumerable<string> projectsToLoad, IList<string> compilerArguments, ILogger logger)
155+
{
156+
logger.Log(Severity.Info, " Loading referenced projects.");
157+
var projects = new Queue<string>(projectsToLoad);
158+
var processed = new HashSet<string>();
159+
while (projects.Count > 0)
160+
{
161+
var project = projects.Dequeue();
162+
var fi = new FileInfo(project);
163+
if (processed.Contains(fi.FullName))
164+
{
165+
continue;
166+
}
167+
168+
processed.Add(fi.FullName);
169+
logger.Log(Severity.Info, " Processing referenced project: " + fi.FullName);
170+
171+
var csProj = new CsProjFile(fi);
172+
173+
foreach (var cs in csProj.Sources)
174+
{
175+
if (cs.Contains("/obj/"))
176+
{
177+
continue;
178+
}
179+
compilerArguments.Add(cs);
180+
}
181+
182+
foreach (var pr in csProj.ProjectReferences)
183+
{
184+
projects.Enqueue(pr);
185+
}
186+
}
187+
}
188+
149189
/// <summary>
150190
/// Gets the complete list of locations to locate references.
151191
/// </summary>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public sealed class Options : CommonOptions
1616
/// </summary>
1717
public string? Framework { get; set; }
1818

19+
/// <summary>
20+
/// Project files whose source files should be added to the compilation.
21+
/// Only used in tests.
22+
/// </summary>
23+
public IList<string> ProjectsToLoad { get; } = new List<string>();
24+
1925
/// <summary>
2026
/// All other arguments passed to the compilation.
2127
/// </summary>
@@ -68,6 +74,9 @@ public override bool HandleOption(string key, string value)
6874
case "framework":
6975
Framework = value;
7076
return true;
77+
case "load-sources-from-project":
78+
ProjectsToLoad.Add(value);
79+
return true;
7180
default:
7281
return base.HandleOption(key, value);
7382
}

csharp/extractor/Semmle.Extraction.CSharp/Semmle.Extraction.CSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
<ItemGroup>
2525
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0" />
26+
<PackageReference Include="Microsoft.Build" Version="16.9.0" />
2627
</ItemGroup>
2728

2829
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Tool to generate C# stubs from a qltest snapshot.
3+
*/
4+
5+
import csharp
6+
import Stubs
7+
8+
/** All public declarations from assemblies. */
9+
class AllExternalPublicDeclarations extends GeneratedDeclaration {
10+
AllExternalPublicDeclarations() { this.fromLibrary() }
11+
}
12+
13+
from Assembly a
14+
select a.getFullName(), a.getName(), a.getVersion().toString(), a.getFile().getAbsolutePath(),
15+
generatedCode(a)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Tool to generate C# stubs from a qltest snapshot.
3+
*/
4+
5+
import csharp
6+
import Stubs
7+
8+
/** All public declarations from source. */
9+
class AllDeclarations extends GeneratedDeclaration {
10+
AllDeclarations() { not this.fromLibrary() }
11+
}
12+
13+
/** Exclude types from these standard assemblies. */
14+
private class DefaultLibs extends ExcludedAssembly {
15+
DefaultLibs() {
16+
this.getName() = "System.Private.CoreLib" or
17+
this.getName() = "mscorlib" or
18+
this.getName() = "System.Runtime"
19+
}
20+
}
21+
22+
select concat(generatedCode(_) + "\n\n")

csharp/ql/src/Stubs/MinimalStubsFromSource.ql

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@ class UsedInSource extends GeneratedDeclaration {
2929
}
3030
}
3131

32-
select generatedCode()
32+
/** Exclude types from these standard assemblies. */
33+
private class DefaultLibs extends ExcludedAssembly {
34+
DefaultLibs() {
35+
this.getName() = "System.Private.CoreLib" or
36+
this.getName() = "mscorlib" or
37+
this.getName() = "System.Runtime"
38+
}
39+
}
40+
41+
select concat(generatedCode(_) + "\n\n")

0 commit comments

Comments
 (0)