asremove.c revision 1.1.1.2 1
2 /******************************************************************************
3 *
4 * Module Name: asremove - Source conversion - removal functions
5 *
6 *****************************************************************************/
7
8 /*
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
30 *
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
43 */
44
45 #include "acpisrc.h"
46
47 /* Local prototypes */
48
49 void
50 AsRemoveStatement (
51 char *Buffer,
52 char *Keyword,
53 UINT32 Type);
54
55
56 /******************************************************************************
57 *
58 * FUNCTION: AsRemoveStatement
59 *
60 * DESCRIPTION: Remove all statements that contain the given keyword.
61 * Limitations: Removes text from the start of the line that
62 * contains the keyword to the next semicolon. Currently
63 * doesn't ignore comments.
64 *
65 ******************************************************************************/
66
67 void
68 AsRemoveStatement (
69 char *Buffer,
70 char *Keyword,
71 UINT32 Type)
72 {
73 char *SubString;
74 char *SubBuffer;
75 int KeywordLength;
76
77
78 KeywordLength = strlen (Keyword);
79 SubBuffer = Buffer;
80 SubString = Buffer;
81
82
83 while (SubString)
84 {
85 SubString = strstr (SubBuffer, Keyword);
86
87 if (SubString)
88 {
89 SubBuffer = SubString;
90
91 if ((Type == REPLACE_WHOLE_WORD) &&
92 (!AsMatchExactWord (SubString, KeywordLength)))
93 {
94 SubBuffer++;
95 continue;
96 }
97
98 /* Find start of this line */
99
100 while (*SubString != '\n')
101 {
102 SubString--;
103 }
104 SubString++;
105
106 /* Find end of this statement */
107
108 SubBuffer = AsSkipPastChar (SubBuffer, ';');
109 if (!SubBuffer)
110 {
111 return;
112 }
113
114 /* Find end of this line */
115
116 SubBuffer = AsSkipPastChar (SubBuffer, '\n');
117 if (!SubBuffer)
118 {
119 return;
120 }
121
122 /* If next line is blank, remove it too */
123
124 if (*SubBuffer == '\n')
125 {
126 SubBuffer++;
127 }
128
129 /* Remove the lines */
130
131 SubBuffer = AsRemoveData (SubString, SubBuffer);
132 }
133 }
134 }
135
136
137 /******************************************************************************
138 *
139 * FUNCTION: AsRemoveConditionalCompile
140 *
141 * DESCRIPTION: Remove a "#ifdef" statement, and all text that it encompasses.
142 * Limitations: cannot handle nested ifdefs.
143 *
144 ******************************************************************************/
145
146 void
147 AsRemoveConditionalCompile (
148 char *Buffer,
149 char *Keyword)
150 {
151 char *SubString;
152 char *SubBuffer;
153 char *IfPtr;
154 char *EndifPtr;
155 char *ElsePtr;
156 char *Comment;
157 int KeywordLength;
158
159
160 KeywordLength = strlen (Keyword);
161 SubBuffer = Buffer;
162 SubString = Buffer;
163
164
165 while (SubString)
166 {
167 SubBuffer = strstr (SubString, Keyword);
168 if (!SubBuffer)
169 {
170 return;
171 }
172
173 /*
174 * Check for translation escape string -- means to ignore
175 * blocks of code while replacing
176 */
177 Comment = strstr (SubString, AS_START_IGNORE);
178
179 if ((Comment) &&
180 (Comment < SubBuffer))
181 {
182 SubString = strstr (Comment, AS_STOP_IGNORE);
183 if (!SubString)
184 {
185 return;
186 }
187
188 SubString += 3;
189 continue;
190 }
191
192 /* Check for ordinary comment */
193
194 Comment = strstr (SubString, "/*");
195
196 if ((Comment) &&
197 (Comment < SubBuffer))
198 {
199 SubString = strstr (Comment, "*/");
200 if (!SubString)
201 {
202 return;
203 }
204
205 SubString += 2;
206 continue;
207 }
208
209 SubString = SubBuffer;
210 if (!AsMatchExactWord (SubString, KeywordLength))
211 {
212 SubString++;
213 continue;
214 }
215
216 /* Find start of this line */
217
218 while (*SubString != '\n' && (SubString > Buffer))
219 {
220 SubString--;
221 }
222 SubString++;
223
224 /* Find the "#ifxxxx" */
225
226 IfPtr = strstr (SubString, "#if");
227 if (!IfPtr)
228 {
229 return;
230 }
231
232 if (IfPtr > SubBuffer)
233 {
234 /* Not the right #if */
235
236 SubString = SubBuffer + strlen (Keyword);
237 continue;
238 }
239
240 /* Find closing #endif or #else */
241
242 EndifPtr = strstr (SubBuffer, "#endif");
243 if (!EndifPtr)
244 {
245 /* There has to be an #endif */
246
247 return;
248 }
249
250 ElsePtr = strstr (SubBuffer, "#else");
251 if ((ElsePtr) &&
252 (EndifPtr > ElsePtr))
253 {
254 /* This #ifdef contains an #else clause */
255 /* Find end of this line */
256
257 SubBuffer = AsSkipPastChar (ElsePtr, '\n');
258 if (!SubBuffer)
259 {
260 return;
261 }
262
263 /* Remove the #ifdef .... #else code */
264
265 AsRemoveData (SubString, SubBuffer);
266
267 /* Next, we will remove the #endif statement */
268
269 EndifPtr = strstr (SubString, "#endif");
270 if (!EndifPtr)
271 {
272 /* There has to be an #endif */
273
274 return;
275 }
276
277 SubString = EndifPtr;
278 }
279
280 /* Remove the ... #endif part */
281 /* Find end of this line */
282
283 SubBuffer = AsSkipPastChar (EndifPtr, '\n');
284 if (!SubBuffer)
285 {
286 return;
287 }
288
289 /* Remove the lines */
290
291 SubBuffer = AsRemoveData (SubString, SubBuffer);
292 }
293 }
294
295
296 /******************************************************************************
297 *
298 * FUNCTION: AsRemoveMacro
299 *
300 * DESCRIPTION: Remove every line that contains the keyword. Does not
301 * skip comments.
302 *
303 ******************************************************************************/
304
305 void
306 AsRemoveMacro (
307 char *Buffer,
308 char *Keyword)
309 {
310 char *SubString;
311 char *SubBuffer;
312 int NestLevel;
313
314
315 SubBuffer = Buffer;
316 SubString = Buffer;
317
318
319 while (SubString)
320 {
321 SubString = strstr (SubBuffer, Keyword);
322
323 if (SubString)
324 {
325 SubBuffer = SubString;
326
327 /* Find start of the macro parameters */
328
329 while (*SubString != '(')
330 {
331 SubString++;
332 }
333 SubString++;
334
335 /* Remove the macro name and opening paren */
336
337 SubString = AsRemoveData (SubBuffer, SubString);
338
339 NestLevel = 1;
340 while (*SubString)
341 {
342 if (*SubString == '(')
343 {
344 NestLevel++;
345 }
346 else if (*SubString == ')')
347 {
348 NestLevel--;
349 }
350
351 SubString++;
352
353 if (NestLevel == 0)
354 {
355 break;
356 }
357 }
358
359 /* Remove the closing paren */
360
361 SubBuffer = AsRemoveData (SubString-1, SubString);
362 }
363 }
364 }
365
366
367 /******************************************************************************
368 *
369 * FUNCTION: AsRemoveLine
370 *
371 * DESCRIPTION: Remove every line that contains the keyword. Does not
372 * skip comments.
373 *
374 ******************************************************************************/
375
376 void
377 AsRemoveLine (
378 char *Buffer,
379 char *Keyword)
380 {
381 char *SubString;
382 char *SubBuffer;
383
384
385 SubBuffer = Buffer;
386 SubString = Buffer;
387
388
389 while (SubString)
390 {
391 SubString = strstr (SubBuffer, Keyword);
392
393 if (SubString)
394 {
395 SubBuffer = SubString;
396
397 /* Find start of this line */
398
399 while (*SubString != '\n')
400 {
401 SubString--;
402 }
403 SubString++;
404
405 /* Find end of this line */
406
407 SubBuffer = AsSkipPastChar (SubBuffer, '\n');
408 if (!SubBuffer)
409 {
410 return;
411 }
412
413 /* Remove the line */
414
415 SubBuffer = AsRemoveData (SubString, SubBuffer);
416 }
417 }
418 }
419
420
421 /******************************************************************************
422 *
423 * FUNCTION: AsReduceTypedefs
424 *
425 * DESCRIPTION: Eliminate certain typedefs
426 *
427 ******************************************************************************/
428
429 void
430 AsReduceTypedefs (
431 char *Buffer,
432 char *Keyword)
433 {
434 char *SubString;
435 char *SubBuffer;
436 int NestLevel;
437
438
439 SubBuffer = Buffer;
440 SubString = Buffer;
441
442
443 while (SubString)
444 {
445 SubString = strstr (SubBuffer, Keyword);
446
447 if (SubString)
448 {
449 /* Remove the typedef itself */
450
451 SubBuffer = SubString + strlen ("typedef") + 1;
452 SubBuffer = AsRemoveData (SubString, SubBuffer);
453
454 /* Find the opening brace of the struct or union */
455
456 while (*SubString != '{')
457 {
458 SubString++;
459 }
460 SubString++;
461
462 /* Find the closing brace. Handles nested braces */
463
464 NestLevel = 1;
465 while (*SubString)
466 {
467 if (*SubString == '{')
468 {
469 NestLevel++;
470 }
471 else if (*SubString == '}')
472 {
473 NestLevel--;
474 }
475
476 SubString++;
477
478 if (NestLevel == 0)
479 {
480 break;
481 }
482 }
483
484 /* Remove an extra line feed if present */
485
486 if (!strncmp (SubString - 3, "\n\n", 2))
487 {
488 *(SubString -2) = '}';
489 SubString--;
490 }
491
492 /* Find the end of the typedef name */
493
494 SubBuffer = AsSkipUntilChar (SubString, ';');
495
496 /* And remove the typedef name */
497
498 SubBuffer = AsRemoveData (SubString, SubBuffer);
499 }
500 }
501 }
502
503
504 /******************************************************************************
505 *
506 * FUNCTION: AsRemoveEmptyBlocks
507 *
508 * DESCRIPTION: Remove any C blocks (e.g., if {}) that contain no code. This
509 * can happen as a result of removing lines such as DEBUG_PRINT.
510 *
511 ******************************************************************************/
512
513 void
514 AsRemoveEmptyBlocks (
515 char *Buffer,
516 char *Filename)
517 {
518 char *SubBuffer;
519 char *BlockStart;
520 BOOLEAN EmptyBlock = TRUE;
521 BOOLEAN AnotherPassRequired = TRUE;
522 UINT32 BlockCount = 0;
523
524
525 while (AnotherPassRequired)
526 {
527 SubBuffer = Buffer;
528 AnotherPassRequired = FALSE;
529
530 while (*SubBuffer)
531 {
532 if (*SubBuffer == '{')
533 {
534 BlockStart = SubBuffer;
535 EmptyBlock = TRUE;
536
537 SubBuffer++;
538 while (*SubBuffer != '}')
539 {
540 if ((*SubBuffer != ' ') &&
541 (*SubBuffer != '\n'))
542 {
543 EmptyBlock = FALSE;
544 break;
545 }
546 SubBuffer++;
547 }
548
549 if (EmptyBlock)
550 {
551 /* Find start of the first line of the block */
552
553 while (*BlockStart != '\n')
554 {
555 BlockStart--;
556 }
557
558 /* Find end of the last line of the block */
559
560 SubBuffer = AsSkipUntilChar (SubBuffer, '\n');
561 if (!SubBuffer)
562 {
563 break;
564 }
565
566 /* Remove the block */
567
568 SubBuffer = AsRemoveData (BlockStart, SubBuffer);
569 BlockCount++;
570 AnotherPassRequired = TRUE;
571 continue;
572 }
573 }
574
575 SubBuffer++;
576 }
577 }
578
579 if (BlockCount)
580 {
581 Gbl_MadeChanges = TRUE;
582 AsPrint ("Code blocks deleted", BlockCount, Filename);
583 }
584 }
585
586
587 /******************************************************************************
588 *
589 * FUNCTION: AsRemoveDebugMacros
590 *
591 * DESCRIPTION: Remove all "Debug" macros -- macros that produce debug output.
592 *
593 ******************************************************************************/
594
595 void
596 AsRemoveDebugMacros (
597 char *Buffer)
598 {
599 AsRemoveConditionalCompile (Buffer, "ACPI_DEBUG_OUTPUT");
600
601 AsRemoveStatement (Buffer, "ACPI_DEBUG_PRINT", REPLACE_WHOLE_WORD);
602 AsRemoveStatement (Buffer, "ACPI_DEBUG_PRINT_RAW", REPLACE_WHOLE_WORD);
603 AsRemoveStatement (Buffer, "DEBUG_EXEC", REPLACE_WHOLE_WORD);
604 AsRemoveStatement (Buffer, "FUNCTION_ENTRY", REPLACE_WHOLE_WORD);
605 AsRemoveStatement (Buffer, "PROC_NAME", REPLACE_WHOLE_WORD);
606 AsRemoveStatement (Buffer, "FUNCTION_TRACE", REPLACE_SUBSTRINGS);
607 AsRemoveStatement (Buffer, "DUMP_", REPLACE_SUBSTRINGS);
608
609 AsReplaceString ("return_VOID", "return", REPLACE_WHOLE_WORD, Buffer);
610 AsReplaceString ("return_PTR", "return", REPLACE_WHOLE_WORD, Buffer);
611 AsReplaceString ("return_ACPI_STATUS", "return", REPLACE_WHOLE_WORD, Buffer);
612 AsReplaceString ("return_acpi_status", "return", REPLACE_WHOLE_WORD, Buffer);
613 AsReplaceString ("return_VALUE", "return", REPLACE_WHOLE_WORD, Buffer);
614 }
615
616
617