]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/VfrCompile/VfrError.cpp
71305364f69ad2d2e4e7c185bcb8ed930b098ff9
[mirror_edk2.git] / BaseTools / Source / C / VfrCompile / VfrError.cpp
1 /** @file
2
3 VfrCompiler error handler.
4
5 Copyright (c) 2004 - 2013, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "stdio.h"
17 #include "string.h"
18 #include "stdlib.h"
19 #include "VfrError.h"
20 #include "EfiUtilityMsgs.h"
21
22 static SVFR_ERROR_HANDLE VFR_ERROR_HANDLE_TABLE [] = {
23 { VFR_RETURN_SUCCESS, NULL },
24 { VFR_RETURN_ERROR_SKIPED, NULL },
25 { VFR_RETURN_FATAL_ERROR, ": fatal error!!" },
26
27 { VFR_RETURN_MISMATCHED, ": unexpected token" },
28 { VFR_RETURN_INVALID_PARAMETER, ": invalid parameter" },
29 { VFR_RETURN_OUT_FOR_RESOURCES, ": system out of memory" },
30 { VFR_RETURN_UNSUPPORTED, ": unsupported" },
31 { VFR_RETURN_REDEFINED, ": already defined" },
32 { VFR_RETURN_FORMID_REDEFINED, ": form id already defined" },
33 { VFR_RETURN_QUESTIONID_REDEFINED, ": question id already defined" },
34 { VFR_RETURN_VARSTOREID_REDEFINED, ": varstore id already defined" },
35 { VFR_RETURN_UNDEFINED, ": undefined" },
36 { VFR_RETURN_VAR_NOTDEFINED_BY_QUESTION, ": some variable has not defined by a question"},
37 { VFR_RETURN_VARSTORE_DATATYPE_REDEFINED_ERROR, ": Data Structure is defined by more than one varstores, it can't be referred as varstore, only varstore name could be used."},
38 { VFR_RETURN_GET_EFIVARSTORE_ERROR, ": get efi varstore error"},
39 { VFR_RETURN_EFIVARSTORE_USE_ERROR, ": can not use the efi varstore like this" },
40 { VFR_RETURN_EFIVARSTORE_SIZE_ERROR, ": unsupport efi varstore size should be <= 8 bytes" },
41 { VFR_RETURN_GET_NVVARSTORE_ERROR, ": get name value varstore error" },
42 { VFR_RETURN_QVAR_REUSE, ": variable reused by more than one question" },
43 { VFR_RETURN_FLAGS_UNSUPPORTED, ": flags unsupported" },
44 { VFR_RETURN_ERROR_ARRARY_NUM, ": array number error, the valid value is in (0 ~ MAX_INDEX-1) for UEFI vfr and in (1 ~ MAX_INDEX) for Framework Vfr" },
45 { VFR_RETURN_DATA_STRING_ERROR, ": data field string error or not support"},
46 { VFR_RETURN_DEFAULT_VALUE_REDEFINED, ": default value re-defined with different value"},
47 { VFR_RETURN_CONSTANT_ONLY, ": only constant is allowed in the expression"},
48 { VFR_RETURN_VARSTORE_NAME_REDEFINED_ERROR, ": Varstore name is defined by more than one varstores, it can't be referred as varstore, only varstore strucure name could be used."},
49 { VFR_RETURN_CODEUNDEFINED, ": undefined Error Code" }
50 };
51
52 static SVFR_WARNING_HANDLE VFR_WARNING_HANDLE_TABLE [] = {
53 { VFR_WARNING_DEFAULT_VALUE_REDEFINED, ": default value re-defined with different value"},
54 { VFR_WARNING_CODEUNDEFINED, ": undefined Warning Code" }
55 };
56
57 CVfrErrorHandle::CVfrErrorHandle (
58 VOID
59 )
60 {
61 mInputFileName = NULL;
62 mScopeRecordListHead = NULL;
63 mScopeRecordListTail = NULL;
64 mVfrErrorHandleTable = VFR_ERROR_HANDLE_TABLE;
65 mVfrWarningHandleTable = VFR_WARNING_HANDLE_TABLE;
66 }
67
68 CVfrErrorHandle::~CVfrErrorHandle (
69 VOID
70 )
71 {
72 SVfrFileScopeRecord *pNode = NULL;
73
74 if (mInputFileName != NULL) {
75 delete mInputFileName;
76 }
77
78 while (mScopeRecordListHead != NULL) {
79 pNode = mScopeRecordListHead;
80 mScopeRecordListHead = mScopeRecordListHead->mNext;
81 delete pNode;
82 }
83
84 mScopeRecordListHead = NULL;
85 mScopeRecordListTail = NULL;
86 mVfrErrorHandleTable = NULL;
87 mVfrWarningHandleTable = NULL;
88 }
89
90 VOID
91 CVfrErrorHandle::SetWarningAsError (
92 IN BOOLEAN WarningAsError
93 )
94 {
95 mWarningAsError = WarningAsError;
96 }
97
98 VOID
99 CVfrErrorHandle::SetInputFile (
100 IN CHAR8 *InputFile
101 )
102 {
103 if (InputFile != NULL) {
104 mInputFileName = new CHAR8[strlen(InputFile) + 1];
105 strcpy (mInputFileName, InputFile);
106 }
107 }
108
109 SVfrFileScopeRecord::SVfrFileScopeRecord (
110 IN CHAR8 *Record,
111 IN UINT32 LineNum
112 )
113 {
114 UINT32 Index;
115 CHAR8 *FileName = NULL;
116 CHAR8 *Str = NULL;
117
118 mWholeScopeLine = LineNum;
119 mNext = NULL;
120
121 Str = strchr (Record, ' ');
122 mScopeLineStart = atoi (++Str);
123
124 Str = strchr (Str, '\"');
125 FileName = ++Str;
126
127 while((Str = strstr (FileName, "\\\\")) != NULL) {
128 FileName = Str + 2;
129 }
130 if ((mFileName = new CHAR8[strlen(FileName)]) != NULL) {
131 for (Index = 0; FileName[Index] != '\"'; Index++) {
132 mFileName[Index] = FileName[Index];
133 }
134 mFileName[Index] = '\0';
135 }
136
137 return;
138 }
139
140 SVfrFileScopeRecord::~SVfrFileScopeRecord (
141 VOID
142 )
143 {
144 if (mFileName != NULL) {
145 delete mFileName;
146 }
147 }
148
149 VOID
150 CVfrErrorHandle::ParseFileScopeRecord (
151 IN CHAR8 *Record,
152 IN UINT32 WholeScopeLine
153 )
154 {
155 SVfrFileScopeRecord *pNode = NULL;
156
157 if (Record == NULL) {
158 return;
159 }
160
161 if ((pNode = new SVfrFileScopeRecord(Record, WholeScopeLine)) == NULL) {
162 return;
163 }
164
165 if (mScopeRecordListHead == NULL) {
166 mScopeRecordListTail = mScopeRecordListHead = pNode;
167 } else {
168 mScopeRecordListTail->mNext = pNode;
169 mScopeRecordListTail = pNode;
170 }
171 }
172
173 VOID
174 CVfrErrorHandle::GetFileNameLineNum (
175 IN UINT32 LineNum,
176 OUT CHAR8 **FileName,
177 OUT UINT32 *FileLine
178 )
179 {
180 SVfrFileScopeRecord *pNode = NULL;
181
182 if ((FileName == NULL) || (FileLine == NULL)) {
183 return;
184 }
185
186 *FileName = NULL;
187 *FileLine = 0xFFFFFFFF;
188
189 //
190 // Some errors occur before scope record list been built.
191 //
192 if (mScopeRecordListHead == NULL) {
193 *FileLine = LineNum;
194 *FileName = mInputFileName;
195 return ;
196 }
197
198 for (pNode = mScopeRecordListHead; pNode->mNext != NULL; pNode = pNode->mNext) {
199 if ((LineNum > pNode->mWholeScopeLine) && (pNode->mNext->mWholeScopeLine > LineNum)) {
200 *FileName = pNode->mFileName;
201 *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1;
202 return ;
203 }
204 }
205
206 *FileName = pNode->mFileName;
207 *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1;
208 }
209
210 VOID
211 CVfrErrorHandle::PrintMsg (
212 IN UINT32 LineNum,
213 IN CHAR8 *TokName,
214 IN CONST CHAR8 *MsgType,
215 IN CONST CHAR8 *ErrorMsg
216 )
217 {
218 CHAR8 *FileName = NULL;
219 UINT32 FileLine;
220
221 if (strncmp ("Warning", MsgType, strlen ("Warning")) == 0) {
222 VerboseMsg ((CHAR8 *) ErrorMsg);
223 return;
224 }
225 GetFileNameLineNum (LineNum, &FileName, &FileLine);
226 Error (FileName, FileLine, 0x3000, TokName, (CHAR8 *) "\t%s\n", (CHAR8 *) ErrorMsg);
227 }
228
229 UINT8
230 CVfrErrorHandle::HandleError (
231 IN EFI_VFR_RETURN_CODE ErrorCode,
232 IN UINT32 LineNum,
233 IN CHAR8 *TokName
234 )
235 {
236 UINT32 Index;
237 CHAR8 *FileName = NULL;
238 UINT32 FileLine;
239 CONST CHAR8 *ErrorMsg = NULL;
240
241 if (mVfrErrorHandleTable == NULL) {
242 return 1;
243 }
244
245 for (Index = 0; mVfrErrorHandleTable[Index].mErrorCode != VFR_RETURN_CODEUNDEFINED; Index++) {
246 if (ErrorCode == mVfrErrorHandleTable[Index].mErrorCode) {
247 ErrorMsg = mVfrErrorHandleTable[Index].mErrorMsg;
248 break;
249 }
250 }
251
252 if (ErrorMsg != NULL) {
253 GetFileNameLineNum (LineNum, &FileName, &FileLine);
254 Error (FileName, FileLine, 0x3000, TokName, (CHAR8 *) "\t%s\n", (CHAR8 *) ErrorMsg);
255 return 1;
256 } else {
257 return 0;
258 }
259 }
260
261 UINT8
262 CVfrErrorHandle::HandleWarning (
263 IN EFI_VFR_WARNING_CODE WarningCode,
264 IN UINT32 LineNum,
265 IN CHAR8 *TokName
266 )
267 {
268 UINT32 Index;
269 CHAR8 *FileName = NULL;
270 UINT32 FileLine;
271 CONST CHAR8 *WarningMsg = NULL;
272
273 if (mVfrWarningHandleTable == NULL) {
274 return 1;
275 }
276
277 GetFileNameLineNum (LineNum, &FileName, &FileLine);
278
279 if (mWarningAsError) {
280 Error (FileName, FileLine, 0x2220, "warning treated as error", NULL);
281 }
282
283 for (Index = 0; mVfrWarningHandleTable[Index].mWarningCode != VFR_WARNING_CODEUNDEFINED; Index++) {
284 if (WarningCode == mVfrWarningHandleTable[Index].mWarningCode) {
285 WarningMsg = mVfrWarningHandleTable[Index].mWarningMsg;
286 break;
287 }
288 }
289
290 if (WarningMsg != NULL) {
291 Warning (FileName, FileLine, 0, TokName, (CHAR8 *) "\t%s\n", (CHAR8 *) WarningMsg);
292 return 1;
293 } else {
294 return 0;
295 }
296 }
297
298 CVfrErrorHandle gCVfrErrorHandle;