Support variables with dashes in their names
Bug: 221946551
Test: go test
Change-Id: I085fc35159c4f3afe53868fbc731fcaeac3a69a8
diff --git a/mk2rbc/variable.go b/mk2rbc/variable.go
index f7adca5..d037481 100644
--- a/mk2rbc/variable.go
+++ b/mk2rbc/variable.go
@@ -278,6 +278,14 @@
// 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 @@
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