Skip to content

Commit

Permalink
[c2cpg] Fixed fileName helper (#4940)
Browse files Browse the repository at this point in the history
Using the CDT API (getContainingFilename) now.

For: #4924
  • Loading branch information
max-leuthaeuser authored Sep 27, 2024
1 parent 8b6992d commit 5e38766
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As
Option(cdtAst.flattenLocationsToFile(node.getNodeLocations.lastOption.toArray)).map(_.asFileLocation())

protected def fileName(node: IASTNode): String = {
val path = nullSafeFileLocation(node).map(_.getFileName).getOrElse(filename)
val path = Try(node.getContainingFilename).getOrElse(filename)
SourceFiles.toRelativePath(path, config.inputPath)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,51 @@ class FileTests extends C2CpgSuite {
}
}

"File test for single file with a header include" should {

val cpg = code(
"""
|#include "fetch.h"
|#include "cache.h"
|const char *write_ref = NULL;
|void pull_say(const char *fmt, const char *hex {
| if (get_verbosely) { fprintf(stderr, fmt, hex); }
|}
|""".stripMargin,
"fetch.c"
)

"contain the correct file nodes" in {
cpg.file.name.sorted.l shouldBe List("<includes>", "<unknown>", "fetch.c")
}

}

"File test for single file with a header include that actually exists" should {

val cpg = code(
"""
|#include "fetch.h"
|#include "cache.h"
|const char *write_ref = NULL;
|void pull_say(const char *fmt, const char *hex {
| if (get_verbosely) { fprintf(stderr, fmt, hex); }
|}
|""".stripMargin,
"fetch.c"
).moreCode(
"""
|extern const char *write_ref;
|""".stripMargin,
"fetch.h"
)

"contain the correct file nodes" in {
cpg.file.name.sorted.l shouldBe List("<includes>", "<unknown>", "fetch.c", "fetch.h")
}

}

"File test for multiple source files and preprocessed files" should {

val cpg = code("int foo() {}", "main.c")
Expand Down

0 comments on commit 5e38766

Please sign in to comment.