Skip to content

Commit

Permalink
vala, codegen: Add o modifier to regex literal
Browse files Browse the repository at this point in the history
Use compile flag `G_REGEX_OPTIMIZE` to make use of JIT optimization
  • Loading branch information
wszqkzqk authored and ricotz committed Mar 22, 2024
1 parent 98c5bff commit e42c1ee
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
3 changes: 3 additions & 0 deletions codegen/valaccodebasemodule.vala
Original file line number Diff line number Diff line change
Expand Up @@ -4494,6 +4494,9 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
if (parts[1].contains ("x")) {
flags += " | G_REGEX_EXTENDED";
}
if (parts[1].contains ("o")) {
flags += " | G_REGEX_OPTIMIZE";
}

var cdecl = new CCodeDeclaration ("GRegex*");

Expand Down
2 changes: 2 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,10 @@ TESTS = \
scanner/regex-escape-x-digit-length.test \
scanner/regex-escape-x-empty.test \
scanner/regex-escape-x.vala \
scanner/regex-modifier-o.vala \
scanner/regex-multiple-i.test \
scanner/regex-multiple-m.test \
scanner/regex-multiple-o.test \
scanner/regex-multiple-s.test \
scanner/regex-multiple-x.test \
scanner/string-escape-u-digit-length.test \
Expand Down
56 changes: 56 additions & 0 deletions tests/scanner/regex-modifier-o.c-expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* scanner_regex_modifier_o.c generated by valac, the Vala compiler
* generated from scanner_regex_modifier_o.vala, do not modify */

#include <glib.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))

static void _vala_main (void);

static GRegex* _tmp_regex_0 = NULL;

static inline GRegex*
_thread_safe_regex_init (GRegex** re,
const gchar * pattern,
GRegexCompileFlags compile_flags)
{
if (g_once_init_enter ((volatile gsize*) re)) {
GRegex* val = g_regex_new (pattern, compile_flags, 0, NULL);
g_once_init_leave ((volatile gsize*) re, (gsize) val);
}
return *re;
}

static gpointer
_g_regex_ref0 (gpointer self)
{
return self ? g_regex_ref (self) : NULL;
}

static void
_vala_main (void)
{
GRegex* foo = NULL;
GRegex* _tmp0_;
_tmp0_ = _g_regex_ref0 (_thread_safe_regex_init (&_tmp_regex_0, "foo(.*)", 0 | G_REGEX_OPTIMIZE));
foo = _tmp0_;
_g_regex_unref0 (foo);
}

int
main (int argc,
char ** argv)
{
_vala_main ();
return 0;
}

3 changes: 3 additions & 0 deletions tests/scanner/regex-modifier-o.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
void main () {
Regex foo = /foo(.*)/o;
}
5 changes: 5 additions & 0 deletions tests/scanner/regex-multiple-o.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Invalid Code

void main () {
Regex foo = /foo(.*)/oo;
}
9 changes: 8 additions & 1 deletion vala/valascanner.vala
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ public class Vala.Scanner {
var fl_s = false;
var fl_m = false;
var fl_x = false;
while (current[0] == 'i' || current[0] == 's' || current[0] == 'm' || current[0] == 'x') {
var fl_o = false;
while (current[0] == 'i' || current[0] == 's' || current[0] == 'm' || current[0] == 'x' || current[0] == 'o') {
switch (current[0]) {
case 'i':
if (fl_i) {
Expand All @@ -148,6 +149,12 @@ public class Vala.Scanner {
}
fl_x = true;
break;
case 'o':
if (fl_o) {
Report.error (get_source_reference (token_length_in_chars), "modifier 'o' used more than once");
}
fl_o = true;
break;
}
current++;
token_length_in_chars++;
Expand Down

0 comments on commit e42c1ee

Please sign in to comment.