@@ -80,7 +80,7 @@ struct CheckString {
8080 // Match itself from the last position after matching CHECK-DAG.
8181 let matchBuffer = buffer. substring ( from: buffer. index ( buffer. startIndex, offsetBy: lastPos) )
8282 guard let ( range, mutVariableTable) = self . pattern. match ( matchBuffer, initialTable) else {
83- diagnoseFailedCheck ( variableTable, options, buffer)
83+ diagnoseFailedCheck ( self . pattern , self . loc , self . prefix , variableTable, options, buffer)
8484 return nil
8585 }
8686 let ( matchPos, matchLen) = ( range. location, range. length)
@@ -251,7 +251,7 @@ struct CheckString {
251251 // With a group of CHECK-DAGs, a single mismatching means the match on
252252 // that group of CHECK-DAGs fails immediately.
253253 guard let ( range, variableTable) = pattern. match ( matchBuffer, finalTable) else {
254- // PrintCheckFailed(SM, Pat.getLoc(), Pat, MatchBuffer, VariableTable )
254+ diagnoseFailedCheck ( pattern , pattern . patternLoc , self . prefix , finalTable , options , matchBuffer )
255255 return nil
256256 }
257257 finalTable = variableTable
@@ -300,112 +300,116 @@ struct CheckString {
300300
301301 return ( lastPos, notStrings, finalTable)
302302 }
303+ }
304+
305+ private func diagnoseFailedCheck(
306+ _ pattern: Pattern , _ loc: CheckLoc , _ prefix: String ,
307+ _ variableTable: [ String : String ] , _ options: FileCheckOptions ,
308+ _ buffer: String
309+ ) {
310+ if let rtm = pattern. computeRegexToMatch ( variableTable) {
311+ if !pattern. fixedString. isEmpty {
312+ diagnose ( . error,
313+ at: loc,
314+ with: prefix + " : could not find ' \( pattern. fixedString) ' (with regex ' \( rtm) ') in input " ,
315+ options: options
316+ )
317+ } else {
318+ diagnose ( . error,
319+ at: loc,
320+ with: prefix + " : could not find a match for regex ' \( rtm) ' in input " ,
321+ options: options
322+ )
323+ }
324+ } else {
325+ diagnose ( . error,
326+ at: loc,
327+ with: prefix + " : could not find ' \( pattern. fixedString) ' in input " ,
328+ options: options
329+ )
330+ }
303331
304- private func diagnoseFailedCheck( _ variableTable: [ String : String ] , _ options: FileCheckOptions , _ buffer: String ) {
305- if let rtm = self . pattern. computeRegexToMatch ( variableTable) {
306- if !self . pattern. fixedString. isEmpty {
307- diagnose ( . error,
308- at: self . loc,
309- with: self . prefix + " : could not find ' \( self . pattern. fixedString) ' (with regex ' \( rtm) ') in input " ,
332+ // Note any variables used by the pattern
333+ for (varName, _) in pattern. variableUses {
334+ if varName. characters. first == " @ " {
335+ // If we failed with a builtin variable like @LINE, try to report
336+ // what it is bound to.
337+ if let value = pattern. evaluateExpression ( varName) {
338+ diagnose ( . note,
339+ at: loc,
340+ with: " with expression ' \( varName) ' equal to ' \( value) ' " ,
310341 options: options
311342 )
312343 } else {
313- diagnose ( . error,
314- at: self . loc,
315- with: self . prefix + " : could not find a match for regex ' \( rtm) ' in input " ,
344+ // If evaluation fails, we must have an incorrect builtin variable.
345+ diagnose ( . note,
346+ at: loc,
347+ with: " uses incorrect expression ' \( varName) ' " ,
316348 options: options
317349 )
318350 }
319351 } else {
320- diagnose ( . error,
321- at: self . loc,
322- with: self . prefix + " : could not find ' \( self . pattern. fixedString) ' in input " ,
323- options: options
324- )
325- }
326-
327- // Note any variables used by the pattern
328- for (varName, _) in self . pattern. variableUses {
329- if varName. characters. first == " @ " {
330- // If we failed with a builtin variable like @LINE, try to report
331- // what it is bound to.
332- if let value = self . pattern. evaluateExpression ( varName) {
333- diagnose ( . note,
334- at: self . loc,
335- with: " with expression ' \( varName) ' equal to ' \( value) ' " ,
336- options: options
337- )
338- } else {
339- // If evaluation fails, we must have an incorrect builtin variable.
340- diagnose ( . note,
341- at: self . loc,
342- with: " uses incorrect expression ' \( varName) ' " ,
343- options: options
344- )
345- }
352+ if let varDef = variableTable [ varName] {
353+ diagnose ( . note,
354+ at: loc,
355+ with: " with variable ' \( varName) ' equal to ' \( varDef) ' " ,
356+ options: options
357+ )
346358 } else {
347- if let varDef = self . pattern. variableDefs [ varName] {
348- diagnose ( . note,
349- at: self . loc,
350- with: " with variable ' \( varName) ' equal to ' \( varDef) ' " ,
351- options: options
352- )
353- } else {
354- diagnose ( . note,
355- at: self . loc,
356- with: " uses undefined variable ' \( varName) ' " ,
357- options: options
358- )
359- }
359+ diagnose ( . note,
360+ at: loc,
361+ with: " uses undefined variable ' \( varName) ' " ,
362+ options: options
363+ )
360364 }
361365 }
366+ }
362367
363- var NumLinesForward = 0
364- var BestLine : Int ? = nil
365- var BestQuality = 0.0
368+ var NumLinesForward = 0
369+ var BestLine : Int ? = nil
370+ var BestQuality = 0.0
366371
367- for i in 0 ..< min ( buffer. characters. count, 4096 ) {
368- let exampleString : String
369- if pattern. fixedString. isEmpty {
370- exampleString = pattern. regExPattern
371- } else {
372- exampleString = pattern. fixedString
373- }
372+ for i in 0 ..< min ( buffer. characters. count, 4096 ) {
373+ let exampleString : String
374+ if pattern. fixedString. isEmpty {
375+ exampleString = pattern. regExPattern
376+ } else {
377+ exampleString = pattern. fixedString
378+ }
374379
375- if exampleString. isEmpty {
376- break
377- }
380+ if exampleString. isEmpty {
381+ break
382+ }
378383
379- let char = buffer [ buffer. index ( buffer. startIndex, offsetBy: i) ]
380- if char == " \n " {
381- NumLinesForward += 1
382- }
384+ let char = buffer [ buffer. index ( buffer. startIndex, offsetBy: i) ]
385+ if char == " \n " {
386+ NumLinesForward += 1
387+ }
383388
384- // Patterns have leading whitespace stripped, so skip whitespace when
385- // looking for something which looks like a pattern.
386- if char == " " || char == " \t " {
387- continue ;
388- }
389+ // Patterns have leading whitespace stripped, so skip whitespace when
390+ // looking for something which looks like a pattern.
391+ if char == " " || char == " \t " {
392+ continue ;
393+ }
389394
390- // Compute the "quality" of this match as an arbitrary combination of
391- // the match distance and the number of lines skipped to get to this
392- // match.
393- let distance = editDistance ( from: Array ( buffer. characters) , to: Array ( exampleString. characters) )
394- let quality = Double ( distance) + ( Double ( NumLinesForward) / 100.0 )
395- if quality < BestQuality || BestLine == nil {
396- BestLine = i
397- BestQuality = quality
398- }
395+ // Compute the "quality" of this match as an arbitrary combination of
396+ // the match distance and the number of lines skipped to get to this
397+ // match.
398+ let distance = editDistance ( from: Array ( buffer. characters) , to: Array ( exampleString. characters) )
399+ let quality = Double ( distance) + ( Double ( NumLinesForward) / 100.0 )
400+ if quality < BestQuality || BestLine == nil {
401+ BestLine = i
402+ BestQuality = quality
399403 }
404+ }
400405
401- if let Best = BestLine, BestQuality < 50 {
402- buffer. utf8CString. withUnsafeBufferPointer { buf in
403- let otherPatternLoc = CheckLoc . inBuffer (
404- buf. baseAddress!. advanced ( by: Best) ,
405- UnsafeBufferPointer ( start: buf. baseAddress? . advanced ( by: Best) , count: buf. count - Best)
406- )
407- diagnose ( . note, at: otherPatternLoc, with: " possible intended match here " , options: options)
408- }
406+ if let Best = BestLine, BestQuality < 50 {
407+ buffer. utf8CString. withUnsafeBufferPointer { buf in
408+ let otherPatternLoc = CheckLoc . inBuffer (
409+ buf. baseAddress!. advanced ( by: Best) ,
410+ UnsafeBufferPointer ( start: buf. baseAddress? . advanced ( by: Best) , count: buf. count - Best)
411+ )
412+ diagnose ( . note, at: otherPatternLoc, with: " possible intended match here " , options: options)
409413 }
410414 }
411415}
0 commit comments