Merge "Simplify equality expressions when comparing to "true""

This commit is contained in:
Treehugger Robot
2021-11-18 21:11:17 +00:00
committed by Gerrit Code Review
2 changed files with 43 additions and 14 deletions

View File

@@ -224,9 +224,9 @@ func (s *toStringExpr) emit(ctx *generationContext) {
s.expr.emit(ctx) s.expr.emit(ctx)
ctx.write("))") ctx.write("))")
case starlarkTypeBool: case starlarkTypeBool:
ctx.write("((") ctx.write(`("true" if (`)
s.expr.emit(ctx) s.expr.emit(ctx)
ctx.write(`) ? "true" : "")`) ctx.write(`) else "")`)
case starlarkTypeVoid: case starlarkTypeVoid:
ctx.write(`""`) ctx.write(`""`)
default: default:
@@ -285,20 +285,33 @@ func (eq *eqExpr) eval(valueMap map[string]starlarkExpr) (res starlarkExpr, same
} }
func (eq *eqExpr) emit(gctx *generationContext) { func (eq *eqExpr) emit(gctx *generationContext) {
emitSimple := func(expr starlarkExpr) { var stringOperand string
var otherOperand starlarkExpr
if s, ok := maybeString(eq.left); ok {
stringOperand = s
otherOperand = eq.right
} else if s, ok := maybeString(eq.right); ok {
stringOperand = s
otherOperand = eq.left
}
// If we've identified one of the operands as being a string literal, check
// for some special cases we can do to simplify the resulting expression.
if otherOperand != nil {
if stringOperand == "" {
if eq.isEq { if eq.isEq {
gctx.write("not ") gctx.write("not ")
} }
expr.emit(gctx) otherOperand.emit(gctx)
return
} }
// Are we checking that a variable is empty? if stringOperand == "true" && otherOperand.typ() == starlarkTypeBool {
if isEmptyString(eq.left) { if !eq.isEq {
emitSimple(eq.right) gctx.write("not ")
}
otherOperand.emit(gctx)
return return
} else if isEmptyString(eq.right) { }
emitSimple(eq.left)
return
} }
if eq.left.typ() != eq.right.typ() { if eq.left.typ() != eq.right.typ() {

View File

@@ -365,6 +365,21 @@ def init(g, handle):
cfg = rblf.cfg(handle) cfg = rblf.cfg(handle)
if "true" == rblf.soong_config_get(g, "art_module", "source_build"): if "true" == rblf.soong_config_get(g, "art_module", "source_build"):
pass pass
`,
},
{
desc: "ifeq with $(NATIVE_COVERAGE)",
mkname: "product.mk",
in: `
ifeq ($(NATIVE_COVERAGE),true)
endif
`,
expected: `load("//build/make/core:product_config.rbc", "rblf")
def init(g, handle):
cfg = rblf.cfg(handle)
if g.get("NATIVE_COVERAGE", False):
pass
`, `,
}, },
{ {
@@ -1079,6 +1094,7 @@ var known_variables = []struct {
class varClass class varClass
starlarkType starlarkType
}{ }{
{"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
{"PRODUCT_NAME", VarClassConfig, starlarkTypeString}, {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
{"PRODUCT_MODEL", VarClassConfig, starlarkTypeString}, {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
{"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList}, {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},