@@ -8,6 +8,15 @@ namespace Semmle.Extraction.CSharp.Entities.Statements
88{
99 internal class ForEach : Statement < ForEachStatementSyntax >
1010 {
11+ internal enum ForeachSymbolType
12+ {
13+ GetEnumeratorMethod = 1 ,
14+ CurrentProperty ,
15+ MoveNextMethod ,
16+ DisposeMethod ,
17+ ElementType
18+ }
19+
1120 private ForEach ( Context cx , ForEachStatementSyntax stmt , IStatementParentEntity parent , int child )
1221 : base ( cx , stmt , StmtKind . FOREACH , parent , child ) { }
1322
@@ -18,18 +27,59 @@ public static ForEach Create(Context cx, ForEachStatementSyntax node, IStatement
1827 return ret ;
1928 }
2029
21- protected override void PopulateStatement ( TextWriter _ )
30+ protected override void PopulateStatement ( TextWriter trapFile )
2231 {
2332 Expression . Create ( cx , Stmt . Expression , this , 1 ) ;
2433
25- var typeSymbol = cx . GetModel ( Stmt ) . GetDeclaredSymbol ( Stmt ) ;
34+ var semanticModel = cx . GetModel ( Stmt ) ;
35+ var typeSymbol = semanticModel . GetDeclaredSymbol ( Stmt ) ;
2636 var type = typeSymbol . GetAnnotatedType ( ) ;
2737
2838 var location = cx . Create ( Stmt . Identifier . GetLocation ( ) ) ;
2939
3040 Expressions . VariableDeclaration . Create ( cx , typeSymbol , type , Stmt . Type , location , Stmt . Type . IsVar , this , 0 ) ;
3141
3242 Statement . Create ( cx , Stmt . Statement , this , 2 ) ;
43+
44+ var info = semanticModel . GetForEachStatementInfo ( Stmt ) ;
45+
46+ if ( info . Equals ( default ) )
47+ {
48+ cx . ExtractionError ( "Could not get foreach statement info" , null , Location . Create ( cx , this . ReportingLocation ) , severity : Util . Logging . Severity . Info ) ;
49+ return ;
50+ }
51+
52+ trapFile . foreach_stmt_info ( this , info . IsAsynchronous ) ;
53+
54+ if ( info . GetEnumeratorMethod != null )
55+ {
56+ var m = Method . Create ( cx , info . GetEnumeratorMethod ) ;
57+ trapFile . foreach_stmt_desugar ( this , m , ForeachSymbolType . GetEnumeratorMethod ) ;
58+ }
59+
60+ if ( info . MoveNextMethod != null )
61+ {
62+ var m = Method . Create ( cx , info . MoveNextMethod ) ;
63+ trapFile . foreach_stmt_desugar ( this , m , ForeachSymbolType . MoveNextMethod ) ;
64+ }
65+
66+ if ( info . DisposeMethod != null )
67+ {
68+ var m = Method . Create ( cx , info . DisposeMethod ) ;
69+ trapFile . foreach_stmt_desugar ( this , m , ForeachSymbolType . DisposeMethod ) ;
70+ }
71+
72+ if ( info . CurrentProperty != null )
73+ {
74+ var p = Property . Create ( cx , info . CurrentProperty ) ;
75+ trapFile . foreach_stmt_desugar ( this , p , ForeachSymbolType . CurrentProperty ) ;
76+ }
77+
78+ if ( info . ElementType != null )
79+ {
80+ var t = Type . Create ( cx , info . ElementType ) ;
81+ trapFile . foreach_stmt_desugar ( this , t , ForeachSymbolType . ElementType ) ;
82+ }
3383 }
3484 }
3585
0 commit comments