[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Gomp-discuss] C parser patch for parsing omp pragmas
From: |
Debian User |
Subject: |
Re: [Gomp-discuss] C parser patch for parsing omp pragmas |
Date: |
Mon, 21 Feb 2005 11:11:58 +0300 |
User-agent: |
Mutt/1.5.6+20040523i |
Hello.
Some updates to the patch.
- fixed not breaking after handling openmp-construct
- fixed token consume when handle #pragma omp parallel
- replaced c_parser_skip_until_found ()
- now variable declaration is checked
- fixed wrong tree type in #pragma omp parallel sections handler
- corrected printf () in shared clause handler
- Added comments to OpenMP pragma parser functions.
- Fixed expression type in schedule clause.
- Fixed wrong line number when handling OpenMP pragma.
- Added handler for num_threads clause.
- Some code clean.
Dmitriy
--- orig/gcc/c-parser.c
+++ mod/gcc/c-parser.c
@@ -3280,6 +3280,7 @@
default:
goto expr_stmt;
}
+ break;
default:
expr_stmt:
stmt = c_finish_expr_stmt (c_parser_expression (parser).value);
@@ -4935,6 +4936,8 @@
/* ====== */
+/* Clauses that are known to the parser. */
+
typedef enum pragma_omp_clause {
PRAGMA_OMP_CLAUSE_NONE = 0,
@@ -4944,6 +4947,7 @@
PRAGMA_OMP_CLAUSE_IF,
PRAGMA_OMP_CLAUSE_LASTPRIVATE,
PRAGMA_OMP_CLAUSE_NOWAIT,
+ PRAGMA_OMP_CLAUSE_NUM_THREADS,
PRAGMA_OMP_CLAUSE_ORDERED,
PRAGMA_OMP_CLAUSE_PRIVATE,
PRAGMA_OMP_CLAUSE_REDUCTION,
@@ -4951,6 +4955,11 @@
PRAGMA_OMP_CLAUSE_SHARED
} pragma_omp_clause;
+/* Returns name of the next clause.
+ If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
+ the token is not consumed. Otherwise appropriate pragma_omp_clause is
+ returned and the token is consumed. */
+
static pragma_omp_clause c_parser_pragma_omp_clause (c_parser *);
static pragma_omp_clause
@@ -4989,6 +4998,10 @@
{
result = PRAGMA_OMP_CLAUSE_NOWAIT;
}
+ else if (!strcmp ("num_threads", p))
+ {
+ result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
+ }
else if (!strcmp ("ordered", p))
{
result = PRAGMA_OMP_CLAUSE_ORDERED;
@@ -5022,6 +5035,26 @@
return result;
}
+/* Skip till the CPP_EOF, CPP_EOF is not consumed like
+ in c_parser_skip_until_found () */
+
+static void c_parser_skip_until_eof (c_parser *);
+
+static void
+c_parser_skip_until_eof (c_parser *parser)
+{
+ while (c_parser_next_token_is_not (parser, CPP_EOF))
+ {
+ c_parser_consume_token (parser);
+ }
+}
+
+/* Parse variable list in form:
+ [ CPP_NAME [ CPP_COMMA CPP_NAME [ ... ] ] ]
+ Return when meets unexpected token, doesn't consume it.
+ Variables must be defined, otherwise returns and doesn't
+ consume the variable. */
+
static void c_parser_pragma_omp_variable_list (c_parser *);
static void
@@ -5031,7 +5064,12 @@
while (c_parser_next_token_is (parser, CPP_NAME))
{
- printf ("%s", IDENTIFIER_POINTER (c_parser_peek_token (parser)->value));
+ tree t = lookup_name (c_parser_peek_token (parser)->value);
+ if (!t)
+ {
+ break;
+ }
+ printf ("%s ", IDENTIFIER_POINTER (c_parser_peek_token (parser)->value));
c_parser_consume_token (parser);
if (c_parser_next_token_is_not (parser, CPP_COMMA))
@@ -5039,12 +5077,16 @@
break;
}
c_parser_consume_token (parser);
-
- printf (", ");
}
printf ("\n");
}
+/* OpenMP pragma clauses handlers. Each CLAUSE has a handler
+ static void c_parser_pragma_omp_clause_CLAUSE (c_parser *).
+ Each handler consumes all tokens the clause consists of.
+ If unexpected token found - skips untill CPP_EOF is found,
+ CPP_EOF is not consumed. */
+
static void c_parser_pragma_omp_clause_copyin (c_parser *);
static void
@@ -5057,12 +5099,12 @@
c_parser_pragma_omp_variable_list (parser);
if (!c_parser_require (parser, CPP_CLOSE_PAREN, "')' expected"))
{
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
else
{
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
@@ -5093,19 +5135,19 @@
c_parser_consume_token (parser);
if (!c_parser_require (parser, CPP_CLOSE_PAREN, "')' expected"))
{
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
else
{
error:
c_parser_error (parser, "'none' or 'shared' expected");
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
else
{
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
@@ -5121,12 +5163,12 @@
c_parser_pragma_omp_variable_list (parser);
if (!c_parser_require (parser, CPP_CLOSE_PAREN, "')' expected"))
{
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
else
{
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
@@ -5146,7 +5188,7 @@
else
{
c_parser_error (parser, "'(' expected");
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
@@ -5162,12 +5204,12 @@
c_parser_pragma_omp_variable_list (parser);
if (!c_parser_require (parser, CPP_CLOSE_PAREN, "')' expected"))
{
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
else
{
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
@@ -5179,6 +5221,38 @@
printf ("nowait\n");
}
+static inline void c_parser_pragma_omp_clause_num_threads (c_parser *);
+
+static inline void
+c_parser_pragma_omp_clause_num_threads (c_parser *parser)
+{
+ printf ("num_threads: ");
+
+ if (c_parser_require (parser, CPP_OPEN_PAREN, "'(' expected"))
+ {
+ tree t = c_parser_expression (parser).value;
+
+ if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
+ {
+ print_generic_expr (stdout, t, 0);
+ if (!c_parser_require (parser, CPP_CLOSE_PAREN, "')' expected"))
+ {
+ c_parser_skip_until_eof (parser);
+ }
+ }
+ else
+ {
+ c_parser_error (parser, "integer expression expected");
+ c_parser_skip_until_eof (parser);
+ }
+ }
+ else
+ {
+ c_parser_skip_until_eof (parser);
+ }
+ printf ("\n");
+}
+
static inline void c_parser_pragma_omp_clause_ordered (c_parser *);
static inline void
@@ -5199,12 +5273,12 @@
c_parser_pragma_omp_variable_list (parser);
if (!c_parser_require (parser, CPP_CLOSE_PAREN, "')' expected"))
{
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
else
{
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
@@ -5255,13 +5329,13 @@
}
else
{
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
else
{
error:
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
@@ -5302,38 +5376,42 @@
c_parser_consume_token (parser);
if (c_parser_next_token_is (parser, CPP_COMMA))
{
- /* Taken from c_parser_paren_condition (). */
- location_t loc = c_parser_peek_token (parser)->location;
tree t;
-
c_parser_consume_token (parser);
- printf (", ");
- t = lang_hooks.truthvalue_conversion (c_parser_expression
(parser).value);
- if (EXPR_P (t))
- SET_EXPR_LOCATION (t, loc);
+ t = c_parser_expression (parser).value;
- print_generic_expr (stdout, t, 0);
- printf ("\n");
+ if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
+ {
+ printf (", ");
- }
- skip_chunk:
- if (!c_parser_require (parser, CPP_CLOSE_PAREN, "')' expected"))
- {
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ print_generic_expr (stdout, t, 0);
+
+ skip_chunk:
+ if (!c_parser_require (parser, CPP_CLOSE_PAREN, "')'
expected"))
+ {
+ c_parser_skip_until_eof (parser);
+ }
+ }
+ else
+ {
+ c_parser_error (parser, "integer expression expected");
+ c_parser_skip_until_eof (parser);
+ }
}
}
else
{
error:
c_parser_error (parser, "unknown schedule kind");
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
else
{
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
+ printf ("\n");
}
static void c_parser_pragma_omp_clause_shared (c_parser *);
@@ -5341,29 +5419,38 @@
static void
c_parser_pragma_omp_clause_shared (c_parser *parser)
{
- printf ("copyin\n");
+ printf ("shared\n");
if (c_parser_require (parser, CPP_OPEN_PAREN, "'(' expected"))
{
c_parser_pragma_omp_variable_list (parser);
if (!c_parser_require (parser, CPP_CLOSE_PAREN, "')' expected"))
{
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
else
{
- c_parser_skip_until_found (parser, CPP_EOF, 0);
+ c_parser_skip_until_eof (parser);
}
}
+/* OpenMP pragma handlers. Each PRAGMA has a handler
+ static inline void c_parser_pragma_omp_PRAGMA (c_parser *).
+ Parses untill CPP_EOF is found. CPP_EOF is consumed. */
+
static inline void c_parser_pragma_omp_atomic (c_parser *);
static inline void
c_parser_pragma_omp_atomic (c_parser *parser)
{
printf ("#pragma omp atomic\n");
- c_parser_skip_until_found (parser, CPP_EOF, "EOF expected");
+
+ if (!c_parser_require (parser, CPP_EOF, "EOF expected"))
+ {
+ c_parser_skip_until_eof (parser);
+ c_parser_consume_token (parser);
+ }
}
static inline void c_parser_pragma_omp_barrier (c_parser *);
@@ -5372,7 +5459,12 @@
c_parser_pragma_omp_barrier (c_parser *parser)
{
printf ("#pragma omp barrier\n");
- c_parser_skip_until_found (parser, CPP_EOF, "EOF expected");
+
+ if (!c_parser_require (parser, CPP_EOF, "EOF expected"))
+ {
+ c_parser_skip_until_eof (parser);
+ c_parser_consume_token (parser);
+ }
}
static inline void c_parser_pragma_omp_critical (c_parser *);
@@ -5388,15 +5480,20 @@
if (c_parser_next_token_is_not (parser, CPP_NAME))
{
c_parser_error (parser, "identifier expected");
- c_parser_skip_until_found (parser, CPP_EOF, "EOF expected");
- return;
+ goto error;
}
printf ("region-phrase: %s\n",
IDENTIFIER_POINTER (c_parser_peek_token (parser)->value));
c_parser_consume_token (parser);
c_parser_require (parser, CPP_CLOSE_PAREN, "')' expected");
}
- c_parser_skip_until_found (parser, CPP_EOF, "EOF expected");
+
+ if (!c_parser_require (parser, CPP_EOF, "EOF expected"))
+ {
+ error:
+ c_parser_skip_until_eof (parser);
+ c_parser_consume_token (parser);
+ }
}
static inline void c_parser_pragma_omp_flush (c_parser *);
@@ -5412,7 +5509,12 @@
c_parser_pragma_omp_variable_list (parser);
c_parser_require (parser, CPP_CLOSE_PAREN, "')' expected");
}
- c_parser_skip_until_found (parser, CPP_EOF, "EOF expected");
+
+ if (!c_parser_require (parser, CPP_EOF, "EOF expected"))
+ {
+ c_parser_skip_until_eof (parser);
+ c_parser_consume_token (parser);
+ }
}
static inline void c_parser_pragma_omp_for (c_parser *);
@@ -5449,8 +5551,9 @@
c_parser_pragma_omp_clause_schedule (parser);
break;
default:
- c_parser_skip_until_found (parser, CPP_EOF, "unknown clause");
- return;
+ c_parser_error (parser, "unknown clause");
+ c_parser_skip_until_eof (parser);
+ break;
}
}
c_parser_consume_token (parser);
@@ -5462,7 +5565,12 @@
c_parser_pragma_omp_master (c_parser *parser)
{
printf ("#pragma omp master\n");
- c_parser_skip_until_found (parser, CPP_EOF, "EOF expected");
+
+ if (!c_parser_require (parser, CPP_EOF, "EOF expected"))
+ {
+ c_parser_skip_until_eof (parser);
+ c_parser_consume_token (parser);
+ }
}
static inline void c_parser_pragma_omp_ordered (c_parser *);
@@ -5471,7 +5579,12 @@
c_parser_pragma_omp_ordered (c_parser *parser)
{
printf ("#pragma omp ordered\n");
- c_parser_skip_until_found (parser, CPP_EOF, "EOF expected");
+
+ if (!c_parser_require (parser, CPP_EOF, "EOF expected"))
+ {
+ c_parser_skip_until_eof (parser);
+ c_parser_consume_token (parser);
+ }
}
static inline void c_parser_pragma_omp_parallel (c_parser *);
@@ -5498,6 +5611,9 @@
case PRAGMA_OMP_CLAUSE_IF:
c_parser_pragma_omp_clause_if (parser);
break;
+ case PRAGMA_OMP_CLAUSE_NUM_THREADS:
+ c_parser_pragma_omp_clause_num_threads (parser);
+ break;
case PRAGMA_OMP_CLAUSE_PRIVATE:
c_parser_pragma_omp_clause_private (parser);
break;
@@ -5508,8 +5624,9 @@
c_parser_pragma_omp_clause_shared (parser);
break;
default:
- c_parser_skip_until_found (parser, CPP_EOF, "unknown clause");
- return;
+ c_parser_error (parser, "unknown clause");
+ c_parser_skip_until_eof (parser);
+ break;
}
}
c_parser_consume_token (parser);
@@ -5542,6 +5659,9 @@
case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
c_parser_pragma_omp_clause_lastprivate (parser);
break;
+ case PRAGMA_OMP_CLAUSE_NUM_THREADS:
+ c_parser_pragma_omp_clause_num_threads (parser);
+ break;
case PRAGMA_OMP_CLAUSE_ORDERED:
c_parser_pragma_omp_clause_ordered (parser);
break;
@@ -5558,8 +5678,9 @@
c_parser_pragma_omp_clause_shared (parser);
break;
default:
- c_parser_skip_until_found (parser, CPP_EOF, "unknown clause");
- return;
+ c_parser_error (parser, "unknown clause");
+ c_parser_skip_until_eof (parser);
+ break;
}
}
c_parser_consume_token (parser);
@@ -5602,8 +5723,9 @@
c_parser_pragma_omp_clause_shared (parser);
break;
default:
- c_parser_skip_until_found (parser, CPP_EOF, "unknown clause");
- return;
+ c_parser_error (parser, "unknown clause");
+ c_parser_skip_until_eof (parser);
+ break;
}
}
c_parser_consume_token (parser);
@@ -5615,7 +5737,12 @@
c_parser_pragma_omp_section (c_parser *parser)
{
printf ("#pragma omp section\n");
- c_parser_skip_until_found (parser, CPP_EOF, "EOF expected");
+
+ if (!c_parser_require (parser, CPP_EOF, "EOF expected"))
+ {
+ c_parser_skip_until_eof (parser);
+ c_parser_consume_token (parser);
+ }
}
static inline void c_parser_pragma_omp_sections (c_parser *);
@@ -5646,8 +5773,9 @@
c_parser_pragma_omp_clause_reduction (parser);
break;
default:
- c_parser_skip_until_found (parser, CPP_EOF, "unknown clause");
- return;
+ c_parser_error (parser, "unknown clause");
+ c_parser_skip_until_eof (parser);
+ break;
}
}
c_parser_consume_token (parser);
@@ -5675,8 +5803,9 @@
c_parser_pragma_omp_clause_private (parser);
break;
default:
- c_parser_skip_until_found (parser, CPP_EOF, "unknown clause");
- return;
+ c_parser_error (parser, "unknown clause");
+ c_parser_skip_until_eof (parser);
+ break;
}
}
c_parser_consume_token (parser);
@@ -5694,10 +5823,15 @@
c_parser_pragma_omp_variable_list (parser);
c_parser_require (parser, CPP_CLOSE_PAREN, "')' expected");
}
- c_parser_skip_until_found (parser, CPP_EOF, "EOF expected");
+
+ if (!c_parser_require (parser, CPP_EOF, "EOF expected"))
+ {
+ c_parser_skip_until_eof (parser);
+ c_parser_consume_token (parser);
+ }
}
-/* Consume and handle a pragma token.
+/* Consume and handle a CPP_PRAGMA token.
Taken from gcc/cp/parser.c: cp_lexer_handle_pragma (). */
static void
@@ -5725,7 +5859,8 @@
static GTY (()) c_parser *the_parser;
-/* Handle pragma omp. */
+/* Handle OpenMP pragmas.
+ Calls appropriate handlers. The pragma must be valid. */
static void c_parser_handle_pragma_omp (cpp_reader *);
@@ -5769,7 +5904,7 @@
c_parser_pragma_omp_parallel_for (the_parser);
}
else if (c_parser_next_token_is (the_parser, CPP_NAME)
- && !strcmp ("sections", TREE_STRING_POINTER
+ && !strcmp ("sections", IDENTIFIER_POINTER
(c_parser_peek_token (the_parser)->value)))
{
c_parser_consume_token (the_parser);
@@ -5777,7 +5912,6 @@
}
else
{
- c_parser_consume_token (the_parser);
c_parser_pragma_omp_parallel (the_parser);
}
break;
--- orig/libcpp/lex.c
+++ mod/libcpp/lex.c
@@ -742,7 +742,8 @@
else
{
result = &pfile->directive_result;
- break;
+ /* Need to change line. */
+ //break;
}
}