@@ -422,10 +422,10 @@ private final class BoxedTable {
422
422
}
423
423
}
424
424
425
- /// Check the input to FileCheck provided in the \p Buffer against the \p
426
- /// CheckStrings read from the check file.
425
+ /// Check the input to FileCheck provided in the buffer against the check
426
+ /// strings read from the check file.
427
427
///
428
- /// Returns false if the input fails to satisfy the checks.
428
+ /// Returns ` false` if the input fails to satisfy the checks.
429
429
private func check( input b : String , against checkStrings : [ CheckString ] ) -> Bool {
430
430
var buffer = b
431
431
var failedChecks = false
@@ -448,13 +448,14 @@ private func check(input b : String, against checkStrings : [CheckString]) -> Bo
448
448
}
449
449
450
450
// Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG
451
- guard let ( matchLabelPos , matchLabelLen ) = checkStr. check ( buffer, true , variableTable) else {
451
+ guard let range = checkStr. check ( buffer, true , variableTable) else {
452
452
// Immediately bail of CHECK-LABEL fails, nothing else we can do.
453
453
return false
454
454
}
455
455
456
- checkRegion = buffer. substring ( to: buffer. index ( buffer. startIndex, offsetBy: matchLabelPos + matchLabelLen) )
457
- buffer = buffer. substring ( from: buffer. index ( buffer. startIndex, offsetBy: matchLabelPos + matchLabelLen) )
456
+
457
+ checkRegion = buffer. substring ( to: buffer. index ( buffer. startIndex, offsetBy: NSMaxRange ( range) ) )
458
+ buffer = buffer. substring ( from: buffer. index ( buffer. startIndex, offsetBy: NSMaxRange ( range) ) )
458
459
j += 1
459
460
}
460
461
@@ -463,13 +464,13 @@ private func check(input b : String, against checkStrings : [CheckString]) -> Bo
463
464
464
465
// Check each string within the scanned region, including a second check
465
466
// of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG)
466
- guard let ( matchPos , matchLen ) = checkStrings [ i] . check ( checkRegion, false , variableTable) else {
467
+ guard let range = checkStrings [ i] . check ( checkRegion, false , variableTable) else {
467
468
failedChecks = true
468
469
i = j
469
470
break
470
471
}
471
472
472
- checkRegion = checkRegion. substring ( from: checkRegion. index ( checkRegion. startIndex, offsetBy: matchPos + matchLen ) )
473
+ checkRegion = checkRegion. substring ( from: checkRegion. index ( checkRegion. startIndex, offsetBy: NSMaxRange ( range ) ) )
473
474
}
474
475
475
476
if j == e {
@@ -614,24 +615,23 @@ private class Pattern {
614
615
/// Matches the pattern string against the input buffer.
615
616
///
616
617
/// This returns the position that is matched or npos if there is no match. If
617
- /// there is a match, the size of the matched string is returned in \p
618
- /// MatchLen.
618
+ /// there is a match, the range of the match is returned.
619
619
///
620
- /// The \p VariableTable StringMap provides the current values of filecheck
621
- /// variables and is updated if this match defines new values.
622
- func match( _ buffer : String , _ variableTable : BoxedTable ) -> ( Int , Int ) ? {
620
+ /// The variable table provides the current values of filecheck variables and
621
+ /// is updated if this match defines new values.
622
+ func match( _ buffer : String , _ variableTable : BoxedTable ) -> NSRange ? {
623
623
var matchLen : Int = 0
624
624
// If this is the EOF pattern, match it immediately.
625
625
if self . type == . EOF {
626
626
matchLen = 0
627
- return ( buffer. utf8. count, matchLen)
627
+ return NSRange ( location : buffer. utf8. count, length : matchLen)
628
628
}
629
629
630
630
// If this is a fixed string pattern, just match it now.
631
631
if !self . fixedString. isEmpty {
632
632
matchLen = self . fixedString. utf8. count
633
633
if let b = buffer. range ( of: self . fixedString) ? . lowerBound {
634
- return ( buffer. distance ( from: buffer. startIndex, to: b) , matchLen)
634
+ return NSRange ( location : buffer. distance ( from: buffer. startIndex, to: b) , length : matchLen)
635
635
}
636
636
return nil
637
637
}
@@ -674,7 +674,7 @@ private class Pattern {
674
674
675
675
// Successful regex match.
676
676
guard let fullMatch = matchInfo. first else {
677
- fatalError ( " Didn't get any matches! " )
677
+ return nil
678
678
}
679
679
680
680
// If this defines any variables, remember their values.
@@ -696,23 +696,23 @@ private class Pattern {
696
696
}
697
697
698
698
matchLen = fullMatch. range. length
699
- return ( fullMatch. range. location, matchLen)
699
+ return NSRange ( location : fullMatch. range. location, length : matchLen)
700
700
}
701
701
702
702
/// Finds the closing sequence of a regex variable usage or definition.
703
703
///
704
- /// \p Str has to point in the beginning of the definition (right after the
705
- /// opening sequence). Returns the offset of the closing sequence within Str,
706
- /// or npos if it was not found.
707
- private func findRegexVarEnd( _ regVar : String ) -> String . Index ? {
704
+ /// The given string has to start in the beginning of the definition
705
+ /// (right after the opening sequence). Returns the offset of the closing
706
+ /// sequence within the string, or nil if it was not found.
707
+ private func findRegexVarEnd( _ regVar : String , brackets : ( open : Character , close : Character ) , terminator : String ) -> String . Index ? {
708
708
var string = regVar
709
709
// Offset keeps track of the current offset within the input Str
710
710
var offset = regVar. startIndex
711
711
// [...] Nesting depth
712
712
var bracketDepth = 0
713
713
714
714
while let firstChar = string. characters. first {
715
- if string. hasPrefix ( " ]] " ) && bracketDepth == 0 {
715
+ if string. hasPrefix ( terminator ) && bracketDepth == 0 {
716
716
return offset
717
717
}
718
718
if firstChar == " \\ " {
@@ -721,11 +721,11 @@ private class Pattern {
721
721
offset = regVar. index ( offset, offsetBy: 2 )
722
722
} else {
723
723
switch firstChar {
724
- case " [ " :
724
+ case brackets . open :
725
725
bracketDepth += 1
726
- case " ] " :
726
+ case brackets . close :
727
727
if bracketDepth == 0 {
728
- diagnose ( . error, . string( regVar) , " missing closing \" ] \" for regex variable " )
728
+ diagnose ( . error, . string( regVar) , " missing closing \" \( brackets . close ) \" for regex variable " )
729
729
return nil
730
730
}
731
731
bracketDepth -= 1
@@ -752,11 +752,6 @@ private class Pattern {
752
752
}
753
753
754
754
/// Parses the given string into the Pattern.
755
- ///
756
- /// \p Prefix provides which prefix is being matched, \p SM provides the
757
- /// SourceMgr used for error reports, and \p LineNumber is the line number in
758
- /// the input file from which the pattern string was read. Returns true in
759
- /// case of an error, false otherwise.
760
755
func parse( in buf : UnsafeBufferPointer < CChar > , pattern : UnsafeBufferPointer < CChar > , withPrefix prefix : String , at lineNumber : Int , options: FileCheckOptions ) -> Bool {
761
756
func mino( _ l : String . Index ? , _ r : String . Index ? ) -> String . Index ? {
762
757
if l == nil && r == nil {
@@ -808,7 +803,8 @@ private class Pattern {
808
803
// RegEx matches.
809
804
if patternStr. range ( of: " {{ " ) ? . lowerBound == patternStr. startIndex {
810
805
// This is the start of a regex match. Scan for the }}.
811
- guard let End = patternStr. range ( of: " }} " ) else {
806
+ patternStr = patternStr. substring ( from: patternStr. index ( patternStr. startIndex, offsetBy: 2 ) )
807
+ guard let end = self . findRegexVarEnd ( patternStr, brackets: ( open: " { " , close: " } " ) , terminator: " }} " ) else {
812
808
let loc = CheckLoc . inBuffer ( pattern. baseAddress!, buf)
813
809
diagnose ( . error, loc, " found start of regex string with no end '}}' " )
814
810
return true
@@ -821,22 +817,15 @@ private class Pattern {
821
817
regExPattern += " ( "
822
818
curParen += 1
823
819
824
- let substr = patternStr. substring (
825
- with: Range < String . Index > (
826
- uncheckedBounds: (
827
- patternStr. index ( patternStr. startIndex, offsetBy: 2 ) ,
828
- End . lowerBound
829
- )
830
- )
831
- )
820
+ let substr = patternStr. substring ( to: end)
832
821
let ( res, paren) = self . addRegExToRegEx ( substr, curParen)
833
822
curParen = paren
834
823
if res {
835
824
return true
836
825
}
837
826
regExPattern += " ) "
838
827
839
- patternStr = patternStr. substring ( from: patternStr. index ( End . lowerBound , offsetBy: 2 ) )
828
+ patternStr = patternStr. substring ( from: patternStr. index ( end , offsetBy: 2 ) )
840
829
continue
841
830
}
842
831
@@ -849,7 +838,7 @@ private class Pattern {
849
838
// Find the closing bracket pair ending the match. End is going to be an
850
839
// offset relative to the beginning of the match string.
851
840
let regVar = patternStr. substring ( from: patternStr. index ( patternStr. startIndex, offsetBy: 2 ) )
852
- guard let end = self . findRegexVarEnd ( regVar) else {
841
+ guard let end = self . findRegexVarEnd ( regVar, brackets : ( open : " [ " , close : " ] " ) , terminator : " ]] " ) else {
853
842
let loc = CheckLoc . inBuffer ( pattern. baseAddress!, buf)
854
843
diagnose ( . error, loc, " invalid named regex reference, no ]] found " )
855
844
return true
@@ -981,22 +970,21 @@ func countNumNewlinesBetween(_ r : String) -> (Int, String.Index?) {
981
970
982
971
/// CheckString - This is a check that we found in the input file.
983
972
private struct CheckString {
984
- /// Pat - The pattern to match.
973
+ /// The pattern to match.
985
974
let pattern : Pattern
986
975
987
- /// Prefix - Which prefix name this check matched.
976
+ /// Which prefix name this check matched.
988
977
let prefix : String
989
978
990
- /// Loc - The location in the match file that the check string was specified.
979
+ /// The location in the match file that the check string was specified.
991
980
let loc : CheckLoc
992
981
993
- /// DagNotStrings - These are all of the strings that are disallowed from
994
- /// occurring between this match string and the previous one (or start of
995
- /// file).
982
+ /// These are all of the strings that are disallowed from occurring between
983
+ /// this match string and the previous one (or start of file).
996
984
let dagNotStrings : Array < Pattern > = [ ]
997
985
998
986
/// Match check string and its "not strings" and/or "dag strings".
999
- func check( _ buffer : String , _ isLabelScanMode : Bool , _ variableTable : BoxedTable ) -> ( Int , Int ) ? {
987
+ func check( _ buffer : String , _ isLabelScanMode : Bool , _ variableTable : BoxedTable ) -> NSRange ? {
1000
988
var lastPos = 0
1001
989
1002
990
// IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL
@@ -1013,10 +1001,17 @@ private struct CheckString {
1013
1001
1014
1002
// Match itself from the last position after matching CHECK-DAG.
1015
1003
let matchBuffer = buffer. substring ( from: buffer. index ( buffer. startIndex, offsetBy: lastPos) )
1016
- guard let ( matchPos, matchLen) = self . pattern. match ( matchBuffer, variableTable) else {
1017
- diagnose ( . error, self . loc, self . prefix + " : could not find ' \( self . pattern. fixedString) ' in input " )
1004
+ guard let range = self . pattern. match ( matchBuffer, variableTable) else {
1005
+ if self . pattern. fixedString. isEmpty {
1006
+ diagnose ( . error, self . loc, self . prefix + " : could not find a match for regex ' \( self . pattern. regExPattern) ' in input " )
1007
+ } else if self . pattern. regExPattern. isEmpty {
1008
+ diagnose ( . error, self . loc, self . prefix + " : could not find ' \( self . pattern. fixedString) ' in input " )
1009
+ } else {
1010
+ diagnose ( . error, self . loc, self . prefix + " : could not find ' \( self . pattern. fixedString) ' (with regex ' \( self . pattern. regExPattern) ') in input " )
1011
+ }
1018
1012
return nil
1019
1013
}
1014
+ let ( matchPos, matchLen) = ( range. location, range. length)
1020
1015
1021
1016
// Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT
1022
1017
// or CHECK-NOT
@@ -1050,7 +1045,7 @@ private struct CheckString {
1050
1045
}
1051
1046
}
1052
1047
1053
- return ( lastPos + matchPos, matchLen)
1048
+ return NSRange ( location : lastPos + matchPos, length : matchLen)
1054
1049
}
1055
1050
1056
1051
/// Verify there is no newline in the given buffer.
@@ -1134,11 +1129,11 @@ private struct CheckString {
1134
1129
for pat in notStrings {
1135
1130
assert ( pat. type == . not, " Expect CHECK-NOT! " )
1136
1131
1137
- guard let ( Pos , _ ) /*(Pos, MatchLen)*/ = pat. match ( buffer, variableTable) else {
1132
+ guard let range = pat. match ( buffer, variableTable) else {
1138
1133
continue
1139
1134
}
1140
1135
buffer. cString ( using: . utf8) ? . withUnsafeBufferPointer { buf in
1141
- let loc = CheckLoc . inBuffer ( buf. baseAddress!. advanced ( by: Pos ) , buf)
1136
+ let loc = CheckLoc . inBuffer ( buf. baseAddress!. advanced ( by: range . location ) , buf)
1142
1137
diagnose ( . error, loc, self . prefix + " -NOT: string occurred! " )
1143
1138
}
1144
1139
diagnose ( . note, pat. patternLoc, self . prefix + " -NOT: pattern specified here " )
@@ -1172,12 +1167,12 @@ private struct CheckString {
1172
1167
let matchBuffer = buffer. substring ( from: buffer. index ( buffer. startIndex, offsetBy: startPos) )
1173
1168
// With a group of CHECK-DAGs, a single mismatching means the match on
1174
1169
// that group of CHECK-DAGs fails immediately.
1175
- guard let t = pattern. match ( matchBuffer, variableTable) else {
1170
+ guard let range = pattern. match ( matchBuffer, variableTable) else {
1176
1171
// PrintCheckFailed(SM, Pat.getLoc(), Pat, MatchBuffer, VariableTable)
1177
1172
return nil
1178
1173
}
1179
- var matchPos = t . 0
1180
- let matchLen = t . 1
1174
+ var matchPos = range . location
1175
+ let matchLen = range . length
1181
1176
1182
1177
// Re-calc it as the offset relative to the start of the original string.
1183
1178
matchPos += startPos
0 commit comments