Skip to content

Commit c8ce838

Browse files
authored
Merge pull request #1 from trill-lang/unique-prefixes
Enforce uniqueness of check prefixes
2 parents 482e229 + 92f81c1 commit c8ce838

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

Sources/FileCheck/FileCheck.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ public enum FileCheckFD {
9494
/// - returns: Whether or not FileCheck succeeded in verifying the file.
9595
public func fileCheckOutput(of FD : FileCheckFD = .stdout, withPrefixes prefixes : [String] = ["CHECK"], checkNot : [String] = [], against file : String = #file, options: FileCheckOptions = [], block : () -> ()) -> Bool {
9696
guard let validPrefixes = validateCheckPrefixes(prefixes) else {
97-
print("Supplied check-prefix is invalid! Prefixes must be unique and ",
98-
"start with a letter and contain only alphanumeric characters, ",
97+
print("Supplied check-prefix is invalid! Prefixes must be unique and",
98+
"start with a letter and contain only alphanumeric characters,",
9999
"hyphens and underscores")
100100
return false
101101
}
@@ -166,11 +166,13 @@ private func overrideFDAndCollectOutput(file : FileCheckFD, of block : () -> ())
166166
private func validateCheckPrefixes(_ prefixes : [String]) -> [String]? {
167167
let validator = try! NSRegularExpression(pattern: "^[a-zA-Z0-9_-]*$")
168168

169+
var prefixUniquer = Set<String>()
169170
for prefix in prefixes {
170171
// Reject empty prefixes.
171-
if prefix.isEmpty {
172+
guard !prefix.isEmpty && !prefixUniquer.contains(prefix) else {
172173
return nil
173174
}
175+
prefixUniquer.insert(prefix)
174176

175177
let range = NSRange(
176178
location: 0,
@@ -181,7 +183,7 @@ private func validateCheckPrefixes(_ prefixes : [String]) -> [String]? {
181183
}
182184
}
183185

184-
return [String](Set<String>(prefixes))
186+
return [String](prefixUniquer)
185187
}
186188

187189
extension CChar {

Tests/FileCheckTests/FileCheckSpec.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,30 @@ class FileCheckSpec : XCTestCase {
163163
})
164164
}
165165

166+
func testInvalidCheckPrefix() {
167+
// BAD_PREFIX: Supplied check-prefix is invalid! Prefixes must be unique and start with a letter and contain only alphanumeric characters, hyphens and underscores
168+
XCTAssert(fileCheckOutput(of: .stdout, withPrefixes: ["BAD_PREFIX"]) {
169+
XCTAssertFalse(fileCheckOutput(of: .stdout, withPrefixes: ["A!"]) { })
170+
})
171+
172+
XCTAssert(fileCheckOutput(of: .stdout, withPrefixes: ["A1a-B_c"]) {
173+
// A1a-B_c: foobar
174+
print("foobar")
175+
})
176+
177+
XCTAssert(fileCheckOutput(of: .stdout, withPrefixes: ["BAD_PREFIX"]) {
178+
XCTAssertFalse(fileCheckOutput(of: .stdout, withPrefixes: ["REPEAT", "REPEAT"]) { })
179+
})
180+
181+
XCTAssert(fileCheckOutput(of: .stdout, withPrefixes: ["BAD_PREFIX"]) {
182+
XCTAssertFalse(fileCheckOutput(of: .stdout, withPrefixes: ["VALID", "A!"]) { })
183+
})
184+
185+
XCTAssert(fileCheckOutput(of: .stdout, withPrefixes: ["BAD_PREFIX"]) {
186+
XCTAssertFalse(fileCheckOutput(of: .stdout, withPrefixes: [" "]) { })
187+
})
188+
}
189+
166190
#if !os(macOS)
167191
static var allTests = testCase([
168192
("testWhitespace", testWhitespace),
@@ -173,6 +197,7 @@ class FileCheckSpec : XCTestCase {
173197
("testNearestPattern", testNearestPattern),
174198
("testNotDiagInfo", testNotDiagInfo),
175199
("testNonExistentPrefix", testNonExistentPrefix),
200+
("testInvalidCheckPrefix", testInvalidCheckPrefix),
176201
])
177202
#endif
178203
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import FileCheck
2+
import XCTest
3+
import Foundation
4+
5+
class VariableRefSpec : XCTestCase {
6+
func testSameLineVarRef() {
7+
XCTAssert(fileCheckOutput(of: .stdout) {
8+
// CHECK: op1 [[REG:r[0-9]+]], {{r[0-9]+}}, [[REG]]
9+
print("op1 r1, r2, r1")
10+
// CHECK: op3 [[REG1:r[0-9]+]], [[REG2:r[0-9]+]], [[REG1]], [[REG2]]
11+
print("op3 r1, r2, r1, r2")
12+
// Test that parens inside the regex don't confuse FileCheck
13+
// CHECK: {{([a-z]+[0-9])+}} [[REG:g[0-9]+]], {{g[0-9]+}}, [[REG]]
14+
print("op4 g1, g2, g1")
15+
})
16+
}
17+
#if !os(macOS)
18+
static var allTests = testCase([
19+
("testSameLineVarRef", testSameLineVarRef),
20+
])
21+
#endif
22+
}
23+
24+

Tests/LinuxMain.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ XCTMain([
1010
LabelSpec.allTests,
1111
LineCountSpec.allTests,
1212
MultiPrefixSpec.allTests,
13+
VariableRefSpec.allTests,
1314
])
1415
#endif

0 commit comments

Comments
 (0)