diff --git a/mk2rbc/mk2rbc.go b/mk2rbc/mk2rbc.go index 74053fefb..d108a0d04 100644 --- a/mk2rbc/mk2rbc.go +++ b/mk2rbc/mk2rbc.go @@ -465,17 +465,17 @@ func newParseContext(ss *StarlarkScript, nodes []mkparser.Node) *parseContext { return ctx } -func (ctx *parseContext) lastAssignment(name string) *assignmentNode { +func (ctx *parseContext) lastAssignment(v variable) *assignmentNode { for va := ctx.varAssignments; va != nil; va = va.outer { - if v, ok := va.vars[name]; ok { + if v, ok := va.vars[v.name()]; ok { return v } } return nil } -func (ctx *parseContext) setLastAssignment(name string, asgn *assignmentNode) { - ctx.varAssignments.vars[name] = asgn +func (ctx *parseContext) setLastAssignment(v variable, asgn *assignmentNode) { + ctx.varAssignments.vars[v.name()] = asgn } func (ctx *parseContext) pushVarAssignments() { @@ -532,7 +532,7 @@ func (ctx *parseContext) handleAssignment(a *mkparser.Assignment) []starlarkNode if lhs == nil { return []starlarkNode{ctx.newBadNode(a, "unknown variable %s", name)} } - _, isTraced := ctx.tracedVariables[name] + _, isTraced := ctx.tracedVariables[lhs.name()] asgn := &assignmentNode{lhs: lhs, mkValue: a.Value, isTraced: isTraced, location: ctx.errorLocation(a)} if lhs.valueType() == starlarkTypeUnknown { // Try to divine variable type from the RHS @@ -565,8 +565,8 @@ func (ctx *parseContext) handleAssignment(a *mkparser.Assignment) []starlarkNode } } - asgn.previous = ctx.lastAssignment(name) - ctx.setLastAssignment(name, asgn) + asgn.previous = ctx.lastAssignment(lhs) + ctx.setLastAssignment(lhs, asgn) switch a.Type { case "=", ":=": asgn.flavor = asgnSet @@ -1268,12 +1268,12 @@ func (ctx *parseContext) parseReference(node mkparser.Node, ref *mkparser.MakeSt args: []starlarkExpr{ &stringLiteralExpr{literal: substParts[0]}, &stringLiteralExpr{literal: substParts[1]}, - NewVariableRefExpr(v, ctx.lastAssignment(v.name()) != nil), + NewVariableRefExpr(v, ctx.lastAssignment(v) != nil), }, } } if v := ctx.addVariable(refDump); v != nil { - return NewVariableRefExpr(v, ctx.lastAssignment(v.name()) != nil) + return NewVariableRefExpr(v, ctx.lastAssignment(v) != nil) } return ctx.newBadExpr(node, "unknown variable %s", refDump) } diff --git a/mk2rbc/mk2rbc_test.go b/mk2rbc/mk2rbc_test.go index 55d48ff54..8feea3d71 100644 --- a/mk2rbc/mk2rbc_test.go +++ b/mk2rbc/mk2rbc_test.go @@ -65,6 +65,10 @@ def init(g, handle): PRODUCT_NAME := Pixel 3 PRODUCT_MODEL := local_var = foo +local-var-with-dashes := bar +$(warning local-var-with-dashes: $(local-var-with-dashes)) +GLOBAL-VAR-WITH-DASHES := baz +$(warning GLOBAL-VAR-WITH-DASHES: $(GLOBAL-VAR-WITH-DASHES)) `, expected: `load("//build/make/core:product_config.rbc", "rblf") @@ -73,6 +77,10 @@ def init(g, handle): cfg["PRODUCT_NAME"] = "Pixel 3" cfg["PRODUCT_MODEL"] = "" _local_var = "foo" + _local_var_with_dashes = "bar" + rblf.mkwarning("pixel3.mk", "local-var-with-dashes: %s" % _local_var_with_dashes) + g["GLOBAL-VAR-WITH-DASHES"] = "baz" + rblf.mkwarning("pixel3.mk", "GLOBAL-VAR-WITH-DASHES: %s" % g["GLOBAL-VAR-WITH-DASHES"]) `, }, { diff --git a/mk2rbc/variable.go b/mk2rbc/variable.go index f7adca568..d037481f5 100644 --- a/mk2rbc/variable.go +++ b/mk2rbc/variable.go @@ -278,6 +278,14 @@ var presetVariables = map[string]bool{ // addVariable returns a variable with a given name. A variable is // added if it does not exist yet. func (ctx *parseContext) addVariable(name string) variable { + // Heuristics: if variable's name is all lowercase, consider it local + // string variable. + isLocalVariable := name == strings.ToLower(name) + // Local variables can't have special characters in them, because they + // will be used as starlark identifiers + if isLocalVariable { + name = strings.ReplaceAll(strings.TrimSpace(name), "-", "_") + } v, found := ctx.variables[name] if !found { _, preset := presetVariables[name] @@ -288,9 +296,7 @@ func (ctx *parseContext) addVariable(name string) variable { case VarClassSoong: v = &otherGlobalVariable{baseVariable{nam: name, typ: vi.valueType, preset: preset}} } - } else if name == strings.ToLower(name) { - // Heuristics: if variable's name is all lowercase, consider it local - // string variable. + } else if isLocalVariable { v = &localVariable{baseVariable{nam: name, typ: starlarkTypeUnknown}} } else { vt := starlarkTypeUnknown