diff --git a/internal/cgen/cgen.go b/internal/cgen/cgen.go index a642ffe7..cdcf4329 100644 --- a/internal/cgen/cgen.go +++ b/internal/cgen/cgen.go @@ -428,8 +428,7 @@ func insertInterfaceDeclarations(buf *buffer) error { } buf.writes(");\n }\n\n") } - buf.writes("#endif // __cplusplus\n\n") - + buf.writes("#endif // __cplusplus\n") buf.printf("}; // struct wuffs_base__%s__struct\n\n", n) buf.writes("#endif // defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION)\n") @@ -1046,7 +1045,7 @@ func (g *gen) writeStructPrivateImpl(b *buffer, n *a.Struct) error { } if n.Classy() { - b.writeb('\n') + needEmptyLine := true for _, file := range g.files { for _, tld := range file.TopLevelDecls() { if tld.Kind() != a.KFunc { @@ -1061,6 +1060,10 @@ func (g *gen) writeStructPrivateImpl(b *buffer, n *a.Struct) error { continue } + if needEmptyLine { + needEmptyLine = false + b.writeb('\n') + } b.printf("uint32_t %s%s[%d];\n", pPrefix, o.FuncName().Str(g.tm), maxDepth) } } @@ -1083,10 +1086,8 @@ func (g *gen) writeStructPrivateImpl(b *buffer, n *a.Struct) error { } b.writes(";\n") } - if oldOuterLenB1 != len(*b) { - b.writeb('\n') - } + needEmptyLine := oldOuterLenB1 != len(*b) for _, file := range g.files { for _, tld := range file.TopLevelDecls() { if tld.Kind() != a.KFunc { @@ -1102,6 +1103,11 @@ func (g *gen) writeStructPrivateImpl(b *buffer, n *a.Struct) error { } oldInnerLenB0 := len(*b) + oldNeedEmptyLine := needEmptyLine + if needEmptyLine { + needEmptyLine = false + b.writeb('\n') + } b.writes("struct {\n") oldInnerLenB1 := len(*b) if k.coroSuspPoint != 0 { @@ -1116,6 +1122,7 @@ func (g *gen) writeStructPrivateImpl(b *buffer, n *a.Struct) error { b.printf("} %s%s[%d];\n", sPrefix, o.FuncName().Str(g.tm), maxDepth) } else { *b = (*b)[:oldInnerLenB0] + needEmptyLine = oldNeedEmptyLine } } } @@ -1240,7 +1247,7 @@ func (g *gen) writeCppMethods(b *buffer, n *a.Struct) error { } } - b.writes("#endif // __cplusplus\n\n") + b.writes("#endif // __cplusplus\n") return nil } diff --git a/internal/cgen/func.go b/internal/cgen/func.go index bba27d6e..db48434a 100644 --- a/internal/cgen/func.go +++ b/internal/cgen/func.go @@ -334,6 +334,8 @@ func writeFuncImplSelfMagicCheck(b *buffer, tm *t.Map, f *a.Func) error { } func (g *gen) writeFuncImplPrologue(b *buffer) error { + oldLenB := len(*b) + // Check the initialized/disabled state and the "self" arg. if g.currFunk.astFunc.Public() && !g.currFunk.astFunc.Receiver().IsZero() { if err := writeFuncImplSelfMagicCheck(b, g.tm, g.currFunk.astFunc); err != nil { @@ -365,10 +367,13 @@ func (g *gen) writeFuncImplPrologue(b *buffer) error { // TODO: rename the "status" variable to "ret"? b.printf("wuffs_base__status status = wuffs_base__make_status(NULL);\n") } - b.writes("\n") + + if oldLenB != len(*b) { + b.writes("\n") + } // Generate the local variables. - oldLenB := len(*b) + oldLenB = len(*b) if err := g.writeVars(b, &g.currFunk, false); err != nil { return err } diff --git a/lib/dumbindent/dumbindent.go b/lib/dumbindent/dumbindent.go index c7c93091..2b805b0e 100644 --- a/lib/dumbindent/dumbindent.go +++ b/lib/dumbindent/dumbindent.go @@ -173,11 +173,10 @@ func FormatBytes(dst []byte, src []byte, opts *Options) []byte { } } - nBlankLines := 0 // The number of preceding blank lines. - nBraces := 0 // The number of unbalanced '{'s. - nParens := 0 // The number of unbalanced '('s. - openBrace := false // Whether the previous non-blank line ends with '{'. - hanging := false // Whether the previous non-blank line ends with '=' or '\\'. + nBlankLines := 0 // The number of preceding blank lines. + nBraces := 0 // The number of unbalanced '{'s. + nParens := 0 // The number of unbalanced '('s. + hanging := false // Whether the previous non-blank line ends with '=' or '\\'. outer: for line, remaining := src, []byte(nil); len(src) > 0; src = remaining { @@ -198,9 +197,7 @@ outer: continue } if nBlankLines > 0 { - if !openBrace && (line[0] != '}') { - dst = appendRepeatedBytes(dst, newLines, nBlankLines) - } + dst = appendRepeatedBytes(dst, newLines, nBlankLines) nBlankLines = 0 } @@ -213,7 +210,6 @@ outer: line = trimTrailingWhiteSpace(line) dst = append(dst, line...) dst = append(dst, '\n') - openBrace = false hanging = lastNonWhiteSpace(line) == '\\' continue } @@ -264,7 +260,6 @@ outer: dst = append(dst, '\n') restOfLine := line[i+1:] remaining = src[lineLength-len(restOfLine):] - openBrace = true hanging = false continue outer } @@ -281,7 +276,6 @@ outer: dst = append(dst, '\n') restOfLine := line[i+1:] remaining = src[lineLength-len(restOfLine):] - openBrace = false hanging = false continue outer } @@ -323,7 +317,6 @@ outer: } break inner } - openBrace = last == '{' hanging = hangingBytes[last] // Output the line (minus any trailing space). diff --git a/lib/dumbindent/dumbindent_test.go b/lib/dumbindent/dumbindent_test.go index 1357edbb..66553ebc 100644 --- a/lib/dumbindent/dumbindent_test.go +++ b/lib/dumbindent/dumbindent_test.go @@ -46,7 +46,7 @@ func TestFormatBytes(tt *testing.T) { }, { // Consecutive blank lines. src: "f {\n\n\ng;\n\n\nh;\n\n\n}\n\n", - want: "f {\n g;\n\n\n h;\n}\n", + want: "f {\n\n\n g;\n\n\n h;\n\n\n}\n", }, { // Single-quote string. src: "a = '{'\nb = {\nc = 0\n", diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 80c3f4f6..6f663f47 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -4192,7 +4192,6 @@ struct wuffs_base__hasher_u32__struct { } #endif // __cplusplus - }; // struct wuffs_base__hasher_u32__struct #endif // defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION) @@ -4414,7 +4413,6 @@ struct wuffs_base__image_decoder__struct { } #endif // __cplusplus - }; // struct wuffs_base__image_decoder__struct #endif // defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION) @@ -4493,7 +4491,6 @@ struct wuffs_base__io_transformer__struct { } #endif // __cplusplus - }; // struct wuffs_base__io_transformer__struct #endif // defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION) @@ -4572,7 +4569,6 @@ struct wuffs_base__token_decoder__struct { } #endif // __cplusplus - }; // struct wuffs_base__token_decoder__struct #endif // defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION)