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