-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathComments.qll
More file actions
222 lines (196 loc) · 5.33 KB
/
Comments.qll
File metadata and controls
222 lines (196 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* Provides classes for working with code comments.
*/
import go
/**
* A code comment.
*
* Examples:
*
* <pre>
* // a line comment
* /* a block
* comment */
* </pre>
*/
class Comment extends @comment, AstNode {
/**
* Gets the text of this comment, not including delimiters.
*/
string getText() { comments(this, _, _, _, result) }
/**
* Gets the comment group to which this comment belongs.
*/
CommentGroup getGroup() { this = result.getAComment() }
override string toString() { result = "comment" }
override string getAPrimaryQlClass() { result = "Comment" }
}
/**
* A comment group, that is, a sequence of comments without any intervening tokens or
* empty lines.
*
* Examples:
*
* <pre>
* // a line comment
* // another line comment
*
* // a line comment
* /* a block
* comment */
*
* /* a block
* comment */
* /* another block comment */
* </pre>
*/
class CommentGroup extends @comment_group, AstNode {
/**
* Gets the file to which this comment group belongs.
*/
override File getParent() { this = result.getACommentGroup() }
/** Gets the `i`th comment in this group (0-based indexing). */
Comment getComment(int i) { comments(result, _, this, i, _) }
/** Gets a comment in this group. */
Comment getAComment() { result = this.getComment(_) }
/** Gets the number of comments in this group. */
int getNumComment() { result = count(this.getAComment()) }
override string toString() { result = "comment group" }
override string getAPrimaryQlClass() { result = "CommentGroup" }
}
/**
* A program element to which a documentation comment group may be attached:
* a file, a field, a specifier, a generic declaration, a function declaration
* or a go.mod expression.
*
* Examples:
*
* ```go
* // function documentation
* func double(x int) int { return 2 * x }
*
* // generic declaration documentation
* const (
* // specifier documentation
* size int64 = 1024
* eof = -1 // not specifier documentation
* )
* ```
*/
class Documentable extends AstNode, @documentable {
/** Gets the documentation comment group attached to this element, if any. */
DocComment getDocumentation() { this = result.getDocumentedElement() }
}
/**
* A comment group that is attached to a program element as documentation.
*
* Examples:
*
* ```go
* // function documentation
* func double(x int) int { return 2 * x }
*
* // generic declaration documentation
* const (
* // specifier documentation
* size int64 = 1024
* eof = -1 // not specifier documentation
* )
* ```
*/
class DocComment extends CommentGroup {
Documentable node;
DocComment() { doc_comments(node, this) }
/** Gets the program element documented by this comment group. */
Documentable getDocumentedElement() { result = node }
override string getAPrimaryQlClass() { result = "DocComment" }
}
/**
* A single-line comment starting with `//`.
*
* Examples:
*
* ```go
* // Single line comment
* ```
*/
class SlashSlashComment extends @slashslashcomment, Comment {
override string getAPrimaryQlClass() { result = "SlashSlashComment" }
}
/**
* A block comment starting with `/*` and ending with <code>*/</code>.
*
* Examples:
*
* <pre>
* /* a block
* comment */
* </pre>
*/
class SlashStarComment extends @slashstarcomment, Comment {
override string getAPrimaryQlClass() { result = "SlashStarComment" }
}
/**
* A single-line comment starting with `//`.
*
* Examples:
*
* ```go
* // Single line comment
* ```
*/
class LineComment = SlashSlashComment;
/**
* A block comment starting with `/*` and ending with <code>*/</code>.
*
* Examples:
*
* <pre>
* /* a block
* comment */
* </pre>
*/
class BlockComment = SlashStarComment;
/** Holds if `c` starts at `line`, `col` in `f`, and precedes the package declaration. */
private predicate isInitialComment(Comment c, File f, int line, int col) {
c.hasLocationInfo(f.getAbsolutePath(), line, col, _, _) and
line < f.getPackageNameExpr().getLocation().getStartLine()
}
/** Gets the `i`th initial comment in `f` (0-based). */
private Comment getInitialComment(File f, int i) {
result =
rank[i + 1](Comment c, int line, int col |
isInitialComment(c, f, line, col)
|
c order by line, col
)
}
/**
* A build constraint comment of the form `// +build ...` or `//go:build ...`.
*
* Examples:
*
* ```go
* // +build darwin freebsd netbsd openbsd
* // +build !linux
* ```
*/
class BuildConstraintComment extends LineComment {
BuildConstraintComment() {
// a line comment preceding the package declaration, itself only preceded by
// line comments
exists(File f, int i |
// correctness of the placement of the build constraint is not checked here;
// this is more lax than the actual rules for build constraints
this = getInitialComment(f, i) and
not getInitialComment(f, [0 .. i - 1]) instanceof BlockComment
) and
// comment text starts with `+build` or `go:build`
this.getText().regexpMatch("\\s*(\\+|go:)build.*")
}
override string getAPrimaryQlClass() { result = "BuildConstraintComment" }
/** Gets the body of this build constraint. */
string getConstraintBody() { result = this.getText().splitAt("build ", 1) }
/** Gets a disjunct of this build constraint. */
string getADisjunct() { result = this.getConstraintBody().splitAt(" ") }
}