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

Skip to content

Commit dfb373b

Browse files
committed
C++: Modify the syntax of inline expectation comments. The syntax is now $ tag1,tag2=value MISSING: tag3=value3 SPURIOUS: tag4=value4.
1 parent 02ca8fe commit dfb373b

1 file changed

Lines changed: 83 additions & 23 deletions

File tree

cpp/ql/test/TestUtilities/InlineExpectationsTest.qll

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,84 @@ abstract class InlineExpectationsTest extends string {
160160
* is treated as part of the expected results, except that the comment may contain a `//` sequence
161161
* to treat the remainder of the line as a regular (non-interpreted) comment.
162162
*/
163-
private string expectationCommentPattern() { result = "\\s*(\\$(?:[^/]|/[^/])*)(?://.*)?" }
163+
private string expectationCommentPattern() { result = "\\s*\\$((?:[^/]|/[^/])*)(?://.*)?" }
164164

165165
/**
166-
* RegEx pattern to match a single expected result, not including the leading `$`. It starts with an
167-
* optional `f+:` or `f-:`, followed by one or more comma-separated tags containing only letters,
168-
* `-`, and `_`, optionally followed by `=` and the expected value.
166+
* The possible columns in an expectation comment. The `TDefaultColumn` branch represents the first
167+
* column in a comment. This column is not precedeeded by a name. `TNamedColumn(name)` represents a
168+
* column containing expected results preceeded by the string `name:`.
169169
*/
170-
private string expectationPattern() {
171-
result = "(?:(f(?:\\+|-)):)?((?:[A-Za-z-_]+)(?:\\s*,\\s*[A-Za-z-_]+)*)(?:=(.*))?"
170+
private newtype TColumn =
171+
TDefaultColumn() or
172+
TNamedColumn(string name) { name = ["MISSING", "SPURIOUS"] }
173+
174+
bindingset[start, content]
175+
private int getEndOfColumnPosition(int start, string content) {
176+
result =
177+
min(string name, int cand |
178+
exists(TNamedColumn(name)) and
179+
cand = content.indexOf(name + ":") and
180+
cand > start
181+
|
182+
cand
183+
)
184+
or
185+
not exists(string name |
186+
exists(TNamedColumn(name)) and
187+
content.indexOf(name + ":") > start
188+
) and
189+
result = content.length()
190+
}
191+
192+
private string getAnExpectation(LineComment comment, TColumn column) {
193+
exists(string content |
194+
content = comment.getContents().regexpCapture(expectationCommentPattern(), 1) and
195+
(
196+
column = TDefaultColumn() and
197+
exists(int end |
198+
end = getEndOfColumnPosition(0, content) and
199+
result = content.prefix(end).splitAt(" ").trim()
200+
)
201+
or
202+
exists(string name, int start, int end |
203+
column = TNamedColumn(name) and
204+
start = content.indexOf(name + ":") + name.length() + 1 and
205+
end = getEndOfColumnPosition(start, content) and
206+
result = content.substring(start, end).splitAt(" ").trim()
207+
)
208+
) and
209+
result != ""
210+
)
211+
}
212+
213+
bindingset[expectation]
214+
private string getATag(string expectation) {
215+
(
216+
result = expectation.prefix(expectation.indexOf("=")).splitAt(",").trim()
217+
or
218+
not exists(expectation.indexOf("=")) and
219+
result = expectation.splitAt(",").trim()
220+
)
172221
}
173222

174-
private string getAnExpectation(LineComment comment) {
175-
result = comment.getContents().regexpCapture(expectationCommentPattern(), 1).splitAt("$").trim() and
176-
result != ""
223+
bindingset[expectation]
224+
private string getValueForTag(string expectation) {
225+
result = expectation.suffix(expectation.indexOf("=") + 1)
226+
}
227+
228+
private string getColumnString(TColumn column) {
229+
column = TDefaultColumn() and result = ""
230+
or
231+
column = TNamedColumn(result)
232+
}
233+
234+
/**
235+
* RegEx pattern to match a single expected result, not including the leading `$`. It consists of one or
236+
* more comma-separated tags containing only letters, `-`, and `_`, optionally followed by `=` and the
237+
* expected value.
238+
*/
239+
private string expectationPattern() {
240+
result = "((?:[A-Za-z-_]+)(?:\\s*,\\s*[A-Za-z-_]+)*)(?:=(.*))?"
177241
}
178242

179243
private newtype TFailureLocatable =
@@ -183,24 +247,19 @@ private newtype TFailureLocatable =
183247
test.hasActualResult(location, element, tag, value)
184248
} or
185249
TValidExpectation(LineComment comment, string tag, string value, string knownFailure) {
186-
exists(string expectation |
187-
expectation = getAnExpectation(comment) and
188-
expectation.regexpMatch(expectationPattern()) and
189-
tag = expectation.regexpCapture(expectationPattern(), 2).splitAt(",").trim() and
250+
exists(string expectation, TColumn column |
251+
expectation = getAnExpectation(comment, column) and
252+
tag = getATag(expectation) and
190253
(
191-
if exists(expectation.regexpCapture(expectationPattern(), 3))
192-
then value = expectation.regexpCapture(expectationPattern(), 3)
254+
if exists(getValueForTag(expectation))
255+
then value = getValueForTag(expectation)
193256
else value = ""
194257
) and
195-
(
196-
if exists(expectation.regexpCapture(expectationPattern(), 1))
197-
then knownFailure = expectation.regexpCapture(expectationPattern(), 1)
198-
else knownFailure = ""
199-
)
258+
knownFailure = getColumnString(column)
200259
)
201260
} or
202261
TInvalidExpectation(LineComment comment, string expectation) {
203-
expectation = getAnExpectation(comment) and
262+
expectation = getAnExpectation(comment, _) and
204263
not expectation.regexpMatch(expectationPattern())
205264
}
206265

@@ -265,16 +324,17 @@ private class ValidExpectation extends Expectation, TValidExpectation {
265324
}
266325
}
267326

327+
/* Note: These next three classes correspond to all the possible values of type `TColumn`. */
268328
class GoodExpectation extends ValidExpectation {
269329
GoodExpectation() { getKnownFailure() = "" }
270330
}
271331

272332
class FalsePositiveExpectation extends ValidExpectation {
273-
FalsePositiveExpectation() { getKnownFailure() = "f+" }
333+
FalsePositiveExpectation() { getKnownFailure() = "SPURIOUS" }
274334
}
275335

276336
class FalseNegativeExpectation extends ValidExpectation {
277-
FalseNegativeExpectation() { getKnownFailure() = "f-" }
337+
FalseNegativeExpectation() { getKnownFailure() = "MISSING" }
278338
}
279339

280340
class InvalidExpectation extends Expectation, TInvalidExpectation {

0 commit comments

Comments
 (0)