Repository: immunant/c2rust (language: rust)

Issue:
transpile panics on stdlib.h with invalid Ident
Attempting to transpile /usr/include/stdlib.h with function macros results in a panic from proc-macro2:
```
$ RUST_BACKTRACE=1 c2rust transpile --preserve-unused-functions --translate-fn-macros --overwrite-existing /usr/include/stdlib.h -o .
Transpiling stdlib.h
thread 'main' panicked at '"__bswap_constant_16 " is not a valid Ident', /home/gphemsley/.cargo/registry/src/github.com-1ecc6299db9ec823/proc-macro2-1.0.57/src/fallback.rs:811:9
stack backtrace:
   0: rust_begin_unwind
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/core/src/panicking.rs:142:14
   2: proc_macro2::fallback::validate_ident
   3: proc_macro2::fallback::Ident::_new
             at /home/gphemsley/.cargo/registry/src/github.com-1ecc6299db9ec823/proc-macro2-1.0.57/src/fallback.rs:753:9
   4: proc_macro2::fallback::Ident::new
             at /home/gphemsley/.cargo/registry/src/github.com-1ecc6299db9ec823/proc-macro2-1.0.57/src/fallback.rs:763:9
   5: proc_macro2::imp::Ident::new
             at /home/gphemsley/.cargo/registry/src/github.com-1ecc6299db9ec823/proc-macro2-1.0.57/src/wrapper.rs:689:50
   6: proc_macro2::Ident::new
             at /home/gphemsley/.cargo/registry/src/github.com-1ecc6299db9ec823/proc-macro2-1.0.57/src/lib.rs:973:21
   7: <&str as c2rust_ast_builder::builder::Make<proc_macro2::Ident>>::make
             at /home/gphemsley/Development/Rust/c2rust/c2rust-ast-builder/src/builder.rs:171:9
   8: <I as c2rust_ast_builder::builder::Make<syn::path::PathSegment>>::make
             at /home/gphemsley/Development/Rust/c2rust/c2rust-ast-builder/src/builder.rs:308:20
   9: <alloc::vec::Vec<S> as c2rust_ast_builder::builder::Make<syn::path::Path>>::make
             at /home/gphemsley/Development/Rust/c2rust/c2rust-ast-builder/src/builder.rs:318:27
  10: <&str as c2rust_ast_builder::builder::Make<syn::path::Path>>::make
             at /home/gphemsley/Development/Rust/c2rust/c2rust-ast-builder/src/builder.rs:202:9
  11: c2rust_ast_builder::builder::Builder::path
             at /home/gphemsley/Development/Rust/c2rust/c2rust-ast-builder/src/builder.rs:714:9
  12: c2rust_transpile::translator::Translation::convert_macro_invocation
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/translator/mod.rs:4020:13
  13: c2rust_transpile::translator::Translation::convert_expr::{{closure}}
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/translator/mod.rs:3230:59
  14: core::option::Option<T>::and_then
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/core/src/option.rs:1262:24
  15: c2rust_transpile::translator::Translation::convert_expr
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/translator/mod.rs:3230:38
  16: c2rust_transpile::cfg::CfgBuilder::convert_stmt_help::{{closure}}
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/cfg/mod.rs:1414:46
  17: core::option::Option<T>::map
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/core/src/option.rs:929:29
  18: c2rust_transpile::cfg::CfgBuilder::convert_stmt_help
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/cfg/mod.rs:1414:33
  19: c2rust_transpile::cfg::CfgBuilder::convert_stmts_help::{{closure}}
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/cfg/mod.rs:1352:23
  20: c2rust_transpile::cfg::CfgBuilder::with_scope
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/cfg/mod.rs:1270:17
  21: c2rust_transpile::cfg::CfgBuilder::convert_stmts_help
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/cfg/mod.rs:1344:9
  22: c2rust_transpile::cfg::Cfg<c2rust_transpile::cfg::Label,c2rust_transpile::cfg::StmtOrDecl>::from_stmts::{{closure}}
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/cfg/mod.rs:585:29
  23: c2rust_transpile::translator::Translation::with_scope
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/translator/mod.rs:4798:22
  24: c2rust_transpile::cfg::Cfg<c2rust_transpile::cfg::Label,c2rust_transpile::cfg::StmtOrDecl>::from_stmts
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/cfg/mod.rs:584:9
  25: c2rust_transpile::translator::Translation::convert_function_body::{{closure}}
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/translator/mod.rs:2520:34
  26: c2rust_transpile::translator::Translation::with_scope
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/translator/mod.rs:4798:22
  27: c2rust_transpile::translator::Translation::convert_function_body
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/translator/mod.rs:2519:9
  28: c2rust_transpile::translator::Translation::convert_function::{{closure}}
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/translator/mod.rs:2352:40
  29: c2rust_transpile::translator::Translation::with_scope
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/translator/mod.rs:4798:22
  30: c2rust_transpile::translator::Translation::convert_function
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/translator/mod.rs:2250:9
  31: c2rust_transpile::translator::Translation::convert_decl
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/translator/mod.rs:1902:42
  32: c2rust_transpile::translator::translate
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/translator/mod.rs:719:23
  33: c2rust_transpile::transpile_single
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/lib.rs:536:9
  34: c2rust_transpile::transpile::{{closure}}
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/lib.rs:328:17
  35: core::iter::adapters::map::map_fold::{{closure}}
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/core/src/iter/adapters/map.rs:84:28
  36: core::iter::traits::iterator::Iterator::fold
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/core/src/iter/traits/iterator.rs:2414:21
  37: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/core/src/iter/adapters/map.rs:124:9
  38: core::iter::traits::iterator::Iterator::for_each
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/core/src/iter/traits/iterator.rs:831:9
  39: <alloc::vec::Vec<T,A> as alloc::vec::spec_extend::SpecExtend<T,I>>::spec_extend
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/alloc/src/vec/spec_extend.rs:40:17
  40: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/alloc/src/vec/spec_from_iter_nested.rs:62:9
  41: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/alloc/src/vec/spec_from_iter.rs:33:9
  42: <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/alloc/src/vec/mod.rs:2648:9
  43: core::iter::traits::iterator::Iterator::collect
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/core/src/iter/traits/iterator.rs:1836:9
  44: c2rust_transpile::transpile
             at /home/gphemsley/Development/Rust/c2rust/c2rust-transpile/src/lib.rs:325:23
  45: c2rust_transpile::main
             at /home/gphemsley/Development/Rust/c2rust/c2rust/src/bin/c2rust-transpile.rs:259:5
  46: core::ops::function::FnOnce::call_once
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/core/src/ops/function.rs:248:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
```

I don't know if transpiling headers is intended to be supported, but I encountered this while attempting to transpile a header file that eventually wound up including stdlib.h.


Discussion / hints:

Fix patch:
diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs
index 6c32f7cf68..10ba422ee2 100644
--- a/c2rust-transpile/src/translator/mod.rs
+++ b/c2rust-transpile/src/translator/mod.rs
@@ -4012,7 +4012,7 @@ impl<'c> Translation<'c> {
         text: &str,
     ) -> Option<WithStmts<Box<Expr>>> {
         let mut split = text.splitn(2, '(');
-        let ident = split.next()?;
+        let ident = split.next()?.trim();
         let args = split.next()?.trim_end_matches(')');
 
         let ts: TokenStream = syn::parse_str(args).ok()?;


Test patch: