Skip to content

Commit

Permalink
[c2cpg] Fixed fullNames for function problem bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
max-leuthaeuser committed Feb 3, 2025
1 parent b02caa7 commit 06170c4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,11 @@ trait FullNameProvider { this: AstCreator =>
safeGetBinding(declarator.getName) match {
case Some(value: ICPPMethod) =>
cleanType(value.getType.getReturnType.toString)
case _ =>
case _ if declarator.getParent.isInstanceOf[IASTSimpleDeclaration] =>
cleanType(typeForDeclSpecifier(declarator.getParent.asInstanceOf[IASTSimpleDeclaration].getDeclSpecifier))
case _ if declarator.getParent.isInstanceOf[IASTFunctionDefinition] =>
cleanType(typeForDeclSpecifier(declarator.getParent.asInstanceOf[IASTFunctionDefinition].getDeclSpecifier))
case _ => Defines.Any
}
}

Expand Down Expand Up @@ -330,10 +333,15 @@ trait FullNameProvider { this: AstCreator =>
case Some(_: IProblemBinding) =>
val fullNameNoSig = replaceOperator(ASTStringUtil.getQualifiedName(declarator.getName))
val fixedFullName = fixQualifiedName(fullNameNoSig)
val returnTpe = declarator.getParent match {
case definition: ICPPASTFunctionDefinition if !isCppConstructor(definition) => returnType(definition)
case _ => returnType(declarator)
}
val signature_ = signature(returnTpe, declarator)
if (fixedFullName.isEmpty) {
Option(s"${X2CpgDefines.UnresolvedNamespace}:${X2CpgDefines.UnresolvedSignature}")
Option(s"${X2CpgDefines.UnresolvedNamespace}:$signature_")
} else {
Option(s"$fixedFullName:${X2CpgDefines.UnresolvedSignature}")
Option(s"$fixedFullName:$signature_")
}
case _ => None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ class AstCreationPassTests extends AstC2CpgSuite {
}
}

"be correct for full names and signatures for method problem bindings" in {
val cpg = code(
"""
|char tpe<wchar_t>::foo(char_type a, char b) const {
| return static_cast<char>(a);
|}
|const wchar_t* tpe<wchar_t>::foo(const char_type* a, const char_type* b, char c, char* d) const {
| return a;
|}
|""".stripMargin,
"foo.cpp"
)
// tpe<wchar_t> can't be resolved for both methods resulting in problem bindings.
// We can however manually reconstruct the signature from the params and return type without
// relying on the resolved function binding signature.
val fullNames = cpg.method.nameNot("<global>").map(m => (m.fullName, m.signature)).l.sortBy(_._1)
fullNames shouldBe List(
("tpe<wchar_t>.foo:char(char_type,char)", "char(char_type,char)"),
(
"tpe<wchar_t>.foo:const wchar_t*(char_type*,char_type*,char,char*)",
"const wchar_t*(char_type*,char_type*,char,char*)"
)
)
}

"be correct for packed args" in {
val cpg = code(
"""
Expand Down

0 comments on commit 06170c4

Please sign in to comment.