Skip to content

Commit

Permalink
Parsing CPPASTLinkageSpecification (#1579)
Browse files Browse the repository at this point in the history
It seems we previously ignored it, which meant that all code that was behind `extern "C"` was ignored.

Fixes #1578
  • Loading branch information
oxisto authored Jun 7, 2024
1 parent 2c3f52d commit 200c6d6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class DeclarationHandler(lang: CXXLanguageFrontend) :
is CPPASTUsingDeclaration -> handleUsingDeclaration(node)
is CPPASTAliasDeclaration -> handleAliasDeclaration(node)
is CPPASTNamespaceAlias -> handleNamespaceAlias(node)
is CPPASTLinkageSpecification -> handleLinkageSpecification(node)
else -> {
return handleNotSupported(node, node.javaClass.name)
}
Expand Down Expand Up @@ -668,6 +669,23 @@ class DeclarationHandler(lang: CXXLanguageFrontend) :
return enum
}

/**
* Translates a C++ (linkage
* specification)[https://en.cppreference.com/w/cpp/language/language_linkage]. Actually, we do
* not care about the linkage specification per-se, but we need to parse the declaration(s) it
* contains.
*/
private fun handleLinkageSpecification(ctx: CPPASTLinkageSpecification): Declaration {
var sequence = DeclarationSequence()

// Just forward its declaration(s) to our handler
for (decl in ctx.declarations) {
handle(decl)?.let { sequence += it }
}

return simplifySequence(sequence)
}

/**
* @param sequence
* @return First Element of DeclarationSequence if the Sequence consist of only one element,
Expand Down Expand Up @@ -707,9 +725,6 @@ class DeclarationHandler(lang: CXXLanguageFrontend) :
val problematicIncludes = HashMap<String?, HashSet<ProblemDeclaration>>()

for (declaration in translationUnit.declarations) {
if (declaration is CPPASTLinkageSpecification) {
continue // do not care about these for now
}
val decl = handle(declaration) ?: continue
(decl as? ProblemDeclaration)?.location?.let {
val problems =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1721,4 +1721,17 @@ internal class CXXLanguageFrontendTest : BaseTest() {
assertTrue(printf.prevEOG.isNotEmpty())
assertTrue(printf.invokes.isNotEmpty())
}

@Test
fun testExternC() {
val file = File("src/test/resources/cxx/extern_c.cpp")
val result =
analyze(listOf(file), file.parentFile.toPath(), true) {
it.registerLanguage<CPPLanguage>()
}
assertNotNull(result)

val test = result.functions["test"]
assertNotNull(test)
}
}
3 changes: 3 additions & 0 deletions cpg-language-cxx/src/test/resources/cxx/extern_c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extern "C" {
void test();
}

0 comments on commit 200c6d6

Please sign in to comment.