Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contrib/rust/feature-fetch/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
37 changes: 37 additions & 0 deletions contrib/rust/feature-fetch/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
OUT = ../../../gcc/rust/checks/errors/feature/rust-feature-defs.h

all: $(OUT)

mk-build-dir:
mkdir -p build

build/parse.c: parse.y mk-build-dir
$(YACC) $(YFLAGS) -o $@ --defines=build/parse.h $<

build/parse.h: build/parse.c;

build/scan.c: scan.l
$(LEX) $(LFLAGS) -o $@ -Ca --header-file=build/scan.h $<

build/scan.h: build/scan.c;

build/%.o: build/%.c build/parse.h build/scan.h
$(CC) $(CFLAGS) -c -Ibuild -o $@ $<

build/feature-extract: build/parse.o build/scan.o
$(CC) $(LDFLAGS) $(LDLIBS) -o $@ $^

build/download.rs: fetch
./$< $@

$(OUT): build/feature-extract build/download.rs
cat build/download.rs | ./$< > build/rust-feature-defs.h
clang-format -i build/rust-feature-defs.h \
--style=file:../../clang-format
mv build/rust-feature-defs.h $(OUT)

clean:
$(RM) -r build

full-clean: clean
$(RM) $(OUT)
12 changes: 12 additions & 0 deletions contrib/rust/feature-fetch/fetch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

RUST_VERSION="1.49.0"

