1 /******************************************************************************
3 * Module Name: asremove - Source conversion - removal functions
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
46 /* Local prototypes */
55 /******************************************************************************
57 * FUNCTION: AsRemoveStatement
59 * DESCRIPTION: Remove all statements that contain the given keyword.
60 * Limitations: Removes text from the start of the line that
61 * contains the keyword to the next semicolon. Currently
62 * doesn't ignore comments.
64 ******************************************************************************/
77 KeywordLength = strlen (Keyword);
84 SubString = strstr (SubBuffer, Keyword);
88 SubBuffer = SubString;
90 if ((Type == REPLACE_WHOLE_WORD) &&
91 (!AsMatchExactWord (SubString, KeywordLength)))
97 /* Find start of this line */
99 while (*SubString != '\n')
105 /* Find end of this statement */
107 SubBuffer = AsSkipPastChar (SubBuffer, ';');
113 /* Find end of this line */
115 SubBuffer = AsSkipPastChar (SubBuffer, '\n');
121 /* If next line is blank, remove it too */
123 if (*SubBuffer == '\n')
128 /* Remove the lines */
130 SubBuffer = AsRemoveData (SubString, SubBuffer);
136 /******************************************************************************
138 * FUNCTION: AsRemoveConditionalCompile
140 * DESCRIPTION: Remove a "#ifdef" statement, and all text that it encompasses.
141 * Limitations: cannot handle nested ifdefs.
143 ******************************************************************************/
146 AsRemoveConditionalCompile (
159 KeywordLength = strlen (Keyword);
166 SubBuffer = strstr (SubString, Keyword);
173 * Check for translation escape string -- means to ignore
174 * blocks of code while replacing
176 if (Gbl_IgnoreTranslationEscapes)
182 Comment = strstr (SubString, AS_START_IGNORE);
186 (Comment < SubBuffer))
188 SubString = strstr (Comment, AS_STOP_IGNORE);
198 /* Check for ordinary comment */
200 Comment = strstr (SubString, "/*");
203 (Comment < SubBuffer))
205 SubString = strstr (Comment, "*/");
215 SubString = SubBuffer;
216 if (!AsMatchExactWord (SubString, KeywordLength))
222 /* Find start of this line */
224 while (*SubString != '\n' && (SubString > Buffer))
230 /* Find the "#ifxxxx" */
232 IfPtr = strstr (SubString, "#if");
238 if (IfPtr > SubBuffer)
240 /* Not the right #if */
242 SubString = SubBuffer + strlen (Keyword);
246 /* Find closing #endif or #else */
248 EndifPtr = strstr (SubBuffer, "#endif");
251 /* There has to be an #endif */
256 ElsePtr = strstr (SubBuffer, "#else");
258 (EndifPtr > ElsePtr))
260 /* This #ifdef contains an #else clause */
261 /* Find end of this line */
263 SubBuffer = AsSkipPastChar (ElsePtr, '\n');
269 /* Remove the #ifdef .... #else code */
271 AsRemoveData (SubString, SubBuffer);
273 /* Next, we will remove the #endif statement */
275 EndifPtr = strstr (SubString, "#endif");
278 /* There has to be an #endif */
283 SubString = EndifPtr;
286 /* Remove the ... #endif part */
287 /* Find end of this line */
289 SubBuffer = AsSkipPastChar (EndifPtr, '\n');
295 /* Remove the lines */
297 SubBuffer = AsRemoveData (SubString, SubBuffer);
302 /******************************************************************************
304 * FUNCTION: AsRemoveMacro
306 * DESCRIPTION: Remove every line that contains the keyword. Does not
309 ******************************************************************************/
327 SubString = strstr (SubBuffer, Keyword);
331 SubBuffer = SubString;
333 /* Find start of the macro parameters */
335 while (*SubString != '(')
341 /* Remove the macro name and opening paren */
343 SubString = AsRemoveData (SubBuffer, SubString);
348 if (*SubString == '(')
352 else if (*SubString == ')')
365 /* Remove the closing paren */
367 SubBuffer = AsRemoveData (SubString-1, SubString);
373 /******************************************************************************
375 * FUNCTION: AsRemoveLine
377 * DESCRIPTION: Remove every line that contains the keyword. Does not
380 ******************************************************************************/
397 SubString = strstr (SubBuffer, Keyword);
401 SubBuffer = SubString;
403 /* Find start of this line */
405 while (*SubString != '\n')
411 /* Find end of this line */
413 SubBuffer = AsSkipPastChar (SubBuffer, '\n');
419 /* Remove the line */
421 SubBuffer = AsRemoveData (SubString, SubBuffer);
427 /******************************************************************************
429 * FUNCTION: AsReduceTypedefs
431 * DESCRIPTION: Eliminate certain typedefs
433 ******************************************************************************/
451 SubString = strstr (SubBuffer, Keyword);
455 /* Remove the typedef itself */
457 SubBuffer = SubString + strlen ("typedef") + 1;
458 SubBuffer = AsRemoveData (SubString, SubBuffer);
460 /* Find the opening brace of the struct or union */
462 while (*SubString != '{')
468 /* Find the closing brace. Handles nested braces */
473 if (*SubString == '{')
477 else if (*SubString == '}')
490 /* Remove an extra line feed if present */
492 if (!strncmp (SubString - 3, "\n\n", 2))
494 *(SubString -2) = '}';
498 /* Find the end of the typedef name */
500 SubBuffer = AsSkipUntilChar (SubString, ';');
502 /* And remove the typedef name */
504 SubBuffer = AsRemoveData (SubString, SubBuffer);
510 /******************************************************************************
512 * FUNCTION: AsRemoveEmptyBlocks
514 * DESCRIPTION: Remove any C blocks (e.g., if {}) that contain no code. This
515 * can happen as a result of removing lines such as DEBUG_PRINT.
517 ******************************************************************************/
520 AsRemoveEmptyBlocks (
526 BOOLEAN EmptyBlock = TRUE;
527 BOOLEAN AnotherPassRequired = TRUE;
528 UINT32 BlockCount = 0;
531 while (AnotherPassRequired)
534 AnotherPassRequired = FALSE;
538 if (*SubBuffer == '{')
540 BlockStart = SubBuffer;
544 while (*SubBuffer != '}')
546 if ((*SubBuffer != ' ') &&
547 (*SubBuffer != '\n'))
557 /* Find start of the first line of the block */
559 while (*BlockStart != '\n')
564 /* Find end of the last line of the block */
566 SubBuffer = AsSkipUntilChar (SubBuffer, '\n');
572 /* Remove the block */
574 SubBuffer = AsRemoveData (BlockStart, SubBuffer);
576 AnotherPassRequired = TRUE;
587 Gbl_MadeChanges = TRUE;
588 AsPrint ("Code blocks deleted", BlockCount, Filename);
593 /******************************************************************************
595 * FUNCTION: AsRemoveDebugMacros
597 * DESCRIPTION: Remove all "Debug" macros -- macros that produce debug output.
599 ******************************************************************************/
602 AsRemoveDebugMacros (
605 AsRemoveConditionalCompile (Buffer, "ACPI_DEBUG_OUTPUT");
607 AsRemoveStatement (Buffer, "ACPI_DEBUG_PRINT", REPLACE_WHOLE_WORD);
608 AsRemoveStatement (Buffer, "ACPI_DEBUG_PRINT_RAW", REPLACE_WHOLE_WORD);
609 AsRemoveStatement (Buffer, "DEBUG_EXEC", REPLACE_WHOLE_WORD);
610 AsRemoveStatement (Buffer, "FUNCTION_ENTRY", REPLACE_WHOLE_WORD);
611 AsRemoveStatement (Buffer, "PROC_NAME", REPLACE_WHOLE_WORD);
612 AsRemoveStatement (Buffer, "FUNCTION_TRACE", REPLACE_SUBSTRINGS);
613 AsRemoveStatement (Buffer, "DUMP_", REPLACE_SUBSTRINGS);
615 AsReplaceString ("return_VOID", "return", REPLACE_WHOLE_WORD, Buffer);
616 AsReplaceString ("return_PTR", "return", REPLACE_WHOLE_WORD, Buffer);
617 AsReplaceString ("return_ACPI_STATUS", "return", REPLACE_WHOLE_WORD, Buffer);
618 AsReplaceString ("return_acpi_status", "return", REPLACE_WHOLE_WORD, Buffer);
619 AsReplaceString ("return_VALUE", "return", REPLACE_WHOLE_WORD, Buffer);
623 /******************************************************************************
625 * FUNCTION: AsCleanupSpecialMacro
627 * DESCRIPTION: For special macro invocations (invoked without ";" at the end
628 * of the lines), do the following:
629 * 1. Remove spaces appended by indent at the beginning of lines.
630 * 2. Add an empty line between two special macro invocations.
632 ******************************************************************************/
635 AsCleanupSpecialMacro (
651 SubString = strstr (SubBuffer, Keyword);
655 /* Find start of the macro parameters */
657 while (*SubString != '(')
666 if (*SubString == '(')
670 else if (*SubString == ')')
685 /* Find end of the line */
688 while (!NewLine && *SubString)
690 if (*SubString == '\n' && *(SubString - 1) != '\\')
697 /* Find end of the line */
699 if (*SubString == '#' || *SubString == '\n')
704 SubBuffer = SubString;
706 /* Find start of the non-space */
708 while (*SubString == ' ')
713 /* Find end of the line */
715 if (*SubString == '#' || *SubString == '\n')
720 /* Find end of the line */
722 if (*SubString == '/' || *SubString == '*')
724 CommentEnd = strstr (SubString, "*/");
727 SubString = CommentEnd + 2;
732 SubString = AsRemoveData (SubBuffer, SubString);