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

Skip to content

Commit c0aaed2

Browse files
committed
Merge branch 'main' into oparray2
2 parents ae807f7 + e2c6a01 commit c0aaed2

67 files changed

Lines changed: 1329 additions & 444 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.

csharp/extractor/Semmle.Extraction/CommentProcessing.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void AddComment(ICommentLine comment)
3535

3636
class LocationComparer : IComparer<Location>
3737
{
38-
public int Compare(Location l1, Location l2) => CommentProcessor.Compare(l1, l2);
38+
public int Compare(Location? l1, Location? l2) => CommentProcessor.Compare(l1, l2);
3939
}
4040

4141
/// <summary>
@@ -44,8 +44,12 @@ class LocationComparer : IComparer<Location>
4444
/// <param name="l1">First location</param>
4545
/// <param name="l2">Second location</param>
4646
/// <returns>&lt;0 if l1 before l2, &gt;0 if l1 after l2, else 0.</returns>
47-
static int Compare(Location l1, Location l2)
47+
static int Compare(Location? l1, Location? l2)
4848
{
49+
if (object.ReferenceEquals(l1, l2)) return 0;
50+
if (l1 == null) return -1;
51+
if (l2 == null) return 1;
52+
4953
int diff = l1.SourceTree == l2.SourceTree ? 0 : l1.SourceTree.FilePath.CompareTo(l2.SourceTree.FilePath);
5054
if (diff != 0) return diff;
5155
diff = l1.SourceSpan.Start - l2.SourceSpan.Start;
@@ -243,7 +247,7 @@ CommentBindingCallback cb
243247
/// Process comments up until nextElement.
244248
/// Group comments into blocks, and associate blocks with elements.
245249
/// </summary>
246-
///
250+
///
247251
/// <param name="commentEnumerator">Enumerator for all comments in the program.</param>
248252
/// <param name="nextElement">The next element in the list.</param>
249253
/// <param name="elementStack">A stack of nested program elements.</param>
@@ -261,7 +265,7 @@ CommentBindingCallback cb
261265
// Iterate comments until the commentEnumerator has gone past nextElement
262266
while (nextElement == null || Compare(commentEnumerator.Current.Value.Location, nextElement.Value.Key) < 0)
263267
{
264-
if(block is null)
268+
if (block is null)
265269
block = new CommentBlock(commentEnumerator.Current.Value);
266270

267271
if (!block.CombinesWith(commentEnumerator.Current.Value))
@@ -284,7 +288,7 @@ CommentBindingCallback cb
284288
}
285289
}
286290

287-
if(!(block is null))
291+
if (!(block is null))
288292
GenerateBindings(block, elementStack, nextElement, cb);
289293

290294
return true;

