Bug 385544 - Validate regular expressions passed to QRegExp and QRegularExpression
Summary: Validate regular expressions passed to QRegExp and QRegularExpression
Status: CONFIRMED
Alias: None
Product: clazy
Classification: Developer tools
Component: general (show other bugs)
Version: unspecified
Platform: Other Linux
: NOR wishlist
Target Milestone: ---
Assignee: Sergio Martins
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-10-10 07:33 UTC by Thomas Fischer
Modified: 2017-10-18 11:24 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Fischer 2017-10-10 07:33:33 UTC
It would be nice if clazy could validate regular expressions passed in text form to QRegExp or QRegularExpression.
This should be either done by trying to parse/compile the regular expression passed to either class or by checking the passed regular expression for common mistakes (see examples below) based on context-sensitive text patterns rather than compiling the expression.

Example for which clazy should warn (passing raw char* to constructors to keep examples brief):
QRegularExpression("\bA"); // should be \\bA
QRegExp("v\\d([.]\\d)*)"); // mismatching parentheses
Comment 1 Sergio Martins 2017-10-10 09:49:01 UTC
we could use a regular expression to validate regular expressions :)
Comment 2 Sergio Martins 2017-10-18 08:02:29 UTC
One option is to link to pcre or Qt and validate the string.
Additionally, I think it should warn if you're not using C++11 raw-string-literals, which makes code much less error prone.
Comment 3 Thomas Fischer 2017-10-18 11:24:53 UTC
(In reply to Sergio Martins from comment #2)
> One option is to link to pcre or Qt and validate the string.
Using pcre would have the advantage of being a 'lighter' dependency than Qt and maybe more generally already installed. Using Qt would have the advantage to be more 'realistic'.

> Additionally, I think it should warn if you're not using C++11
> raw-string-literals, which makes code much less error prone.

If it would be technically possible, make the dependency on pcre or Qt compile-time optional, i.e. a configuration/build flag.
Coded in clazy could be some basic checks that would be applied before any pcre/Qt tests and be available even if pcre/Qt support would be disabled. Those basic checks could be:
- Correct usage of raw-string literals as you mention, e.g. \b vs \\b
- Correct matching of parenthesises: (..[..]{..}..) with some support for special cases such as \( or [^{]
... in general some quick and dirty checks for common mistakes and always cheaper than parsing the regexp in pcre or Qt. For more inspiration check StackExchange for common problems programmers have with regexps ... ;-)