Skip to content

Commit

Permalink
Checking operator names
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Jul 21, 2024
1 parent c9a9bb9 commit 9cd5df4
Showing 1 changed file with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

/**
Expand Down

0 comments on commit 9cd5df4

Please sign in to comment.