Skip to content

Commit

Permalink
Implement overriding Combine not operator
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Jan 12, 2025
1 parent 86c39f2 commit a0d4abd
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 22 deletions.
29 changes: 29 additions & 0 deletions src/clang_ql/matchers/combine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use super::Matcher;

#[derive(Clone)]
enum CombineUnaryMatcherKind {
Not,
}

#[derive(Clone)]
pub struct UnaryCombineMatcher<T> {
matcher: Box<dyn Matcher<T>>,
kind: CombineUnaryMatcherKind,
}

impl<T: Clone> UnaryCombineMatcher<T> {
pub fn not(matcher: Box<dyn Matcher<T>>) -> Self {
UnaryCombineMatcher {
matcher,
kind: CombineUnaryMatcherKind::Not,
}
}
}

impl<T: Clone> Matcher<T> for UnaryCombineMatcher<T> {
fn is_match(&self, node: &T) -> bool {
match &self.kind {
CombineUnaryMatcherKind::Not => !self.matcher.is_match(node),
}
}
}
28 changes: 14 additions & 14 deletions src/clang_ql/matchers/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ use clang_sys::CX_CXXPublic;

use crate::clang_ql::values::FunctionNode;

use super::FunctionMatcher;
use super::Matcher;

#[derive(Clone)]
pub struct IsVirtualMatcher;

impl FunctionMatcher for IsVirtualMatcher {
impl Matcher<FunctionNode> for IsVirtualMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_CXXMethod_isVirtual(function.cursor) != 0 }
}
Expand All @@ -32,7 +32,7 @@ impl FunctionMatcher for IsVirtualMatcher {
#[derive(Clone)]
pub struct IsPureVirtualMatcher;

impl FunctionMatcher for IsPureVirtualMatcher {
impl Matcher<FunctionNode> for IsPureVirtualMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_CXXMethod_isPureVirtual(function.cursor) != 0 }
}
Expand All @@ -41,7 +41,7 @@ impl FunctionMatcher for IsPureVirtualMatcher {
#[derive(Clone)]
pub struct IsStaticMethodMatcher;

impl FunctionMatcher for IsStaticMethodMatcher {
impl Matcher<FunctionNode> for IsStaticMethodMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_CXXMethod_isStatic(function.cursor) != 0 }
}
Expand All @@ -50,7 +50,7 @@ impl FunctionMatcher for IsStaticMethodMatcher {
#[derive(Clone)]
pub struct IsConstMethodMatcher;

impl FunctionMatcher for IsConstMethodMatcher {
impl Matcher<FunctionNode> for IsConstMethodMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_CXXMethod_isConst(function.cursor) != 0 }
}
Expand All @@ -59,7 +59,7 @@ impl FunctionMatcher for IsConstMethodMatcher {
#[derive(Clone)]
pub struct IsDeletedMethodMatcher;

impl FunctionMatcher for IsDeletedMethodMatcher {
impl Matcher<FunctionNode> for IsDeletedMethodMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_CXXMethod_isConst(function.cursor) != 0 }
}
Expand All @@ -68,7 +68,7 @@ impl FunctionMatcher for IsDeletedMethodMatcher {
#[derive(Clone)]
pub struct IsMethodMatcher;

impl FunctionMatcher for IsMethodMatcher {
impl Matcher<FunctionNode> for IsMethodMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_getCursorKind(function.cursor) == CXCursor_CXXMethod }
}
Expand All @@ -77,7 +77,7 @@ impl FunctionMatcher for IsMethodMatcher {
#[derive(Clone)]
pub struct IsConstructorMatcher;

impl FunctionMatcher for IsConstructorMatcher {
impl Matcher<FunctionNode> for IsConstructorMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_getCursorKind(function.cursor) == CXCursor_Constructor }
}
Expand All @@ -86,7 +86,7 @@ impl FunctionMatcher for IsConstructorMatcher {
#[derive(Clone)]
pub struct IsDefaultConstructorMatcher;

impl FunctionMatcher for IsDefaultConstructorMatcher {
impl Matcher<FunctionNode> for IsDefaultConstructorMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_CXXConstructor_isDefaultConstructor(function.cursor) != 0 }
}
Expand All @@ -95,7 +95,7 @@ impl FunctionMatcher for IsDefaultConstructorMatcher {
#[derive(Clone)]
pub struct IsCopyConstructorMatcher;

impl FunctionMatcher for IsCopyConstructorMatcher {
impl Matcher<FunctionNode> for IsCopyConstructorMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_CXXConstructor_isCopyConstructor(function.cursor) != 0 }
}
Expand All @@ -104,7 +104,7 @@ impl FunctionMatcher for IsCopyConstructorMatcher {
#[derive(Clone)]
pub struct IsMoveConstructorMatcher;

impl FunctionMatcher for IsMoveConstructorMatcher {
impl Matcher<FunctionNode> for IsMoveConstructorMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_CXXConstructor_isMoveConstructor(function.cursor) != 0 }
}
Expand All @@ -113,7 +113,7 @@ impl FunctionMatcher for IsMoveConstructorMatcher {
#[derive(Clone)]
pub struct IsConvertingConstructorMatcher;

impl FunctionMatcher for IsConvertingConstructorMatcher {
impl Matcher<FunctionNode> for IsConvertingConstructorMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_CXXConstructor_isConvertingConstructor(function.cursor) != 0 }
}
Expand All @@ -122,7 +122,7 @@ impl FunctionMatcher for IsConvertingConstructorMatcher {
#[derive(Clone)]
pub struct IsDestructorMatcher;

impl FunctionMatcher for IsDestructorMatcher {
impl Matcher<FunctionNode> for IsDestructorMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_getCursorKind(function.cursor) == CXCursor_Destructor }
}
Expand Down Expand Up @@ -153,7 +153,7 @@ impl AccessSpecifierMatcher {
}
}

