Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Housekeeping things: fix clippy lints, run formatter, upgrade dependencies #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ repository = "https://github.com/KyleMayes/clang-rs"

autotests = false

edition = "2018"

[features]

clang_3_5 = ["clang-sys/clang_3_5"]
Expand All @@ -35,8 +37,8 @@ static = ["clang-sys/static"]

[dependencies]

clang-sys = "1"
libc = "0.2.39"
clang-sys = "1.3.0"
libc = "0.2.108"

[[test]]

Expand Down
17 changes: 12 additions & 5 deletions examples/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ fn main() {
let tu = index.parser("examples/structs.c").parse().unwrap();

// Get the structs in this translation unit
let structs = tu.get_entity().get_children().into_iter().filter(|e| {
e.get_kind() == EntityKind::StructDecl
}).collect::<Vec<_>>();
let structs = tu
.get_entity()
.get_children()
.into_iter()
.filter(|e| e.get_kind() == EntityKind::StructDecl)
.collect::<Vec<_>>();

// Print information about the structs
for struct_ in structs {
let type_ = struct_.get_type().unwrap();
let type_ = struct_.get_type().unwrap();
let size = type_.get_sizeof().unwrap();
println!("struct: {:?} (size: {} bytes)", struct_.get_name().unwrap(), size);
println!(
"struct: {:?} (size: {} bytes)",
struct_.get_name().unwrap(),
size
);

for field in struct_.get_children() {
let name = field.get_name().unwrap();
Expand Down
96 changes: 63 additions & 33 deletions src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@

//! Code completion.

use std::cmp::{self, Ordering};
use std::fmt;
use std::marker::PhantomData;
use std::mem;
use std::path::PathBuf;
use std::ptr;
use std::slice;
use std::cmp::{self, Ordering};
use std::marker::{PhantomData};
use std::path::{PathBuf};

use clang_sys::*;

use libc::{c_uint};
use libc::c_uint;

use utility;
use super::diagnostic::Diagnostic;
use super::{Availability, EntityKind, TranslationUnit, Unsaved, Usr};
use super::diagnostic::{Diagnostic};
use crate::utility;

//================================================
// Enums
Expand Down Expand Up @@ -102,14 +102,14 @@ impl<'r> CompletionChunk<'r> {
CompletionChunk::RightParenthesis => Some(")".into()),
CompletionChunk::LeftSquareBracket => Some("[".into()),
CompletionChunk::RightSquareBracket => Some("]".into()),
CompletionChunk::CurrentParameter(ref text) |
CompletionChunk::Informative(ref text) |
CompletionChunk::Placeholder(ref text) |
CompletionChunk::ResultType(ref text) |
CompletionChunk::TypedText(ref text) |
CompletionChunk::Text(ref text) |
CompletionChunk::HorizontalSpace(ref text) |
CompletionChunk::VerticalSpace(ref text) => Some(text.clone()),
CompletionChunk::CurrentParameter(ref text)
| CompletionChunk::Informative(ref text)
| CompletionChunk::Placeholder(ref text)
| CompletionChunk::ResultType(ref text)
| CompletionChunk::TypedText(ref text)
| CompletionChunk::Text(ref text)
| CompletionChunk::HorizontalSpace(ref text)
| CompletionChunk::VerticalSpace(ref text) => Some(text.clone()),
CompletionChunk::Optional(_) => None,
}
}
Expand Down Expand Up @@ -153,11 +153,21 @@ impl<'tu> Completer<'tu> {

#[doc(hidden)]
pub fn new<F: Into<PathBuf>>(
tu: &'tu TranslationUnit<'tu>, file: F, line: u32, column: u32
tu: &'tu TranslationUnit<'tu>,
file: F,
line: u32,
column: u32,
) -> Completer<'tu> {
let file = file.into();
let flags = unsafe { clang_defaultCodeCompleteOptions() };
Completer { tu, file, line, column, unsaved: vec![], flags }
Completer {
tu,
file,
line,
column,
unsaved: vec![],
flags,
}
}

//- Mutators ---------------------------------
Expand Down Expand Up @@ -258,7 +268,10 @@ impl<'r> CompletionResult<'r> {

fn from_raw(raw: CXCompletionResult) -> CompletionResult<'r> {
let kind = unsafe { mem::transmute(raw.CursorKind) };
CompletionResult { kind, string: CompletionString::from_ptr(raw.CompletionString) }
CompletionResult {
kind,
string: CompletionString::from_ptr(raw.CompletionString),
}
}
}

Expand All @@ -284,9 +297,9 @@ pub struct CompletionResults {
impl CompletionResults {
//- Constructors -----------------------------

fn from_ptr(ptr: *mut CXCodeCompleteResults) -> CompletionResults {
fn from_ptr(ptr: *mut CXCodeCompleteResults) -> Self {
assert!(!ptr.is_null());
CompletionResults { ptr }
Self { ptr }
}

//- Accessors --------------------------------
Expand All @@ -297,7 +310,9 @@ impl CompletionResults {
iter!(
clang_codeCompleteGetNumDiagnostics(self.ptr),
clang_codeCompleteGetDiagnostic(self.ptr),
).map(|d| Diagnostic::from_ptr(d, tu)).collect()
)
.map(|d| Diagnostic::from_ptr(d, tu))
.collect()
}

/// Returns the code completion context for this set of code completion results, if any.
Expand Down Expand Up @@ -338,20 +353,26 @@ impl CompletionResults {
pub fn get_results(&self) -> Vec<CompletionResult> {
unsafe {
let raws = slice::from_raw_parts((*self.ptr).Results, (*self.ptr).NumResults as usize);
raws.iter().cloned().map(CompletionResult::from_raw).collect()
raws.iter()
.copied()
.map(CompletionResult::from_raw)
.collect()
}
}
}

impl Drop for CompletionResults {
fn drop(&mut self) {
unsafe { clang_disposeCodeCompleteResults(self.ptr); }
unsafe {
clang_disposeCodeCompleteResults(self.ptr);
}
}
}

impl fmt::Debug for CompletionResults {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.debug_struct("CompletionResults")
formatter
.debug_struct("CompletionResults")
.field("results", &self.get_results())
.finish()
}
Expand All @@ -363,7 +384,7 @@ impl fmt::Debug for CompletionResults {
#[derive(Copy, Clone)]
pub struct CompletionString<'r> {
ptr: CXCompletionString,
_marker: PhantomData<&'r CompletionResults>
_marker: PhantomData<&'r CompletionResults>,
}

impl<'r> CompletionString<'r> {
Expand All @@ -372,7 +393,10 @@ impl<'r> CompletionString<'r> {
#[doc(hidden)]
pub fn from_ptr(ptr: CXCompletionString) -> CompletionString<'r> {
assert!(!ptr.is_null());
CompletionString { ptr, _marker: PhantomData }
CompletionString {
ptr,
_marker: PhantomData,
}
}

//- Accessors --------------------------------
Expand All @@ -388,7 +412,9 @@ impl<'r> CompletionString<'r> {
iter!(
clang_getCompletionNumAnnotations(self.ptr),
clang_getCompletionAnnotation(self.ptr),
).map(utility::to_string).collect()
)
.map(utility::to_string)
.collect()
}

/// Returns the availability of this completion string.
Expand Down Expand Up @@ -423,12 +449,14 @@ impl<'r> CompletionString<'r> {
iter!(
clang_getNumCompletionChunks(self.ptr),
clang_getCompletionChunkKind(self.ptr),
).enumerate().map(|(i, k)| {
)
.enumerate()
.map(|(i, k)| {
macro_rules! text {
($variant:ident) => ({
($variant:ident) => {{
let text = unsafe { clang_getCompletionChunkText(self.ptr, i as c_uint) };
CompletionChunk::$variant(utility::to_string(text))
});
}};
}

match k {
Expand Down Expand Up @@ -456,16 +484,18 @@ impl<'r> CompletionString<'r> {
let i = i as c_uint;
let ptr = unsafe { clang_getCompletionChunkCompletionString(self.ptr, i) };
CompletionChunk::Optional(CompletionString::from_ptr(ptr))
},
}
_ => panic!("unexpected completion chunk kind: {:?}", k),
}
}).collect()
})
.collect()
}
}

