Skip to content

Commit

Permalink
Implement m_default_constructor, m_copy_destructor, `m_move_destr…
Browse files Browse the repository at this point in the history
…uctor` matchers functions
  • Loading branch information
AmrDeveloper committed Jan 11, 2025
1 parent b649e8a commit 86a6e5c
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ gitql-cli = "0.35.0"
gitql-ast = "0.31.0"
gitql-parser = "0.34.0"
gitql-engine = "0.35.0"
clang-sys = "1.8.1"
clang-sys = { version = "1.8.1", features = ["clang_16_0"] }
dyn-clone = "1.0.17"
16 changes: 16 additions & 0 deletions docs/MatcherFunctions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
### AST matchers functions

| Function | Parameters | Return | Description |
| :----------------------: | :--------: | :-------------: | :---------------------------------------------------------------: |
| m_virtual | () | FunctionMatcher | Create Matcher to check if the function is virtual |
| m_pure_virtual | () | FunctionMatcher | Create Matcher to check if the function is pure virtual |
| m_method | () | FunctionMatcher | Create Matcher to check if the function is a method |
| m_static | () | FunctionMatcher | Create Matcher to check if the function is static |
| m_const | () | FunctionMatcher | Create Matcher to check if the function is const |
| m_deleted | () | FunctionMatcher | Create Matcher to check if the function is deleted |
| m_constructor | () | FunctionMatcher | Create Matcher to check if the function is constructor |
| m_default_constructor | () | FunctionMatcher | Create Matcher to check if the function is default constructor |
| m_copy_constructor | () | FunctionMatcher | Create Matcher to check if the function is copy constructor |
| m_move_constructor | () | FunctionMatcher | Create Matcher to check if the function is move constructor |
| m_converting_constructor | () | FunctionMatcher | Create Matcher to check if the function is converting constructor |
| m_destructor | () | FunctionMatcher | Create Matcher to check if the function is destructor |
76 changes: 76 additions & 0 deletions src/clang_ql/functions/matchers/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ use gitql_core::values::base::Value;
use gitql_core::values::boolean::BoolValue;

use crate::clang_ql::matchers::IsConstMethodMatcher;
use crate::clang_ql::matchers::IsConstructorMatcher;
use crate::clang_ql::matchers::IsConvertingConstructorMatcher;
use crate::clang_ql::matchers::IsCopyConstructorMatcher;
use crate::clang_ql::matchers::IsDefaultConstructorMatcher;
use crate::clang_ql::matchers::IsDeletedMethodMatcher;
use crate::clang_ql::matchers::IsDestructorMatcher;
use crate::clang_ql::matchers::IsMethodMatcher;
use crate::clang_ql::matchers::IsMoveConstructorMatcher;
use crate::clang_ql::matchers::IsPureVirtualMatcher;
use crate::clang_ql::matchers::IsStaticMethodMatcher;
use crate::clang_ql::matchers::IsVirtualMatcher;
Expand All @@ -29,6 +35,16 @@ pub(crate) fn register_function_matchers_functions(
map.insert("m_const", match_const_function);
map.insert("m_deleted", match_deleted_function);
map.insert("m_method", match_method_function);

map.insert("m_constructor", match_constructor_function);
map.insert("m_default_constructor", match_default_constructor_function);
map.insert("m_copy_constructor", match_copy_constructor_function);
map.insert("m_move_constructor", match_move_constructor_function);
map.insert(
"m_converting_constructor",
match_converting_constructor_function,
);
map.insert("m_destructor", match_destructor_function);
}

#[inline(always)]
Expand Down Expand Up @@ -64,6 +80,36 @@ pub(crate) fn register_function_matchers_signatures(map: &mut HashMap<&'static s
"m_method",
Signature::with_return(Box::new(FunctionMatcherType)),
);

map.insert(
"m_constructor",
Signature::with_return(Box::new(FunctionMatcherType)),
);

map.insert(
"m_default_constructor",
Signature::with_return(Box::new(FunctionMatcherType)),
);

map.insert(
"m_copy_constructor",
Signature::with_return(Box::new(FunctionMatcherType)),
);

map.insert(
"m_move_constructor",
Signature::with_return(Box::new(FunctionMatcherType)),
);

map.insert(
"m_converting_constructor",
Signature::with_return(Box::new(FunctionMatcherType)),
);

map.insert(
"m_destructor",
Signature::with_return(Box::new(FunctionMatcherType)),
);
}

