-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds support for an operator declaration. They are not resolved yet.
- Loading branch information
Showing
9 changed files
with
202 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/declarations/OperatorDeclaration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2024, Fraunhofer AISEC. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* $$$$$$\ $$$$$$$\ $$$$$$\ | ||
* $$ __$$\ $$ __$$\ $$ __$$\ | ||
* $$ / \__|$$ | $$ |$$ / \__| | ||
* $$ | $$$$$$$ |$$ |$$$$\ | ||
* $$ | $$ ____/ $$ |\_$$ | | ||
* $$ | $$\ $$ | $$ | $$ | | ||
* \$$$$$ |$$ | \$$$$$ | | ||
* \______/ \__| \______/ | ||
* | ||
*/ | ||
package de.fraunhofer.aisec.cpg.graph.declarations | ||
|
||
import de.fraunhofer.aisec.cpg.frontends.HasOperatorOverloading | ||
import de.fraunhofer.aisec.cpg.graph.HasOperatorCode | ||
import de.fraunhofer.aisec.cpg.graph.types.ObjectType | ||
|
||
/** | ||
* Some languages allow to either overload operators or to add custom operators to classes (see | ||
* [HasOperatorOverloading]). In both cases, this special function class denotes that this handles | ||
* this particular operator call. | ||
* | ||
* We need to derive from [MethodDeclaration] because all operators have a base class on which they | ||
* operate on. Therefore, we need to associate them with an [ObjectType] and/or [RecordDeclaration]. | ||
* There are some very special cases for C++, where we can have a global operator for a particular | ||
* class. In this case we just pretend like it is a method operator. | ||
*/ | ||
class OperatorDeclaration : MethodDeclaration(), HasOperatorCode { | ||
/** The operator code which this operator declares. */ | ||
override var operatorCode: String? = null | ||
|
||
val isPrefix: Boolean = false | ||
val isPostfix: Boolean = false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
cpg-language-cxx/src/test/resources/cxx/operators/arithmetic.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Integer { | ||
public: | ||
Integer(int i) : i(i) {} | ||
void operator++(int) { | ||
i++; | ||
} | ||
|
||
Integer operator+(int j) { | ||
return Integer(this->i+j); | ||
} | ||
|
||
Integer operator+(Integer &j) { | ||
return Integer(this->i+j.i); | ||
} | ||
|
||
void test() {}; | ||
|
||
int i; | ||
}; | ||
|
||
int main() { | ||
Integer i(5); | ||
i++; | ||
|
||
Integer j = i + 2; | ||
auto k = i + j; | ||
} |