diff options
author | Emilio Cobos Álvarez <emilio@crisal.io> | 2019-10-11 15:47:35 +0200 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2019-10-14 14:15:46 +0200 |
commit | 71e0d9d5a74d64f29c31fa14c0be6096eadf30f7 (patch) | |
tree | f5e69f8d48d910d527178425549c28fe0d6d8c4c | |
parent | 5d1c517d1357a9bb61c4cc2b5c28c1c982cf4116 (diff) |
function: Fix #[must_use] support in libclang 9+.
-rw-r--r-- | src/clang.rs | 33 | ||||
-rw-r--r-- | src/ir/function.rs | 2 |
2 files changed, 21 insertions, 14 deletions
diff --git a/src/clang.rs b/src/clang.rs index 0a59c309..efb53074 100644 --- a/src/clang.rs +++ b/src/clang.rs @@ -527,25 +527,32 @@ impl Cursor { } } - /// Does this cursor have the given simple attribute? + /// Whether this cursor has the `warn_unused_result` attribute. + pub fn has_warn_unused_result_attr(&self) -> bool { + // FIXME(emilio): clang-sys doesn't expose this (from clang 9). + const CXCursor_WarnUnusedResultAttr: CXCursorKind = 440; + self.has_attr("warn_unused_result", Some(CXCursor_WarnUnusedResultAttr)) + } + + /// Does this cursor have the given attribute? /// - /// Note that this will only work for attributes that don't have an existing libclang - /// CursorKind, e.g. pure, const, etc. - pub fn has_simple_attr(&self, attr: &str) -> bool { + /// `name` is checked against unexposed attributes. + fn has_attr(&self, name: &str, clang_kind: Option<CXCursorKind>) -> bool { let mut found_attr = false; self.visit(|cur| { - if cur.kind() == CXCursor_UnexposedAttr { - found_attr = cur.tokens().iter().any(|t| { + let kind = cur.kind(); + found_attr = clang_kind.map_or(false, |k| k == kind) || + (kind == CXCursor_UnexposedAttr && + cur.tokens().iter().any(|t| { t.kind == CXToken_Identifier && - t.spelling() == attr.as_bytes() - }); + t.spelling() == name.as_bytes() + })); - if found_attr { - return CXChildVisit_Break; - } + if found_attr { + CXChildVisit_Break + } else { + CXChildVisit_Continue } - - CXChildVisit_Continue }); found_attr diff --git a/src/ir/function.rs b/src/ir/function.rs index 9ccf4e14..0715ec54 100644 --- a/src/ir/function.rs +++ b/src/ir/function.rs @@ -424,7 +424,7 @@ impl FunctionSig { }; let must_use = ctx.options().enable_function_attribute_detection && - cursor.has_simple_attr("warn_unused_result"); + cursor.has_warn_unused_result_attr(); let is_method = kind == CXCursor_CXXMethod; let is_constructor = kind == CXCursor_Constructor; let is_destructor = kind == CXCursor_Destructor; |