rust-cpp icon indicating copy to clipboard operation
rust-cpp copied to clipboard

Extra blanks in generated cpp_closures.cpp

Open ratijas opened this issue 2 years ago • 4 comments

In the generated cpp_closures.cpp, every chunk of code copied verbatim from cpp!([]{}) content is preceded by #line pragma and a line with an inconsistently large amount of spaces. Which probably indicated there is some minor bug in parser.

For example, these fragments of Rust:

    fn get_object_description() -> &'static QObjectDescriptor
    where
        Self: Sized,
    {
        unsafe {
            &*cpp!([]-> *const QObjectDescriptor as "RustQObjectDescriptor const*" {
                return RustQObjectDescriptor::instance<RustObject<QObject>>();
            })
        }
    }
    pub unsafe fn as_qvariant(&self) -> QVariant {
        let self_ = self.get_cpp_object();
        cpp!([self_ as "QObject*"] -> QVariant as "QVariant"  {
            return QVariant::fromValue(self_);
        })
    }
    pub fn destroyed_signal() -> Signal<fn()> {
        unsafe {
            Signal::new(cpp!([] -> SignalInner as "SignalInner"  {
                return &QObject::destroyed;
            }))
        }
    }
    pub fn set_object_name(&self, name: QString) {
        let self_ = self.get_cpp_object();
        unsafe {
            cpp!([self_ as "QObject*", name as "QString"] {
                if (self_) self_->setObjectName(std::move(name));
            })
        }
    }

produce this C++ output:

Extra blanks

Macro code responsible for this is probably somewhere around here:

https://github.com/mystor/rust-cpp/blob/b826bc5beb7abc15c4862be748e0c3ac0b8b2d61/cpp_build/src/lib.rs#L43-L52

ratijas avatar Jul 21 '21 08:07 ratijas

After careful observation it becomes clear that whitespaces were actually a macro invocation code and rust syntax tokens. They got replaced instead of being removed to keep inner macro tokens at the same relative line:column locations as in original code.

So even though this

            &*cpp!([]-> *const QObjectDescriptor as "RustQObjectDescriptor const*" {
                return RustQObjectDescriptor::instance<RustObject<QObject>>();
            })

becomes this

#line 339 "src/lib.rs"
                                                                                    
                return RustQObjectDescriptor::instance<RustObject<QObject>>();
            
}

that's only to keep things in place which could possibly come after cpp!([]-> *const QObjectDescriptor as "RustQObjectDescriptor const*" { characters. It could be solved by "stripping" lines at the beginning & end of a macro, and incrementing #line counter accordingly.

ratijas avatar Jul 23 '21 08:07 ratijas

But the extra whitespace is not really a problem, is it?

ogoffart avatar Jul 23 '21 08:07 ogoffart

No, not really a technical issue AFAICT. It's just my stupid perfectionism. Cuts my eye, so to say. Probably not worth fixing something that just works.

ratijas avatar Jul 23 '21 09:07 ratijas

What's important is that the error message the the compiler still points to the right location. The trailing whitespace at the end of the line don't matter, but if it is not trailing, it is important to keep them.

the error location could be perfected. For example, the return type and the argument type are not in lines that are marked with #line so if there is a typo in these, it leads to difficult errors.

ogoffart avatar Jul 23 '21 09:07 ogoffart