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