Skip to content

Commit 7912e68

Browse files
committed
Compute regex for the diagnostic
1 parent df5f7c5 commit 7912e68

File tree

3 files changed

+50
-67
lines changed

3 files changed

+50
-67
lines changed

Sources/FileCheck/CheckString.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,24 @@ struct CheckString {
8585
// Match itself from the last position after matching CHECK-DAG.
8686
let matchBuffer = buffer.substring(from: buffer.index(buffer.startIndex, offsetBy: lastPos))
8787
guard let range = self.pattern.match(matchBuffer, variableTable) else {
88-
if self.pattern.fixedString.isEmpty {
89-
diagnose(.error,
90-
at: self.loc,
91-
with: self.prefix + ": could not find a match for regex '\(self.pattern.regExPattern)' in input",
92-
options: options
93-
)
94-
} else if self.pattern.regExPattern.isEmpty {
95-
diagnose(.error,
96-
at: self.loc,
97-
with: self.prefix + ": could not find '\(self.pattern.fixedString)' in input",
98-
options: options
99-
)
88+
if let rtm = self.pattern.computeRegexToMatch(variableTable) {
89+
if !self.pattern.fixedString.isEmpty {
90+
diagnose(.error,
91+
at: self.loc,
92+
with: self.prefix + ": could not find '\(self.pattern.fixedString)' (with regex '\(rtm)') in input",
93+
options: options
94+
)
95+
} else {
96+
diagnose(.error,
97+
at: self.loc,
98+
with: self.prefix + ": could not find a match for regex '\(rtm)' in input",
99+
options: options
100+
)
101+
}
100102
} else {
101103
diagnose(.error,
102104
at: self.loc,
103-
with: self.prefix + ": could not find '\(self.pattern.fixedString)' (with regex '\(self.pattern.regExPattern)') in input",
105+
with: self.prefix + ": could not find '\(self.pattern.fixedString)' in input",
104106
options: options
105107
)
106108
}

Sources/FileCheck/Pattern.swift

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ final class Pattern {
208208
}
209209
self.addBackrefToRegEx(varParenNum)
210210
} else {
211-
variableUses.append((name, regExPattern.characters.count))
211+
variableUses.append((name, self.regExPattern.characters.count))
212212
}
213213
continue
214214
}
215215

216216
// Handle [[foo:.*]].
217217
self.variableDefs[name] = curParen
218-
regExPattern += "("
218+
self.regExPattern += "("
219219
curParen += 1
220220

221221
let (res, paren) = self.addRegExToRegEx(matchStr.substring(from: matchStr.index(after: ne.lowerBound)), curParen)
@@ -224,7 +224,7 @@ final class Pattern {
224224
return nil
225225
}
226226

227-
regExPattern += ")"
227+
self.regExPattern += ")"
228228
}
229229

230230
// Handle fixed string matches.
@@ -240,8 +240,8 @@ final class Pattern {
240240

241241
if options.contains(.matchFullLines) {
242242
if !options.contains(.strictWhitespace) {
243-
regExPattern += " *"
244-
regExPattern += "$"
243+
self.regExPattern += " *"
244+
self.regExPattern += "$"
245245
}
246246
}
247247
}
@@ -276,29 +276,7 @@ final class Pattern {
276276
return "\(self.lineNumber + offset)"
277277
}
278278

279-
/// Matches the pattern string against the input buffer.
280-
///
281-
/// This returns the position that is matched or npos if there is no match. If
282-
/// there is a match, the range of the match is returned.
283-
///
284-
/// The variable table provides the current values of filecheck variables and
285-
/// is updated if this match defines new values.
286-
func match(_ buffer : String, _ variableTable : BoxedTable) -> NSRange? {
287-
// If this is the EOF pattern, match it immediately.
288-
if self.type == .EOF {
289-
return NSRange(location: buffer.utf8.count, length: 0)
290-
}
291-
292-
// If this is a fixed string pattern, just match it now.
293-
if !self.fixedString.isEmpty {
294-
if let b = buffer.range(of: self.fixedString)?.lowerBound {
295-
return NSRange(location: buffer.distance(from: buffer.startIndex, to: b), length: self.fixedString.utf8.count)
296-
}
297-
return nil
298-
}
299-
300-
// Regex match.
301-
279+
func computeRegexToMatch(_ variableTable : BoxedTable) -> String? {
302280
// If there are variable uses, we need to create a temporary string with the
303281
// actual value.
304282
var regExToMatch = self.regExPattern
@@ -326,6 +304,35 @@ final class Pattern {
326304
insertOffset += value.utf8.count
327305
}
328306
}
307+
return regExToMatch
308+
}
309+
310+
/// Matches the pattern string against the input buffer.
311+
///
312+
/// This returns the position that is matched or npos if there is no match. If
313+
/// there is a match, the range of the match is returned.
314+
///
315+
/// The variable table provides the current values of filecheck variables and
316+
/// is updated if this match defines new values.
317+
func match(_ buffer : String, _ variableTable : BoxedTable) -> NSRange? {
318+
// If this is the EOF pattern, match it immediately.
319+
if self.type == .EOF {
320+
return NSRange(location: buffer.utf8.count, length: 0)
321+
}
322+
323+
// If this is a fixed string pattern, just match it now.
324+
if !self.fixedString.isEmpty {
325+
if let b = buffer.range(of: self.fixedString)?.lowerBound {
326+
return NSRange(location: buffer.distance(from: buffer.startIndex, to: b), length: self.fixedString.utf8.count)
327+
}
328+
return nil
329+
}
330+
331+
// Regex match.
332+
333+
guard let regExToMatch = computeRegexToMatch(variableTable) else {
334+
return nil
335+
}
329336

330337
// Match the newly constructed regex.
331338
guard let r = try? NSRegularExpression(pattern: regExToMatch, options: []) else {

Tests/FileCheckTests/FileCheckSpec.swift

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,6 @@ class FileCheckSpec : XCTestCase {
5252
})
5353
}
5454

55-
func testCheckDAG() {
56-
XCTAssert(fileCheckOutput(withPrefixes: ["TESTDAG"]) {
57-
// TESTDAG-DAG: add [[REG1:r[0-9]+]], r1, r2
58-
print("add r10, r1, r2")
59-
// TESTDAG-DAG: add [[REG2:r[0-9]+]], r3, r4
60-
print("add r11, r3, r4")
61-
// TESTDAG: mul r5, [[REG1]], [[REG2]]
62-
print("mul r5, r10, r11")
63-
64-
// TESTDAG-DAG: mul [[REG1:r[0-9]+]], r1, r2
65-
print("mul r11, r3, r4")
66-
// TESTDAG-DAG: mul [[REG2:r[0-9]+]], r3, r4
67-
print("mul r10, r1, r2")
68-
// TESTDAG: add r5, [[REG1]], [[REG2]]
69-
print("add r5, r10, r11")
70-
71-
// TESTDAG-DAG: add [[REG1:r[0-9]+]], r1, r2
72-
// TESTDAG-DAG: add [[REG2:r[0-9]+]], r3, r4
73-
// TESTDAG-NOT: xor
74-
// TESTDAG-DAG: mul r5, [[REG1]], [[REG2]]
75-
print("add r11, r3, r4")
76-
print("add r10, r1, r2")
77-
print("mul r5, r10, r11")
78-
})
79-
}
80-
8155
func testImplicitCheckNot() {
8256
XCTAssert(fileCheckOutput(of: .stdout, withPrefixes: ["CHECK-NOTCHECK"]) {
8357
// CHECK-NOTCHECK: error: NOTCHECK-NOT: string occurred!

0 commit comments

Comments
 (0)