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

Properly handle namespaces for enum configuration options #1162

Merged
merged 1 commit into from
Nov 28, 2017
Merged
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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ Released YYYY/MM/DD

## Changed

* TODO (or remove section if none)
* The `bindgen::Builder::{constified_enum_module,{bitfield,rustified}_enum}`
builder methods and their corresponding CLI flags now compare their argument
to the C/C++ `enum`'s "canonical path", which includes leading namespaces,
rather than its "canonical name", which does not. This is a breaking change
that requires callers which target a namespaced C++ enum to call e.g.
`bitfield_enum("<namespace>::<enum_name>")` rather than e.g.
`bitfield_enum("<enum_name>")`. [#1162][]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


## Deprecated

Expand Down
2 changes: 0 additions & 2 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2419,8 +2419,6 @@ impl CodeGenerator for Enum {
}
};

// FIXME(emilio): These should probably use the path so it can
// disambiguate between namespaces, just like is_opaque etc.
let variation = if self.is_bitfield(ctx, item) {
EnumVariation::Bitfield
} else if self.is_rustified_enum(ctx, item) {
Expand Down
14 changes: 7 additions & 7 deletions src/ir/enum_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::item::Item;
use super::ty::TypeKind;
use clang;
use ir::annotations::Annotations;
use ir::item::ItemCanonicalName;
use ir::item::ItemCanonicalPath;
use parse::{ClangItemParser, ParseError};

/// An enum representing custom handling that can be given to a variant.
Expand Down Expand Up @@ -130,10 +130,10 @@ impl Enum {

/// Whether the enum should be a bitfield
pub fn is_bitfield(&self, ctx: &BindgenContext, item: &Item) -> bool {
let name = item.canonical_name(ctx);
let path = item.canonical_path(ctx);
let enum_ty = item.expect_type();

ctx.options().bitfield_enums.matches(&name) ||
ctx.options().bitfield_enums.matches(&path[1..].join("::")) ||
(enum_ty.name().is_none() &&
self.variants().iter().any(|v| {
ctx.options().bitfield_enums.matches(&v.name())
Expand All @@ -146,10 +146,10 @@ impl Enum {
ctx: &BindgenContext,
item: &Item,
) -> bool {
let name = item.canonical_name(ctx);
let path = item.canonical_path(ctx);
let enum_ty = item.expect_type();

ctx.options().constified_enum_modules.matches(&name) ||
ctx.options().constified_enum_modules.matches(&path[1..].join("::")) ||
(enum_ty.name().is_none() &&
self.variants().iter().any(|v| {
ctx.options().constified_enum_modules.matches(&v.name())
Expand All @@ -158,10 +158,10 @@ impl Enum {

/// Whether the enum should be a Rust enum
pub fn is_rustified_enum(&self, ctx: &BindgenContext, item: &Item) -> bool {
let name = item.canonical_name(ctx);
let path = item.canonical_path(ctx);
let enum_ty = item.expect_type();

ctx.options().rustified_enums.matches(&name) ||
ctx.options().rustified_enums.matches(&path[1..].join("::")) ||
(enum_ty.name().is_none() &&
self.variants().iter().any(|v| {
ctx.options().rustified_enums.matches(&v.name())
Expand Down
2 changes: 1 addition & 1 deletion tests/headers/constify-module-enums-namespace.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// bindgen-flags: --enable-cxx-namespaces --constified-enum-module foo
// bindgen-flags: --enable-cxx-namespaces --constified-enum-module ns1::ns2::foo

namespace ns1 {
namespace ns2 {
Expand Down
2 changes: 1 addition & 1 deletion tests/headers/constify-module-enums-simple-nonamespace.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// bindgen-flags: --constified-enum-module one_Foo
// bindgen-flags: --constified-enum-module one::Foo

namespace one {
enum class Foo {
Expand Down