Skip to content

Commit

Permalink
Merge pull request #2165 from lf-lang/cpp-praemble-import
Browse files Browse the repository at this point in the history
Fixed generation of preambles for unused imports
  • Loading branch information
cmnrd authored Jan 26, 2024
2 parents e2e36ea + 4c421ac commit e85fc79
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
19 changes: 14 additions & 5 deletions core/src/main/kotlin/org/lflang/generator/cpp/CppGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class CppGenerator(
if (targetConfig.get(Ros2Property.INSTANCE)) CppRos2Generator(this) else CppStandaloneGenerator(this)

// generate all core files
generateFiles(platformGenerator.srcGenPath)
generateFiles(platformGenerator.srcGenPath, getAllImportedResources(resource))

// generate platform specific files
platformGenerator.generatePlatformFiles()
Expand Down Expand Up @@ -117,7 +117,15 @@ class CppGenerator(
commandFactory.createCommand("git", listOf("checkout", version), libPath).run()
}

private fun generateFiles(srcGenPath: Path) {
private fun getAllImportedResources(resource: Resource): Set<Resource> {
val resources: MutableSet<Resource> = scopeProvider.getImportedResources(resource)
val importedRresources = resources.subtract(setOf(resource))
resources.addAll(importedRresources.map { getAllImportedResources(it) }.flatten())
resources.add(resource)
return resources
}

private fun generateFiles(srcGenPath: Path, resources: Set<Resource>) {
// copy static library files over to the src-gen directory
val genIncludeDir = srcGenPath.resolve("__include__")
listOf("lfutil.hh", "time_parser.hh").forEach {
Expand Down Expand Up @@ -158,11 +166,12 @@ class CppGenerator(
FileUtil.writeToFile(reactorCodeMap.generatedCode, srcGenPath.resolve(sourceFile), true)
}


// generate file level preambles for all resources
for (r in resources) {
val generator = CppPreambleGenerator(r.eResource, fileConfig, scopeProvider)
val sourceFile = fileConfig.getPreambleSourcePath(r.eResource)
val headerFile = fileConfig.getPreambleHeaderPath(r.eResource)
val generator = CppPreambleGenerator(r, fileConfig, scopeProvider)
val sourceFile = fileConfig.getPreambleSourcePath(r)
val headerFile = fileConfig.getPreambleHeaderPath(r)
val preambleCodeMap = CodeMap.fromGeneratedCode(generator.generateSource())
cppSources.add(sourceFile)
codeMaps[srcGenPath.resolve(sourceFile)] = preambleCodeMap
Expand Down
10 changes: 10 additions & 0 deletions test/Cpp/src/PreambleImport.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This tests checks if the preamble of an imported reactor is included correctly even if the reactor is not used.
target Cpp

import PreambleFoo from "PreambleTest.lf"

main reactor {
reaction(startup) {=
std::cout << foo() << std::endl;
=}
}
27 changes: 25 additions & 2 deletions test/Cpp/src/PreambleTest.lf
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
target Cpp

// This is used to test for nested unused imports in PreambleImport.lf
import Imported from "lib/Imported.lf"

public preamble {=
int foo();
=}

private preamble {=
int foo() {
return 42;
}
=}

// This is a dummy reactor to test importing of preambles in PreambleImport.lf
reactor PreambleFoo {
}

main reactor {
// This declaration is required on the public reactor interface and therefore needs to be placed
// in the generated header. This goes to Preamble/Preamble.hh.
Expand All @@ -15,17 +32,23 @@ main reactor {
// This goes to Preamble/Preamble.cc
private preamble {=
int add_42(int i) {
return i + 42;
return i + foo();
}
=}
logical action a: MyStruct

reaction(startup) -> a {=
a.schedule({add_42(42), "baz"});
a.schedule({add_42(foo()), "baz"});
=}

reaction(a) {=
auto& value = *a.get();
std::cout << "Received " << value.foo << " and '" << value.bar << "'\n";
if (value.foo == 84 && value.bar == "baz") {
std::cout << "Success!\n";
} else {
std::cout << "Error!\n";
exit(1);
}
=}
}

0 comments on commit e85fc79

Please sign in to comment.