Skip to content

Recognize inline namespaces using clang's dedicated API for that #2951

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

Merged
merged 2 commits into from
Nov 28, 2024
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
20 changes: 20 additions & 0 deletions bindgen-tests/tests/expectations/tests/inline_namespace_macro.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions bindgen-tests/tests/headers/inline_namespace_macro.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// bindgen-flags: --enable-cxx-namespaces -- -std=c++11

#include "namespace/nsdefine.h"

BEGIN_NAMESPACE

class duration {};

END_NAMESPACE
4 changes: 4 additions & 0 deletions bindgen-tests/tests/headers/namespace/nsdefine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

#define BEGIN_NAMESPACE namespace repro { inline namespace __1 {
#define END_NAMESPACE } }
5 changes: 5 additions & 0 deletions bindgen/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,11 @@ impl Cursor {
})
}
}

/// Is this cursor's referent a namespace that is inline?
pub(crate) fn is_inline_namespace(&self) -> bool {
unsafe { clang_Cursor_isInlineNamespace(self.x) != 0 }
Copy link
Member Author

Choose a reason for hiding this comment

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

According to https://docs.rs/clang-sys/1.8.1/clang_sys/fn.clang_Cursor_isInlineNamespace.html, this function is available in libclang 9.0 and later.

Since #2932, bindgen only supports libclang 9.0 or later.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for saving me the time of checking it :)

}
}

/// A struct that owns the tokenizer result from a given cursor.
Expand Down
4 changes: 4 additions & 0 deletions bindgen/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2328,6 +2328,10 @@ If you encounter an error missing from this list, please file an issue or a PR!"
}
}

if cursor.is_inline_namespace() {
kind = ModuleKind::Inline;
}

(module_name, kind)
}

Expand Down
Loading