Skip to content

Commit fd6f2b1

Browse files
authored
Merge pull request #45 from GitHubSecurityLab/cfg-upgrade
CFG Upgrade
2 parents 3fee620 + 3c256ba commit fd6f2b1

File tree

10 files changed

+885
-149
lines changed

10 files changed

+885
-149
lines changed

ql/lib/codeql/bicep/ast/Calls.qll

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ private import Misc
66
private import internal.Calls
77
private import internal.CallExpression
88
private import internal.LambdaExpression
9+
private import internal.UserDefinedFunction
10+
private import internal.Type
911

1012
/**
1113
* Represents a callable expression in the AST, such as a function or method call.
@@ -105,8 +107,8 @@ class CallExpression extends Call instanceof CallExpressionImpl {
105107
* filters, or other functional programming patterns. A lambda expression
106108
* consists of parameters and a body that defines the computation to be performed.
107109
*/
108-
class LambdaExpression extends Expr instanceof LambdaExpressionImpl {
109-
Idents getIdentifier() { none() }
110+
class LambdaExpression extends Callable instanceof LambdaExpressionImpl {
111+
override Idents getIdentifier() { none() }
110112

111113
/**
112114
* Gets the parameters of this lambda expression.
@@ -137,3 +139,55 @@ class LambdaExpression extends Expr instanceof LambdaExpressionImpl {
137139
*/
138140
int getNumberOfParameters() { result = LambdaExpressionImpl.super.getNumberOfParameters() }
139141
}
142+
143+
/**
144+
* A user-defined function in the AST.
145+
*
146+
* Represents a function declaration in Bicep, which can be called by name
147+
* from other parts of the code. User-defined functions consist of a name,
148+
* parameters, return type, and a body that defines the computation.
149+
*/
150+
class UserDefinedFunction extends Callable instanceof UserDefinedFunctionImpl {
151+
/**
152+
* Gets the identifier of this user-defined function.
153+
*
154+
* @return The identifier node of the function
155+
*/
156+
override Idents getIdentifier() { result = UserDefinedFunctionImpl.super.getIdentifier() }
157+
158+
/**
159+
* Gets the parameters of this user-defined function.
160+
*
161+
* @return The parameters node of the function
162+
*/
163+
Expr getParameters() { result = UserDefinedFunctionImpl.super.getParameters() }
164+
165+
/**
166+
* Gets the parameter at the specified index.
167+
*
168+
* @param n The index of the parameter to retrieve
169+
* @return The parameter at the specified index
170+
*/
171+
Expr getParameter(int n) { result = UserDefinedFunctionImpl.super.getParameter(n) }
172+
173+
/**
174+
* Gets the return type of this user-defined function.
175+
*
176+
* @return The return type node of the function
177+
*/
178+
Type getReturnType() { result = UserDefinedFunctionImpl.super.getReturnType() }
179+
180+
/**
181+
* Gets the body of this user-defined function.
182+
*
183+
* @return The body node of the function
184+
*/
185+
Stmts getBody() { result = UserDefinedFunctionImpl.super.getBody() }
186+
187+
/**
188+
* Gets the number of parameters in this user-defined function.
189+
*
190+
* @return The number of parameters
191+
*/
192+
int getNumberOfParameters() { result = UserDefinedFunctionImpl.super.getNumberOfParameters() }
193+
}

ql/lib/codeql/bicep/ast/Stmts.qll

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ private import internal.Parameter
2121
private import internal.Parameters
2222
private import internal.ParameterDeclaration
2323
private import internal.OutputDeclaration
24-
private import internal.UserDefinedFunction
2524
// CFG
2625
private import codeql.bicep.CFG
2726
private import codeql.bicep.controlflow.internal.ControlFlowGraphImpl as CfgImpl
@@ -262,78 +261,6 @@ class OutputDeclaration extends Stmts instanceof OutputDeclarationImpl {
262261
Expr getValue() { result = OutputDeclarationImpl.super.getValue() }
263262
}
264263

265-
/**
266-
* Represents a user-defined function in the AST.
267-
*
268-
* User-defined functions allow creating reusable pieces of logic in Bicep templates.
269-
* They encapsulate calculations or transformations that can be called from multiple
270-
* places in the template, promoting code reuse and maintainability.
271-
*/
272-
class UserDefinedFunction extends Stmts instanceof UserDefinedFunctionImpl {
273-
/**
274-
* Gets the identifier of the user-defined function.
275-
*
276-
* This is the name token of the function as it appears in the source code.
277-
*
278-
* @return The identifier node of the function
279-
*/
280-
Identifier getIdentifier() { result = UserDefinedFunctionImpl.super.getName() }
281-
282-
/**
283-
* Gets the name of the user-defined function as a string.
284-
*
285-
* This is a convenience method that returns the name from the identifier.
286-
*
287-
* @return The name of the function
288-
*/
289-
string getName() { result = this.getIdentifier().getName() }
290-
291-
/**
292-
* Gets the return type of the user-defined function.
293-
*
294-
* This specifies what kind of value the function returns,
295-
* such as 'string', 'int', 'bool', or more complex types.
296-
*
297-
* @return The return type node of the function
298-
*/
299-
Type getReturnType() { result = UserDefinedFunctionImpl.super.getReturnType() }
300-
301-
/**
302-
* Gets the declared parameters node of the user-defined function.
303-
*
304-
* This contains all the parameter declarations for the function.
305-
*
306-
* @return The parameters node of the function
307-
*/
308-
Parameters getDeclaredParameters() { result = UserDefinedFunctionImpl.super.getParameters() }
309-
310-
/**
311-
* Gets all individual parameters of the user-defined function.
312-
*
313-
* @return All parameter nodes of the function
314-
*/
315-
Parameter getParameters() { result = this.getDeclaredParameters().getParameter(_) }
316-
317-
/**
318-
* Gets the parameter at the specified index.
319-
*
320-
* @param index The index of the parameter to retrieve
321-
* @return The parameter at the specified index
322-
*/
323-
Parameter getParameter(int index) { result = this.getDeclaredParameters().getParameter(index) }
324-
325-
/**
326-
* Gets the body of the user-defined function.
327-
*
328-
* This is the expression that defines the computation performed by the function.
329-
* When the function is called, this expression is evaluated with the provided
330-
* argument values bound to the function parameters.
331-
*
332-
* @return The body expression of the function
333-
*/
334-
Expr getBody() { result = UserDefinedFunctionImpl.super.getBody() }
335-
}
336-
337264
/**
338265
* Represents a function parameter node in the AST.
339266
*
Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
/**
22
* Internal implementation for UserDefinedFunction
3-
*
4-
* WARNING: this file is generated, do not edit manually
53
*/
64

75
private import AstNodes
86
private import TreeSitter
97
private import codeql.bicep.ast.AstNodes
8+
private import Calls
109
private import Stmts
1110
private import Identifier
12-
private import Stmts
1311
private import Type
1412
private import Parameter
1513
private import Parameters
14+
private import Expr
1615

1716
/**
1817
* A UserDefinedFunction AST Node.
1918
*/
20-
class UserDefinedFunctionImpl extends TUserDefinedFunction, StmtsImpl {
19+
class UserDefinedFunctionImpl extends TUserDefinedFunction, CallableImpl {
2120
private BICEP::UserDefinedFunction ast;
2221

2322
override string getAPrimaryQlClass() { result = "UserDefinedFunction" }
@@ -26,11 +25,20 @@ class UserDefinedFunctionImpl extends TUserDefinedFunction, StmtsImpl {
2625

2726
override string toString() { result = ast.toString() }
2827

29-
IdentifierImpl getName() { toTreeSitter(result) = ast.getName() }
28+
override IdentifierImpl getIdentifier() { toTreeSitter(result) = ast.getName() }
29+
30+
override ParametersImpl getParameters() { toTreeSitter(result) = ast.getChild(0) }
3031

31-
ParametersImpl getParameters() { toTreeSitter(result) = ast.getChild(0) }
32+
override ParameterImpl getParameter(int n) {
33+
exists(ParametersImpl params |
34+
params = this.getParameters() and
35+
result = params.getParameter(n)
36+
)
37+
}
3238

3339
TypeImpl getReturnType() { toTreeSitter(result) = ast.getReturns() }
3440

35-
StmtsImpl getBody() { toTreeSitter(result) = ast.getChild(1) }
41+
override StmtSequenceImpl getBody() { toTreeSitter(result) = ast.getChild(1) }
42+
43+
override TypeImpl getType() { result = this.getReturnType() }
3644
}

0 commit comments

Comments
 (0)