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