[ $# = 1 ] || exit 1

# Fetches files from the official rustc git repository

URL_PREFIX='https://raw.githubusercontent.com/rust-lang/rust/refs/tags'
URL_TEMPLATE="$URL_PREFIX/$RUST_VERSION/compiler/rustc_feature/src"

wget -O $1 "$URL_TEMPLATE"/{accepted,active,removed}.rs
143 changes: 143 additions & 0 deletions contrib/rust/feature-fetch/parse.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/* Copyright (C) 2025 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */

%{

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int yylex (void);
void yyerror (char const *);

#include "scan.h"

// expands to three %s parameters
#define UNWRAP_OPT_STR(prefix, s) (s ? prefix "_SOME (" : prefix "_NONE"), (s ? s : ""), (s ? ")" : "")

%}

%union
{
char *str;
};

%token <str> IDENT STR NUM
%token SCOPE
%token K_SOME K_NONE
%token K_ACTIVE K_ACCEPTED K_REMOVED K_STABLE_REMOVED
%token K_E_START K_E_2018

%type <str> issue
%type <str> edition
%type <str> reason

%%

multi_database: multi_database database
| database
;

database: '(' entry_list ')';

entry_list: entry_list entry ','
| entry ','
;

entry: '(' K_ACTIVE ',' IDENT ',' STR ',' issue ',' edition ')' {
char *ident_upper = strdup ($4);
for (size_t i = 0; ident_upper[i]; i++)
ident_upper[i] = toupper (ident_upper[i]);
printf ("FEATURE_ACTIVE (\"%s\", %s, %s, %s%s%s, EDITION_%s)\n", $4, ident_upper, $6, UNWRAP_OPT_STR ("ISSUE", $8), $10 ? $10 : "NONE");
free ($4);
free (ident_upper);
free ($6);
free ($8);
}
| '(' K_ACCEPTED ',' IDENT ',' STR ',' issue ',' K_NONE ')' {
char *ident_upper = strdup ($4);
for (size_t i = 0; ident_upper[i]; i++)
ident_upper[i] = toupper (ident_upper[i]);
printf ("FEATURE_ACCEPTED (\"%s\", %s, %s, %s%s%s)\n", $4, ident_upper, $6, UNWRAP_OPT_STR ("ISSUE", $8));
free ($4);
free (ident_upper);
free ($6);
free ($8);
}
| '(' K_REMOVED ',' IDENT ',' STR ',' issue ',' K_NONE ',' reason ')' {
char *ident_upper;
// HACK: convert no_debug to F_NO_DEBUG instead
// since NO_DEBUG is used as an unrelated macro
if (!strcmp ($4, "no_debug"))
{
ident_upper = strdup ("F_NO_DEBUG");
}
else
{
ident_upper = strdup ($4);
for (size_t i = 0; ident_upper[i]; i++)
ident_upper[i] = toupper (ident_upper[i]);
}
printf ("FEATURE_REMOVED (\"%s\", %s, %s, %s%s%s, %s%s%s)\n", $4, ident_upper, $6, UNWRAP_OPT_STR ("ISSUE", $8), UNWRAP_OPT_STR ("REASON", $12));
free ($4);
free (ident_upper);
free ($6);
free ($8);
free ($12);
}
| '(' K_STABLE_REMOVED ',' IDENT ',' STR ',' issue ',' K_NONE ')' {
char *ident_upper = strdup ($4);
for (size_t i = 0; ident_upper[i]; i++)
ident_upper[i] = toupper (ident_upper[i]);
printf ("FEATURE_STABLE_REMOVED (\"%s\", %s, %s, %s%s%s)\n", $4, ident_upper, $6, UNWRAP_OPT_STR ("ISSUE", $8));
free ($4);
free (ident_upper);
free ($6);
free ($8);
}
;

issue: K_SOME '(' NUM ')' { $$ = $3; }
| K_NONE { $$ = NULL; }
;

/* TODO: expand this as needed */
edition: K_NONE { $$ = NULL; }
| K_SOME '(' K_E_START SCOPE K_E_2018 ')' { $$ = "2018"; }
;

reason: K_SOME '(' STR ')' { $$ = $3; }
| K_NONE { $$ = NULL; }
;

%%

void yyerror (const char *msg)
{
fprintf (stderr, "%s\n", msg);
}

int yywrap (void)
{
return 1;
}

int main (void)
{
return yyparse ();
}
5 changes: 5 additions & 0 deletions contrib/rust/feature-fetch/regen
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

cd "$(dirname "$0")"
rm -f ../../../gcc/rust/checks/errors/feature/rust-feature-defs.h
make all
55 changes: 55 additions & 0 deletions contrib/rust/feature-fetch/scan.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Copyright (C) 2025 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */

%{

#include "parse.h"

static int p_count = 0;

%}

%x INSIDE COMMENT

%%

declare_features! BEGIN (INSIDE);
.|\n /* ignore */

<INSIDE>\( p_count++; return '(';
<INSIDE>\) if (!--p_count) { BEGIN (0); } return ')';
<INSIDE>, return ',';
<INSIDE>:: return SCOPE;
<INSIDE>Some return K_SOME;
<INSIDE>None return K_NONE;
<INSIDE>active return K_ACTIVE;
<INSIDE>accepted return K_ACCEPTED;
<INSIDE>removed return K_REMOVED;
<INSIDE>stable_removed return K_STABLE_REMOVED;
<INSIDE>Edition return K_E_START;
<INSIDE>Edition2018 return K_E_2018;

<INSIDE>[A-Za-z_][A-Za-z0-9_]* yylval.str = strdup (yytext); return IDENT;
<INSIDE>[1-9][0-9]* yylval.str = strdup (yytext); return NUM;
<INSIDE>\"[^"]+\" yylval.str = strdup (yytext); return STR;
<INSIDE>"/""/" BEGIN (COMMENT);
<INSIDE>[ \n] /* ignore */
<INSIDE>. { fprintf (stderr, "unrecognized character %u\n", (unsigned int) yytext[0]); exit (1); }

<COMMENT>. /* skip */
<COMMENT>\n BEGIN (INSIDE);
6 changes: 6 additions & 0 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ RUST_INCLUDES = -I $(srcdir)/rust \
-I $(srcdir)/rust/checks/errors \
-I $(srcdir)/rust/checks/errors/privacy \
-I $(srcdir)/rust/checks/errors/borrowck \
-I $(srcdir)/rust/checks/errors/feature \
-I $(srcdir)/rust/util \
-I $(srcdir)/rust/metadata \
-I $(srcdir)/../libgrust
Expand Down Expand Up @@ -516,6 +517,11 @@ rust/%.o: rust/checks/errors/%.cc
$(COMPILE) $(RUST_CXXFLAGS) $(RUST_INCLUDES) $<
$(POSTCOMPILE)

# build feature related files in rust folder
rust/%.o: rust/checks/errors/feature/%.cc
$(COMPILE) $(RUST_CXXFLAGS) $(RUST_INCLUDES) $<
$(POSTCOMPILE)

# build privacy pass files in rust folder
rust/%.o: rust/checks/errors/privacy/%.cc
$(COMPILE) $(RUST_CXXFLAGS) $(RUST_INCLUDES) $<
Expand Down
Loading
Loading