2 /******************************************************************************
4 * Module Name: asconvrt - Source conversion code
7 *****************************************************************************/
9 /******************************************************************************
13 * Some or all of this work - Copyright (c) 1999 - 2005, Intel Corp.
14 * All rights reserved.
18 * 2.1. This is your license from Intel Corp. under its intellectual property
19 * rights. You may have additional license terms from the party that provided
20 * you this software, covering your right to use that party's intellectual
23 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24 * copy of the source code appearing in this file ("Covered Code") an
25 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26 * base code distributed originally by Intel ("Original Intel Code") to copy,
27 * make derivatives, distribute, use and display any portion of the Covered
28 * Code in any form, with the right to sublicense such rights; and
30 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31 * license (with the right to sublicense), under only those claims of Intel
32 * patents that are infringed by the Original Intel Code, to make, use, sell,
33 * offer to sell, and import the Covered Code and derivative works thereof
34 * solely to the minimum extent necessary to exercise the above copyright
35 * license, and in no event shall the patent license extend to any additions
36 * to or modifications of the Original Intel Code. No other license or right
37 * is granted directly or by implication, estoppel or otherwise;
39 * The above copyright and patent license is granted only if the following
44 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45 * Redistribution of source code of any substantial portion of the Covered
46 * Code or modification with rights to further distribute source must include
47 * the above Copyright Notice, the above License, this list of Conditions,
48 * and the following Disclaimer and Export Compliance provision. In addition,
49 * Licensee must cause all Covered Code to which Licensee contributes to
50 * contain a file documenting the changes Licensee made to create that Covered
51 * Code and the date of any change. Licensee must include in that file the
52 * documentation of any changes made by any predecessor Licensee. Licensee
53 * must include a prominent statement that the modification is derived,
54 * directly or indirectly, from Original Intel Code.
56 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57 * Redistribution of source code of any substantial portion of the Covered
58 * Code or modification without rights to further distribute source must
59 * include the following Disclaimer and Export Compliance provision in the
60 * documentation and/or other materials provided with distribution. In
61 * addition, Licensee may not authorize further sublicense of source of any
62 * portion of the Covered Code, and must include terms to the effect that the
63 * license from Licensee to its licensee is limited to the intellectual
64 * property embodied in the software Licensee provides to its licensee, and
65 * not to intellectual property embodied in modifications its licensee may
68 * 3.3. Redistribution of Executable. Redistribution in executable form of any
69 * substantial portion of the Covered Code or modification must reproduce the
70 * above Copyright Notice, and the following Disclaimer and Export Compliance
71 * provision in the documentation and/or other materials provided with the
74 * 3.4. Intel retains all right, title, and interest in and to the Original
77 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78 * Intel shall be used in advertising or otherwise to promote the sale, use or
79 * other dealings in products derived from or relating to the Covered Code
80 * without prior written authorization from Intel.
82 * 4. Disclaimer and Export Compliance
84 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
87 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
88 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
89 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
92 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
98 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
101 * 4.3. Licensee shall not export, either directly or indirectly, any of this
102 * software or system incorporating such software without first obtaining any
103 * required license or other approval from the U. S. Department of Commerce or
104 * any other agency or department of the United States Government. In the
105 * event Licensee exports any such software from the United States or
106 * re-exports any such software from a foreign destination, Licensee shall
107 * ensure that the distribution and export/re-export of the software is in
108 * compliance with all laws, regulations, orders, or other restrictions of the
109 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110 * any of its subsidiaries will export/re-export any technical data, process,
111 * software, or service, directly or indirectly, to any country for which the
112 * United States government or any agency thereof requires an export license,
113 * other governmental approval, or letter of assurance, without first obtaining
114 * such license, approval or letter.
116 *****************************************************************************/
121 /* Opening signature of the Intel legal header */
123 char *HeaderBegin = "/******************************************************************************\n *\n * 1. Copyright Notice";
126 /******************************************************************************
128 * FUNCTION: AsMatchExactWord
130 * DESCRIPTION: Check previous and next characters for whitespace
132 ******************************************************************************/
143 NextChar = Word[WordLength];
144 PrevChar = * (Word -1);
146 if (isalnum (NextChar) ||
148 isalnum (PrevChar) ||
158 /******************************************************************************
162 * DESCRIPTION: Common formatted print
164 ******************************************************************************/
174 printf ("-- %4u %28.28s : %s\n", Count, Message, Filename);
178 /******************************************************************************
180 * FUNCTION: AsCheckAndSkipLiterals
182 * DESCRIPTION: Generic routine to skip comments and quoted string literals.
183 * Keeps a line count.
185 ******************************************************************************/
188 AsCheckAndSkipLiterals (
193 char *SubBuffer = Buffer;
197 /* Ignore comments */
199 if ((SubBuffer[0] == '/') &&
200 (SubBuffer[1] == '*'))
202 LiteralEnd = strstr (SubBuffer, "*/");
203 SubBuffer += 2; /* Get past comment opening */
210 while (SubBuffer < LiteralEnd)
212 if (*SubBuffer == '\n')
220 SubBuffer += 2; /* Get past comment close */
223 /* Ignore quoted strings */
225 else if (*SubBuffer == '\"')
228 LiteralEnd = AsSkipPastChar (SubBuffer, '\"');
237 (*TotalLines) += NewLines;
243 /******************************************************************************
245 * FUNCTION: AsAsCheckForBraces
247 * DESCRIPTION: Check for an open brace after each if statement
249 ******************************************************************************/
256 char *SubBuffer = Buffer;
260 UINT32 TotalLines = 1;
266 SubBuffer = AsCheckAndSkipLiterals (SubBuffer, &TotalLines);
268 if (*SubBuffer == '\n')
272 else if (!(strncmp (" if", SubBuffer, 3)))
275 NextBrace = strstr (SubBuffer, "{");
276 NextSemicolon = strstr (SubBuffer, ";");
277 NextIf = strstr (SubBuffer, " if");
280 (NextSemicolon && (NextBrace > NextSemicolon)) ||
281 (NextIf && (NextBrace > NextIf)))
284 printf ("Missing braces for <if>, line %d: %s\n", TotalLines, Filename);
287 else if (!(strncmp (" else if", SubBuffer, 8)))
290 NextBrace = strstr (SubBuffer, "{");
291 NextSemicolon = strstr (SubBuffer, ";");
292 NextIf = strstr (SubBuffer, " if");
295 (NextSemicolon && (NextBrace > NextSemicolon)) ||
296 (NextIf && (NextBrace > NextIf)))
299 printf ("Missing braces for <if>, line %d: %s\n", TotalLines, Filename);
302 else if (!(strncmp (" else", SubBuffer, 5)))
305 NextBrace = strstr (SubBuffer, "{");
306 NextSemicolon = strstr (SubBuffer, ";");
307 NextIf = strstr (SubBuffer, " if");
310 (NextSemicolon && (NextBrace > NextSemicolon)) ||
311 (NextIf && (NextBrace > NextIf)))
314 printf ("Missing braces for <else>, line %d: %s\n", TotalLines, Filename);
323 /******************************************************************************
325 * FUNCTION: AsTrimLines
327 * DESCRIPTION: Remove extra blanks from the end of source lines. Does not
330 ******************************************************************************/
337 char *SubBuffer = Buffer;
338 char *StartWhiteSpace = NULL;
339 UINT32 SpaceCount = 0;
344 while (*SubBuffer != '\n')
351 if (*SubBuffer == ' ')
353 if (!StartWhiteSpace)
355 StartWhiteSpace = SubBuffer;
360 StartWhiteSpace = NULL;
368 SpaceCount += (SubBuffer - StartWhiteSpace);
370 /* Remove the spaces */
372 SubBuffer = AsRemoveData (StartWhiteSpace, SubBuffer);
373 StartWhiteSpace = NULL;
383 Gbl_MadeChanges = TRUE;
384 AsPrint ("Extraneous spaces removed", SpaceCount, Filename);
389 /******************************************************************************
391 * FUNCTION: AsTrimWhitespace
393 * DESCRIPTION: Remove "excess" blank lines - any more than 2 blank lines.
394 * this can happen during the translation when lines are removed.
396 ******************************************************************************/
402 int ReplaceCount = 1;
407 ReplaceCount = AsReplaceString ("\n\n\n\n", "\n\n\n", REPLACE_SUBSTRINGS, Buffer);
412 /******************************************************************************
414 * FUNCTION: AsReplaceHeader
416 * DESCRIPTION: Replace the default Intel legal header with a new header
418 ******************************************************************************/
429 /* Find the original header */
431 SubBuffer = strstr (Buffer, HeaderBegin);
437 /* Find the end of the original header */
439 TokenEnd = strstr (SubBuffer, "*/");
440 TokenEnd = AsSkipPastChar (TokenEnd, '\n');
442 /* Delete old header, insert new one */
444 AsReplaceData (SubBuffer, TokenEnd - SubBuffer, NewHeader, strlen (NewHeader));
448 /******************************************************************************
450 * FUNCTION: AsReplaceString
452 * DESCRIPTION: Replace all instances of a target string with a replacement
453 * string. Returns count of the strings replaced.
455 ******************************************************************************/
468 int ReplacementLength;
469 int ReplaceCount = 0;
472 TargetLength = strlen (Target);
473 ReplacementLength = strlen (Replacement);
480 /* Find the target string */
482 SubString1 = strstr (SubBuffer, Target);
489 * Check for translation escape string -- means to ignore
490 * blocks of code while replacing
493 SubString2 = strstr (SubBuffer, "/*!");
496 (SubString2 < SubString1))
498 /* Find end of the escape block starting at "Substring2" */
500 SubString2 = strstr (SubString2, "!*/");
503 /* Didn't find terminator */
508 /* Move buffer to end of escape block and continue */
510 SubBuffer = SubString2;
513 /* Do the actual replace if the target was found */
517 if ((Type & REPLACE_MASK) == REPLACE_WHOLE_WORD)
519 if (!AsMatchExactWord (SubString1, TargetLength))
521 SubBuffer = SubString1 + 1;
526 SubBuffer = AsReplaceData (SubString1, TargetLength, Replacement, ReplacementLength);
528 if (((Type & INDENT_MASK) == EXTRA_INDENT_C) &&
529 (Gbl_FileType == FILE_TYPE_SOURCE))
531 SubBuffer = AsReplaceData (SubBuffer, 0, " ", 4);
543 /******************************************************************************
545 * FUNCTION: AsConvertToLineFeeds
549 ******************************************************************************/
552 AsConvertToLineFeeds (
564 /* Find the target string */
566 SubString = strstr (SubBuffer, "\r\n");
572 SubBuffer = AsReplaceData (SubString, 1, NULL, 0);
578 /******************************************************************************
580 * FUNCTION: AsInsertCarriageReturns
584 ******************************************************************************/
587 AsInsertCarriageReturns (
599 /* Find the target string */
601 SubString = strstr (SubBuffer, "\n");
607 SubBuffer = AsInsertData (SubString, "\r", 1);
614 /******************************************************************************
616 * FUNCTION: AsBracesOnSameLine
618 * DESCRIPTION: Move opening braces up to the same line as an if, for, else,
619 * or while statement (leave function opening brace on separate
622 ******************************************************************************/
629 char *SubBuffer = Buffer;
631 char *StartOfThisLine;
632 BOOLEAN BlockBegin = TRUE;
637 /* Ignore comments */
639 if ((SubBuffer[0] == '/') &&
640 (SubBuffer[1] == '*'))
642 SubBuffer = strstr (SubBuffer, "*/");
652 /* Ignore quoted strings */
654 if (*SubBuffer == '\"')
657 SubBuffer = AsSkipPastChar (SubBuffer, '\"');
664 if (!strncmp ("\n}", SubBuffer, 2))
667 * A newline followed by a closing brace closes a function
668 * or struct or initializer block
673 /* Move every standalone brace up to the previous line */
675 if (*SubBuffer == '{')
684 * Backup to previous non-whitespace
686 Beginning = SubBuffer - 1;
687 while ((*Beginning == ' ') ||
688 (*Beginning == '\n'))
693 StartOfThisLine = Beginning;
694 while (*StartOfThisLine != '\n')
700 * Move the brace up to the previous line, UNLESS:
702 * 1) There is a conditional compile on the line (starts with '#')
703 * 2) Previous line ends with an '=' (Start of initializer block)
704 * 3) Previous line ends with a comma (part of an init list)
707 if ((StartOfThisLine[1] != '#') &&
708 (*Beginning != '=') &&
713 Length = strlen (SubBuffer);
715 Gbl_MadeChanges = TRUE;
717 #ifdef ADD_EXTRA_WHITESPACE
718 AsReplaceData (Beginning, SubBuffer-Beginning, " {\n", 3);
720 AsReplaceData (Beginning, SubBuffer-Beginning, " {", 2);
731 /******************************************************************************
733 * FUNCTION: AsTabify4
735 * DESCRIPTION: Convert the text to tabbed text. Alignment of text is
738 ******************************************************************************/
744 char *SubBuffer = Buffer;
746 UINT32 SpaceCount = 0;
752 if (*SubBuffer == '\n')
761 /* Ignore comments */
763 if ((SubBuffer[0] == '/') &&
764 (SubBuffer[1] == '*'))
766 SubBuffer = strstr (SubBuffer, "*/");
776 /* Ignore quoted strings */
778 if (*SubBuffer == '\"')
781 SubBuffer = AsSkipPastChar (SubBuffer, '\"');
789 if (*SubBuffer == ' ')
797 NewSubBuffer = (SubBuffer + 1) - 4;
798 *NewSubBuffer = '\t';
801 /* Remove the spaces */
803 SubBuffer = AsRemoveData (NewSubBuffer, SubBuffer + 1);
806 if ((Column % 4) == 0)
821 /******************************************************************************
823 * FUNCTION: AsTabify8
825 * DESCRIPTION: Convert the text to tabbed text. Alignment of text is
828 ******************************************************************************/
834 char *SubBuffer = Buffer;
836 char *CommentEnd = NULL;
837 UINT32 SpaceCount = 0;
840 UINT32 LastLineTabCount = 0;
841 UINT32 LastLineColumnStart = 0;
842 UINT32 ThisColumnStart = 0;
843 UINT32 ThisTabCount = 0;
844 char *FirstNonBlank = NULL;
849 if (*SubBuffer == '\n')
851 /* This is a standalone blank line */
853 FirstNonBlank = NULL;
863 /* Find the first non-blank character on this line */
865 FirstNonBlank = SubBuffer;
866 while (*FirstNonBlank == ' ')
872 * This mechanism limits the difference in tab counts from
873 * line to line. It helps avoid the situation where a second
874 * continuation line (which was indented correctly for tabs=4) would
875 * get indented off the screen if we just blindly converted to tabs.
877 ThisColumnStart = FirstNonBlank - SubBuffer;
879 if (LastLineTabCount == 0)
883 else if (ThisColumnStart == LastLineColumnStart)
885 ThisTabCount = LastLineTabCount -1;
889 ThisTabCount = LastLineTabCount + 1;
895 /* Check if we are in a comment */
897 if ((SubBuffer[0] == '*') &&
898 (SubBuffer[1] == '/'))
903 if (*SubBuffer == '\n')
907 LastLineTabCount = TabCount;
910 FirstNonBlank = NULL;
911 LastLineColumnStart = ThisColumnStart;
918 /* Check for comment open */
920 if ((SubBuffer[0] == '/') &&
921 (SubBuffer[1] == '*'))
923 /* Find the end of the comment, it must exist */
925 CommentEnd = strstr (SubBuffer, "*/");
931 /* Toss the rest of this line or single-line comment */
933 while ((SubBuffer < CommentEnd) &&
934 (*SubBuffer != '\n'))
939 if (*SubBuffer == '\n')
943 LastLineTabCount = TabCount;
946 FirstNonBlank = NULL;
947 LastLineColumnStart = ThisColumnStart;
954 /* Ignore quoted strings */
956 if ((!CommentEnd) && (*SubBuffer == '\"'))
959 SubBuffer = AsSkipPastChar (SubBuffer, '\"');
967 if (*SubBuffer != ' ')
969 /* Not a space, skip to end of line */
971 SubBuffer = AsSkipUntilChar (SubBuffer, '\n');
978 LastLineTabCount = TabCount;
982 FirstNonBlank = NULL;
983 LastLineColumnStart = ThisColumnStart;
995 /* Replace this group of spaces with a tab character */
999 NewSubBuffer = SubBuffer - 3;
1001 if (TabCount <= ThisTabCount ? (ThisTabCount +1) : 0)
1003 *NewSubBuffer = '\t';
1009 /* Remove the spaces */
1011 SubBuffer = AsRemoveData (NewSubBuffer, SubBuffer);
1021 /******************************************************************************
1023 * FUNCTION: AsCountLines
1025 * DESCRIPTION: Count the number of lines in the input buffer. Also count
1026 * the number of long lines (lines longer than 80 chars).
1028 ******************************************************************************/
1035 char *SubBuffer = Buffer;
1037 UINT32 LineCount = 0;
1038 UINT32 LongLineCount = 0;
1043 EndOfLine = AsSkipUntilChar (SubBuffer, '\n');
1046 Gbl_TotalLines += LineCount;
1050 if ((EndOfLine - SubBuffer) > 80)
1056 SubBuffer = EndOfLine + 1;
1061 VERBOSE_PRINT (("%d Lines longer than 80 found in %s\n", LongLineCount, Filename));
1062 Gbl_LongLines += LongLineCount;
1065 Gbl_TotalLines += LineCount;
1070 /******************************************************************************
1072 * FUNCTION: AsCountTabs
1074 * DESCRIPTION: Simply count the number of tabs in the input file buffer
1076 ******************************************************************************/
1084 UINT32 TabCount = 0;
1087 for (i = 0; Buffer[i]; i++)
1089 if (Buffer[i] == '\t')
1097 AsPrint ("Tabs found", TabCount, Filename);
1098 Gbl_Tabs += TabCount;
1101 AsCountLines (Buffer, Filename);
1105 /******************************************************************************
1107 * FUNCTION: AsCountNonAnsiComments
1109 * DESCRIPTION: Count the number of "//" comments. This type of comment is
1112 ******************************************************************************/
1115 AsCountNonAnsiComments (
1119 char *SubBuffer = Buffer;
1120 UINT32 CommentCount = 0;
1125 SubBuffer = strstr (SubBuffer, "//");
1135 AsPrint ("Non-ANSI Comments found", CommentCount, Filename);
1136 Gbl_NonAnsiComments += CommentCount;
1141 /******************************************************************************
1143 * FUNCTION: AsCountSourceLines
1145 * DESCRIPTION: Count the number of C source lines. Defined by 1) not a
1146 * comment, and 2) not a blank line.
1148 ******************************************************************************/
1151 AsCountSourceLines (
1155 char *SubBuffer = Buffer;
1156 UINT32 LineCount = 0;
1157 UINT32 WhiteCount = 0;
1158 UINT32 CommentCount = 0;
1163 /* Ignore comments */
1165 if ((SubBuffer[0] == '/') &&
1166 (SubBuffer[1] == '*'))
1171 while (SubBuffer[0] && SubBuffer[1] &&
1172 !(((SubBuffer[0] == '*') &&
1173 (SubBuffer[1] == '/'))))
1175 if (SubBuffer[0] == '\n')
1184 /* A linefeed followed by a non-linefeed is a valid source line */
1186 else if ((SubBuffer[0] == '\n') &&
1187 (SubBuffer[1] != '\n'))
1192 /* Two back-to-back linefeeds indicate a whitespace line */
1194 else if ((SubBuffer[0] == '\n') &&
1195 (SubBuffer[1] == '\n'))
1203 /* Adjust comment count for legal header */
1205 CommentCount -= LINES_IN_LEGAL_HEADER;
1207 Gbl_SourceLines += LineCount;
1208 Gbl_WhiteLines += WhiteCount;
1209 Gbl_CommentLines += CommentCount;
1211 VERBOSE_PRINT (("%d Comment %d White %d Code %d Lines in %s\n",
1212 CommentCount, WhiteCount, LineCount, LineCount+WhiteCount+CommentCount, Filename));
1216 /******************************************************************************
1218 * FUNCTION: AsInsertPrefix
1220 * DESCRIPTION: Insert struct or union prefixes
1222 ******************************************************************************/
1237 char LowerKeyword[128];
1243 case SRC_TYPE_STRUCT:
1244 InsertString = "struct ";
1247 case SRC_TYPE_UNION:
1248 InsertString = "union ";
1255 strcpy (LowerKeyword, Keyword);
1256 strlwr (LowerKeyword);
1260 InsertLength = strlen (InsertString);
1261 KeywordLength = strlen (Keyword);
1266 /* Find an instance of the keyword */
1268 SubString = strstr (SubBuffer, LowerKeyword);
1275 SubBuffer = SubString;
1277 /* Must be standalone word, not a substring */
1279 if (AsMatchExactWord (SubString, KeywordLength))
1281 /* Make sure the keyword isn't already prefixed with the insert */
1283 if (!strncmp (SubString - InsertLength, InsertString, InsertLength))
1285 /* Add spaces if not already at the end-of-line */
1287 if (*(SubBuffer + KeywordLength) != '\n')
1289 /* Already present, add spaces after to align structure members */
1291 AsInsertData (SubBuffer + KeywordLength, " ", 8);
1296 /* Make sure the keyword isn't at the end of a struct/union */
1297 /* Note: This code depends on a single space after the brace */
1299 if (*(SubString - 2) == '}')
1304 /* Prefix the keyword with the insert string */
1306 Gbl_MadeChanges = TRUE;
1307 StrLength = strlen (SubString);
1309 /* Is there room for insertion */
1311 EndKeyword = SubString + strlen (LowerKeyword);
1314 while (EndKeyword[TrailingSpaces] == ' ')
1320 * Use "if (TrailingSpaces > 1)" if we want to ignore casts
1322 SubBuffer = SubString + InsertLength;
1324 if (TrailingSpaces > InsertLength)
1326 /* Insert the keyword */
1328 memmove (SubBuffer, SubString, KeywordLength);
1330 /* Insert the keyword */
1332 memmove (SubString, InsertString, InsertLength);
1336 AsInsertData (SubString, InsertString, InsertLength);
1341 SubBuffer += KeywordLength;