aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/asm/internal/lex/input.go
diff options
context:
space:
mode:
authorMichael Hudson-Doyle <michael.hudson@canonical.com>2015-09-03 22:49:18 +1200
committerMichael Hudson-Doyle <michael.hudson@canonical.com>2015-09-04 05:23:28 +0000
commiteaea5ade2b5f60c6dfd72a08c9243e1651778332 (patch)
tree0dc3e00df2e555dc22d84a336da188777b69413b /src/cmd/asm/internal/lex/input.go
parent13e06d89c3ccba9fd4f0456d68407ff941409b57 (diff)
downloadgo-eaea5ade2b5f60c6dfd72a08c9243e1651778332.tar.xz
cmd/asm: fix handling of nested #if[n]defs
The lexer needs to process all #if[n]defs, even those found when processing is disabled by a preceding failed conditional, or the first #endif in something like: #ifdef <undefined> #ifdef whatever #endif #endif terminates the first #ifdef and the second causes an error. And then the processing of the inner #ifdefs needs to ignore their argument when they are disabled by an outer failed condition. Change-Id: Iba259498f1e16042f5b7580b9c000bb0599733d0 Reviewed-on: https://go-review.googlesource.com/14253 Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/cmd/asm/internal/lex/input.go')
-rw-r--r--src/cmd/asm/internal/lex/input.go13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/cmd/asm/internal/lex/input.go b/src/cmd/asm/internal/lex/input.go
index 7e495b8edf..cd9168064d 100644
--- a/src/cmd/asm/internal/lex/input.go
+++ b/src/cmd/asm/internal/lex/input.go
@@ -136,10 +136,11 @@ func (in *Input) hash() bool {
in.expectText("expected identifier after '#'")
}
if !in.enabled() {
- // Can only start including again if we are at #else or #endif.
+ // Can only start including again if we are at #else or #endif but also
+ // need to keep track of nested #if[n]defs.
// We let #line through because it might affect errors.
switch in.Stack.Text() {
- case "else", "endif", "line":
+ case "else", "endif", "ifdef", "ifndef", "line":
// Press on.
default:
return false
@@ -360,7 +361,9 @@ func (in *Input) collectArgument(macro *Macro) ([]Token, ScanToken) {
func (in *Input) ifdef(truth bool) {
name := in.macroName()
in.expectNewline("#if[n]def")
- if _, defined := in.macros[name]; !defined {
+ if !in.enabled() {
+ truth = false
+ } else if _, defined := in.macros[name]; !defined {
truth = !truth
}
in.ifdefStack = append(in.ifdefStack, truth)
@@ -372,7 +375,9 @@ func (in *Input) else_() {
if len(in.ifdefStack) == 0 {
in.Error("unmatched #else")
}
- in.ifdefStack[len(in.ifdefStack)-1] = !in.ifdefStack[len(in.ifdefStack)-1]
+ if len(in.ifdefStack) == 1 || in.ifdefStack[len(in.ifdefStack)-2] {
+ in.ifdefStack[len(in.ifdefStack)-1] = !in.ifdefStack[len(in.ifdefStack)-1]
+ }
}
// #endif processing.