From 227e0373ab0f0caf49d8b942421f57d9cdb490f4 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 28 May 2017 11:10:57 +1000 Subject: [PATCH] Generate an include guard. --- cmd/internal/cgen/cgen.go | 5 +++++ gen/c/gif.c | 5 +++++ gen/h/gif.h | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/cmd/internal/cgen/cgen.go b/cmd/internal/cgen/cgen.go index c9450c1d..69af33f2 100644 --- a/cmd/internal/cgen/cgen.go +++ b/cmd/internal/cgen/cgen.go @@ -10,6 +10,7 @@ import ( "fmt" "os" "os/exec" + "strings" a "github.com/google/puffs/lang/ast" t "github.com/google/puffs/lang/token" @@ -31,6 +32,9 @@ type Generator struct { func (g *Generator) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byte, error) { b := &bytes.Buffer{} + includeGuard := "PUFFS_" + strings.ToUpper(pkgName) + "_H" + fmt.Fprintf(b, "#ifndef %s\n#define %s\n\n", includeGuard, includeGuard) + // Write preamble. fmt.Fprintf(b, "// Code generated by puffs-gen-%c. DO NOT EDIT.\n\n", g.Extension) b.WriteString(preamble) @@ -66,6 +70,7 @@ func (g *Generator) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byt // Finish up the header, which is also the first part of the .c file. b.WriteString("\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n") + fmt.Fprintf(b, "#endif // %s\n\n", includeGuard) if g.Extension == 'h' { return format(b) } diff --git a/gen/c/gif.c b/gen/c/gif.c index b59a3d4d..587e326c 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -1,3 +1,6 @@ +#ifndef PUFFS_GIF_H +#define PUFFS_GIF_H + // Code generated by puffs-gen-c. DO NOT EDIT. // Use of this source code is governed by a BSD-style license that can be found @@ -70,6 +73,8 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self); } // extern "C" #endif +#endif // PUFFS_GIF_H + // ---------------- Private Structs // ---------------- Private Constructor and Destructor Prototypes diff --git a/gen/h/gif.h b/gen/h/gif.h index 618091a0..4a2d6652 100644 --- a/gen/h/gif.h +++ b/gen/h/gif.h @@ -1,3 +1,6 @@ +#ifndef PUFFS_GIF_H +#define PUFFS_GIF_H + // Code generated by puffs-gen-h. DO NOT EDIT. // Use of this source code is governed by a BSD-style license that can be found @@ -69,3 +72,5 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self); #ifdef __cplusplus } // extern "C" #endif + +#endif // PUFFS_GIF_H