diff --git a/core/src/main/java/org/lflang/generator/c/CGenerator.java b/core/src/main/java/org/lflang/generator/c/CGenerator.java index b02c2ed5b0..ff433cba00 100644 --- a/core/src/main/java/org/lflang/generator/c/CGenerator.java +++ b/core/src/main/java/org/lflang/generator/c/CGenerator.java @@ -948,7 +948,7 @@ private void generateReactorClass(TypeParameterizedReactor tpr) throws IOExcepti CodeBuilder header = new CodeBuilder(); CodeBuilder src = new CodeBuilder(); final String headerName = CUtil.getName(tpr) + ".h"; - var guardMacro = headerName.toUpperCase().replace(".", "_"); + var guardMacro = CUtil.internalIncludeGuard(tpr); header.pr("#ifndef " + guardMacro); header.pr("#define " + guardMacro); generateReactorClassHeaders(tpr, headerName, header, src); diff --git a/core/src/main/java/org/lflang/generator/c/CReactorHeaderFileGenerator.java b/core/src/main/java/org/lflang/generator/c/CReactorHeaderFileGenerator.java index ced74cb160..5a9ec91ac1 100644 --- a/core/src/main/java/org/lflang/generator/c/CReactorHeaderFileGenerator.java +++ b/core/src/main/java/org/lflang/generator/c/CReactorHeaderFileGenerator.java @@ -55,7 +55,7 @@ private static String generateHeaderFile( GenerateAuxiliaryStructs generator, String topLevelPreamble) { CodeBuilder builder = new CodeBuilder(); - appendIncludeGuard(builder, tpr); + appendIncludeGuards(builder, tpr); builder.pr(topLevelPreamble); appendPoundIncludes(builder); tpr.doDefines(builder); @@ -64,14 +64,20 @@ private static String generateHeaderFile( for (Reaction reaction : tpr.reactor().getReactions()) { appendSignature(builder, types, reaction, tpr); } - builder.pr("#endif"); + closeIncludeGuards(builder); return builder.getCode(); } - private static void appendIncludeGuard(CodeBuilder builder, TypeParameterizedReactor r) { + private static void appendIncludeGuards(CodeBuilder builder, TypeParameterizedReactor r) { String macro = CUtil.getName(r) + "_H"; builder.pr("#ifndef " + macro); builder.pr("#define " + macro); + builder.pr("#ifndef " + CUtil.internalIncludeGuard(r)); + } + + private static void closeIncludeGuards(CodeBuilder builder) { + builder.pr("#endif"); + builder.pr("#endif"); } private static void appendPoundIncludes(CodeBuilder builder) { diff --git a/core/src/main/java/org/lflang/generator/c/CUtil.java b/core/src/main/java/org/lflang/generator/c/CUtil.java index 37d088c044..0158faa40a 100644 --- a/core/src/main/java/org/lflang/generator/c/CUtil.java +++ b/core/src/main/java/org/lflang/generator/c/CUtil.java @@ -148,6 +148,14 @@ public static String getName(TypeParameterizedReactor reactor) { return name; } + /** + * Return the name used in the internal (non-user-facing) include guard for the given reactor. + */ + public static String internalIncludeGuard(TypeParameterizedReactor tpr) { + final String headerName = CUtil.getName(tpr) + ".h"; + return headerName.toUpperCase().replace(".", "_"); + } + /** * Return a reference to the specified port. * diff --git a/test/C/src/arduino/DualReactorBlink.lf b/test/C/src/arduino/DualReactorBlink.lf new file mode 100644 index 0000000000..57252c6986 --- /dev/null +++ b/test/C/src/arduino/DualReactorBlink.lf @@ -0,0 +1,35 @@ +target C { + platform: { + name: "arduino", + board: "arduino:avr:mega" + } +} + +reactor Blinker { + input in_port:bool + + reaction(startup) {= + pinMode(LED_BUILTIN, OUTPUT); + =} + + reaction(in_port) {= + digitalWrite(LED_BUILTIN, in_port->value ? HIGH : LOW); + =} +} + +reactor Timer { + timer t1(0, 500 msec) + state on_off:bool = false + output out_port:bool + + reaction (t1) -> out_port {= + self->on_off = !self->on_off; + lf_set(out_port, self->on_off); + =} +} + +main reactor { + the_blinker = new Blinker() + the_timer = new Timer() + the_timer.out_port -> the_blinker.in_port; +}