csharp/ql/src/semmle/code/csharp/PrintAst.qll

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,13 @@ class PrintAstNode extends TPrintAstNode {
151151
/**
152152
* Gets a textual representation of this node in the PrintAst output tree.
153153
*/
154-
abstract string toString();
154+
string toString() { none() }
155155

156156
/**
157157
* Gets the child node at index `childIndex`. Child indices must be unique,
158158
* but need not be contiguous.
159159
*/
160-
abstract PrintAstNode getChild(int childIndex);
160+
PrintAstNode getChild(int childIndex) { none() }
161161

162162
/**
163163
* Gets a child of this node.
@@ -172,7 +172,7 @@ class PrintAstNode extends TPrintAstNode {
172172
/**
173173
* Gets the location of this node in the source code.
174174
*/
175-
abstract Location getLocation();
175+
Location getLocation() { none() }
176176

177177
/**
178178
* Gets the value of the property of this node, where the name of the property
@@ -194,6 +194,30 @@ class PrintAstNode extends TPrintAstNode {
194194
}
195195
}
196196

197+
/** A top-level AST node. */
198+
class TopLevelPrintAstNode extends PrintAstNode {
199+
TopLevelPrintAstNode() { not exists(this.getParent()) }
200+
201+
private int getOrder() {
202+
this =
203+
rank[result](TopLevelPrintAstNode n, Location l |
204+
l = n.getLocation()
205+
|
206+
n
207+
order by
208+
l.getFile().getRelativePath(), l.getStartLine(), l.getStartColumn(), l.getEndLine(),
209+
l.getEndColumn()
210+
)
211+
}
212+
213+
override string getProperty(string key) {
214+
result = super.getProperty(key)
215+
or
216+
key = "semmle.order" and
217+
result = this.getOrder().toString()
218+
}
219+
}
220+
197221
/**
198222
* A node representing an AST node with an underlying `Element`.
199223
*/

csharp/ql/src/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ abstract class NodeImpl extends Node {
2626

2727
/** Gets the type of this node used for type pruning. */
2828
cached
29-
DataFlowType getDataFlowType() {
29+
Gvn::GvnType getDataFlowType() {
3030
Stages::DataFlowStage::forceCachingInSameStage() and
3131
exists(Type t0 | result = Gvn::getGlobalValueNumber(t0) |
3232
t0 = getCSharpType(this.getType())
@@ -490,14 +490,23 @@ private Type getCSharpType(DotNet::Type t) {
490490
result.matchesHandle(t)
491491
}
492492

493+
/** A GVN type that is either a `DataFlowType` or unifiable with a `DataFlowType`. */
494+
private class DataFlowTypeOrUnifiable extends Gvn::GvnType {
495+
pragma[nomagic]
496+
DataFlowTypeOrUnifiable() {
497+
this instanceof DataFlowType or
498+
Gvn::unifiable(any(DataFlowType t), this)
499+
}
500+
}
501+
493502
pragma[noinline]
494-
private TypeParameter getATypeParameterSubType(DataFlowType t) {
503+
private TypeParameter getATypeParameterSubType(DataFlowTypeOrUnifiable t) {
495504
not t instanceof Gvn::TypeParameterGvnType and
496505
exists(Type t0 | t = Gvn::getGlobalValueNumber(t0) | implicitConversionRestricted(result, t0))
497506
}
498507

499508
pragma[noinline]
500-
private DataFlowType getANonTypeParameterSubType(DataFlowType t) {
509+
private Gvn::GvnType getANonTypeParameterSubType(DataFlowTypeOrUnifiable t) {
501510
not t instanceof Gvn::TypeParameterGvnType and
502511
not result instanceof Gvn::TypeParameterGvnType and
503512
exists(Type t1, Type t2 |
@@ -728,12 +737,8 @@ private module Cached {
728737
)
729738
}
730739

731-
/**
732-
* Holds if GVNs `t1` and `t2` may have a common sub type. Neither `t1` nor
733-
* `t2` are allowed to be type parameters.
734-
*/
735-
cached
736-
predicate commonSubType(DataFlowType t1, DataFlowType t2) {
740+
pragma[nomagic]
741+
private predicate commonSubTypeGeneral(DataFlowTypeOrUnifiable t1, DataFlowType t2) {
737742
not t1 instanceof Gvn::TypeParameterGvnType and
738743
t1 = t2
739744
or
@@ -742,11 +747,18 @@ private module Cached {
742747
getANonTypeParameterSubType(t1) = getANonTypeParameterSubType(t2)
743748
}
744749

750+
/**
751+
* Holds if GVNs `t1` and `t2` may have a common sub type. Neither `t1` nor
752+
* `t2` are allowed to be type parameters.
753+
*/
754+
cached
755+
predicate commonSubType(DataFlowType t1, DataFlowType t2) { commonSubTypeGeneral(t1, t2) }
756+
745757
cached
746758
predicate commonSubTypeUnifiableLeft(DataFlowType t1, DataFlowType t2) {
747-
exists(DataFlowType t |
759+
exists(Gvn::GvnType t |
748760
Gvn::unifiable(t1, t) and
749-
commonSubType(t, t2)
761+
commonSubTypeGeneral(t, t2)
750762
)
751763
}
752764

@@ -2004,7 +2016,7 @@ module LibraryFlow {
20042016

20052017
/** Gets the type of content `c`. */
20062018
pragma[noinline]
2007-
private DataFlowType getContentType(Content c) {
2019+
private Gvn::GvnType getContentType(Content c) {
20082020
exists(Type t | result = Gvn::getGlobalValueNumber(t) |
20092021
t = c.(FieldContent).getField().getType()
20102022
or
@@ -2031,7 +2043,7 @@ class LibraryCodeNode extends NodeImpl, TLibraryCodeNode {
20312043

20322044
override Callable getEnclosingCallableImpl() { result = callCfn.getEnclosingCallable() }
20332045

2034-
override DataFlowType getDataFlowType() {
2046+
override Gvn::GvnType getDataFlowType() {
20352047
exists(LibraryFlow::AdjustedAccessPath ap |
20362048
state = LibraryFlow::TLibraryCodeNodeAfterReadState(ap) and
20372049
if sinkAp.length() = 0 and state.isLastReadState() and preservesValue = true
@@ -2194,6 +2206,20 @@ private class ReadStepConfiguration extends ControlFlowReachabilityConfiguration
21942206

21952207
predicate readStep = readStepImpl/3;
21962208

2209+
/**
2210+
* An entity used to represent the type of data-flow node. Two nodes will have
2211+
* the same `DataFlowType` when the underlying `Type`s are structurally equal
2212+
* modulo type parameters and identity conversions.
2213+
*
2214+
* For example, `Func<T, int>` and `Func<S, int>` are mapped to the same
2215+
* `DataFlowType`, while `Func<T, int>` and `Func<string, int>` are not, because
2216+
* `string` is not a type parameter.
2217+
*/
2218+
class DataFlowType extends Gvn::GvnType {
2219+
pragma[nomagic]
2220+
DataFlowType() { this = any(NodeImpl n).getDataFlowType() }
2221+
}
2222+
21972223
/** Gets the type of `n` used for type pruning. */
21982224
DataFlowType getNodeType(NodeImpl n) { result = n.getDataFlowType() }
21992225

@@ -2323,8 +2349,6 @@ class CastNode extends Node {
23232349

23242350
class DataFlowExpr = DotNet::Expr;
23252351

2326-
class DataFlowType = Gvn::GvnType;
2327-
23282352
/** Holds if `e` is an expression that always has the same Boolean value `val`. */
23292353
private predicate constantBooleanExpr(Expr e, boolean val) {
23302354
e = any(AbstractValues::BooleanValue bv | val = bv.getValue()).getAnExpr()

csharp/ql/src/semmle/code/csharp/dataflow/internal/DataFlowPublic.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ class Content extends TContent {
222222
Location getLocation() { none() }
223223

224224
/** Gets the type of the object containing this content. */
225-
deprecated DataFlowType getContainerType() { none() }
225+
deprecated Gvn::GvnType getContainerType() { none() }
226226

227227
/** Gets the type of this content. */
228-
deprecated DataFlowType getType() { none() }
228+
deprecated Gvn::GvnType getType() { none() }
229229
}
230230

231231
/** A reference to a field. */
@@ -241,11 +241,11 @@ class FieldContent extends Content, TFieldContent {
241241

242242
override Location getLocation() { result = f.getLocation() }
243243

244-
deprecated override DataFlowType getContainerType() {
244+
deprecated override Gvn::GvnType getContainerType() {
245245
result = Gvn::getGlobalValueNumber(f.getDeclaringType())
246246
}
247247

248-
deprecated override DataFlowType getType() { result = Gvn::getGlobalValueNumber(f.getType()) }
248+
deprecated override Gvn::GvnType getType() { result = Gvn::getGlobalValueNumber(f.getType()) }
249249
}
250250

251251
/** A reference to a property. */
@@ -261,11 +261,11 @@ class PropertyContent extends Content, TPropertyContent {
261261

262262
override Location getLocation() { result = p.getLocation() }
263263

264-
deprecated override DataFlowType getContainerType() {
264+
deprecated override Gvn::GvnType getContainerType() {
265265
result = Gvn::getGlobalValueNumber(p.getDeclaringType())
266266
}
267267

268-
deprecated override DataFlowType getType() { result = Gvn::getGlobalValueNumber(p.getType()) }
268+
deprecated override Gvn::GvnType getType() { result = Gvn::getGlobalValueNumber(p.getType()) }
269269
}
270270

271271
/** A reference to an element in a collection. */

csharp/ql/test/experimental/ir/ir/PrintAst.expected

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ assignop.cs:
148148
# 17| 0: [IntLiteral] 2
149149
# 17| 1: [LocalVariableAccess] access to local variable c
150150
casts.cs:
151+
# 1| [Class] Casts_A
152+
# 5| [Class] Casts_B
153+
#-----| 3: (Base types)
154+
# 5| 0: [Class] Casts_A
151155
# 9| [Class] Casts
152156
# 11| 5: [Method] Main
153157
# 12| 4: [BlockStmt] {...}
@@ -167,10 +171,6 @@ casts.cs:
167171
# 15| 0: [LocalVariableAccess] access to local variable Aobj
168172
# 15| 1: [TypeAccess] access to type Casts_B
169173
# 15| 1: [LocalVariableAccess] access to local variable bobjAS
170-
# 1| [Class] Casts_A
171-
# 5| [Class] Casts_B
172-
#-----| 3: (Base types)
173-
# 5| 0: [Class] Casts_A
174174
collections.cs:
175175
# 3| [Class] Collections
176176
# 5| 5: [Class] MyClass
@@ -469,6 +469,9 @@ inheritance_polymorphism.cs:
469469
# 4| 4: [BlockStmt] {...}
470470
# 5| 0: [ReturnStmt] return ...;
471471
# 5| 0: [IntLiteral] 0
472+
# 9| [Class] B
473+
#-----| 3: (Base types)
474+
# 9| 0: [Class] A
472475
# 13| [Class] C
473476
#-----| 3: (Base types)
474477
# 13| 0: [Class] B
@@ -502,9 +505,6 @@ inheritance_polymorphism.cs:
502505
# 34| 6: [ExprStmt] ...;
503506
# 34| 0: [MethodCall] call to method function
504507
# 34| -1: [LocalVariableAccess] access to local variable objC
505-
# 9| [Class] B
506-
#-----| 3: (Base types)
507-
# 9| 0: [Class] A
508508
inoutref.cs:
509509
# 1| [Class] MyClass
510510
# 2| 5: [Field] fld

csharp/ql/test/library-tests/csharp7.2/PrintAst.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ csharp72.cs:
2222
# 28| 0: [RefExpr] ref ...
2323
# 28| 0: [FieldAccess] access to field s
2424
# 31| 7: [DelegateType] Del
25+
# 34| [Struct] ReadonlyStruct
26+
# 38| [Struct] RefStruct
27+
# 42| [Struct] ReadonlyRefStruct
2528
# 46| [Class] NumericLiterals
2629
# 48| 5: [Field] binaryValue
2730
# 48| 1: [AssignExpr] ... = ...
@@ -34,6 +37,3 @@ csharp72.cs:
3437
# 53| 1: [FieldAccess] access to field X
3538
# 55| 6: [Method] F
3639
# 55| 4: [BlockStmt] {...}
37-
# 34| [Struct] ReadonlyStruct
38-
# 38| [Struct] RefStruct
39-
# 42| [Struct] ReadonlyRefStruct

csharp/ql/test/library-tests/csharp7.3/PrintAst.expected

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ csharp73.cs:
5050
# 22| 0: [IntLiteral] 10
5151
# 22| 1: [LocalVariableAccess] access to local variable t
5252
# 25| 1: [BlockStmt] {...}
53+
# 30| [Class] UnmanagedConstraint<>
54+
#-----| 1: (Type parameters)
55+
# 30| 0: [TypeParameter] T
56+
# 34| [Class] EnumConstraint<>
57+
#-----| 1: (Type parameters)
58+
# 34| 0: [TypeParameter] T
59+
# 38| [Class] DelegateConstraint<>
60+
#-----| 1: (Type parameters)
61+
# 38| 0: [TypeParameter] T
5362
# 42| [Class] ExpressionVariables
5463
# 44| 4: [InstanceConstructor] ExpressionVariables
5564
#-----| 2: (Parameters)
@@ -69,12 +78,3 @@ csharp73.cs:
6978
# 51| 0: [InterpolatedStringExpr] $"..."
7079
# 51| 0: [StringLiteral] "x is "
7180
# 51| 1: [LocalVariableAccess] access to local variable x
72-
# 30| [Class] UnmanagedConstraint<>
73-
#-----| 1: (Type parameters)
74-
# 30| 0: [TypeParameter] T
75-
# 34| [Class] EnumConstraint<>
76-
#-----| 1: (Type parameters)
77-
# 34| 0: [TypeParameter] T
78-
# 38| [Class] DelegateConstraint<>
79-
#-----| 1: (Type parameters)
80-
# 38| 0: [TypeParameter] T

0 commit comments

Comments
 (0)