summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/substcontext.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkgtools/pkglint/files/substcontext.go')
-rw-r--r--pkgtools/pkglint/files/substcontext.go793
1 files changed, 469 insertions, 324 deletions
diff --git a/pkgtools/pkglint/files/substcontext.go b/pkgtools/pkglint/files/substcontext.go
index 49aa13296c0..1ee397e665f 100644
--- a/pkgtools/pkglint/files/substcontext.go
+++ b/pkgtools/pkglint/files/substcontext.go
@@ -3,30 +3,25 @@ package pkglint
import "netbsd.org/pkglint/textproc"
// SubstContext records the state of a block of variable assignments
-// that make up a SUBST class (see `mk/subst.mk`).
+// that make up a SUBST class.
+//
+// See mk/subst.mk.
type SubstContext struct {
- queuedIds []string
- id string
- doneIds map[string]bool
+ active *substBlock
- foreignAllowed map[string]struct{}
- foreign []*MkLine
-
- conds []*substCond
+ scopes []*substScope
once Once
}
func NewSubstContext() *SubstContext {
- ctx := SubstContext{}
- ctx.reset()
- return &ctx
+ return &SubstContext{scopes: []*substScope{newSubstScope()}}
}
func (ctx *SubstContext) Process(mkline *MkLine) {
switch {
case mkline.IsEmpty():
- ctx.finishClass(mkline)
+ ctx.emptyLine()
case mkline.IsVarassign():
ctx.varassign(mkline)
case mkline.IsDirective():
@@ -35,8 +30,20 @@ func (ctx *SubstContext) Process(mkline *MkLine) {
}
func (ctx *SubstContext) Finish(diag Diagnoser) {
- ctx.finishClass(diag)
- ctx.finishFile(diag)
+ // Prevent panics on unbalanced conditionals.
+ for len(ctx.scopes) > 1 {
+ ctx.leave(diag)
+ }
+
+ for _, scope := range ctx.scopes {
+ scope.finish(diag)
+ }
+}
+
+func (ctx *SubstContext) emptyLine() {
+ for _, scope := range ctx.scopes {
+ scope.emptyLine()
+ }
}
func (ctx *SubstContext) varassign(mkline *MkLine) {
@@ -47,8 +54,8 @@ func (ctx *SubstContext) varassign(mkline *MkLine) {
}
if ctx.isForeign(mkline.Varcanon()) {
- if ctx.isActive() {
- ctx.rememberForeign(mkline)
+ if ctx.isActive() && !ctx.block().seenEmpty {
+ ctx.block().rememberForeign(mkline)
}
return
}
@@ -59,115 +66,341 @@ func (ctx *SubstContext) varassign(mkline *MkLine) {
}
}
- if hasPrefix(mkline.Varname(), "SUBST_") && !ctx.isActiveId(mkline.Varparam()) {
+ if hasPrefix(mkline.Varname(), "SUBST_") && mkline.Varparam() != ctx.activeId() {
if !ctx.varassignDifferentClass(mkline) {
return
}
}
+ block := ctx.block()
switch varcanon {
case "SUBST_STAGE.*":
- ctx.varassignStage(mkline)
+ block.varassignStage(mkline)
case "SUBST_MESSAGE.*":
- ctx.varassignMessages(mkline)
+ block.varassignMessages(mkline)
case "SUBST_FILES.*":
- ctx.varassignFiles(mkline)
+ block.varassignFiles(mkline)
case "SUBST_SED.*":
- ctx.varassignSed(mkline)
+ block.varassignSed(mkline)
case "SUBST_VARS.*":
- ctx.varassignVars(mkline)
+ block.varassignVars(mkline)
case "SUBST_FILTER_CMD.*":
- ctx.varassignFilterCmd(mkline)
+ block.varassignFilterCmd(mkline)
}
}
func (ctx *SubstContext) varassignClasses(mkline *MkLine) {
- classes := mkline.ValueFields(mkline.WithoutMakeVariables(mkline.Value()))
- if len(classes) == 0 {
+ ids := mkline.ValueFields(mkline.WithoutMakeVariables(mkline.Value()))
+ if len(ids) == 0 {
return
}
- if len(classes) > 1 {
+ if len(ids) > 1 {
mkline.Notef("Please add only one class at a time to SUBST_CLASSES.")
mkline.Explain(
"This way, each substitution class forms a block in the package Makefile,",
"and to delete this block, it is not necessary to look anywhere else.")
}
- for _, class := range classes {
- ctx.queue(class)
- }
- id := classes[0]
- if ctx.isActive() && !ctx.isActiveId(id) {
- id := ctx.activeId() // since ctx.condEndif may reset it
+ ctx.prepareSubstClasses(mkline)
+ ctx.deactivate(mkline)
- for ctx.isConditional() {
- // This will be confusing for the outer SUBST block,
- // but since that block is assumed to be finished,
- // this doesn't matter.
- ctx.condEndif(mkline)
- }
-
- complete := ctx.isComplete() // since ctx.finishClass will reset it
- ctx.finishClass(mkline)
- if !complete {
- mkline.Warnf("Subst block %q should be finished before adding the next class to SUBST_CLASSES.", id)
+ for _, id := range ids {
+ if ctx.lookup(id) == nil {
+ ctx.scopes[len(ctx.scopes)-1].define(id)
+ } else if mkline.Varparam() == "" {
+ mkline.Errorf("Duplicate SUBST class %q.", id)
}
}
+}
- ctx.setActiveId(id)
-
- return
+func (ctx *SubstContext) prepareSubstClasses(mkline *MkLine) {
+ for _, scope := range ctx.scopes {
+ scope.prepareSubstClasses(mkline)
+ }
}
// varassignOutsideBlock handles variable assignments of SUBST variables that
// appear without a directly corresponding SUBST block.
func (ctx *SubstContext) varassignOutsideBlock(mkline *MkLine) (continueWithNewId bool) {
- varparam := mkline.Varparam()
+ id := mkline.Varparam()
- if ctx.isListCanon(mkline.Varcanon()) && ctx.isDone(varparam) {
+ if id != "" && ctx.isListCanon(mkline.Varcanon()) && ctx.isDone(id) {
if mkline.Op() != opAssignAppend {
mkline.Warnf("Late additions to a SUBST variable should use the += operator.")
}
return
}
- if containsWord(mkline.Rationale(), varparam) {
- return
+
+ return ctx.activate(mkline, ctx.lookup(id) == nil)
+}
+
+func (ctx *SubstContext) varassignDifferentClass(mkline *MkLine) (ok bool) {
+ varname := mkline.Varname()
+ unknown := ctx.lookup(mkline.Varparam()) == nil
+ if unknown && ctx.isActive() && !ctx.block().isComplete() {
+ mkline.Warnf("Variable %q does not match SUBST class %q.",
+ varname, ctx.activeId())
+ if !ctx.block().seenEmpty {
+ return false
+ }
+ }
+
+ return ctx.activate(mkline, unknown)
+}
+
+func (ctx *SubstContext) directive(mkline *MkLine) {
+ dir := mkline.Directive()
+ switch dir {
+ case "if":
+ ctx.enter()
+ case "elif":
+ ctx.nextBranch(mkline, false)
+ case "else":
+ ctx.nextBranch(mkline, true)
+ case "endif":
+ ctx.leave(mkline)
+ }
+}
+
+func (ctx *SubstContext) enter() {
+ for _, scope := range ctx.scopes {
+ scope.enter()
+ }
+ ctx.scopes = append(ctx.scopes, newSubstScope())
+}
+
+func (ctx *SubstContext) nextBranch(diag Diagnoser, isElse bool) {
+ if ctx.isActive() && !ctx.block().isConditional() {
+ ctx.deactivate(diag)
+ }
+
+ for _, scope := range ctx.scopes {
+ scope.nextBranch(diag, isElse)
+ }
+}
+
+func (ctx *SubstContext) leave(diag Diagnoser) {
+ ctx.deactivate(diag)
+
+ for _, scope := range ctx.scopes {
+ scope.leave(diag)
}
- if ctx.start(varparam) {
+ if len(ctx.scopes) > 1 {
+ ctx.scopes = ctx.scopes[:len(ctx.scopes)-1]
+ }
+}
+
+func (ctx *SubstContext) activate(mkline *MkLine, deactivate bool) bool {
+ id := mkline.Varparam()
+ if id == "" {
+ mkline.Errorf("Invalid SUBST class %q in variable name.", id)
+ return false
+ }
+
+ if deactivate {
+ ctx.deactivate(mkline)
+ }
+
+ if block := ctx.lookup(id); block != nil {
+ ctx.active = block
return true
}
- if ctx.once.FirstTime(varparam) {
+ if ctx.once.FirstTime(id) && !containsWord(mkline.Rationale(), id) {
mkline.Warnf("Before defining %s, the SUBST class "+
"should be declared using \"SUBST_CLASSES+= %s\".",
- mkline.Varname(), varparam)
+ mkline.Varname(), id)
}
- return
+ return false
}
-func (ctx *SubstContext) varassignDifferentClass(mkline *MkLine) (ok bool) {
- varname := mkline.Varname()
- varparam := mkline.Varparam()
+func (ctx *SubstContext) deactivate(diag Diagnoser) {
+ if !ctx.isActive() {
+ return
+ }
- if !ctx.isComplete() {
- mkline.Warnf("Variable %q does not match SUBST class %q.", varname, ctx.activeId())
+ block := ctx.block()
+ if !block.isConditional() {
+ block.finish(diag)
+ }
+ ctx.active = nil
+}
+
+func (*SubstContext) isForeign(varcanon string) bool {
+ switch varcanon {
+ case
+ "SUBST_STAGE.*",
+ "SUBST_MESSAGE.*",
+ "SUBST_FILES.*",
+ "SUBST_SED.*",
+ "SUBST_VARS.*",
+ "SUBST_FILTER_CMD.*":
return false
}
+ return true
+}
+
+func (*SubstContext) isListCanon(varcanon string) bool {
+ switch varcanon {
+ case
+ "SUBST_FILES.*",
+ "SUBST_SED.*",
+ "SUBST_VARS.*":
+ return true
+ }
+ return false
+}
- ctx.finishClass(mkline)
+func (ctx *SubstContext) block() *substBlock {
+ assertNotNil(ctx.active)
+ return ctx.active
+}
- ctx.start(varparam)
- return true
+func (ctx *SubstContext) lookup(id string) *substBlock {
+ for i := len(ctx.scopes) - 1; i >= 0; i-- {
+ if def := ctx.scopes[i].def(id); def != nil {
+ return def
+ }
+ }
+ return nil
+}
+
+func (ctx *SubstContext) isDone(id string) bool {
+ for _, scope := range ctx.scopes {
+ if scope.isDone(id) {
+ return true
+ }
+ }
+ return false
+}
+
+func (ctx *SubstContext) isActive() bool { return ctx.active != nil }
+
+func (ctx *SubstContext) activeId() string {
+ assert(ctx.isActive())
+ return ctx.active.id
+}
+
+type substScope struct {
+ defs []*substBlock
+}
+
+func newSubstScope() *substScope {
+ return &substScope{nil}
+}
+
+func (s *substScope) def(id string) *substBlock {
+ for _, def := range s.defs {
+ if def.id == id {
+ return def
+ }
+ }
+ return nil
+}
+
+func (s *substScope) define(id string) {
+ assert(s.def(id) == nil)
+ s.defs = append(s.defs, newSubstBlock(id))
+}
+
+func (s *substScope) isDone(id string) bool {
+ def := s.def(id)
+ return def != nil && def.done
}
-func (ctx *SubstContext) varassignStage(mkline *MkLine) {
- if ctx.isConditional() {
+func (s *substScope) emptyLine() {
+ for _, block := range s.defs {
+ block.seenEmpty = true
+ }
+}
+
+// finish brings all blocks that are defined in the current scope
+// to an end.
+func (s *substScope) finish(diag Diagnoser) {
+ for _, block := range s.defs {
+ block.finish(diag)
+ }
+}
+
+func (s *substScope) prepareSubstClasses(diag Diagnoser) {
+ for _, block := range s.defs {
+ if block.hasStarted() && !block.isComplete() {
+ diag.Warnf("Subst block %q should be finished "+
+ "before adding the next class to SUBST_CLASSES.",
+ block.id)
+ }
+ }
+}
+
+func (s *substScope) enter() {
+ for _, block := range s.defs {
+ block.enter()
+ }
+}
+
+func (s *substScope) nextBranch(diag Diagnoser, isElse bool) {
+ for _, block := range s.defs {
+ if block.isConditional() {
+ block.nextBranch(isElse)
+ } else {
+ block.finish(diag)
+ }
+ }
+}
+
+func (s *substScope) leave(diag Diagnoser) {
+ for _, block := range s.defs {
+ if block.isConditional() {
+ block.leave()
+ } else {
+ block.finish(diag)
+ }
+ }
+}
+
+type substBlock struct {
+ id string
+
+ // Records which of the SUBST variables have been seen either
+ // directly or in a conditional branch.
+ conds []*substCond
+
+ // In the paragraph of a SUBST block, there should be only variables
+ // that actually belong to the SUBST block.
+ //
+ // In addition, variables that are mentioned in SUBST_VARS may also
+ // be defined there because they closely relate to the SUBST block.
+ foreignAllowed map[string]struct{}
+ foreign []*MkLine
+
+ // Whether there has been an empty line between the SUBST_CLASSES
+ // line and the current line.
+ //
+ // Before the empty line, variables that don't obviously belong to
+ // this SUBST block generate warnings since they may be typos,
+ // such as for a different SUBST block.
+ seenEmpty bool
+
+ // Whether the SUBST_CLASSES has already gone out of scope.
+ //
+ // XXX: When it is out of scope, it should also be unreachable
+ // by any pkglint code. There's something inconsistent here.
+ done bool
+}
+
+func newSubstBlock(id string) *substBlock {
+ assert(id != "")
+ return &substBlock{id: id, conds: []*substCond{newSubstCond()}}
+}
+
+func (b *substBlock) varassignStage(mkline *MkLine) {
+ if b.isConditional() {
mkline.Warnf("%s should not be defined conditionally.", mkline.Varname())
}
- ctx.dupString(mkline, ssStage)
+ b.dupString(mkline, ssStage)
value := mkline.Value()
if value == "pre-patch" || value == "post-patch" {
@@ -196,84 +429,55 @@ func (ctx *SubstContext) varassignStage(mkline *MkLine) {
}
}
-func (ctx *SubstContext) varassignMessages(mkline *MkLine) {
+func (b *substBlock) varassignMessages(mkline *MkLine) {
varname := mkline.Varname()
- if ctx.isConditional() {
+ if b.isConditional() {
mkline.Warnf("%s should not be defined conditionally.", varname)
}
- ctx.dupString(mkline, ssMessage)
+ b.dupString(mkline, ssMessage)
}
-func (ctx *SubstContext) varassignFiles(mkline *MkLine) {
- ctx.dupList(mkline, ssFiles, ssNone)
+func (b *substBlock) varassignFiles(mkline *MkLine) {
+ b.dupList(mkline, ssFiles, ssNone)
}
-func (ctx *SubstContext) varassignSed(mkline *MkLine) {
- ctx.dupList(mkline, ssSed, ssNone)
- ctx.seen().set(ssTransform)
+func (b *substBlock) varassignSed(mkline *MkLine) {
+ b.dupList(mkline, ssSed, ssNone)
+ b.addSeen(ssTransform)
- ctx.suggestSubstVars(mkline)
+ b.suggestSubstVars(mkline)
}
-func (ctx *SubstContext) varassignVars(mkline *MkLine) {
- ctx.dupList(mkline, ssVars, ssVarsAutofix)
- ctx.seen().set(ssTransform)
+func (b *substBlock) varassignVars(mkline *MkLine) {
+ b.dupList(mkline, ssVars, ssVarsAutofix)
+ b.addSeen(ssTransform)
for _, substVar := range mkline.Fields() {
- ctx.allowVar(substVar)
+ b.allowVar(substVar)
}
}
-func (ctx *SubstContext) varassignFilterCmd(mkline *MkLine) {
- ctx.dupString(mkline, ssFilterCmd)
- ctx.seen().set(ssTransform)
+func (b *substBlock) varassignFilterCmd(mkline *MkLine) {
+ b.dupString(mkline, ssFilterCmd)
+ b.addSeen(ssTransform)
}
-func (ctx *SubstContext) dupList(mkline *MkLine, part substSeen, autofixPart substSeen) {
- if ctx.seenInBranch(part) && mkline.Op() != opAssignAppend {
- ctx.fixOperatorAppend(mkline, ctx.seenInBranch(autofixPart))
- }
- ctx.seen().set(part)
-}
-
-func (ctx *SubstContext) dupString(mkline *MkLine, part substSeen) {
- if ctx.seenInBranch(part) {
- mkline.Warnf("Duplicate definition of %q.", mkline.Varname())
- }
- ctx.seen().set(part)
-}
-
-func (ctx *SubstContext) fixOperatorAppend(mkline *MkLine, dueToAutofix bool) {
- before := mkline.ValueAlign()
- after := alignWith(mkline.Varname()+"+=", before)
-
- fix := mkline.Autofix()
- if dueToAutofix {
- fix.Notef(SilentAutofixFormat)
- } else {
- fix.Warnf("All but the first assignment to %q should use the \"+=\" operator.",
- mkline.Varname())
- }
- fix.Replace(before, after)
- fix.Apply()
-}
-
-func (ctx *SubstContext) suggestSubstVars(mkline *MkLine) {
+func (b *substBlock) suggestSubstVars(mkline *MkLine) {
tokens, _ := splitIntoShellTokens(mkline.Line, mkline.Value())
for _, token := range tokens {
- varname := ctx.extractVarname(mkline.UnquoteShell(token, false))
+ varname := b.extractVarname(mkline.UnquoteShell(token, false))
if varname == "" {
continue
}
- id := ctx.activeId()
+ id := b.id
varop := sprintf("SUBST_VARS.%s%s%s",
id,
condStr(hasSuffix(id, "+"), " ", ""),
- condStr(ctx.seenInBranch(ssVars), "+=", "="))
+ condStr(b.hasSeen(ssVars), "+=", "="))
fix := mkline.Autofix()
fix.Notef("The substitution command %q can be replaced with \"%s %s\".",
@@ -291,13 +495,43 @@ func (ctx *SubstContext) suggestSubstVars(mkline *MkLine) {
// assignment operators on them. It's probably not worth the
// effort, though.
- ctx.seen().set(ssVars | ssVarsAutofix)
+ b.addSeen(ssVars)
+ b.addSeen(ssVarsAutofix)
+ }
+}
+
+func (b *substBlock) dupString(mkline *MkLine, part substSeen) {
+ if b.hasSeen(part) {
+ mkline.Warnf("Duplicate definition of %q.", mkline.Varname())
+ }
+ b.addSeen(part)
+}
+
+func (b *substBlock) dupList(mkline *MkLine, part substSeen, autofixPart substSeen) {
+ if b.hasSeenAnywhere(part) && mkline.Op() != opAssignAppend {
+ b.fixOperatorAppend(mkline, b.hasSeen(autofixPart))
+ }
+ b.addSeen(part)
+}
+
+func (b *substBlock) fixOperatorAppend(mkline *MkLine, dueToAutofix bool) {
+ before := mkline.ValueAlign()
+ after := alignWith(mkline.Varname()+"+=", before)
+
+ fix := mkline.Autofix()
+ if dueToAutofix {
+ fix.Notef(SilentAutofixFormat)
+ } else {
+ fix.Warnf("All but the first assignment to %q should use the \"+=\" operator.",
+ mkline.Varname())
}
+ fix.Replace(before, after)
+ fix.Apply()
}
// extractVarname extracts the variable name from a sed command of the form
// s,@VARNAME@,${VARNAME}, and some related variants thereof.
-func (*SubstContext) extractVarname(token string) string {
+func (*substBlock) extractVarname(token string) string {
parser := NewMkLexer(token, nil)
lexer := parser.lexer
if !lexer.SkipByte('s') {
@@ -337,235 +571,111 @@ func (*SubstContext) extractVarname(token string) string {
return varname
}
-func (*SubstContext) isForeign(varcanon string) bool {
- switch varcanon {
- case
- "SUBST_STAGE.*",
- "SUBST_MESSAGE.*",
- "SUBST_FILES.*",
- "SUBST_SED.*",
- "SUBST_VARS.*",
- "SUBST_FILTER_CMD.*":
- return false
- }
- return true
+func (b *substBlock) isComplete() bool {
+ return b.allSeen().hasAll(ssStage | ssFiles | ssTransform)
}
-func (*SubstContext) isListCanon(varcanon string) bool {
- switch varcanon {
- case
- "SUBST_FILES.*",
- "SUBST_SED.*",
- "SUBST_VARS.*":
- return true
+func (b *substBlock) hasSeen(part substSeen) bool {
+ for _, cond := range b.conds {
+ if cond.hasSeen(part) {
+ return true
+ }
}
return false
}
-func (ctx *SubstContext) directive(mkline *MkLine) {
- dir := mkline.Directive()
- switch dir {
- case "if":
- ctx.condIf()
-
- case "elif", "else":
- ctx.condElse(mkline, dir)
-
- case "endif":
- ctx.condEndif(mkline)
+func (b *substBlock) hasSeenAnywhere(part substSeen) bool {
+ for _, cond := range b.conds {
+ if cond.hasSeenAnywhere(part) {
+ return true
+ }
}
+ return false
}
-func (ctx *SubstContext) condIf() {
- top := substCond{total: ssAll}
- ctx.conds = append(ctx.conds, &top)
+func (b *substBlock) allSeen() substSeen {
+ all := ssNone
+ for _, cond := range b.conds {
+ all.addAll(cond.curr)
+ }
+ return all
}
-func (ctx *SubstContext) condElse(mkline *MkLine, dir string) {
- top := ctx.cond()
- top.total.retain(top.curr)
- if !ctx.isConditional() {
- ctx.finishClass(mkline)
- }
- top.curr = ssNone
- top.seenElse = dir == "else"
+func (b *substBlock) addSeen(part substSeen) {
+ b.conds[len(b.conds)-1].addSeen(part)
}
-func (ctx *SubstContext) condEndif(diag Diagnoser) {
- top := ctx.cond()
- top.total.retain(top.curr)
- if !ctx.isConditional() {
- ctx.finishClass(diag)
- }
- if !top.seenElse {
- top.total = ssNone
- }
- if len(ctx.conds) > 1 {
- ctx.conds = ctx.conds[:len(ctx.conds)-1]
- }
- ctx.seen().union(top.total)
+func (b *substBlock) rememberForeign(mkline *MkLine) {
+ b.foreign = append(b.foreign, mkline)
}
-func (ctx *SubstContext) finishClass(diag Diagnoser) {
- if !ctx.isActive() {
- return
- }
+// isConditional returns whether the current line is at a deeper conditional
+// level than the corresponding SUBST_CLASSES line.
+func (b *substBlock) isConditional() bool { return len(b.conds) > 1 }
- if ctx.seen().get(ssAll) {
- ctx.checkBlockComplete(diag)
- ctx.checkForeignVariables()
- } else {
- ctx.markAsNotDone()
+func (b *substBlock) allowVar(varname string) {
+ if b.foreignAllowed == nil {
+ b.foreignAllowed = map[string]struct{}{}
}
-
- ctx.reset()
+ b.foreignAllowed[varname] = struct{}{}
}
-func (ctx *SubstContext) checkBlockComplete(diag Diagnoser) {
- id := ctx.activeId()
- seen := ctx.seen()
- if !seen.get(ssStage) {
- diag.Warnf("Incomplete SUBST block: SUBST_STAGE.%s missing.", id)
- }
- if !seen.get(ssFiles) {
- diag.Warnf("Incomplete SUBST block: SUBST_FILES.%s missing.", id)
- }
- if !seen.get(ssTransform) {
- diag.Warnf("Incomplete SUBST block: SUBST_SED.%[1]s, SUBST_VARS.%[1]s or SUBST_FILTER_CMD.%[1]s missing.", id)
- }
-}
+func (b *substBlock) enter() { b.conds = append(b.conds, newSubstCond()) }
-func (ctx *SubstContext) checkForeignVariables() {
- ctx.forEachForeignVar(func(mkline *MkLine) {
- mkline.Warnf("Foreign variable %q in SUBST block.", mkline.Varname())
- })
+func (b *substBlock) nextBranch(isElse bool) {
+ cond := b.conds[len(b.conds)-1]
+ cond.leaveBranch()
+ cond.enterBranch(isElse)
}
-func (ctx *SubstContext) finishFile(diag Diagnoser) {
- for _, id := range ctx.queuedIds {
- if id != "" && !ctx.isDone(id) {
- ctx.warnUndefinedBlock(diag, id)
- }
- }
-}
+func (b *substBlock) leave() {
+ assert(b.isConditional())
-func (*SubstContext) warnUndefinedBlock(diag Diagnoser, id string) {
- diag.Warnf("Missing SUBST block for %q.", id)
- diag.Explain(
- "After adding a SUBST class to SUBST_CLASSES,",
- "the remaining SUBST variables should be defined in the same file.",
- "",
- "See mk/subst.mk for the comprehensive documentation.")
+ n := len(b.conds)
+ b.conds[n-2].leaveLevel(b.conds[n-1])
+ b.conds = b.conds[:n-1]
}
-// In the paragraph of a SUBST block, there should be only variables
-// that actually belong to the SUBST block.
-//
-// In addition, variables that are mentioned in SUBST_VARS may also
-// be defined there because they closely relate to the SUBST block.
+func (b *substBlock) finish(diag Diagnoser) {
+ assert(len(b.conds) == 1)
-func (ctx *SubstContext) allowVar(varname string) {
- if ctx.foreignAllowed == nil {
- ctx.foreignAllowed = make(map[string]struct{})
+ if b.done {
+ return
}
- ctx.foreignAllowed[varname] = struct{}{}
-}
+ b.done = true
-func (ctx *SubstContext) rememberForeign(mkline *MkLine) {
- ctx.foreign = append(ctx.foreign, mkline)
-}
-
-// forEachForeignVar performs the given action for each variable that
-// is defined in the SUBST block and is not mentioned in SUBST_VARS.
-func (ctx *SubstContext) forEachForeignVar(action func(*MkLine)) {
- for _, foreign := range ctx.foreign {
- if _, ok := ctx.foreignAllowed[foreign.Varname()]; !ok {
- action(foreign)
- }
+ if !b.hasStarted() {
+ diag.Warnf("Missing SUBST block for %q.", b.id)
+ return
}
-}
-
-func (ctx *SubstContext) reset() {
- ctx.id = ""
- ctx.foreignAllowed = nil
- ctx.foreign = nil
- ctx.conds = []*substCond{{seenElse: true}}
-}
-
-func (ctx *SubstContext) isActive() bool { return ctx.id != "" }
-
-func (ctx *SubstContext) isActiveId(id string) bool { return ctx.id == id }
-
-func (ctx *SubstContext) activeId() string {
- assert(ctx.isActive())
- return ctx.id
-}
-
-func (ctx *SubstContext) setActiveId(id string) {
- ctx.id = id
- ctx.cond().top = true
- ctx.markAsDone(id)
-}
-func (ctx *SubstContext) queue(id string) {
- ctx.queuedIds = append(ctx.queuedIds, id)
-}
-
-func (ctx *SubstContext) start(id string) bool {
- for i, queuedId := range ctx.queuedIds {
- if queuedId == id {
- ctx.queuedIds[i] = ""
- ctx.setActiveId(id)
- return true
- }
+ if !b.hasSeen(ssStage) {
+ diag.Warnf("Incomplete SUBST block: SUBST_STAGE.%s missing.", b.id)
}
- return false
-}
-
-func (ctx *SubstContext) markAsDone(id string) {
- if ctx.doneIds == nil {
- ctx.doneIds = map[string]bool{}
+ if !b.hasSeen(ssFiles) {
+ diag.Warnf("Incomplete SUBST block: SUBST_FILES.%s missing.", b.id)
+ }
+ if !b.hasSeen(ssTransform) {
+ diag.Warnf(
+ "Incomplete SUBST block: SUBST_SED.%[1]s, "+
+ "SUBST_VARS.%[1]s or SUBST_FILTER_CMD.%[1]s missing.",
+ b.id)
}
- ctx.doneIds[id] = true
-}
-
-func (ctx *SubstContext) markAsNotDone() {
- ctx.doneIds[ctx.id] = false
-}
-
-func (ctx *SubstContext) isDone(varparam string) bool {
- return ctx.doneIds[varparam]
-}
-
-func (ctx *SubstContext) isComplete() bool {
- return ctx.seen().hasAll(ssStage | ssFiles | ssTransform)
-}
-
-// isConditional returns whether the current line is at a deeper conditional
-// level than the assignment where the corresponding class ID is added to
-// SUBST_CLASSES.
-//
-// TODO: Adjust the implementation to this description.
-func (ctx *SubstContext) isConditional() bool {
- return !ctx.cond().top
-}
-// cond returns information about the parts of the SUBST block that
-// have already been seen in the current leaf branch of the conditionals.
-func (ctx *SubstContext) seen() *substSeen {
- return &ctx.cond().curr
+ b.checkForeignVariables()
}
-// cond returns information about the current branch of conditionals.
-func (ctx *SubstContext) cond() *substCond {
- return ctx.conds[len(ctx.conds)-1]
+func (b *substBlock) checkForeignVariables() {
+ for _, mkline := range b.foreign {
+ if _, ok := b.foreignAllowed[mkline.Varname()]; !ok {
+ mkline.Warnf("Foreign variable %q in SUBST block.", mkline.Varname())
+ }
+ }
}
-// Returns true if the given flag from substSeen has been seen
-// somewhere in the conditional path of the current line.
-func (ctx *SubstContext) seenInBranch(part substSeen) bool {
- for _, cond := range ctx.conds {
- if cond.curr.get(part) {
+func (b *substBlock) hasStarted() bool {
+ for _, cond := range b.conds {
+ if cond.currAny != ssNone {
return true
}
}
@@ -573,23 +683,16 @@ func (ctx *SubstContext) seenInBranch(part substSeen) bool {
}
type substCond struct {
- // Tells whether a SUBST block has started at this conditional level.
- // All variable assignments that belong to this class must happen at
- // this conditional level or below it.
- //
- // TODO: For Test_SubstContext_Directive__conditional_complete,
- // this needs to be changed to the set of classes that have been
- // added to SUBST_CLASSES at this level.
- top bool
-
// Collects the parts of the SUBST block that have been defined in all
// branches that have been parsed completely.
- total substSeen
+ total substSeen
+ totalAny substSeen
// Collects the parts of the SUBST block that are defined in the current
// branch of the conditional. At the end of the branch, they are merged
// into the total.
- curr substSeen
+ curr substSeen
+ currAny substSeen
// Marks whether the current conditional statement has
// an .else branch. If it doesn't, this means that all variables
@@ -597,6 +700,40 @@ type substCond struct {
seenElse bool
}
+func newSubstCond() *substCond { return &substCond{total: ssAll} }
+
+func (c *substCond) enterBranch(isElse bool) {
+ c.curr = ssNone
+ c.currAny = ssNone
+ c.seenElse = isElse
+}
+
+func (c *substCond) leaveBranch() {
+ c.total.retainAll(c.curr)
+ c.totalAny.addAll(c.currAny)
+ c.curr = ssNone
+ c.currAny = ssNone
+}
+
+func (c *substCond) leaveLevel(child *substCond) {
+ child.leaveBranch()
+ if child.seenElse {
+ c.curr.addAll(child.total)
+ }
+ c.currAny.addAll(child.totalAny)
+}
+
+func (c *substCond) hasSeen(part substSeen) bool { return c.curr.has(part) }
+
+func (c *substCond) hasSeenAnywhere(part substSeen) bool {
+ return c.currAny.has(part)
+}
+
+func (c *substCond) addSeen(part substSeen) {
+ c.curr.set(part)
+ c.currAny.set(part)
+}
+
// substSeen contains all variables that depend on a particular SUBST
// class ID. These variables can be set in conditional branches, and
// pkglint keeps track whether they are set in all branches or only
@@ -617,8 +754,16 @@ const (
ssNone substSeen = 0
)
-func (s *substSeen) set(part substSeen) { *s |= part }
-func (s *substSeen) get(part substSeen) bool { return *s&part != 0 }
-func (s *substSeen) hasAll(other substSeen) bool { return *s&other == other }
-func (s *substSeen) union(other substSeen) { *s |= other }
-func (s *substSeen) retain(other substSeen) { *s &= other }
+func (s *substSeen) set(part substSeen) {
+ assert(part&(part-1) == 0)
+ *s |= part
+}
+
+func (s *substSeen) has(part substSeen) bool {
+ assert(part&(part-1) == 0)
+ return *s&part != 0
+}
+
+func (s substSeen) hasAll(other substSeen) bool { return s&other == other }
+func (s *substSeen) addAll(other substSeen) { *s |= other }
+func (s *substSeen) retainAll(other substSeen) { *s &= other }