PIPS
comment2pragma.l
Go to the documentation of this file.
1 %{
2 #include "defines-local.h"
3 
4 static statement current_statement = statement_undefined;
5 
6 static char *directive_txt = NULL;
7 static char *comment_txt = NULL;
8 
9 static void directive_start(int type);
10 static void directive_reset(void);
11 static void append(char **buffer);
12 static void clean_buffer(char **buffer);
13 static void end_directive(void);
14 static void end_comment(void);
15 
16 static int directive_syntaxe = -1;
17 static int directive_type = -1;
18 
19 #define DIRECTIVE_STEP 0
20 #define DIRECTIVE_OMP 1
21 
22 %}
23 %option warn
24 %option noyywrap
25 %option nounput
26 %option noinput
27 
28  /*
29  Attention PIPS supprime les blancs en début de commentaire en syntaxe libre, et insÚre un saut de ligne supplémentaire :
30  | !txt1
31  | !txt2
32  devient :
33  |!txt1
34  |
35  |!txt2
36  |
37  */
38 
39 comment_fixe [cC*!]
40 comment_libre [:blank:]+!
41 
42 continuation_fixe [^[:blank:]0]
43 continuation_libre ([:blank:]*&[:blank:]*)?
44 
45 sentinelle_step $(?i:step)
46 sentinelle_omp $(?i:omp)
47 sentinelle {sentinelle_step}|{sentinelle_omp}
48 
49 
50 %x comment_fixe directive_fixe directive_fixe_comment directive_fixe_continuation
51 %x comment_libre directive_libre directive_libre_comment directive_libre_continuation
52 %x other_line
53 
54 %%
55 
56  /*
57  Directive à syntaxe libre
58  */
59 <directive_libre>[^&!\n]* {
60  append(&directive_txt); // ajout du texte de la directive
61  BEGIN(directive_libre_comment);
62  }
63 
64 <directive_libre_comment>&[^!\n]* { // détection d'une continuation du texte de la directive sur la ligne suivante
65  BEGIN(directive_libre_continuation);
66  }
67 
68 <directive_libre_continuation>{ // controle de la continuation
69  \n{comment_libre}{sentinelle_omp}{continuation_libre} {
70  if (directive_type != DIRECTIVE_OMP)
71  yy_fatal_error("erreur continuation omp libre\n");
72  BEGIN(directive_libre);
73  }
74  \n{comment_libre}{sentinelle_step}{continuation_libre} {
75  if (directive_type != DIRECTIVE_STEP)
76  yy_fatal_error("erreur continuation omp libre\n");
77  BEGIN(directive_libre);
78  }
79  }
80 
81  /*
82  Directive à syntaxe fixe
83  */
84 <directive_fixe>[^!\n]* {
85  append(&directive_txt); // ajout du texte de la directive
86  BEGIN(directive_fixe_comment);
87  }
88 
89 <directive_fixe_comment>\n{comment_fixe}/({sentinelle}{continuation_fixe}) { // détection d'une continuation du texte de la directive sur la ligne suivante
90  BEGIN(directive_fixe_continuation);
91  }
92 
93 <directive_fixe_continuation>{ // controle de la continuation
94  {sentinelle_omp}{continuation_fixe} {
95  if (directive_type != DIRECTIVE_OMP)
96  yy_fatal_error("erreur continuation omp fixe\n");
97  BEGIN(directive_fixe);
98  }
99  {sentinelle_step}{continuation_fixe} {
100  if (directive_type != DIRECTIVE_STEP)
101  yy_fatal_error("erreur continuation step fixe\n");
102  BEGIN(directive_fixe);
103  }
104  }
105 
106  /*
107  Collecte d'un commentaire porté par une directive
108  */
109 <directive_fixe_comment,directive_libre_comment,directive_libre_continuation>!.* {
110  append(&comment_txt);
111  }
112 
113  /*
114  Détection de la fin d'une directive et de son commentaire associé éventuel
115  */
116 <directive_fixe_comment,directive_libre_comment>\n {
117  append(&comment_txt);
118 
119  end_directive();
120 
121  directive_reset();
122  BEGIN(INITIAL);
123  }
124 
125  /*
126  Identification d'une nouvelle directive
127  */
128 <comment_fixe,comment_libre>{
129  {sentinelle_omp}[[:blank:]0] {
130  directive_start(DIRECTIVE_OMP);
131  }
132  {sentinelle_step}[[:blank:]0] {
133  directive_start(DIRECTIVE_STEP);
134  }
135  }
136 
137  /*
138  Identification des commentaires et autres lignes
139  */
140 <comment_fixe,comment_libre>\n|([^$\n].*\n) {
141  append(&comment_txt);
142  end_comment();
143  clean_buffer(&comment_txt);
144  BEGIN(INITIAL);
145  }
146 <other_line>.*\n {
147  append(&comment_txt);
148  end_comment();
149  clean_buffer(&comment_txt);
150  BEGIN(INITIAL);
151  }
152 
153 
154 <INITIAL>{
155  ^{comment_fixe} {
156  BEGIN(comment_fixe);
157  yymore();
158  }
159  ^{comment_libre} {
160  BEGIN(comment_libre);
161  yymore();
162  }
163  ^\n {
164  append(&comment_txt);
165  end_comment();
166  clean_buffer(&comment_txt);
167  }
168  ^. {
169  BEGIN(other_line);
170  yymore();
171  }
172  }
173 
174 %%
175 
176 static void directive_start(int type)
177 {
178  directive_type = type;
179 
180  switch (YY_START)
181  {
182  case comment_fixe:
183  directive_syntaxe = directive_fixe;
184  BEGIN(directive_fixe);
185  break;
186  case comment_libre:
187  directive_syntaxe = directive_libre;
188  BEGIN(directive_libre);
189  break;
190  default:
191  yy_fatal_error("erreur syntaxe\n");
192  }
193 }
194 
195 static void directive_reset(void)
196 {
197  clean_buffer(&directive_txt);
198  clean_buffer(&comment_txt);
199  directive_syntaxe = -1;
200  directive_type = -1;
201 }
202 
203 static void append(char **buffer)
204 {
205  if (*buffer == NULL)
206  *buffer = strdup(yytext);
207  else
208  {
209  *buffer = realloc(*buffer, sizeof(*buffer) * (strlen(*buffer) + yyleng));
210  *buffer = strncat(*buffer, yytext, yyleng);
211  }
212 }
213 
214 static void clean_buffer(char **buffer)
215 {
216  if(*buffer != NULL)
217  {
218  free(*buffer);
219  *buffer = NULL;
220  }
221 }
222 
223 static void remove_blank(void)
224 {
225  char *dest;
226  char *src;
227 
228  dest = directive_txt;
229  for(src=directive_txt; *src !='\0'; src++)
230  if (*src != ' ' && *src != '\t')
231  {
232  *dest=*src;
233  dest++;
234  }
235  *dest = '\0';
236 }
237 
238 static char *get_directive_type(void)
239 {
240  switch (directive_type)
241  {
242  case DIRECTIVE_STEP:
243  return strdup("step");
244  break;
245  case DIRECTIVE_OMP:
246  return strdup("omp");
247  break;
248  default:
249  return NULL;
250  break;
251  }
252 }
253 
254 
255 static void end_comment(void)
256 {
257  if (!statement_undefined_p(current_statement))
258  {
259  pips_debug(2,"COMMENTAIRE_TXT-> \"%s\"\n", comment_txt);
260  append_comments_to_statement(current_statement, comment_txt);
261  }
262  else
263  {
264  ifdebug(1)
265  {
266  if (comment_txt !=NULL)
267  pips_debug(1,"%s :%s",(YY_START==other_line)?"OTHER_LINE":"COMMENT", comment_txt);
268  else
269  pips_debug(1,"\n");
270  }
271  }
272 }
273 
274 static void end_directive(void)
275 {
276  string directive_type = get_directive_type();
277 
278  // suppression du saut de ligne
279  comment_txt[strlen(comment_txt)-1]='\0';
280 
281  if (directive_syntaxe == directive_fixe)
282  remove_blank();
283 
284  pips_debug(2,"DIRECTIVE-> %s \"%s\"\n", directive_type, directive_txt);
285  pips_debug(2,"\tCOMMENTAIRE_DIR-> \"%s\"\n", comment_txt);
286 
287  if(!statement_undefined_p(current_statement))
288  {
289  extensions_extension(statement_extensions(current_statement))=gen_nreverse(extensions_extension(statement_extensions(current_statement)));
290  add_pragma_str_to_statement(current_statement, strdup(concatenate(directive_type,directive_txt,NULL)), false);
291  extensions_extension(statement_extensions(current_statement))=gen_nreverse(extensions_extension(statement_extensions(current_statement)));
292 
293  if (strlen(comment_txt))
294  {
295  if (!empty_comments_p(statement_comments(current_statement)))
296  {
297  free(statement_comments(current_statement));
298  statement_comments(current_statement) = string_undefined;
299  }
300  append_comments_to_statement(current_statement, comment_txt);
301  }
302  }
303  else
304  {
305  pips_debug(1, "DIRECTIVE in decls_txt\n");
306  }
307 
308  free(directive_type);
309 }
310 
311 /*
312  * For fortran programs
313  * converting comments into pragmas
314  *
315 */
316 
317 void step_comment2pragma_handle(statement stmt)
318 {
319  pips_debug(1,"begin\n");
320  if(!statement_undefined_p(stmt))
321  {
322  string comment = statement_comments(stmt);
323 
324  if (!empty_comments_p(comment))
325  {
326  pips_assert("statement undefined", statement_undefined_p(current_statement));
327  current_statement = stmt;
328  statement_comments(stmt) = string_undefined;
329 
330  pips_debug(1,"Comment txt : begin scan\n");
331  yy_scan_string(comment);
332  yylex();
333  pips_debug(1,"Comment txt : end scan\n");
334 
335  free(comment);
336  current_statement = statement_undefined;
337  }
338  else
339  {
340  pips_debug(1,"Comment txt : Empty\n");
341  }
342  }
343  else
344  {
345  string decls_txt = code_decls_text(entity_code(get_current_module_entity()));
346  if (!empty_comments_p(decls_txt))
347  {
348  pips_debug(1,"Declaration txt : begin scan\n");
349  yy_scan_string(decls_txt);
350  yylex();
351  pips_debug(1,"Declaration txt : end scan\n");
352  }
353  else
354  {
355  pips_debug(1,"Declaration txt : Empty\n");
356  }
357  }
358  pips_debug(1,"end\n");
359 }