impl FunctionMatcher for AccessSpecifierMatcher {
impl Matcher<FunctionNode> for AccessSpecifierMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_getCXXAccessSpecifier(function.cursor) == self.access }
}
Expand Down
11 changes: 6 additions & 5 deletions src/clang_ql/matchers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use dyn_clone::DynClone;

use super::values::FunctionNode;

mod function;
pub use function::AccessSpecifierMatcher;
pub use function::IsConstMethodMatcher;
Expand All @@ -17,8 +15,11 @@ pub use function::IsPureVirtualMatcher;
pub use function::IsStaticMethodMatcher;
pub use function::IsVirtualMatcher;

dyn_clone::clone_trait_object!(FunctionMatcher);
mod combine;
pub use combine::UnaryCombineMatcher;

dyn_clone::clone_trait_object!(<T> Matcher<T>);

pub trait FunctionMatcher: DynClone {
fn is_match(&self, node: &FunctionNode) -> bool;
pub trait Matcher<T: Clone>: DynClone {
fn is_match(&self, node: &T) -> bool;
}
8 changes: 8 additions & 0 deletions src/clang_ql/types/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ impl DataType for FunctionMatcherType {
fn as_any(&self) -> &dyn Any {
self
}

fn can_perform_bang_op(&self) -> bool {
true
}

fn bang_op_result_type(&self) -> Box<dyn DataType> {
Box::new(FunctionMatcherType)
}
}
3 changes: 3 additions & 0 deletions src/clang_ql/values/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use super::FileLocation;
pub struct FunctionNode {
pub name: String,
pub cursor: CXCursor,

#[allow(dead_code)]
pub parent: CXCursor,

pub signature: String,
pub return_type: String,
pub location: FileLocation,
Expand Down
13 changes: 10 additions & 3 deletions src/clang_ql/values/matcher.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use gitql_core::values::base::Value;

use crate::clang_ql::matchers::FunctionMatcher;
use crate::clang_ql::matchers::{Matcher, UnaryCombineMatcher};
use crate::clang_ql::types::FunctionMatcherType;

use super::FunctionNode;

#[derive(Clone)]
pub struct FunctionMatcherValue {
pub matcher: Box<dyn FunctionMatcher>,
pub matcher: Box<dyn Matcher<FunctionNode>>,
}

impl FunctionMatcherValue {
pub fn new(matcher: Box<dyn FunctionMatcher>) -> Self {
pub fn new(matcher: Box<dyn Matcher<FunctionNode>>) -> Self {
FunctionMatcherValue { matcher }
}
}
Expand All @@ -34,4 +36,9 @@ impl Value for FunctionMatcherValue {
fn as_any(&self) -> &dyn std::any::Any {
self
}

fn bang_op(&self) -> Result<Box<dyn Value>, String> {
let combine_matcher = Box::new(UnaryCombineMatcher::not(self.matcher.clone()));
Ok(Box::new(FunctionMatcherValue::new(combine_matcher)))
}
}

0 comments on commit a0d4abd

Please sign in to comment.