impl<'r> fmt::Debug for CompletionString<'r> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.debug_struct("CompletionString")
formatter
.debug_struct("CompletionString")
.field("chunks", &self.get_chunks())
.finish()
}
Expand All @@ -477,7 +507,7 @@ impl<'r> cmp::PartialEq for CompletionString<'r> {
}
}

impl<'r> cmp::Eq for CompletionString<'r> { }
impl<'r> cmp::Eq for CompletionString<'r> {}

impl<'r> cmp::PartialOrd for CompletionString<'r> {
fn partial_cmp(&self, other: &CompletionString<'r>) -> Option<Ordering> {
Expand Down
45 changes: 26 additions & 19 deletions src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

//! Issues with source files.

use std::cmp::{self, Ordering};
use std::fmt;
use std::mem;
use std::cmp::{self, Ordering};

use clang_sys::*;

use utility;
use super::{TranslationUnit};
use super::source::{SourceLocation, SourceRange};
use super::TranslationUnit;
use crate::utility;

//================================================
// Enums
Expand Down Expand Up @@ -104,25 +104,29 @@ impl<'tu> Diagnostic<'tu> {
iter!(
clang_getDiagnosticNumRanges(self.ptr),
clang_getDiagnosticRange(self.ptr),
).map(|r| SourceRange::from_raw(r, self.tu)).collect()
)
.map(|r| SourceRange::from_raw(r, self.tu))
.collect()
}

/// Returns the fix-its for this diagnostic.
pub fn get_fix_its(&self) -> Vec<FixIt<'tu>> {
unsafe {
(0..clang_getDiagnosticNumFixIts(self.ptr)).map(|i| {
let mut range = mem::MaybeUninit::uninit();
let fixit = clang_getDiagnosticFixIt(self.ptr, i, range.as_mut_ptr());
let string = utility::to_string(fixit);
let range = SourceRange::from_raw(range.assume_init(), self.tu);
if string.is_empty() {
FixIt::Deletion(range)
} else if range.get_start() == range.get_end() {
FixIt::Insertion(range.get_start(), string)
} else {
FixIt::Replacement(range, string)
}
}).collect()
(0..clang_getDiagnosticNumFixIts(self.ptr))
.map(|i| {
let mut range = mem::MaybeUninit::uninit();
let fixit = clang_getDiagnosticFixIt(self.ptr, i, range.as_mut_ptr());
let string = utility::to_string(fixit);
let range = SourceRange::from_raw(range.assume_init(), self.tu);
if string.is_empty() {
FixIt::Deletion(range)
} else if range.get_start() == range.get_end() {
FixIt::Insertion(range.get_start(), string)
} else {
FixIt::Replacement(range, string)
}
})
.collect()
}
}

Expand All @@ -132,7 +136,9 @@ impl<'tu> Diagnostic<'tu> {
iter!(
clang_getNumDiagnosticsInSet(ptr),
clang_getDiagnosticInSet(ptr),
).map(|d| Diagnostic::from_ptr(d, self.tu)).collect()
)
.map(|d| Diagnostic::from_ptr(d, self.tu))
.collect()
}

/// Returns a diagnostic formatter that builds a formatted string from this diagnostic.
Expand All @@ -157,7 +163,8 @@ impl<'tu> cmp::PartialOrd for Diagnostic<'tu> {

impl<'tu> fmt::Debug for Diagnostic<'tu> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.debug_struct("Diagnostic")
formatter
.debug_struct("Diagnostic")
.field("location", &self.get_location())
.field("severity", &self.get_severity())
.field("text", &self.get_text())
Expand Down
Loading