[Qt-creator] Parse macro arguments for Spell Checking

Carel Combrink carel.combrink at gmail.com
Tue Jan 19 21:20:17 CET 2016


Hi,

In an attempt to handle macros correctly from my SpellChecker plugin I need
to get the arguments from functionLike() macros to check for string
literals.

So I have the following code that obtains the MacroUse from the
Document::Ptr:

        QList<CPlusPlus::Document::MacroUse> macroUse = docPtr->macroUses();

        for(const CPlusPlus::Document::MacroUse& mac: macroUse) {

            const CPlusPlus::Macro& macro = mac.macro();

            qDebug() << "Name            : " << macro.nameToQString();

            qDebug() << "isFunctionLike(): " << mac.isFunctionLike();

            qDebug() << "beginLine()     : " << mac.beginLine();

            const QVector<CPlusPlus::Document::Block>& args = mac.arguments();

            qDebug() << "Arguments:      : " << args.count();

            for(const CPlusPlus::Document::Block& block: args) {

                qDebug() << "bytesBegin" << block.bytesBegin();

                qDebug() << "bytesEnd" << block.bytesEnd();

                qDebug() << "Bytes Diff: " << block.bytesEnd() -
block.bytesBegin();

                qDebug() << "utf16charsBegin" << block.utf16charsBegin();

                qDebug() << "utf16charsEnd" << block.utf16charsEnd();

                qDebug() << "UTF Diff: " << block.utf16charsEnd() -
block.utf16charsBegin();

            }

        }


What I am struggling with is to get the argument before the macro is
replaced/expanded.
Say I have the following on line 46:

QString stringLiteral = QStringLiteral("A String Literal wirth spellig
mistakes");


The above code prints out:

Name : "QStringLiteral"

isFunctionLike(): true

beginLine() : 46

Arguments: : 1

bytesBegin 1894

bytesEnd 1935

Bytes Diff: 41

utf16charsBegin 1893

utf16charsEnd 1934

UTF Diff: 41

>From the above I see that I get the correct Macro, with the correct number
of arguments and the length of the argument is correct.

Now the issue: How do I get the actual argument: "A String Literal wirth
spellig mistakes"

If I can get the argument, the plan is to extract string literals (text in
between "") and then spell check that text.
The second thing I will then need to do is to underline any spelling
mistakes, so I would need to get the line and column number if possible, of
the misspelled word before expanded.

In a previous discussion about this issue on the mailing list last year (
here
<http://qt-creator.qt-project.narkive.com/BKuwfUiS/issue-parsing-qstringliteral-in-spellchecker-plugin>)
I got something working for QStringLiteral, but the issue was that other
macros would break down, such as the __FILE__ macro. Thus I am revisiting
the issue and hopefully can get it resolved for all macros with arguments.
In the referenced discussion a user (Typz) suggested to run the fast
preprocessor before parsing strings, but I have no idea how to do this
(correctly) if this is the recommended approach above what I am attempting
above.

Any help will be much appreciated. If I get this sorted out I want to start
investigating the possibility of integrating my plugin with QtCreator.

PS: The above code is still for QtC 3.6. Also I am using QStringLiteral as
the example since that is the most used macro in these circumstances, but
the same should apply for any macro, even custom ones defined in the
project.


Regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/qt-creator/attachments/20160119/75409173/attachment.html>


More information about the Qt-creator mailing list