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

context: Fix tokenization of C++20 inline namespace. #2294

Merged
merged 2 commits into from Oct 5, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions bindgen-tests/tests/expectations/tests/inline_namespace_nested.rs

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

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

5 changes: 5 additions & 0 deletions bindgen-tests/tests/headers/inline_namespace_nested.hpp
@@ -0,0 +1,5 @@
// bindgen-flags: --enable-cxx-namespaces -- -std=c++2a

namespace ranges::inline foo::bar {
static int bar = 0;
}
2 changes: 1 addition & 1 deletion bindgen-tests/tests/tests.rs
Expand Up @@ -252,7 +252,7 @@ fn compare_generated_header(
let actual = bindings.to_string();
rustfmt(actual)
}
Err(_) => ("<error generating bindings>".to_string(), "".to_string()),
Err(_) => ("/* error generating bindings */\n".into(), "".to_string()),
};
println!("{}", rustfmt_stderr);

Expand Down
71 changes: 35 additions & 36 deletions bindgen/ir/context.rs
Expand Up @@ -2107,13 +2107,18 @@ If you encounter an error missing from this list, please file an issue or a PR!"
}

let mut kind = ModuleKind::Normal;
let mut found_namespace_keyword = false;
let mut looking_for_name = false;
for token in cursor.tokens().iter() {
match token.spelling() {
b"inline" => {
assert!(!found_namespace_keyword);
assert!(kind != ModuleKind::Inline);
debug_assert!(
kind != ModuleKind::Inline,
"Multiple inline keywords?"
);
kind = ModuleKind::Inline;
// When hitting a nested inline namespace we get a spelling
// that looks like ["inline", "foo"]. Deal with it properly.
looking_for_name = true;
}
// The double colon allows us to handle nested namespaces like
// namespace foo::bar { }
Expand All @@ -2122,45 +2127,39 @@ If you encounter an error missing from this list, please file an issue or a PR!"
// but the tokenization of the second begins with the double
// colon. That's ok, so we only need to handle the weird
// tokenization here.
//
// Fortunately enough, inline nested namespace specifiers aren't
// a thing, and are invalid C++ :)
b"namespace" | b"::" => {
found_namespace_keyword = true;
looking_for_name = true;
}
b"{" => {
assert!(found_namespace_keyword);
// This should be an anonymous namespace.
assert!(looking_for_name);
break;
}
name if found_namespace_keyword => {
if module_name.is_none() {
module_name =
Some(String::from_utf8_lossy(name).into_owned());
name => {
if looking_for_name {
if module_name.is_none() {
module_name = Some(
String::from_utf8_lossy(name).into_owned(),
);
}
break;
} else {
// This is _likely_, but not certainly, a macro that's
// been placed just before the namespace keyword.
// Unfortunately, clang tokens don't let us easily see
// through the ifdef tokens, so we don't know what this
// token should really be. Instead of panicking though,
// we warn the user that we assumed the token was blank,
// and then move on.
//
// See also https://github.com/rust-lang/rust-bindgen/issues/1676.
warn!(
"Ignored unknown namespace prefix '{}' at {:?} in {:?}",
String::from_utf8_lossy(name),
token,
cursor
);
}
break;
}
spelling if !found_namespace_keyword => {
// This is _likely_, but not certainly, a macro that's been placed just before
// the namespace keyword. Unfortunately, clang tokens don't let us easily see
// through the ifdef tokens, so we don't know what this token should really be.
// Instead of panicking though, we warn the user that we assumed the token was
// blank, and then move on.
//
// See also https://github.com/rust-lang/rust-bindgen/issues/1676.
warn!(
"Ignored unknown namespace prefix '{}' at {:?} in {:?}",
String::from_utf8_lossy(spelling),
token,
cursor
);
}
spelling => {
panic!(
"Unknown token '{}' while processing namespace at {:?} in {:?}",
String::from_utf8_lossy(spelling),
token,
cursor
);
}
}
}
Expand Down