@@ -6,27 +6,58 @@ private import codeql.bicep.ast.internal.TreeSitter
66private import codeql.bicep.controlflow.ControlFlowGraph
77
88/**
9- * An AST node of a Bicep program
9+ * An AST node of a Bicep program.
10+ *
11+ * This is the base class for all syntax nodes in the Bicep language. All
12+ * Bicep entities that have a representation in the abstract syntax tree
13+ * (such as expressions, statements, resources, parameters, etc.) extend this class.
1014 */
1115class AstNode extends TAstNode {
1216 private BICEP:: AstNode node ;
1317
1418 AstNode ( ) { toTreeSitter ( this ) = node }
1519
20+ /**
21+ * Gets a string representation of this AST node.
22+ *
23+ * By default, returns the primary QL class name of this node.
24+ */
1625 string toString ( ) { result = this .getAPrimaryQlClass ( ) }
1726
18- /** Gets the location of the AST node. */
27+ /**
28+ * Gets the location of the AST node.
29+ *
30+ * The location includes information about the file, and the start and
31+ * end positions of the node in the file (line and column).
32+ */
1933 cached
2034 Location getLocation ( ) { result = this .getFullLocation ( ) } // overridden in some subclasses
2135
22- /** Gets the file containing this AST node. */
36+ /**
37+ * Gets the file containing this AST node.
38+ *
39+ * This is the source file where this AST node is defined.
40+ */
2341 cached
2442 File getFile ( ) { result = this .getFullLocation ( ) .getFile ( ) }
2543
26- /** Gets the location that spans the entire AST node. */
44+ /**
45+ * Gets the location that spans the entire AST node.
46+ *
47+ * This includes the full text range covered by this node and all its children.
48+ */
2749 cached
2850 final Location getFullLocation ( ) { result = toTreeSitter ( this ) .getLocation ( ) }
2951
52+ /**
53+ * Holds if this AST node has the specified location information.
54+ *
55+ * @param filepath The file path
56+ * @param startline The start line
57+ * @param startcolumn The start column
58+ * @param endline The end line
59+ * @param endcolumn The end column
60+ */
3061 predicate hasLocationInfo (
3162 string filepath , int startline , int startcolumn , int endline , int endcolumn
3263 ) {
@@ -43,18 +74,29 @@ class AstNode extends TAstNode {
4374
4475 /**
4576 * Gets the parent in the AST for this node.
77+ *
78+ * This is the immediately enclosing syntax node in the AST.
79+ * For example, the parent of an expression might be a statement that contains it.
4680 */
4781 cached
4882 AstNode getParent ( ) { result .getAChild ( ) = this }
4983
5084 /**
51- * Gets a child of this node, which can also be retrieved using a predicate
52- * named `pred`.
85+ * Gets a child of this node.
86+ *
87+ * This returns any direct child node in the AST hierarchy.
88+ * The exact set of child nodes depends on the specific type of AST node.
5389 */
5490 cached
5591 AstNode getAChild ( ) { toTreeSitter ( result ) = node .getAFieldOrChild ( ) }
5692
57- /** Gets the CFG scope that encloses this node, if any. */
93+ /**
94+ * Gets the CFG scope that encloses this node, if any.
95+ *
96+ * A CFG scope is a region of code that has its own control flow graph.
97+ * This can include the entire file, functions, or other code blocks with
98+ * independent control flow.
99+ */
58100 cached
59101 CfgScope getEnclosingCfgScope ( ) {
60102 exists ( AstNode p | p = this .getParent * ( ) |
@@ -66,19 +108,36 @@ class AstNode extends TAstNode {
66108 }
67109
68110 /**
69- * Gets the primary QL class for the ast node.
111+ * Gets the primary QL class for the AST node.
112+ *
113+ * This returns the name of the most specific QL class that describes this node.
114+ * Used primarily for debugging and toString() representations.
70115 */
71116 string getAPrimaryQlClass ( ) { result = "???" }
72117}
73118
74- /** A Bicep file */
119+ /**
120+ * A Bicep file.
121+ *
122+ * Represents a source file written in the Bicep language. This class provides
123+ * access to file metadata and metrics such as the total number of lines and
124+ * lines of code.
125+ */
75126class BicepFile extends File {
76127 BicepFile ( ) { exists ( Location loc | bicep_ast_node_location ( _, loc ) and this = loc .getFile ( ) ) }
77128
78- /** Gets a token in this file. */
129+ /**
130+ * Gets a token in this file.
131+ *
132+ * A token is a basic syntactic unit recognized by the lexer.
133+ */
79134 private BICEP:: Token getAToken ( ) { result .getLocation ( ) .getFile ( ) = this }
80135
81- /** Holds if `line` contains a token. */
136+ /**
137+ * Holds if `line` contains a token.
138+ *
139+ * This is used to calculate metrics like the number of lines of code.
140+ */
82141 private predicate line ( int line ) {
83142 exists ( BICEP:: Token token , Location l |
84143 token = this .getAToken ( ) and
@@ -87,15 +146,28 @@ class BicepFile extends File {
87146 )
88147 }
89148
90- /** Gets the number of lines in this file. */
149+ /**
150+ * Gets the number of lines in this file.
151+ *
152+ * This counts the total number of lines in the file, including empty lines
153+ * and comment lines.
154+ */
91155 int getNumberOfLines ( ) { result = max ( [ 0 , this .getAToken ( ) .getLocation ( ) .getEndLine ( ) ] ) }
92156
93- /** Gets the number of lines of code in this file. */
157+ /**
158+ * Gets the number of lines of code in this file.
159+ *
160+ * This counts only lines that contain at least one token, which excludes
161+ * completely empty lines.
162+ */
94163 int getNumberOfLinesOfCode ( ) { result = count ( int line | this .line ( line ) ) }
95164}
96165
97166/**
98- * A comment in a Bicep program
167+ * A comment in a Bicep program.
168+ *
169+ * Represents a single- or multi-line comment in Bicep source code.
170+ * Comments may be either single-line (// comment) or multi-line (/* comment *\/).
99171 */
100172class Comment extends AstNode , TComment {
101173 private BICEP:: Comment comment ;
@@ -105,9 +177,14 @@ class Comment extends AstNode, TComment {
105177 Comment ( ) { this = TComment ( comment ) }
106178
107179 /**
108- * Gets the text of the comment.
180+ * Gets the text content of the comment.
181+ *
182+ * For a single-line comment, this is the text after the // delimiter.
183+ * For a multi-line comment, this is the text between the /* and *\/ delimiters.
109184 *
110- * TODO: Implement this method
185+ * Note: This method is currently a placeholder and needs implementation.
186+ *
187+ * @return The text content of the comment (currently an empty string)
111188 */
112189 string getContents ( ) { result = "" }
113190}
0 commit comments