From f1f44d3d2115fb750710212e20ea0bb55e115a96 Mon Sep 17 00:00:00 2001 From: Cole Faust Date: Tue, 16 Nov 2021 14:52:12 -0800 Subject: [PATCH] Simplify equality expressions when comparing to "true" If a boolean type is compared to a string literal with the value "true", it should just output that boolean type unchanged (or prefixed with "not" if ifneq). Fixes: 206637085 Test: go test Change-Id: I0c116bb68b96d21ba3783c01fc4ca524aaa04143 --- mk2rbc/expr.go | 41 +++++++++++++++++++++++++++-------------- mk2rbc/mk2rbc_test.go | 16 ++++++++++++++++ 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/mk2rbc/expr.go b/mk2rbc/expr.go index 8279d2e17..0c5bc40cf 100644 --- a/mk2rbc/expr.go +++ b/mk2rbc/expr.go @@ -224,9 +224,9 @@ func (s *toStringExpr) emit(ctx *generationContext) { s.expr.emit(ctx) ctx.write("))") case starlarkTypeBool: - ctx.write("((") + ctx.write(`("true" if (`) s.expr.emit(ctx) - ctx.write(`) ? "true" : "")`) + ctx.write(`) else "")`) case starlarkTypeVoid: ctx.write(`""`) default: @@ -285,20 +285,33 @@ func (eq *eqExpr) eval(valueMap map[string]starlarkExpr) (res starlarkExpr, same } func (eq *eqExpr) emit(gctx *generationContext) { - emitSimple := func(expr starlarkExpr) { - if eq.isEq { - gctx.write("not ") - } - expr.emit(gctx) + 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 } - // Are we checking that a variable is empty? - if isEmptyString(eq.left) { - emitSimple(eq.right) - return - } else if isEmptyString(eq.right) { - emitSimple(eq.left) - return + // 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 { + gctx.write("not ") + } + otherOperand.emit(gctx) + return + } + if stringOperand == "true" && otherOperand.typ() == starlarkTypeBool { + if !eq.isEq { + gctx.write("not ") + } + otherOperand.emit(gctx) + return + } } if eq.left.typ() != eq.right.typ() { diff --git a/mk2rbc/mk2rbc_test.go b/mk2rbc/mk2rbc_test.go index c2c46543e..869c0e67c 100644 --- a/mk2rbc/mk2rbc_test.go +++ b/mk2rbc/mk2rbc_test.go @@ -365,6 +365,21 @@ def init(g, handle): cfg = rblf.cfg(handle) if "true" == rblf.soong_config_get(g, "art_module", "source_build"): 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 starlarkType }{ + {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool}, {"PRODUCT_NAME", VarClassConfig, starlarkTypeString}, {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString}, {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},