-
Notifications
You must be signed in to change notification settings - Fork 57
Turn on FileCheck Variables #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -407,6 +407,21 @@ private func readCheckStrings(in buf : UnsafeBufferPointer<CChar>, withPrefixes | |
return contents | ||
} | ||
|
||
private final class BoxedTable { | ||
var table : [String:String] = [:] | ||
|
||
init() {} | ||
|
||
subscript(_ i : String) -> String? { | ||
set { | ||
self.table[i] = newValue! | ||
} | ||
get { | ||
return self.table[i] | ||
} | ||
} | ||
} | ||
|
||
/// Check the input to FileCheck provided in the \p Buffer against the \p | ||
/// CheckStrings read from the check file. | ||
/// | ||
|
@@ -416,7 +431,7 @@ private func check(input b : String, against checkStrings : [CheckString]) -> Bo | |
var failedChecks = false | ||
|
||
// This holds all the current filecheck variables. | ||
var variableTable = [String:String]() | ||
var variableTable = BoxedTable() | ||
|
||
var i = 0 | ||
var j = 0 | ||
|
@@ -604,7 +619,7 @@ private class Pattern { | |
/// | ||
/// The \p VariableTable StringMap provides the current values of filecheck | ||
/// variables and is updated if this match defines new values. | ||
func match(_ buffer : String, _ variableTable : [String:String]) -> (Int, Int)? { | ||
func match(_ buffer : String, _ variableTable : BoxedTable) -> (Int, Int)? { | ||
var matchLen : Int = 0 | ||
// If this is the EOF pattern, match it immediately. | ||
if self.type == .EOF { | ||
|
@@ -626,9 +641,9 @@ private class Pattern { | |
// If there are variable uses, we need to create a temporary string with the | ||
// actual value. | ||
var regExToMatch = self.regExPattern | ||
if !variableUses.isEmpty { | ||
if !self.variableUses.isEmpty { | ||
var insertOffset = 0 | ||
for (v, offset) in variableUses { | ||
for (v, offset) in self.variableUses { | ||
var value : String = "" | ||
|
||
if let c = v.characters.first, c == "@" { | ||
|
@@ -663,9 +678,21 @@ private class Pattern { | |
} | ||
|
||
// If this defines any variables, remember their values. | ||
for (_, index) in self.variableDefs { | ||
assert(index < matchInfo.count, "Internal paren error") | ||
// VariableTable[VariableDef.0] = MatchInfo[VariableDef.second] | ||
for (v, index) in self.variableDefs { | ||
assert(index < fullMatch.numberOfRanges, "Internal paren error") | ||
#if os(macOS) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sad! |
||
let r = fullMatch.rangeAt(index) | ||
#else | ||
let r = fullMatch.range(at: index) | ||
#endif | ||
variableTable[v] = buffer.substring( | ||
with: Range<String.Index>( | ||
uncheckedBounds: ( | ||
buffer.index(buffer.startIndex, offsetBy: r.location), | ||
buffer.index(buffer.startIndex, offsetBy: NSMaxRange(r)) | ||
) | ||
) | ||
) | ||
} | ||
|
||
matchLen = fullMatch.range.length | ||
|
@@ -829,7 +856,7 @@ private class Pattern { | |
if let end = nameEnd?.lowerBound { | ||
name = matchStr.substring(to: end) | ||
} else { | ||
name = "" | ||
name = matchStr | ||
} | ||
|
||
if name.isEmpty { | ||
|
@@ -868,14 +895,14 @@ private class Pattern { | |
guard let ne = nameEnd else { | ||
// Handle variables that were defined earlier on the same line by | ||
// emitting a backreference. | ||
if let VarParenNum = self.variableDefs[name] { | ||
if VarParenNum < 1 || VarParenNum > 9 { | ||
if let varParenNum = self.variableDefs[name] { | ||
if varParenNum < 1 || varParenNum > 9 { | ||
diagnose(.error, diagLoc, "Can't back-reference more than 9 variables") | ||
return true | ||
} | ||
self.addBackrefToRegEx(VarParenNum) | ||
self.addBackrefToRegEx(varParenNum) | ||
} else { | ||
variableUses.append((name, regExPattern.utf8.count)) | ||
variableUses.append((name, regExPattern.characters.count)) | ||
} | ||
continue | ||
} | ||
|
@@ -899,6 +926,9 @@ private class Pattern { | |
if let fixedMatchEnd = mino(patternStr.range(of: "{{")?.lowerBound, patternStr.range(of: "[[")?.lowerBound) { | ||
self.regExPattern += NSRegularExpression.escapedPattern(for: patternStr.substring(to: fixedMatchEnd)) | ||
patternStr = patternStr.substring(from: fixedMatchEnd) | ||
} else { | ||
// No more matches, time to quit. | ||
break | ||
} | ||
} | ||
|
||
|
@@ -958,7 +988,7 @@ private struct CheckString { | |
let dagNotStrings : Array<Pattern> = [] | ||
|
||
/// Match check string and its "not strings" and/or "dag strings". | ||
func check(_ buffer : String, _ isLabelScanMode : Bool, _ variableTable : [String:String]) -> (Int, Int)? { | ||
func check(_ buffer : String, _ isLabelScanMode : Bool, _ variableTable : BoxedTable) -> (Int, Int)? { | ||
var lastPos = 0 | ||
|
||
// IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL | ||
|
@@ -1092,11 +1122,11 @@ private struct CheckString { | |
} | ||
|
||
/// Verify there's no "not strings" in the given buffer. | ||
private func checkNot(_ buffer : String, _ notStrings : [Pattern], _ VariableTable : [String:String]) -> Bool { | ||
private func checkNot(_ buffer : String, _ notStrings : [Pattern], _ variableTable : BoxedTable) -> Bool { | ||
for pat in notStrings { | ||
assert(pat.type == .not, "Expect CHECK-NOT!") | ||
|
||
guard let (Pos, _)/*(Pos, MatchLen)*/ = pat.match(buffer, VariableTable) else { | ||
guard let (Pos, _)/*(Pos, MatchLen)*/ = pat.match(buffer, variableTable) else { | ||
continue | ||
} | ||
buffer.cString(using: .utf8)?.withUnsafeBufferPointer { buf in | ||
|
@@ -1111,7 +1141,7 @@ private struct CheckString { | |
} | ||
|
||
/// Match "dag strings" and their mixed "not strings". | ||
func checkDAG(_ buffer : String, _ variableTable : [String:String]) -> Int? { | ||
func checkDAG(_ buffer : String, _ variableTable : BoxedTable) -> Int? { | ||
var notStrings = [Pattern]() | ||
if dagNotStrings.isEmpty { | ||
return 0 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No
Box<T>
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered it, then realized I only wrote this thing because the C++ just throws a reference to this dictionary everywhere so I only need reference semantics until I can find a way to remove the abstraction altogether.