Skip to content

Replace getRelativePath with getNeighborPath in C++ code gen #880

@bocchino

Description

@bocchino

The C++ code generation uses the function getRelativePath to generate include paths for definitions in the model:

/** Gets the relative path for a file */
def getRelativePath(fileName: String): File.JavaPath = {
val path = java.nio.file.Paths.get(fileName).toAbsolutePath.normalize
removeLongestPathPrefix(path)
}

For example, the ArrayCppWriter calls this function to generate the header file for the array class corresponding to the FPP array definition:

private def getCppIncludes: CppDoc.Member = {
val userHeaders = List(
"Fw/Types/Assert.hpp",
s"${s.getRelativePath(fileName).toString}.hpp"
).sorted.map(CppWriter.headerString).map(line)
linesMember(Line.blank :: userHeaders, CppDoc.Lines.Cpp)
}

This usage of getRelativePath is not actually correct, because it generates paths relative to the current directory (where fpp-to-cpp is run) instead of relative to the location of the definition. We should replace this and other calls to getRelativePath with calls to a new function getNeighborPath(symbol, fileName). This function can work like getIncludePath, which is correct:

def getIncludePath(
sym: Symbol,
fileNameBase: String,
headerExtension: String = "hpp"
): String = {
val loc = sym.getLoc.tuLocation
val fullPath = loc.getNeighborPath(fileNameBase)
val path = removeLongestPathPrefix(fullPath)
s"${path.toString}.$headerExtension"
}

We can refactor getIncludePath to use this function.

I believe this bug or anomaly in the code generation has never been noticed because we always run fpp-to-cpp at the location of the definition, so the current directory is the parent directory for the definition. However, if you run fpp-to-cpp foo/bar.fpp, you can see that the headers are generated relative to ., not relative to foo.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingcode generationIssues related to code generation

Type

No type

Projects

Status

Assigned

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions