From 9cd5df4f8b7c21e9d728f845c6b80f29d8610c1b Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Thu, 18 Jul 2024 20:58:49 +0200 Subject: [PATCH] Checking operator names --- .../cpg/frontends/cxx/DeclaratorHandler.kt | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/DeclaratorHandler.kt b/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/DeclaratorHandler.kt index 55f271a5e0..6d3eb646af 100644 --- a/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/DeclaratorHandler.kt +++ b/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/DeclaratorHandler.kt @@ -26,6 +26,7 @@ package de.fraunhofer.aisec.cpg.frontends.cxx import de.fraunhofer.aisec.cpg.ResolveInFrontend +import de.fraunhofer.aisec.cpg.frontends.HasOperatorOverloading import de.fraunhofer.aisec.cpg.graph.* import de.fraunhofer.aisec.cpg.graph.declarations.* import de.fraunhofer.aisec.cpg.graph.scopes.NameScope @@ -167,8 +168,8 @@ class DeclaratorHandler(lang: CXXLanguageFrontend) : val func = when { - // Check if it's an operator. In this case, the name begins with operator - name.localName.startsWith("operator") -> { + // Check if it's an operator + name.isKnownOperatorName -> { // retrieve the operator code var operatorCode = name.localName.drop("operator".length) newOperatorDeclaration(name, operatorCode, rawNode = ctx) @@ -224,9 +225,10 @@ class DeclaratorHandler(lang: CXXLanguageFrontend) : /* * As always, there are some special cases to consider and one of those are C++ operators. * They are regarded as functions and eclipse CDT for some reason introduces a whitespace - * in the function name, which will complicate things later on + * in the function name, which will complicate things later on. But we only want to replace + * the whitespace for "standard" operators. */ - if (name.startsWith("operator")) { + if (nameDecl.name is CPPASTOperatorName && name.replace(" ", "").isKnownOperatorName) { name = name.replace(" ", "") } val declaration: FunctionDeclaration @@ -507,6 +509,17 @@ class DeclaratorHandler(lang: CXXLanguageFrontend) : frontend.declarationHandler.handle(member) } } + + /** Checks whether the [Name] for a function is a known operator name. */ + val Name.isKnownOperatorName: Boolean + get() { + var language = language + if (language !is HasOperatorOverloading) { + return false + } + + return language.operatorNames.containsValue(this.localName) + } } /**