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