[cfe-dev] Proposal: parsing Qt signals/slots with attributes

erikjv erikjv at me.com
Tue Sep 20 01:10:26 PDT 2011


Hello,

I would like to add support for recognising Qt's signal/slot mechanism to clang by using attributes. For that, I have a proposal for a few small changes to the clang parser. These changes use the attributes to mark AST nodes as signal/slot/invokable, and expose those attributes through libclang.

A bit of background: if you are more familiar with Objective-C, then the signal/slot mechanism works nearly exactly the same as Cocoa's IBAction/IBOutlet: it uses a meta-type system to invoke the methods, or let slots connected to a signal know that the signal was emitted. Qt's meta-type system also allows for "invokable methods", which can be called/invoked without being tagged as slot.

Example:

class Test {
public:
    Q_INVOKABLE void invokableA();

public slots:
    void slotA();
    void inlineSlotB() {}

signals:
    void signalC();

private:
    Q_SIGNAL void signalD(), signalE();
    Q_SLOT void slotF(), slotG();
    void not_a_slot_or_slot();
    Q_SLOT void inlineSlotH() {}
};

(To be used properly with Qt, the class would also have to inherit from QObject, and have the Q_OBJECT macro right after the opening brace.)

Taking a leaf from Cocoa's book, we can use the pre-processor to inject:

#define signals protected __attribute__((q_signal))
#define slots __attribute__((q_slot))
#define Q_SIGNAL __attribute__((q_signal))
#define Q_SLOT __attribute__((q_slot))
#define Q_INVOKABLE __attribute__((q_invokable))

Then the proposed change to clang is to handle it as follows:
- for method declarations or inline method definitions, accept the q_signal/q_slot/q_invokable attributes, and add the attribute to the AST node
- for C++ access specifiers, accept an additional (trailing) q_* attribute, and add the attribute to all AST nodes for methods until the next specifier

This is equivalent to how moc (Qt's Meta-Object Compiler) generates the meta-object for a QObject.

Attached is a proof-of-concept implementation/patch (with the above example as test-case). It also adds the attributes as cursor kinds to libclang, so a client application (like Qt Creator) can use it to find out which of the methods are "tagged" as a signal/slot/invokable.

-- Erik.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110920/a4681f80/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Added-support-for-q_signal-q_slot-and-q_invokable-at.patch
Type: application/octet-stream
Size: 23666 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110920/a4681f80/attachment.obj>


More information about the cfe-dev mailing list