cbindgen icon indicating copy to clipboard operation
cbindgen copied to clipboard

Allow `=` and multiline prefix/postfix in annotations

Open novafacing opened this issue 2 years ago • 4 comments

This adds two things:

  1. = is allowed in annotations. Previously, it was an error to have an = in the annotation but I'm doing hacky stuff (see below) and it didn't seem like there was a good reason not to permit it.
  2. Multiline annotations are now supported by using a \ at the end of the annotated line and any following lines that should be concatenated together with newlines.

What this allows is basically:

#[cfg(target_arch = "i386")]
pub mod foo {
  #[no_mangle]
  /// cbindgen:prefix= \
  /// #define FUNC_THAT_NEEDS_TO_BE_IFDEFED(x) \ \
  ///     do { \ \
  ///         do_something(x); \ \
  ///    } while (0) \
  ///
  pub extern "C" fn bar() {}
}

Note the \ \: the last \ becomes a newline, then the line is trimmed and appended to the annotation value. This means if you want a literal \ in your code (as you frequently do in a macro), you need two \.

after_include is useful, but it has trouble when you need to conditionally compile based on the ifdef generated for a cfg attribute. This fixes that and makes it possible to somewhat hackily embed arbitrary C code wherever you want (at the cost of having to declare something). Eventually an annotation that can occur on a free comment might be nice to eliminate this need, but the two changes added by this PR, the = allowing and multiline allowing would still be needed.

novafacing avatar Jul 31 '23 23:07 novafacing

Whoops, looks like I failed the MSRV check. Will fix that.

novafacing avatar Jul 31 '23 23:07 novafacing

Hmm, the clippy error is a false positive...I'll see what I can do.

novafacing avatar Aug 01 '23 00:08 novafacing

At the very least this needs a test. Not opposed to this in principle tho?

emilio avatar Sep 04 '23 18:09 emilio

Yeah it's breaking a lot of existing comments, I want to rework it and use some less silly parsing!

novafacing avatar Sep 05 '23 17:09 novafacing