]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/VfrCompile/VfrError.cpp
BaseTools: Clean up source files
[mirror_edk2.git] / BaseTools / Source / C / VfrCompile / VfrError.cpp
1 /** @file
2
3 VfrCompiler error handler.
4
5 Copyright (c) 2004 - 2018, 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_BIT_WIDTH_ERROR, ": bit width must be <= sizeof (type) * 8 and the max width can not > 32" },
50 { VFR_RETURN_CODEUNDEFINED, ": undefined Error Code" }
51 };
52
53 static SVFR_WARNING_HANDLE VFR_WARNING_HANDLE_TABLE [] = {
54 { VFR_WARNING_DEFAULT_VALUE_REDEFINED, ": default value re-defined with different value"},
55 { VFR_WARNING_STRING_TO_UINT_OVERFLOW, ": String to UINT* Overflow"},
56 { VFR_WARNING_ACTION_WITH_TEXT_TWO, ": Action opcode should not have TextTwo part"},
57 { VFR_WARNING_OBSOLETED_FRAMEWORK_OPCODE, ": Not recommend to use obsoleted framework opcode"},
58 { VFR_WARNING_CODEUNDEFINED, ": undefined Warning Code" }
59 };
60
61 CVfrErrorHandle::CVfrErrorHandle (
62 VOID
63 )
64 {
65 mInputFileName = NULL;
66 mScopeRecordListHead = NULL;
67 mScopeRecordListTail = NULL;
68 mVfrErrorHandleTable = VFR_ERROR_HANDLE_TABLE;
69 mVfrWarningHandleTable = VFR_WARNING_HANDLE_TABLE;
70 mWarningAsError = FALSE;
71 }
72
73 CVfrErrorHandle::~CVfrErrorHandle (
74 VOID
75 )
76 {
77 SVfrFileScopeRecord *pNode = NULL;
78
79 if (mInputFileName != NULL) {
80 delete[] mInputFileName;
81 }
82
83 while (mScopeRecordListHead != NULL) {
84 pNode = mScopeRecordListHead;
85 mScopeRecordListHead = mScopeRecordListHead->mNext;
86 delete pNode;
87 }
88
89 mScopeRecordListHead = NULL;
90 mScopeRecordListTail = NULL;
91 mVfrErrorHandleTable = NULL;
92 mVfrWarningHandleTable = NULL;
93 }
94
95 VOID
96 CVfrErrorHandle::SetWarningAsError (
97 IN BOOLEAN WarningAsError
98 )
99 {
100 mWarningAsError = WarningAsError;
101 }
102
103 VOID
104 CVfrErrorHandle::SetInputFile (
105 IN CHAR8 *InputFile
106 )
107 {
108 if (InputFile != NULL) {
109 mInputFileName = new CHAR8[strlen(InputFile) + 1];
110 strcpy (mInputFileName, InputFile);
111 }
112 }
113
114 SVfrFileScopeRecord::SVfrFileScopeRecord (
115 IN CHAR8 *Record,
116 IN UINT32 LineNum
117 )
118 {
119 UINT32 Index;
120 CHAR8 *FileName = NULL;
121 CHAR8 *Str = NULL;
122
123 mWholeScopeLine = LineNum;
124 mNext = NULL;
125
126 Str = strchr (Record, ' ');
127 mScopeLineStart = atoi (++Str);
128
129 Str = strchr (Str, '\"');
130 FileName = ++Str;
131
132 while((Str = strstr (FileName, "\\\\")) != NULL) {
133 FileName = Str + 2;
134 }
135 if ((mFileName = new CHAR8[strlen(FileName)]) != NULL) {
136 for (Index = 0; FileName[Index] != '\"'; Index++) {
137 mFileName[Index] = FileName[Index];
138 }
139 mFileName[Index] = '\0';
140 }
141
142 return;
143 }
144
145 SVfrFileScopeRecord::~SVfrFileScopeRecord (
146 VOID
147 )
148 {
149 if (mFileName != NULL) {
150 delete[] mFileName;
151 }
152 }
153
154 VOID
155 CVfrErrorHandle::ParseFileScopeRecord (
156 IN CHAR8 *Record,
157 IN UINT32 WholeScopeLine
158 )
159 {
160 SVfrFileScopeRecord *pNode = NULL;
161
162 if (Record == NULL) {
163 return;
164 }
165
166 if ((pNode = new SVfrFileScopeRecord(Record, WholeScopeLine)) == NULL) {
167 return;
168 }
169
170 if (mScopeRecordListHead == NULL) {
171 mScopeRecordListTail = mScopeRecordListHead = pNode;
172 } else {
173 mScopeRecordListTail->mNext = pNode;
174 mScopeRecordListTail = pNode;
175 }
176 }
177
178 VOID
179 CVfrErrorHandle::GetFileNameLineNum (
180 IN UINT32 LineNum,
181 OUT CHAR8 **FileName,
182 OUT UINT32 *FileLine
183 )
184 {
185 SVfrFileScopeRecord *pNode = NULL;
186
187 if ((FileName == NULL) || (FileLine == NULL)) {
188 return;
189 }
190
191 *FileName = NULL;
192 *FileLine = 0xFFFFFFFF;
193
194 //
195 // Some errors occur before scope record list been built.
196 //
197 if (mScopeRecordListHead == NULL) {
198 *FileLine = LineNum;
199 *FileName = mInputFileName;
200 return ;
201 }
202
203 for (pNode = mScopeRecordListHead; pNode->mNext != NULL; pNode = pNode->mNext) {
204 if ((LineNum > pNode->mWholeScopeLine) && (pNode->mNext->mWholeScopeLine > LineNum)) {
205 *FileName = pNode->mFileName;
206 *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1;
207 return ;
208 }
209 }
210
211 *FileName = pNode->mFileName;
212 *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1;
213 }
214
215 VOID
216 CVfrErrorHandle::PrintMsg (
217 IN UINT32 LineNum,
218 IN CHAR8 *TokName,
219 IN CONST CHAR8 *MsgType,
220 IN CONST CHAR8 *ErrorMsg
221 )
222 {
223 CHAR8 *FileName = NULL;
224 UINT32 FileLine;
225
226 if (strncmp ("Warning", MsgType, strlen ("Warning")) == 0) {
227 VerboseMsg ((CHAR8 *) ErrorMsg);
228 return;
229 }
230 GetFileNameLineNum (LineNum, &FileName, &FileLine);
231 Error (FileName, FileLine, 0x3000, TokName, (CHAR8 *) "\t%s\n", (CHAR8 *) ErrorMsg);
232 }
233
234 UINT8
235 CVfrErrorHandle::HandleError (
236 IN EFI_VFR_RETURN_CODE ErrorCode,
237 IN UINT32 LineNum,
238 IN CHAR8 *TokName
239 )
240 {
241 UINT32 Index;
242 CHAR8 *FileName = NULL;
243 UINT32 FileLine;
244 CONST CHAR8 *ErrorMsg = NULL;
245
246 if (mVfrErrorHandleTable == NULL) {
247 return 1;
248 }
249
250 for (Index = 0; mVfrErrorHandleTable[Index].mErrorCode != VFR_RETURN_CODEUNDEFINED; Index++) {
251 if (ErrorCode == mVfrErrorHandleTable[Index].mErrorCode) {
252 ErrorMsg = mVfrErrorHandleTable[Index].mErrorMsg;
253 break;
254 }
255 }
256
257 if (ErrorMsg != NULL) {
258 GetFileNameLineNum (LineNum, &FileName, &FileLine);
259 Error (FileName, FileLine, 0x3000, TokName, (CHAR8 *) "\t%s\n", (CHAR8 *) ErrorMsg);
260 return 1;
261 } else {
262 return 0;
263 }
264 }
265
266 UINT8
267 CVfrErrorHandle::HandleWarning (
268 IN EFI_VFR_WARNING_CODE WarningCode,
269 IN UINT32 LineNum,
270 IN CHAR8 *TokName
271 )
272 {
273 UINT32 Index;
274 CHAR8 *FileName = NULL;
275 UINT32 FileLine;
276 CONST CHAR8 *WarningMsg = NULL;
277
278 if (mVfrWarningHandleTable == NULL) {
279 return 1;
280 }
281
282 GetFileNameLineNum (LineNum, &FileName, &FileLine);
283
284 if (mWarningAsError) {
285 Error (FileName, FileLine, 0x2220, (CHAR8 *) "warning treated as error", NULL);
286 }
287
288 for (Index = 0; mVfrWarningHandleTable[Index].mWarningCode != VFR_WARNING_CODEUNDEFINED; Index++) {
289 if (WarningCode == mVfrWarningHandleTable[Index].mWarningCode) {
290 WarningMsg = mVfrWarningHandleTable[Index].mWarningMsg;
291 break;
292 }
293 }
294
295 if (WarningMsg != NULL) {
296 Warning (FileName, FileLine, 0, TokName, (CHAR8 *) "\t%s\n", (CHAR8 *) WarningMsg);
297 return 1;
298 } else {
299 return 0;
300 }
301 }
302
303 CVfrErrorHandle gCVfrErrorHandle;