Merge "Allow comparing $(wildcard) results to non-empty values"

This commit is contained in:
Cole Faust
2022-04-28 19:54:32 +00:00
committed by Gerrit Code Review
2 changed files with 18 additions and 20 deletions

View File

@@ -1074,6 +1074,18 @@ func (ctx *parseContext) parseCompare(cond *mkparser.Directive) starlarkExpr {
return otherOperand
}
}
if otherOperand.typ() == starlarkTypeList {
fields := strings.Fields(stringOperand)
elements := make([]starlarkExpr, len(fields))
for i, s := range fields {
elements[i] = &stringLiteralExpr{literal: s}
}
return &eqExpr{
left: otherOperand,
right: &listExpr{elements},
isEq: isEq,
}
}
if intOperand, err := strconv.Atoi(strings.TrimSpace(stringOperand)); err == nil && otherOperand.typ() == starlarkTypeInt {
return &eqExpr{
left: otherOperand,
@@ -1119,8 +1131,6 @@ func (ctx *parseContext) parseCompareSpecialCases(directive *mkparser.Directive,
switch call.name {
case baseName + ".filter":
return ctx.parseCompareFilterFuncResult(directive, call, value, isEq)
case baseName + ".expand_wildcard":
return ctx.parseCompareWildcardFuncResult(directive, call, value, !isEq), true
case baseName + ".findstring":
return ctx.parseCheckFindstringFuncResult(directive, call, value, !isEq), true
case baseName + ".strip":
@@ -1165,22 +1175,6 @@ func (ctx *parseContext) parseCompareFilterFuncResult(cond *mkparser.Directive,
}
}
func (ctx *parseContext) parseCompareWildcardFuncResult(directive *mkparser.Directive,
xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr {
if !isEmptyString(xValue) {
return ctx.newBadExpr(directive, "wildcard result can be compared only to empty: %s", xValue)
}
callFunc := baseName + ".file_wildcard_exists"
if s, ok := xCall.args[0].(*stringLiteralExpr); ok && !strings.ContainsAny(s.literal, "*?{[") {
callFunc = baseName + ".file_exists"
}
var cc starlarkExpr = &callExpr{name: callFunc, args: xCall.args, returnType: starlarkTypeBool}
if !negate {
cc = &notExpr{cc}
}
return cc
}
func (ctx *parseContext) parseCheckFindstringFuncResult(directive *mkparser.Directive,
xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr {
if isEmptyString(xValue) {