1 /******************************************************************************
3 * Module Name: asconvrt - Source conversion code
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12 * All rights reserved.
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
72 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
119 * Redistribution and use in source and binary forms, with or without
120 * modification, are permitted provided that the following conditions
122 * 1. Redistributions of source code must retain the above copyright
123 * notice, this list of conditions, and the following disclaimer,
124 * without modification.
125 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126 * substantially similar to the "NO WARRANTY" disclaimer below
127 * ("Disclaimer") and any redistribution must be conditioned upon
128 * including a substantially similar Disclaimer requirement for further
129 * binary redistribution.
130 * 3. Neither the names of the above-listed copyright holders nor the names
131 * of any contributors may be used to endorse or promote products derived
132 * from this software without specific prior written permission.
134 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146 * Alternatively, you may choose to be licensed under the terms of the
147 * GNU General Public License ("GPL") version 2 as published by the Free
148 * Software Foundation.
150 *****************************************************************************/
154 AS_BRACE_INFO Gbl_BraceInfo
[] =
165 /* Local prototypes */
172 AS_SCAN_CALLBACK Callback
);
175 AsCheckBracesCallback (
186 #define MODULE_HEADER_BEGIN "/******************************************************************************\n *\n * Module Name:";
187 #define MODULE_HEADER_END " *****************************************************************************/\n\n"
188 #define INTEL_COPYRIGHT " * Copyright (C) 2000 - 2018, Intel Corp.\n"
190 /* Opening signature of the Intel legal header */
192 char *HeaderBegin
= "/******************************************************************************\n *\n * 1. Copyright Notice";
194 UINT32 NonAnsiCommentCount
;
196 char CopyRightHeaderEnd
[] = INTEL_COPYRIGHT
" *\n" MODULE_HEADER_END
;
198 /******************************************************************************
200 * FUNCTION: AsCountNonAnsiComments
202 * DESCRIPTION: Count the number of "//" comments. This type of comment is
205 * NOTE: July 2014: Allows // within quoted strings and within normal
206 * comments. Eliminates extraneous warnings from this utility.
208 ******************************************************************************/
211 AsCountNonAnsiComments (
216 AsMatchValidToken (Buffer
, Filename
, 0, NULL
);
218 /* Error if any slash-slash comments found */
220 if (NonAnsiCommentCount
)
222 AsPrint ("Non-ANSI // Comments Found", NonAnsiCommentCount
, Filename
);
223 Gbl_NonAnsiComments
+= NonAnsiCommentCount
;
228 /******************************************************************************
230 * FUNCTION: AsCheckForBraces
232 * DESCRIPTION: Check for an open brace after each if/else/do (etc.)
235 ******************************************************************************/
243 AsMatchValidToken (Buffer
, Filename
, 0, AsCheckBracesCallback
);
247 /******************************************************************************
249 * FUNCTION: AsCheckBracesCallback
251 * DESCRIPTION: Check if/else/do statements. Ensure that braces
254 * TBD: Currently, don't check while() statements. The problem is that there
255 * are two forms: do {} while (); and while () {}.
257 ******************************************************************************/
260 AsCheckBracesCallback (
265 char *SubBuffer
= Buffer
;
268 AS_BRACE_INFO
*BraceInfo
;
271 for (BraceInfo
= Gbl_BraceInfo
; BraceInfo
->Operator
; BraceInfo
++)
273 if (!(strncmp (BraceInfo
->Operator
, SubBuffer
, BraceInfo
->Length
)))
275 SubBuffer
+= (BraceInfo
->Length
- 1);
277 /* Find next brace and the next semicolon */
279 NextBrace
= AsMatchValidToken (SubBuffer
, Filename
, '{', NULL
);
280 NextSemicolon
= AsMatchValidToken (SubBuffer
, Filename
, ';', NULL
);
282 /* Next brace should appear before next semicolon */
285 (NextSemicolon
&& (NextBrace
> NextSemicolon
)))
291 printf ("Missing braces for <%s>, line %u: %s\n",
292 BraceInfo
->Operator
+ 1, LineNumber
, Filename
);
300 /* No match, just return original buffer */
306 /******************************************************************************
308 * FUNCTION: AsMatchValidToken
310 * DESCRIPTION: Find the next matching token in the input buffer.
312 ******************************************************************************/
319 AS_SCAN_CALLBACK Callback
)
321 char *SubBuffer
= Buffer
;
327 NonAnsiCommentCount
= 0;
329 /* Scan from current position up to the end if necessary */
333 /* Skip normal comments */
335 if ((*SubBuffer
== '/') &&
336 (*(SubBuffer
+ 1) == '*'))
338 /* Must maintain line count */
341 while (strncmp ("*/", SubBuffer
, 2))
343 if (*SubBuffer
== '\n')
354 /* Skip single quoted chars */
356 if (*SubBuffer
== '\'')
364 if (*SubBuffer
== '\\')
373 /* Skip quoted strings */
375 if (*SubBuffer
== '"')
377 StringStart
= SubBuffer
;
384 while (*SubBuffer
!= '"')
386 if ((*SubBuffer
== '\n') ||
389 AsPrint ("Unbalanced quoted string",1, Filename
);
390 printf (" %.32s (line %u)\n", StringStart
, TotalLines
);
394 /* Handle escapes within the string */
396 if (*SubBuffer
== '\\')
408 /* Now we can check for a slash-slash comment */
410 if ((*SubBuffer
== '/') &&
411 (*(SubBuffer
+ 1) == '/'))
413 NonAnsiCommentCount
++;
415 /* Skip to end-of-line */
417 while ((*SubBuffer
!= '\n') &&
428 if (*SubBuffer
== '\n')
437 /* Finally, check for a newline */
439 if (*SubBuffer
== '\n')
446 /* Normal character, do the user actions */
450 SubBuffer
= Callback (SubBuffer
, Filename
, TotalLines
);
453 if (TargetChar
&& (*SubBuffer
== TargetChar
))
465 /******************************************************************************
467 * FUNCTION: AsRemoveExtraLines
469 * DESCRIPTION: Remove all extra lines at the start and end of the file.
471 ******************************************************************************/
482 /* Remove any extra lines at the start of the file */
484 while (*FileBuffer
== '\n')
486 printf ("Removing extra line at start of file: %s\n", Filename
);
487 AsRemoveData (FileBuffer
, FileBuffer
+ 1);
490 /* Remove any extra lines at the end of the file */
492 Length
= strlen (FileBuffer
);
493 FileEnd
= FileBuffer
+ (Length
- 2);
495 while (*FileEnd
== '\n')
497 printf ("Removing extra line at end of file: %s\n", Filename
);
498 AsRemoveData (FileEnd
, FileEnd
+ 1);
504 /******************************************************************************
506 * FUNCTION: AsRemoveSpacesAfterPeriod
508 * DESCRIPTION: Remove an extra space after a period.
510 ******************************************************************************/
513 AsRemoveSpacesAfterPeriod (
517 int ReplaceCount
= 0;
521 Possible
= FileBuffer
;
524 Possible
= strstr (Possible
, ". ");
527 if ((*(Possible
-1) == '.') ||
528 (*(Possible
-1) == '\"') ||
529 (*(Possible
-1) == '\n'))
535 Possible
= AsReplaceData (Possible
, 3, ". ", 2);
542 printf ("Removed %d extra blanks after a period: %s\n",
543 ReplaceCount
, Filename
);
548 /******************************************************************************
550 * FUNCTION: AsMatchExactWord
552 * DESCRIPTION: Check previous and next characters for whitespace
554 ******************************************************************************/
565 NextChar
= Word
[WordLength
];
566 PrevChar
= * (Word
-1);
568 if (isalnum ((int) NextChar
) ||
570 isalnum ((int) PrevChar
) ||
580 /******************************************************************************
584 * DESCRIPTION: Common formatted print
586 ******************************************************************************/
600 printf ("-- %4u %28.28s : %s\n", Count
, Message
, Filename
);
604 /******************************************************************************
606 * FUNCTION: AsTrimLines
608 * DESCRIPTION: Remove extra blanks from the end of source lines. Does not
611 ******************************************************************************/
618 char *SubBuffer
= Buffer
;
619 char *StartWhiteSpace
= NULL
;
620 UINT32 SpaceCount
= 0;
625 while (*SubBuffer
!= '\n')
632 if (*SubBuffer
== ' ')
634 if (!StartWhiteSpace
)
636 StartWhiteSpace
= SubBuffer
;
641 StartWhiteSpace
= NULL
;
649 SpaceCount
+= (SubBuffer
- StartWhiteSpace
);
651 /* Remove the spaces */
653 SubBuffer
= AsRemoveData (StartWhiteSpace
, SubBuffer
);
654 StartWhiteSpace
= NULL
;
664 Gbl_MadeChanges
= TRUE
;
665 AsPrint ("Extraneous spaces removed", SpaceCount
, Filename
);
670 /******************************************************************************
672 * FUNCTION: AsTrimWhitespace
674 * DESCRIPTION: Remove "excess" blank lines - any more than 2 blank lines.
675 * this can happen during the translation when lines are removed.
677 ******************************************************************************/
684 int ReplaceCount
= 1;
689 ReplaceCount
= AsReplaceString ("\n\n\n\n", "\n\n\n",
690 REPLACE_SUBSTRINGS
, Buffer
);
694 * Check for exactly one blank line after the copyright header
697 /* Find the header */
699 SubBuffer
= strstr (Buffer
, HeaderBegin
);
705 /* Find the end of the header */
707 SubBuffer
= strstr (SubBuffer
, "*/");
708 SubBuffer
= AsSkipPastChar (SubBuffer
, '\n');
710 /* Replace a double blank line with a single */
712 if (!strncmp (SubBuffer
, "\n\n", 2))
714 AsReplaceData (SubBuffer
, 2, "\n", 1);
715 AcpiOsPrintf ("Found multiple blank lines after copyright\n");
718 /* If no blank line after header, insert one */
720 else if (*SubBuffer
!= '\n')
722 AsInsertData (SubBuffer
, "\n", 1);
723 AcpiOsPrintf ("Inserted blank line after copyright\n");
728 /******************************************************************************
730 * FUNCTION: AsReplaceHeader
732 * DESCRIPTION: Replace the default Intel legal header with a new header
734 ******************************************************************************/
745 /* Find the original header */
747 SubBuffer
= strstr (Buffer
, HeaderBegin
);
753 /* Find the end of the original header */
755 TokenEnd
= strstr (SubBuffer
, "*/");
756 TokenEnd
= AsSkipPastChar (TokenEnd
, '\n');
758 /* Delete old header, insert new one */
760 AsReplaceData (SubBuffer
, TokenEnd
- SubBuffer
,
761 NewHeader
, strlen (NewHeader
));
765 /******************************************************************************
767 * FUNCTION: AsDoSpdxHeader
769 * DESCRIPTION: Replace the default Intel legal header with a new header
771 ******************************************************************************/
781 /* Place an SPDX header at the very top */
783 AsReplaceData (Buffer
, 0,
784 SpdxHeader
, strlen (SpdxHeader
));
786 /* Place an Intel copyright notice in the module header */
788 SubBuffer
= strstr (Buffer
, MODULE_HEADER_END
);
794 AsReplaceData (SubBuffer
, strlen (MODULE_HEADER_END
),
795 CopyRightHeaderEnd
, strlen (CopyRightHeaderEnd
));
798 /******************************************************************************
800 * FUNCTION: AsReplaceString
802 * DESCRIPTION: Replace all instances of a target string with a replacement
803 * string. Returns count of the strings replaced.
805 ******************************************************************************/
818 int ReplacementLength
;
819 int ReplaceCount
= 0;
822 TargetLength
= strlen (Target
);
823 ReplacementLength
= strlen (Replacement
);
830 /* Find the target string */
832 SubString1
= strstr (SubBuffer
, Target
);
835 return (ReplaceCount
);
839 * Check for translation escape string -- means to ignore
840 * blocks of code while replacing
842 if (Gbl_IgnoreTranslationEscapes
)
848 SubString2
= strstr (SubBuffer
, AS_START_IGNORE
);
852 (SubString2
< SubString1
))
854 /* Find end of the escape block starting at "Substring2" */
856 SubString2
= strstr (SubString2
, AS_STOP_IGNORE
);
859 /* Didn't find terminator */
861 return (ReplaceCount
);
864 /* Move buffer to end of escape block and continue */
866 SubBuffer
= SubString2
;
869 /* Do the actual replace if the target was found */
873 if ((Type
& REPLACE_MASK
) == REPLACE_WHOLE_WORD
)
875 if (!AsMatchExactWord (SubString1
, TargetLength
))
877 SubBuffer
= SubString1
+ 1;
882 SubBuffer
= AsReplaceData (SubString1
, TargetLength
,
883 Replacement
, ReplacementLength
);
885 if ((Type
& EXTRA_INDENT_C
) &&
888 SubBuffer
= AsInsertData (SubBuffer
, " ", 8);
895 return (ReplaceCount
);
899 /******************************************************************************
901 * FUNCTION: AsConvertToLineFeeds
903 * DESCRIPTION: Convert all CR/LF pairs to LF only.
905 ******************************************************************************/
908 AsConvertToLineFeeds (
920 /* Find the target string */
922 SubString
= strstr (SubBuffer
, "\r\n");
928 SubBuffer
= AsReplaceData (SubString
, 1, NULL
, 0);
933 /******************************************************************************
935 * FUNCTION: AsInsertCarriageReturns
937 * DESCRIPTION: Convert lone LFs to CR/LF pairs.
939 ******************************************************************************/
942 AsInsertCarriageReturns (
954 /* Find the target string */
956 SubString
= strstr (SubBuffer
, "\n");
962 SubBuffer
= AsInsertData (SubString
, "\r", 1);
968 /******************************************************************************
970 * FUNCTION: AsBracesOnSameLine
972 * DESCRIPTION: Move opening braces up to the same line as an if, for, else,
973 * or while statement (leave function opening brace on separate
976 ******************************************************************************/
982 char *SubBuffer
= Buffer
;
984 char *StartOfThisLine
;
986 BOOLEAN BlockBegin
= TRUE
;
991 /* Ignore comments */
993 if ((SubBuffer
[0] == '/') &&
994 (SubBuffer
[1] == '*'))
996 SubBuffer
= strstr (SubBuffer
, "*/");
1006 /* Ignore quoted strings */
1008 if (*SubBuffer
== '\"')
1011 SubBuffer
= AsSkipPastChar (SubBuffer
, '\"');
1018 if (!strncmp ("\n}", SubBuffer
, 2))
1021 * A newline followed by a closing brace closes a function
1022 * or struct or initializer block
1028 * Move every standalone brace up to the previous line
1029 * Check for digit will ignore initializer lists surrounded by braces.
1030 * This will work until we we need more complex detection.
1032 if ((*SubBuffer
== '{') && !isdigit ((int) SubBuffer
[1]))
1041 * Backup to previous non-whitespace
1043 Beginning
= SubBuffer
- 1;
1044 while ((*Beginning
== ' ') ||
1045 (*Beginning
== '\n'))
1050 StartOfThisLine
= Beginning
;
1051 while (*StartOfThisLine
!= '\n')
1057 * Move the brace up to the previous line, UNLESS:
1059 * 1) There is a conditional compile on the line (starts with '#')
1060 * 2) Previous line ends with an '=' (Start of initializer block)
1061 * 3) Previous line ends with a comma (part of an init list)
1062 * 4) Previous line ends with a backslash (part of a macro)
1064 if ((StartOfThisLine
[1] != '#') &&
1065 (*Beginning
!= '\\') &&
1066 (*Beginning
!= '/') &&
1067 (*Beginning
!= '{') &&
1068 (*Beginning
!= '=') &&
1069 (*Beginning
!= ','))
1074 Gbl_MadeChanges
= TRUE
;
1076 #ifdef ADD_EXTRA_WHITESPACE
1077 AsReplaceData (Beginning
, SubBuffer
- Beginning
, " {\n", 3);
1079 /* Find non-whitespace start of next line */
1081 Next
= SubBuffer
+ 1;
1082 while ((*Next
== ' ') ||
1088 /* Find non-whitespace start of this line */
1091 while ((*StartOfThisLine
== ' ') ||
1092 (*StartOfThisLine
== '\t'))
1098 * Must be a single-line comment to need more whitespace
1099 * Even then, we don't need more if the previous statement
1102 if ((Next
[0] == '/') &&
1104 (Next
[2] != '\n') &&
1106 (!strncmp (StartOfThisLine
, "else if", 7) ||
1107 !strncmp (StartOfThisLine
, "else while", 10) ||
1108 strncmp (StartOfThisLine
, "else", 4)))
1110 AsReplaceData (Beginning
, SubBuffer
- Beginning
, " {\n", 3);
1114 AsReplaceData (Beginning
, SubBuffer
- Beginning
, " {", 2);
1126 /******************************************************************************
1128 * FUNCTION: AsTabify4
1130 * DESCRIPTION: Convert the text to tabbed text. Alignment of text is
1133 ******************************************************************************/
1139 char *SubBuffer
= Buffer
;
1141 UINT32 SpaceCount
= 0;
1147 if (*SubBuffer
== '\n')
1156 /* Ignore comments */
1158 if ((SubBuffer
[0] == '/') &&
1159 (SubBuffer
[1] == '*'))
1161 SubBuffer
= strstr (SubBuffer
, "*/");
1171 /* Ignore quoted strings */
1173 if (*SubBuffer
== '\"')
1176 SubBuffer
= AsSkipPastChar (SubBuffer
, '\"');
1184 if (*SubBuffer
== ' ')
1188 if (SpaceCount
>= 4)
1192 NewSubBuffer
= (SubBuffer
+ 1) - 4;
1193 *NewSubBuffer
= '\t';
1196 /* Remove the spaces */
1198 SubBuffer
= AsRemoveData (NewSubBuffer
, SubBuffer
+ 1);
1201 if ((Column
% 4) == 0)
1216 /******************************************************************************
1218 * FUNCTION: AsTabify8
1220 * DESCRIPTION: Convert the text to tabbed text. Alignment of text is
1223 ******************************************************************************/
1229 char *SubBuffer
= Buffer
;
1231 char *CommentEnd
= NULL
;
1232 UINT32 SpaceCount
= 0;
1234 UINT32 TabCount
= 0;
1235 UINT32 LastLineTabCount
= 0;
1236 UINT32 LastLineColumnStart
= 0;
1237 UINT32 ThisColumnStart
= 0;
1238 UINT32 ThisTabCount
= 0;
1239 char *FirstNonBlank
= NULL
;
1244 if (*SubBuffer
== '\n')
1246 /* This is a standalone blank line */
1248 FirstNonBlank
= NULL
;
1258 /* Find the first non-blank character on this line */
1260 FirstNonBlank
= SubBuffer
;
1261 while (*FirstNonBlank
== ' ')
1267 * This mechanism limits the difference in tab counts from
1268 * line to line. It helps avoid the situation where a second
1269 * continuation line (which was indented correctly for tabs=4) would
1270 * get indented off the screen if we just blindly converted to tabs.
1272 ThisColumnStart
= FirstNonBlank
- SubBuffer
;
1274 if (LastLineTabCount
== 0)
1278 else if (ThisColumnStart
== LastLineColumnStart
)
1280 ThisTabCount
= LastLineTabCount
-1;
1284 ThisTabCount
= LastLineTabCount
+ 1;
1290 /* Check if we are in a comment */
1292 if ((SubBuffer
[0] == '*') &&
1293 (SubBuffer
[1] == '/'))
1298 if (*SubBuffer
== '\n')
1302 LastLineTabCount
= TabCount
;
1306 FirstNonBlank
= NULL
;
1307 LastLineColumnStart
= ThisColumnStart
;
1314 /* Check for comment open */
1316 if ((SubBuffer
[0] == '/') &&
1317 (SubBuffer
[1] == '*'))
1319 /* Find the end of the comment, it must exist */
1321 CommentEnd
= strstr (SubBuffer
, "*/");
1327 /* Toss the rest of this line or single-line comment */
1329 while ((SubBuffer
< CommentEnd
) &&
1330 (*SubBuffer
!= '\n'))
1335 if (*SubBuffer
== '\n')
1339 LastLineTabCount
= TabCount
;
1343 FirstNonBlank
= NULL
;
1344 LastLineColumnStart
= ThisColumnStart
;
1351 /* Ignore quoted strings */
1353 if ((!CommentEnd
) && (*SubBuffer
== '\"'))
1356 SubBuffer
= AsSkipPastChar (SubBuffer
, '\"');
1365 if (*SubBuffer
!= ' ')
1367 /* Not a space, skip to end of line */
1369 SubBuffer
= AsSkipUntilChar (SubBuffer
, '\n');
1376 LastLineTabCount
= TabCount
;
1380 FirstNonBlank
= NULL
;
1381 LastLineColumnStart
= ThisColumnStart
;
1391 if (SpaceCount
>= 4)
1393 /* Replace this group of spaces with a tab character */
1397 NewSubBuffer
= SubBuffer
- 3;
1399 if (TabCount
<= ThisTabCount
? (ThisTabCount
+1) : 0)
1401 *NewSubBuffer
= '\t';
1407 /* Remove the spaces */
1409 SubBuffer
= AsRemoveData (NewSubBuffer
, SubBuffer
);
1419 /******************************************************************************
1421 * FUNCTION: AsCountLines
1423 * DESCRIPTION: Count the number of lines in the input buffer. Also count
1424 * the number of long lines (lines longer than 80 chars).
1426 ******************************************************************************/
1433 char *SubBuffer
= Buffer
;
1435 UINT32 LineCount
= 0;
1436 UINT32 LongLineCount
= 0;
1441 EndOfLine
= AsSkipUntilChar (SubBuffer
, '\n');
1444 Gbl_TotalLines
+= LineCount
;
1448 if ((EndOfLine
- SubBuffer
) > 80)
1451 VERBOSE_PRINT (("long: %.80s\n", SubBuffer
));
1455 SubBuffer
= EndOfLine
+ 1;
1460 VERBOSE_PRINT (("%u Lines longer than 80 found in %s\n",
1461 LongLineCount
, Filename
));
1463 Gbl_LongLines
+= LongLineCount
;
1466 Gbl_TotalLines
+= LineCount
;
1471 /******************************************************************************
1473 * FUNCTION: AsCountTabs
1475 * DESCRIPTION: Simply count the number of tabs in the input file buffer
1477 ******************************************************************************/
1485 UINT32 TabCount
= 0;
1488 for (i
= 0; Buffer
[i
]; i
++)
1490 if (Buffer
[i
] == '\t')
1498 AsPrint ("Tabs found", TabCount
, Filename
);
1499 Gbl_Tabs
+= TabCount
;
1502 AsCountLines (Buffer
, Filename
);
1506 /******************************************************************************
1508 * FUNCTION: AsCountSourceLines
1510 * DESCRIPTION: Count the number of C source lines. Defined by 1) not a
1511 * comment, and 2) not a blank line.
1513 ******************************************************************************/
1516 AsCountSourceLines (
1520 char *SubBuffer
= Buffer
;
1521 UINT32 LineCount
= 0;
1522 UINT32 WhiteCount
= 0;
1523 UINT32 CommentCount
= 0;
1528 /* Detect comments (// comments are not used, non-ansii) */
1530 if ((SubBuffer
[0] == '/') &&
1531 (SubBuffer
[1] == '*'))
1535 /* First line of multi-line comment is often just whitespace */
1537 if (SubBuffer
[0] == '\n')
1547 /* Find end of comment */
1549 while (SubBuffer
[0] && SubBuffer
[1] &&
1550 !(((SubBuffer
[0] == '*') &&
1551 (SubBuffer
[1] == '/'))))
1553 if (SubBuffer
[0] == '\n')
1562 /* A linefeed followed by a non-linefeed is a valid source line */
1564 else if ((SubBuffer
[0] == '\n') &&
1565 (SubBuffer
[1] != '\n'))
1570 /* Two back-to-back linefeeds indicate a whitespace line */
1572 else if ((SubBuffer
[0] == '\n') &&
1573 (SubBuffer
[1] == '\n'))
1581 /* Adjust comment count for legal header */
1583 if (Gbl_HeaderSize
< CommentCount
)
1585 CommentCount
-= Gbl_HeaderSize
;
1586 Gbl_HeaderLines
+= Gbl_HeaderSize
;
1589 Gbl_SourceLines
+= LineCount
;
1590 Gbl_WhiteLines
+= WhiteCount
;
1591 Gbl_CommentLines
+= CommentCount
;
1593 VERBOSE_PRINT (("%u Comment %u White %u Code %u Lines in %s\n",
1594 CommentCount
, WhiteCount
, LineCount
,
1595 LineCount
+ WhiteCount
+ CommentCount
, Filename
));
1599 /******************************************************************************
1601 * FUNCTION: AsInsertPrefix
1603 * DESCRIPTION: Insert struct or union prefixes
1605 ******************************************************************************/
1619 char LowerKeyword
[128];
1625 case SRC_TYPE_STRUCT
:
1627 InsertString
= "struct ";
1630 case SRC_TYPE_UNION
:
1632 InsertString
= "union ";
1640 strcpy (LowerKeyword
, Keyword
);
1641 AcpiUtStrlwr (LowerKeyword
);
1645 InsertLength
= strlen (InsertString
);
1646 KeywordLength
= strlen (Keyword
);
1651 /* Find an instance of the keyword */
1653 SubString
= strstr (SubBuffer
, LowerKeyword
);
1659 SubBuffer
= SubString
;
1661 /* Must be standalone word, not a substring */
1663 if (AsMatchExactWord (SubString
, KeywordLength
))
1665 /* Make sure the keyword isn't already prefixed with the insert */
1667 if (!strncmp (SubString
- InsertLength
, InsertString
, InsertLength
))
1669 /* Add spaces if not already at the end-of-line */
1671 if (*(SubBuffer
+ KeywordLength
) != '\n')
1673 /* Already present, add spaces after to align structure members */
1676 /* ONLY FOR C FILES */
1677 AsInsertData (SubBuffer
+ KeywordLength
, " ", 8);
1683 /* Make sure the keyword isn't at the end of a struct/union */
1684 /* Note: This code depends on a single space after the brace */
1686 if (*(SubString
- 2) == '}')
1691 /* Prefix the keyword with the insert string */
1693 Gbl_MadeChanges
= TRUE
;
1695 /* Is there room for insertion */
1697 EndKeyword
= SubString
+ strlen (LowerKeyword
);
1700 while (EndKeyword
[TrailingSpaces
] == ' ')
1706 * Use "if (TrailingSpaces > 1)" if we want to ignore casts
1708 SubBuffer
= SubString
+ InsertLength
;
1710 if (TrailingSpaces
> InsertLength
)
1712 /* Insert the keyword */
1714 memmove (SubBuffer
, SubString
, KeywordLength
);
1716 /* Insert the keyword */
1718 memmove (SubString
, InsertString
, InsertLength
);
1722 AsInsertData (SubString
, InsertString
, InsertLength
);
1727 SubBuffer
+= KeywordLength
;
1731 #ifdef ACPI_FUTURE_IMPLEMENTATION
1732 /******************************************************************************
1734 * FUNCTION: AsTrimComments
1736 * DESCRIPTION: Finds 3-line comments with only a single line of text
1738 ******************************************************************************/
1745 char *SubBuffer
= Buffer
;
1749 UINT32 ShortCommentCount
= 0;
1754 /* Find comment open, within procedure level */
1756 SubBuffer
= strstr (SubBuffer
, " /*");
1762 /* Find comment terminator */
1764 Ptr1
= strstr (SubBuffer
, "*/");
1770 /* Find next EOL (from original buffer) */
1772 Ptr2
= strstr (SubBuffer
, "\n");
1778 /* Ignore one-line comments */
1782 /* Normal comment, ignore and continue; */
1788 /* Examine multi-line comment */
1796 Ptr2
= strstr (Ptr2
, "\n");
1809 ShortCommentCount
++;
1816 if (ShortCommentCount
)
1818 AsPrint ("Short Comments found", ShortCommentCount
, Filename
);
1823 #ifdef ACPI_UNUSED_FUNCTIONS
1824 /******************************************************************************
1826 * FUNCTION: AsCheckAndSkipLiterals
1828 * DESCRIPTION: Generic routine to skip comments and quoted string literals.
1829 * Keeps a line count.
1831 ******************************************************************************/
1834 AsCheckAndSkipLiterals (
1836 UINT32
*TotalLines
);
1840 AsCheckAndSkipLiterals (
1844 UINT32 NewLines
= 0;
1845 char *SubBuffer
= Buffer
;
1849 /* Ignore comments */
1851 if ((SubBuffer
[0] == '/') &&
1852 (SubBuffer
[1] == '*'))
1854 LiteralEnd
= strstr (SubBuffer
, "*/");
1855 SubBuffer
+= 2; /* Get past comment opening */
1862 while (SubBuffer
< LiteralEnd
)
1864 if (*SubBuffer
== '\n')
1872 SubBuffer
+= 2; /* Get past comment close */
1875 /* Ignore quoted strings */
1877 else if (*SubBuffer
== '\"')
1880 LiteralEnd
= AsSkipPastChar (SubBuffer
, '\"');
1889 (*TotalLines
) += NewLines
;