]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/contrib/dev/acpica/compiler/aslerror.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / contrib / dev / acpica / compiler / aslerror.c
1
2 /******************************************************************************
3  *
4  * Module Name: aslerror - Error handling and statistics
5  *              $Revision: 1.92 $
6  *
7  *****************************************************************************/
8
9 /******************************************************************************
10  *
11  * 1. Copyright Notice
12  *
13  * Some or all of this work - Copyright (c) 1999 - 2007, Intel Corp.
14  * All rights reserved.
15  *
16  * 2. License
17  *
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
21  * property rights.
22  *
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
29  *
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;
38  *
39  * The above copyright and patent license is granted only if the following
40  * conditions are met:
41  *
42  * 3. Conditions
43  *
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.
55  *
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
66  * make.
67  *
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
72  * distribution.
73  *
74  * 3.4. Intel retains all right, title, and interest in and to the Original
75  * Intel Code.
76  *
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.
81  *
82  * 4. Disclaimer and Export Compliance
83  *
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
90  * PARTICULAR PURPOSE.
91  *
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
99  * LIMITED REMEDY.
100  *
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.
115  *
116  *****************************************************************************/
117
118 #define ASL_EXCEPTIONS
119 #include <contrib/dev/acpica/compiler/aslcompiler.h>
120
121 #define _COMPONENT          ACPI_COMPILER
122         ACPI_MODULE_NAME    ("aslerror")
123
124 /* Local prototypes */
125
126 static void
127 AeAddToErrorLog (
128     ASL_ERROR_MSG           *Enode);
129
130
131 /*******************************************************************************
132  *
133  * FUNCTION:    AeAddToErrorLog
134  *
135  * PARAMETERS:  Enode       - An error node to add to the log
136  *
137  * RETURN:      None
138  *
139  * DESCRIPTION: Add a new error node to the error log.  The error log is
140  *              ordered by the "logical" line number (cumulative line number
141  *              including all include files.)
142  *
143  ******************************************************************************/
144
145 static void
146 AeAddToErrorLog (
147     ASL_ERROR_MSG           *Enode)
148 {
149     ASL_ERROR_MSG           *Next;
150     ASL_ERROR_MSG           *Prev;
151
152
153     if (!Gbl_ErrorLog)
154     {
155         Gbl_ErrorLog = Enode;
156         return;
157     }
158
159     /* List is sorted according to line number */
160
161     if (!Gbl_ErrorLog)
162     {
163         Gbl_ErrorLog = Enode;
164         return;
165     }
166
167     /* Walk error list until we find a line number greater than ours */
168
169     Prev = NULL;
170     Next = Gbl_ErrorLog;
171
172     while ((Next) &&
173            (Next->LogicalLineNumber <= Enode->LogicalLineNumber))
174     {
175         Prev = Next;
176         Next = Next->Next;
177     }
178
179     /* Found our place in the list */
180
181     Enode->Next = Next;
182
183     if (Prev)
184     {
185         Prev->Next = Enode;
186     }
187     else
188     {
189         Gbl_ErrorLog = Enode;
190     }
191 }
192
193
194 /*******************************************************************************
195  *
196  * FUNCTION:    AePrintException
197  *
198  * PARAMETERS:  FileId          - ID of output file
199  *              Enode           - Error node to print
200  *              Header          - Additional text before each message
201  *
202  * RETURN:      None
203  *
204  * DESCRIPTION: Print the contents of an error node.
205  *
206  * NOTE:        We don't use the FlxxxFile I/O functions here because on error
207  *              they abort the compiler and call this function!  Since we
208  *              are reporting errors here, we ignore most output errors and
209  *              just try to get out as much as we can.
210  *
211  ******************************************************************************/
212
213 void
214 AePrintException (
215     UINT32                  FileId,
216     ASL_ERROR_MSG           *Enode,
217     char                    *Header)
218 {
219     UINT8                   SourceByte;
220     int                     Actual;
221     size_t                  RActual;
222     UINT32                  MsgLength;
223     char                    *MainMessage;
224     char                    *ExtraMessage;
225     UINT32                  SourceColumn;
226     UINT32                  ErrorColumn;
227     FILE                    *OutputFile;
228     FILE                    *SourceFile;
229
230
231     /*
232      * Only listing files have a header, and remarks/optimizations
233      * are always output
234      */
235     if (!Header)
236     {
237         /* Ignore remarks if requested */
238
239         switch (Enode->Level)
240         {
241         case ASL_REMARK:
242             if (!Gbl_DisplayRemarks)
243             {
244                 return;
245             }
246             break;
247
248         case ASL_OPTIMIZATION:
249             if (!Gbl_DisplayOptimizations)
250             {
251                 return;
252             }
253             break;
254
255         default:
256             break;
257         }
258     }
259
260     /* Get the file handles */
261
262     OutputFile = Gbl_Files[FileId].Handle;
263     SourceFile = Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle;
264
265     if (Header)
266     {
267         fprintf (OutputFile, "%s", Header);
268     }
269
270     /* Print filename and line number if present and valid */
271
272     if (Enode->Filename)
273     {
274         if (Gbl_VerboseErrors)
275         {
276             fprintf (OutputFile, "%6s", Enode->Filename);
277
278             if (Enode->LineNumber)
279             {
280                 fprintf (OutputFile, "%6u: ", Enode->LineNumber);
281
282                 /*
283                  * Seek to the offset in the combined source file, read the source
284                  * line, and write it to the output.
285                  */
286                 Actual = fseek (SourceFile, (long) Enode->LogicalByteOffset,
287                             (int) SEEK_SET);
288                 if (Actual)
289                 {
290                     fprintf (OutputFile,
291                         "[*** iASL: Seek error on source code temp file ***]");
292                 }
293                 else
294                 {
295                     RActual = fread (&SourceByte, 1, 1, SourceFile);
296                     if (!RActual)
297                     {
298                         fprintf (OutputFile,
299                             "[*** iASL: Read error on source code temp file ***]");
300                     }
301
302                     else while (RActual && SourceByte && (SourceByte != '\n'))
303                     {
304                         fwrite (&SourceByte, 1, 1, OutputFile);
305                         RActual = fread (&SourceByte, 1, 1, SourceFile);
306                     }
307                 }
308                 fprintf (OutputFile, "\n");
309             }
310         }
311         else
312         {
313             fprintf (OutputFile, "%s", Enode->Filename);
314
315             if (Enode->LineNumber)
316             {
317                 fprintf (OutputFile, "(%u) : ", Enode->LineNumber);
318             }
319         }
320     }
321
322     /* NULL message ID, just print the raw message */
323
324     if (Enode->MessageId == 0)
325     {
326         fprintf (OutputFile, "%s\n", Enode->Message);
327     }
328     else
329     {
330         /* Decode the message ID */
331
332         fprintf (OutputFile, "%s %4.4d -",
333                     AslErrorLevel[Enode->Level],
334                     Enode->MessageId + ((Enode->Level+1) * 1000));
335
336         MainMessage = AslMessages[Enode->MessageId];
337         ExtraMessage = Enode->Message;
338
339         if (Enode->LineNumber)
340         {
341             MsgLength = strlen (MainMessage);
342             if (MsgLength == 0)
343             {
344                 MainMessage = Enode->Message;
345
346                 MsgLength = strlen (MainMessage);
347                 ExtraMessage = NULL;
348             }
349
350             if (Gbl_VerboseErrors)
351             {
352                 SourceColumn = Enode->Column + Enode->FilenameLength + 6 + 2;
353                 ErrorColumn = ASL_ERROR_LEVEL_LENGTH + 5 + 2 + 1;
354
355                 if ((MsgLength + ErrorColumn) < (SourceColumn - 1))
356                 {
357                     fprintf (OutputFile, "%*s%s",
358                         (int) ((SourceColumn - 1) - ErrorColumn),
359                         MainMessage, " ^ ");
360                 }
361                 else
362                 {
363                     fprintf (OutputFile, "%*s %s",
364                         (int) ((SourceColumn - ErrorColumn) + 1), "^",
365                         MainMessage);
366                 }
367             }
368             else
369             {
370                 fprintf (OutputFile, " %s", MainMessage);
371             }
372
373             /* Print the extra info message if present */
374
375             if (ExtraMessage)
376             {
377                 fprintf (OutputFile, " (%s)", ExtraMessage);
378             }
379
380             fprintf (OutputFile, "\n");
381             if (Gbl_VerboseErrors)
382             {
383                 fprintf (OutputFile, "\n");
384             }
385         }
386         else
387         {
388             fprintf (OutputFile, " %s %s\n\n", MainMessage, ExtraMessage);
389         }
390     }
391 }
392
393
394 /*******************************************************************************
395  *
396  * FUNCTION:    AePrintErrorLog
397  *
398  * PARAMETERS:  FileId           - Where to output the error log
399  *
400  * RETURN:      None
401  *
402  * DESCRIPTION: Print the entire contents of the error log
403  *
404  ******************************************************************************/
405
406 void
407 AePrintErrorLog (
408     UINT32                  FileId)
409 {
410     ASL_ERROR_MSG           *Enode = Gbl_ErrorLog;
411
412
413     /* Walk the error node list */
414
415     while (Enode)
416     {
417         AePrintException (FileId, Enode, NULL);
418         Enode = Enode->Next;
419     }
420 }
421
422
423 /*******************************************************************************
424  *
425  * FUNCTION:    AslCommonError
426  *
427  * PARAMETERS:  Level               - Seriousness (Warning/error, etc.)
428  *              MessageId           - Index into global message buffer
429  *              CurrentLineNumber   - Actual file line number
430  *              LogicalLineNumber   - Cumulative line number
431  *              LogicalByteOffset   - Byte offset in source file
432  *              Column              - Column in current line
433  *              Filename            - source filename
434  *              ExtraMessage        - additional error message
435  *
436  * RETURN:      None
437  *
438  * DESCRIPTION: Create a new error node and add it to the error log
439  *
440  ******************************************************************************/
441
442 void
443 AslCommonError (
444     UINT8                   Level,
445     UINT8                   MessageId,
446     UINT32                  CurrentLineNumber,
447     UINT32                  LogicalLineNumber,
448     UINT32                  LogicalByteOffset,
449     UINT32                  Column,
450     char                    *Filename,
451     char                    *ExtraMessage)
452 {
453     UINT32                  MessageSize;
454     char                    *MessageBuffer = NULL;
455     ASL_ERROR_MSG           *Enode;
456
457
458     Enode = UtLocalCalloc (sizeof (ASL_ERROR_MSG));
459
460     if (ExtraMessage)
461     {
462         /* Allocate a buffer for the message and a new error node */
463
464         MessageSize   = strlen (ExtraMessage) + 1;
465         MessageBuffer = UtLocalCalloc (MessageSize);
466
467         /* Keep a copy of the extra message */
468
469         ACPI_STRCPY (MessageBuffer, ExtraMessage);
470     }
471
472     /* Initialize the error node */
473
474     if (Filename)
475     {
476         Enode->Filename       = Filename;
477         Enode->FilenameLength = strlen (Filename);
478         if (Enode->FilenameLength < 6)
479         {
480             Enode->FilenameLength = 6;
481         }
482     }
483
484     Enode->MessageId            = MessageId;
485     Enode->Level                = Level;
486     Enode->LineNumber           = CurrentLineNumber;
487     Enode->LogicalLineNumber    = LogicalLineNumber;
488     Enode->LogicalByteOffset    = LogicalByteOffset;
489     Enode->Column               = Column;
490     Enode->Message              = MessageBuffer;
491
492     /* Add the new node to the error node list */
493
494     AeAddToErrorLog (Enode);
495
496     if (Gbl_DebugFlag)
497     {
498         /* stderr is a file, send error to it immediately */
499
500         AePrintException (ASL_FILE_STDERR, Enode, NULL);
501     }
502
503     Gbl_ExceptionCount[Level]++;
504     if (Gbl_ExceptionCount[ASL_ERROR] > ASL_MAX_ERROR_COUNT)
505     {
506         printf ("\nMaximum error count (%d) exceeded\n", ASL_MAX_ERROR_COUNT);
507
508         Gbl_SourceLine = 0;
509         Gbl_NextError = Gbl_ErrorLog;
510         CmDoOutputFiles ();
511         CmCleanupAndExit ();
512     }
513
514     return;
515 }
516
517
518 /*******************************************************************************
519  *
520  * FUNCTION:    AslError
521  *
522  * PARAMETERS:  Level               - Seriousness (Warning/error, etc.)
523  *              MessageId           - Index into global message buffer
524  *              Op                  - Parse node where error happened
525  *              ExtraMessage        - additional error message
526  *
527  * RETURN:      None
528  *
529  * DESCRIPTION: Main error reporting routine for the ASL compiler (all code
530  *              except the parser.)
531  *
532  ******************************************************************************/
533
534 void
535 AslError (
536     UINT8                   Level,
537     UINT8                   MessageId,
538     ACPI_PARSE_OBJECT       *Op,
539     char                    *ExtraMessage)
540 {
541
542     switch (Level)
543     {
544     case ASL_WARNING2:
545     case ASL_WARNING3:
546         if (Gbl_WarningLevel < Level)
547         {
548             return;
549         }
550         break;
551
552     default:
553         break;
554     }
555
556
557     if (Op)
558     {
559         AslCommonError (Level, MessageId, Op->Asl.LineNumber,
560                         Op->Asl.LogicalLineNumber,
561                         Op->Asl.LogicalByteOffset,
562                         Op->Asl.Column,
563                         Op->Asl.Filename, ExtraMessage);
564     }
565     else
566     {
567         AslCommonError (Level, MessageId, 0,
568                         0, 0, 0, NULL, ExtraMessage);
569     }
570 }
571
572
573 /*******************************************************************************
574  *
575  * FUNCTION:    AslCoreSubsystemError
576  *
577  * PARAMETERS:  Op                  - Parse node where error happened
578  *              Status              - The ACPI CA Exception
579  *              ExtraMessage        - additional error message
580  *              Abort               - TRUE -> Abort compilation
581  *
582  * RETURN:      None
583  *
584  * DESCRIPTION: Error reporting routine for exceptions returned by the ACPI
585  *              CA core subsystem.
586  *
587  ******************************************************************************/
588
589 void
590 AslCoreSubsystemError (
591     ACPI_PARSE_OBJECT       *Op,
592     ACPI_STATUS             Status,
593     char                    *ExtraMessage,
594     BOOLEAN                 Abort)
595 {
596
597     sprintf (MsgBuffer, "%s %s", AcpiFormatException (Status), ExtraMessage);
598
599     if (Op)
600     {
601         AslCommonError (ASL_ERROR, ASL_MSG_CORE_EXCEPTION, Op->Asl.LineNumber,
602                         Op->Asl.LogicalLineNumber,
603                         Op->Asl.LogicalByteOffset,
604                         Op->Asl.Column,
605                         Op->Asl.Filename, MsgBuffer);
606     }
607     else
608     {
609         AslCommonError (ASL_ERROR, ASL_MSG_CORE_EXCEPTION, 0,
610                         0, 0, 0, NULL, MsgBuffer);
611     }
612
613     if (Abort)
614     {
615         AslAbort ();
616     }
617 }
618
619
620 /*******************************************************************************
621  *
622  * FUNCTION:    AslCompilererror
623  *
624  * PARAMETERS:  CompilerMessage         - Error message from the parser
625  *
626  * RETURN:      Status (0 for now)
627  *
628  * DESCRIPTION: Report an error situation discovered in a production
629  *              NOTE: don't change the name of this function, it is called
630  *              from the auto-generated parser.
631  *
632  ******************************************************************************/
633
634 int
635 AslCompilererror (
636     char                    *CompilerMessage)
637 {
638
639     AslCommonError (ASL_ERROR, ASL_MSG_SYNTAX, Gbl_CurrentLineNumber,
640                     Gbl_LogicalLineNumber, Gbl_CurrentLineOffset,
641                     Gbl_CurrentColumn, Gbl_Files[ASL_FILE_INPUT].Filename,
642                     CompilerMessage);
643
644     return 0;
645 }
646
647