fn match_function(values: &[Box<dyn Value>]) -> Box<dyn Value> {
Expand Down Expand Up @@ -105,3 +151,33 @@ fn match_method_function(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
let matcher = Box::new(IsMethodMatcher);
Box::new(FunctionMatcherValue::new(matcher))
}

fn match_constructor_function(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
let matcher = Box::new(IsConstructorMatcher);
Box::new(FunctionMatcherValue::new(matcher))
}

fn match_copy_constructor_function(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
let matcher = Box::new(IsCopyConstructorMatcher);
Box::new(FunctionMatcherValue::new(matcher))
}

fn match_move_constructor_function(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
let matcher = Box::new(IsMoveConstructorMatcher);
Box::new(FunctionMatcherValue::new(matcher))
}

fn match_default_constructor_function(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
let matcher = Box::new(IsDefaultConstructorMatcher);
Box::new(FunctionMatcherValue::new(matcher))
}

fn match_converting_constructor_function(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
let matcher = Box::new(IsConvertingConstructorMatcher);
Box::new(FunctionMatcherValue::new(matcher))
}

fn match_destructor_function(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
let matcher = Box::new(IsDestructorMatcher);
Box::new(FunctionMatcherValue::new(matcher))
}
66 changes: 66 additions & 0 deletions src/clang_ql/matchers/function.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use clang_sys::clang_CXXConstructor_isConvertingConstructor;
use clang_sys::clang_CXXConstructor_isCopyConstructor;
use clang_sys::clang_CXXConstructor_isDefaultConstructor;
use clang_sys::clang_CXXConstructor_isMoveConstructor;
use clang_sys::clang_CXXMethod_isConst;
use clang_sys::clang_CXXMethod_isPureVirtual;
use clang_sys::clang_CXXMethod_isStatic;
use clang_sys::clang_CXXMethod_isVirtual;
use clang_sys::clang_getCursorKind;
use clang_sys::CXCursor_CXXMethod;
use clang_sys::CXCursor_Constructor;
use clang_sys::CXCursor_Destructor;

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

Expand Down Expand Up @@ -65,3 +71,63 @@ impl FunctionMatcher for IsMethodMatcher {
}
}
}

#[derive(Clone)]
pub struct IsConstructorMatcher;

impl FunctionMatcher for IsConstructorMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe {
let cursor_kind = clang_getCursorKind(function.cursor);
cursor_kind == CXCursor_Constructor
}
}
}

#[derive(Clone)]
pub struct IsDefaultConstructorMatcher;

impl FunctionMatcher for IsDefaultConstructorMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_CXXConstructor_isDefaultConstructor(function.cursor) != 0 }
}
}

#[derive(Clone)]
pub struct IsCopyConstructorMatcher;

impl FunctionMatcher for IsCopyConstructorMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_CXXConstructor_isCopyConstructor(function.cursor) != 0 }
}
}

#[derive(Clone)]
pub struct IsMoveConstructorMatcher;

impl FunctionMatcher for IsMoveConstructorMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_CXXConstructor_isMoveConstructor(function.cursor) != 0 }
}
}

#[derive(Clone)]
pub struct IsConvertingConstructorMatcher;

impl FunctionMatcher for IsConvertingConstructorMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe { clang_CXXConstructor_isConvertingConstructor(function.cursor) != 0 }
}
}

#[derive(Clone)]
pub struct IsDestructorMatcher;

impl FunctionMatcher for IsDestructorMatcher {
fn is_match(&self, function: &FunctionNode) -> bool {
unsafe {
let cursor_kind = clang_getCursorKind(function.cursor);
cursor_kind == CXCursor_Destructor
}
}
}
6 changes: 6 additions & 0 deletions src/clang_ql/matchers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ use super::values::FunctionNode;

mod function;
pub use function::IsConstMethodMatcher;
pub use function::IsConstructorMatcher;
pub use function::IsConvertingConstructorMatcher;
pub use function::IsCopyConstructorMatcher;
pub use function::IsDefaultConstructorMatcher;
pub use function::IsDeletedMethodMatcher;
pub use function::IsDestructorMatcher;
pub use function::IsMethodMatcher;
pub use function::IsMoveConstructorMatcher;
pub use function::IsPureVirtualMatcher;
pub use function::IsStaticMethodMatcher;
pub use function::IsVirtualMatcher;
Expand Down
2 changes: 2 additions & 0 deletions src/clang_ql/visitors/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ extern "C" fn visit_children(
if cursor_kind == CXCursor_FunctionDecl
|| cursor_kind == CXCursor_CXXMethod
|| cursor_kind == CXCursor_FunctionTemplate
|| cursor_kind == CXCursor_Constructor
|| cursor_kind == CXCursor_Destructor
{
let functions = &mut *(data as *mut Vec<FunctionNode>);

Expand Down

0 comments on commit 86a6e5c

Please sign in to comment.