]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Ecc/Check.py
BaseTools: Update SkuId checker to make sure it be valid UINT64 value
[mirror_edk2.git] / BaseTools / Source / Python / Ecc / Check.py
1 ## @file
2 # This file is used to define checkpoints used by ECC tool
3 #
4 # Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
9 #
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 #
13 import Common.LongFilePathOs as os
14 import re
15 from CommonDataClass.DataClass import *
16 import Common.DataType as DT
17 from EccToolError import *
18 from MetaDataParser import ParseHeaderCommentSection
19 import EccGlobalData
20 import c
21 from Common.LongFilePathSupport import OpenLongFilePath as open
22 from Common.MultipleWorkspace import MultipleWorkspace as mws
23
24 ## Check
25 #
26 # This class is to define checkpoints used by ECC tool
27 #
28 # @param object: Inherited from object class
29 #
30 class Check(object):
31 def __init__(self):
32 pass
33
34 # Check all required checkpoints
35 def Check(self):
36 self.GeneralCheck()
37 self.MetaDataFileCheck()
38 self.DoxygenCheck()
39 self.IncludeFileCheck()
40 self.PredicateExpressionCheck()
41 self.DeclAndDataTypeCheck()
42 self.FunctionLayoutCheck()
43 self.NamingConventionCheck()
44 self.SmmCommParaCheck()
45
46 def SmmCommParaCheck(self):
47 self.SmmCommParaCheckBufferType()
48
49
50 # Check if SMM communication function has correct parameter type
51 # 1. Get function calling with instance./->Communicate() interface
52 # and make sure the protocol instance is of type EFI_SMM_COMMUNICATION_PROTOCOL.
53 # 2. Find the origin of the 2nd parameter of Communicate() interface, if -
54 # a. it is a local buffer on stack
55 # report error.
56 # b. it is a global buffer, check the driver that holds the global buffer is of type DXE_RUNTIME_DRIVER
57 # report success.
58 # c. it is a buffer by AllocatePage/AllocatePool (may be wrapped by nested function calls),
59 # check the EFI_MEMORY_TYPE to be EfiRuntimeServicesCode,EfiRuntimeServicesData,
60 # EfiACPIMemoryNVS or EfiReservedMemoryType
61 # report success.
62 # d. it is a buffer located via EFI_SYSTEM_TABLE.ConfigurationTable (may be wrapped by nested function calls)
63 # report warning to indicate human code review.
64 # e. it is a buffer from other kind of pointers (may need to trace into nested function calls to locate),
65 # repeat checks in a.b.c and d.
66 def SmmCommParaCheckBufferType(self):
67 if EccGlobalData.gConfig.SmmCommParaCheckBufferType == '1' or EccGlobalData.gConfig.SmmCommParaCheckAll == '1':
68 EdkLogger.quiet("Checking SMM communication parameter type ...")
69 # Get all EFI_SMM_COMMUNICATION_PROTOCOL interface
70 CommApiList = []
71 for IdentifierTable in EccGlobalData.gIdentifierTableList:
72 SqlCommand = """select ID, Name, BelongsToFile from %s
73 where Modifier = 'EFI_SMM_COMMUNICATION_PROTOCOL*' """ % (IdentifierTable)
74 RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
75 if RecordSet:
76 for Record in RecordSet:
77 if Record[1] not in CommApiList:
78 CommApiList.append(Record[1])
79 # For each interface, check the second parameter
80 for CommApi in CommApiList:
81 for IdentifierTable in EccGlobalData.gIdentifierTableList:
82 SqlCommand = """select ID, Name, Value, BelongsToFile, StartLine from %s
83 where Name = '%s->Communicate' and Model = %s""" \
84 % (IdentifierTable, CommApi, MODEL_IDENTIFIER_FUNCTION_CALLING)
85 RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
86 if RecordSet:
87 # print IdentifierTable
88 for Record in RecordSet:
89 # Get the second parameter for Communicate function
90 SecondPara = Record[2].split(',')[1].strip()
91 SecondParaIndex = None
92 if SecondPara.startswith('&'):
93 SecondPara = SecondPara[1:]
94 if SecondPara.endswith(']'):
95 SecondParaIndex = SecondPara[SecondPara.find('[') + 1:-1]
96 SecondPara = SecondPara[:SecondPara.find('[')]
97 # Get the ID
98 Id = Record[0]
99 # Get the BelongsToFile
100 BelongsToFile = Record[3]
101 # Get the source file path
102 SqlCommand = """select FullPath from File where ID = %s""" % BelongsToFile
103 NewRecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
104 FullPath = NewRecordSet[0][0]
105 # Get the line no of function calling
106 StartLine = Record[4]
107 # Get the module type
108 SqlCommand = """select Value3 from INF where BelongsToFile = (select ID from File
109 where Path = (select Path from File where ID = %s) and Model = 1011)
110 and Value2 = 'MODULE_TYPE'""" % BelongsToFile
111 NewRecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
112 ModuleType = NewRecordSet[0][0] if NewRecordSet else None
113
114 # print BelongsToFile, FullPath, StartLine, ModuleType, SecondPara
115
116 Value = FindPara(FullPath, SecondPara, StartLine)
117 # Find the value of the parameter
118 if Value:
119 if 'AllocatePage' in Value \
120 or 'AllocatePool' in Value \
121 or 'AllocateRuntimePool' in Value \
122 or 'AllocateZeroPool' in Value:
123 pass
124 else:
125 if '->' in Value:
126 if not EccGlobalData.gException.IsException(
127 ERROR_SMM_COMM_PARA_CHECK_BUFFER_TYPE, Value):
128 EccGlobalData.gDb.TblReport.Insert(ERROR_SMM_COMM_PARA_CHECK_BUFFER_TYPE,
129 OtherMsg="Please review the buffer type"
130 + "is correct or not. If it is correct" +
131 " please add [%s] to exception list"
132 % Value,
133 BelongsToTable=IdentifierTable,
134 BelongsToItem=Id)
135 else:
136 if not EccGlobalData.gException.IsException(
137 ERROR_SMM_COMM_PARA_CHECK_BUFFER_TYPE, Value):
138 EccGlobalData.gDb.TblReport.Insert(ERROR_SMM_COMM_PARA_CHECK_BUFFER_TYPE,
139 OtherMsg="Please review the buffer type"
140 + "is correct or not. If it is correct" +
141 " please add [%s] to exception list"
142 % Value,
143 BelongsToTable=IdentifierTable,
144 BelongsToItem=Id)
145
146
147 # Not find the value of the parameter
148 else:
149 SqlCommand = """select ID, Modifier, Name, Value, Model, BelongsToFunction from %s
150 where Name = '%s' and StartLine < %s order by StartLine DESC""" \
151 % (IdentifierTable, SecondPara, StartLine)
152 NewRecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
153 if NewRecordSet:
154 Value = NewRecordSet[0][1]
155 if 'AllocatePage' in Value \
156 or 'AllocatePool' in Value \
157 or 'AllocateRuntimePool' in Value \
158 or 'AllocateZeroPool' in Value:
159 pass
160 else:
161 if not EccGlobalData.gException.IsException(
162 ERROR_SMM_COMM_PARA_CHECK_BUFFER_TYPE, Value):
163 EccGlobalData.gDb.TblReport.Insert(ERROR_SMM_COMM_PARA_CHECK_BUFFER_TYPE,
164 OtherMsg="Please review the buffer type"
165 + "is correct or not. If it is correct" +
166 " please add [%s] to exception list"
167 % Value,
168 BelongsToTable=IdentifierTable,
169 BelongsToItem=Id)
170 else:
171 pass
172
173 # Check UNI files
174 def UniCheck(self):
175 if EccGlobalData.gConfig.GeneralCheckUni == '1' or EccGlobalData.gConfig.GeneralCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
176 EdkLogger.quiet("Checking whether UNI file is UTF-16 ...")
177 SqlCommand = """select ID, FullPath, ExtName from File where ExtName like 'uni'"""
178 RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
179 for Record in RecordSet:
180 File = Record[1]
181 FileIn = open(File, 'rb').read(2)
182 if FileIn != '\xff\xfe':
183 OtherMsg = "File %s is not a valid UTF-16 UNI file" % Record[1]
184 EccGlobalData.gDb.TblReport.Insert(ERROR_GENERAL_CHECK_UNI, OtherMsg=OtherMsg, BelongsToTable='File', BelongsToItem=Record[0])
185
186 # General Checking
187 def GeneralCheck(self):
188 self.GeneralCheckNonAcsii()
189 self.UniCheck()
190
191 # Check whether file has non ACSII char
192 def GeneralCheckNonAcsii(self):
193 if EccGlobalData.gConfig.GeneralCheckNonAcsii == '1' or EccGlobalData.gConfig.GeneralCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
194 EdkLogger.quiet("Checking Non-ACSII char in file ...")
195 SqlCommand = """select ID, FullPath, ExtName from File where ExtName in ('.dec', '.inf', '.dsc', 'c', 'h')"""
196 RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
197 for Record in RecordSet:
198 if Record[2].upper() not in EccGlobalData.gConfig.BinaryExtList:
199 op = open(Record[1]).readlines()
200 IndexOfLine = 0
201 for Line in op:
202 IndexOfLine += 1
203 IndexOfChar = 0
204 for Char in Line:
205 IndexOfChar += 1
206 if ord(Char) > 126:
207 OtherMsg = "File %s has Non-ASCII char at line %s column %s" % (Record[1], IndexOfLine, IndexOfChar)
208 EccGlobalData.gDb.TblReport.Insert(ERROR_GENERAL_CHECK_NON_ACSII, OtherMsg=OtherMsg, BelongsToTable='File', BelongsToItem=Record[0])
209
210 # C Function Layout Checking
211 def FunctionLayoutCheck(self):
212 self.FunctionLayoutCheckReturnType()
213 self.FunctionLayoutCheckModifier()
214 self.FunctionLayoutCheckName()
215 self.FunctionLayoutCheckPrototype()
216 self.FunctionLayoutCheckBody()
217 self.FunctionLayoutCheckLocalVariable()
218
219 def WalkTree(self):
220 IgnoredPattern = c.GetIgnoredDirListPattern()
221 for Dirpath, Dirnames, Filenames in os.walk(EccGlobalData.gTarget):
222 for Dir in Dirnames:
223 Dirname = os.path.join(Dirpath, Dir)
224 if os.path.islink(Dirname):
225 Dirname = os.path.realpath(Dirname)
226 if os.path.isdir(Dirname):
227 # symlinks to directories are treated as directories
228 Dirnames.remove(Dir)
229 Dirnames.append(Dirname)
230 if IgnoredPattern.match(Dirpath.upper()):
231 continue
232 for f in Filenames[:]:
233 if f.lower() in EccGlobalData.gConfig.SkipFileList:
234 Filenames.remove(f)
235 yield (Dirpath, Dirnames, Filenames)
236
237 # Check whether return type exists and in the first line
238 def FunctionLayoutCheckReturnType(self):
239 if EccGlobalData.gConfig.CFunctionLayoutCheckReturnType == '1' or EccGlobalData.gConfig.CFunctionLayoutCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
240 EdkLogger.quiet("Checking function layout return type ...")
241
242 # for Dirpath, Dirnames, Filenames in self.WalkTree():
243 # for F in Filenames:
244 # if os.path.splitext(F)[1] in ('.c', '.h'):
245 # FullName = os.path.join(Dirpath, F)
246 # c.CheckFuncLayoutReturnType(FullName)
247 for FullName in EccGlobalData.gCFileList + EccGlobalData.gHFileList:
248 c.CheckFuncLayoutReturnType(FullName)
249
250 # Check whether any optional functional modifiers exist and next to the return type
251 def FunctionLayoutCheckModifier(self):
252 if EccGlobalData.gConfig.CFunctionLayoutCheckOptionalFunctionalModifier == '1' or EccGlobalData.gConfig.CFunctionLayoutCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
253 EdkLogger.quiet("Checking function layout modifier ...")
254
255 # for Dirpath, Dirnames, Filenames in self.WalkTree():
256 # for F in Filenames:
257 # if os.path.splitext(F)[1] in ('.c', '.h'):
258 # FullName = os.path.join(Dirpath, F)
259 # c.CheckFuncLayoutModifier(FullName)
260 for FullName in EccGlobalData.gCFileList + EccGlobalData.gHFileList:
261 c.CheckFuncLayoutModifier(FullName)
262
263 # Check whether the next line contains the function name, left justified, followed by the beginning of the parameter list
264 # Check whether the closing parenthesis is on its own line and also indented two spaces
265 def FunctionLayoutCheckName(self):
266 if EccGlobalData.gConfig.CFunctionLayoutCheckFunctionName == '1' or EccGlobalData.gConfig.CFunctionLayoutCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
267 EdkLogger.quiet("Checking function layout function name ...")
268
269 # for Dirpath, Dirnames, Filenames in self.WalkTree():
270 # for F in Filenames:
271 # if os.path.splitext(F)[1] in ('.c', '.h'):
272 # FullName = os.path.join(Dirpath, F)
273 # c.CheckFuncLayoutName(FullName)
274 for FullName in EccGlobalData.gCFileList + EccGlobalData.gHFileList:
275 c.CheckFuncLayoutName(FullName)
276
277 # Check whether the function prototypes in include files have the same form as function definitions
278 def FunctionLayoutCheckPrototype(self):
279 if EccGlobalData.gConfig.CFunctionLayoutCheckFunctionPrototype == '1' or EccGlobalData.gConfig.CFunctionLayoutCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
280 EdkLogger.quiet("Checking function layout function prototype ...")
281
282 # for Dirpath, Dirnames, Filenames in self.WalkTree():
283 # for F in Filenames:
284 # if os.path.splitext(F)[1] in ('.c'):
285 # FullName = os.path.join(Dirpath, F)
286 # EdkLogger.quiet("[PROTOTYPE]" + FullName)
287 # c.CheckFuncLayoutPrototype(FullName)
288 for FullName in EccGlobalData.gCFileList:
289 EdkLogger.quiet("[PROTOTYPE]" + FullName)
290 c.CheckFuncLayoutPrototype(FullName)
291
292 # Check whether the body of a function is contained by open and close braces that must be in the first column
293 def FunctionLayoutCheckBody(self):
294 if EccGlobalData.gConfig.CFunctionLayoutCheckFunctionBody == '1' or EccGlobalData.gConfig.CFunctionLayoutCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
295 EdkLogger.quiet("Checking function layout function body ...")
296
297 # for Dirpath, Dirnames, Filenames in self.WalkTree():
298 # for F in Filenames:
299 # if os.path.splitext(F)[1] in ('.c'):
300 # FullName = os.path.join(Dirpath, F)
301 # c.CheckFuncLayoutBody(FullName)
302 for FullName in EccGlobalData.gCFileList:
303 c.CheckFuncLayoutBody(FullName)
304
305 # Check whether the data declarations is the first code in a module.
306 # self.CFunctionLayoutCheckDataDeclaration = 1
307 # Check whether no initialization of a variable as part of its declaration
308 def FunctionLayoutCheckLocalVariable(self):
309 if EccGlobalData.gConfig.CFunctionLayoutCheckNoInitOfVariable == '1' or EccGlobalData.gConfig.CFunctionLayoutCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
310 EdkLogger.quiet("Checking function layout local variables ...")
311
312 # for Dirpath, Dirnames, Filenames in self.WalkTree():
313 # for F in Filenames:
314 # if os.path.splitext(F)[1] in ('.c'):
315 # FullName = os.path.join(Dirpath, F)
316 # c.CheckFuncLayoutLocalVariable(FullName)
317
318 for FullName in EccGlobalData.gCFileList:
319 c.CheckFuncLayoutLocalVariable(FullName)
320
321 # Check whether no use of STATIC for functions
322 # self.CFunctionLayoutCheckNoStatic = 1
323
324 # Declarations and Data Types Checking
325 def DeclAndDataTypeCheck(self):
326 self.DeclCheckNoUseCType()
327 self.DeclCheckInOutModifier()
328 self.DeclCheckEFIAPIModifier()
329 self.DeclCheckEnumeratedType()
330 self.DeclCheckStructureDeclaration()
331 self.DeclCheckSameStructure()
332 self.DeclCheckUnionType()
333
334
335 # Check whether no use of int, unsigned, char, void, static, long in any .c, .h or .asl files.
336 def DeclCheckNoUseCType(self):
337 if EccGlobalData.gConfig.DeclarationDataTypeCheckNoUseCType == '1' or EccGlobalData.gConfig.DeclarationDataTypeCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
338 EdkLogger.quiet("Checking Declaration No use C type ...")
339
340 # for Dirpath, Dirnames, Filenames in self.WalkTree():
341 # for F in Filenames:
342 # if os.path.splitext(F)[1] in ('.h', '.c'):
343 # FullName = os.path.join(Dirpath, F)
344 # c.CheckDeclNoUseCType(FullName)
345 for FullName in EccGlobalData.gCFileList + EccGlobalData.gHFileList:
346 c.CheckDeclNoUseCType(FullName)
347
348 # Check whether the modifiers IN, OUT, OPTIONAL, and UNALIGNED are used only to qualify arguments to a function and should not appear in a data type declaration
349 def DeclCheckInOutModifier(self):
350 if EccGlobalData.gConfig.DeclarationDataTypeCheckInOutModifier == '1' or EccGlobalData.gConfig.DeclarationDataTypeCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
351 EdkLogger.quiet("Checking Declaration argument modifier ...")
352
353 # for Dirpath, Dirnames, Filenames in self.WalkTree():
354 # for F in Filenames:
355 # if os.path.splitext(F)[1] in ('.h', '.c'):
356 # FullName = os.path.join(Dirpath, F)
357 # c.CheckDeclArgModifier(FullName)
358 for FullName in EccGlobalData.gCFileList + EccGlobalData.gHFileList:
359 c.CheckDeclArgModifier(FullName)
360
361 # Check whether the EFIAPI modifier should be used at the entry of drivers, events, and member functions of protocols
362 def DeclCheckEFIAPIModifier(self):
363 if EccGlobalData.gConfig.DeclarationDataTypeCheckEFIAPIModifier == '1' or EccGlobalData.gConfig.DeclarationDataTypeCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
364 pass
365
366 # Check whether Enumerated Type has a 'typedef' and the name is capital
367 def DeclCheckEnumeratedType(self):
368 if EccGlobalData.gConfig.DeclarationDataTypeCheckEnumeratedType == '1' or EccGlobalData.gConfig.DeclarationDataTypeCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
369 EdkLogger.quiet("Checking Declaration enum typedef ...")
370
371 # for Dirpath, Dirnames, Filenames in self.WalkTree():
372 # for F in Filenames:
373 # if os.path.splitext(F)[1] in ('.h', '.c'):
374 # FullName = os.path.join(Dirpath, F)
375 # EdkLogger.quiet("[ENUM]" + FullName)
376 # c.CheckDeclEnumTypedef(FullName)
377 for FullName in EccGlobalData.gCFileList + EccGlobalData.gHFileList:
378 EdkLogger.quiet("[ENUM]" + FullName)
379 c.CheckDeclEnumTypedef(FullName)
380
381 # Check whether Structure Type has a 'typedef' and the name is capital
382 def DeclCheckStructureDeclaration(self):
383 if EccGlobalData.gConfig.DeclarationDataTypeCheckStructureDeclaration == '1' or EccGlobalData.gConfig.DeclarationDataTypeCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
384 EdkLogger.quiet("Checking Declaration struct typedef ...")
385
386 # for Dirpath, Dirnames, Filenames in self.WalkTree():
387 # for F in Filenames:
388 # if os.path.splitext(F)[1] in ('.h', '.c'):
389 # FullName = os.path.join(Dirpath, F)
390 # EdkLogger.quiet("[STRUCT]" + FullName)
391 # c.CheckDeclStructTypedef(FullName)
392 for FullName in EccGlobalData.gCFileList + EccGlobalData.gHFileList:
393 EdkLogger.quiet("[STRUCT]" + FullName)
394 c.CheckDeclStructTypedef(FullName)
395
396 # Check whether having same Structure
397 def DeclCheckSameStructure(self):
398 if EccGlobalData.gConfig.DeclarationDataTypeCheckSameStructure == '1' or EccGlobalData.gConfig.DeclarationDataTypeCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
399 EdkLogger.quiet("Checking same struct ...")
400 AllStructure = {}
401 for IdentifierTable in EccGlobalData.gIdentifierTableList:
402 SqlCommand = """select ID, Name, BelongsToFile from %s where Model = %s""" % (IdentifierTable, MODEL_IDENTIFIER_STRUCTURE)
403 RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
404 for Record in RecordSet:
405 if Record[1] != '':
406 if Record[1] not in AllStructure.keys():
407 AllStructure[Record[1]] = Record[2]
408 else:
409 ID = AllStructure[Record[1]]
410 SqlCommand = """select FullPath from File where ID = %s """ % ID
411 NewRecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
412 OtherMsg = "The structure name '%s' is duplicate" % Record[1]
413 if NewRecordSet != []:
414 OtherMsg = "The structure name [%s] is duplicate with the one defined in %s, maybe struct NOT typedefed or the typedef new type NOT used to qualify variables" % (Record[1], NewRecordSet[0][0])
415 if not EccGlobalData.gException.IsException(ERROR_DECLARATION_DATA_TYPE_CHECK_SAME_STRUCTURE, Record[1]):
416 EccGlobalData.gDb.TblReport.Insert(ERROR_DECLARATION_DATA_TYPE_CHECK_SAME_STRUCTURE, OtherMsg=OtherMsg, BelongsToTable=IdentifierTable, BelongsToItem=Record[0])
417
418 # Check whether Union Type has a 'typedef' and the name is capital
419 def DeclCheckUnionType(self):
420 if EccGlobalData.gConfig.DeclarationDataTypeCheckUnionType == '1' or EccGlobalData.gConfig.DeclarationDataTypeCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
421 EdkLogger.quiet("Checking Declaration union typedef ...")
422
423 # for Dirpath, Dirnames, Filenames in self.WalkTree():
424 # for F in Filenames:
425 # if os.path.splitext(F)[1] in ('.h', '.c'):
426 # FullName = os.path.join(Dirpath, F)
427 # EdkLogger.quiet("[UNION]" + FullName)
428 # c.CheckDeclUnionTypedef(FullName)
429 for FullName in EccGlobalData.gCFileList + EccGlobalData.gHFileList:
430 EdkLogger.quiet("[UNION]" + FullName)
431 c.CheckDeclUnionTypedef(FullName)
432
433 # Predicate Expression Checking
434 def PredicateExpressionCheck(self):
435 self.PredicateExpressionCheckBooleanValue()
436 self.PredicateExpressionCheckNonBooleanOperator()
437 self.PredicateExpressionCheckComparisonNullType()
438
439 # Check whether Boolean values, variable type BOOLEAN not use explicit comparisons to TRUE or FALSE
440 def PredicateExpressionCheckBooleanValue(self):
441 if EccGlobalData.gConfig.PredicateExpressionCheckBooleanValue == '1' or EccGlobalData.gConfig.PredicateExpressionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
442 EdkLogger.quiet("Checking predicate expression Boolean value ...")
443
444 # for Dirpath, Dirnames, Filenames in self.WalkTree():
445 # for F in Filenames:
446 # if os.path.splitext(F)[1] in ('.c'):
447 # FullName = os.path.join(Dirpath, F)
448 # EdkLogger.quiet("[BOOLEAN]" + FullName)
449 # c.CheckBooleanValueComparison(FullName)
450 for FullName in EccGlobalData.gCFileList:
451 EdkLogger.quiet("[BOOLEAN]" + FullName)
452 c.CheckBooleanValueComparison(FullName)
453
454 # Check whether Non-Boolean comparisons use a compare operator (==, !=, >, < >=, <=).
455 def PredicateExpressionCheckNonBooleanOperator(self):
456 if EccGlobalData.gConfig.PredicateExpressionCheckNonBooleanOperator == '1' or EccGlobalData.gConfig.PredicateExpressionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
457 EdkLogger.quiet("Checking predicate expression Non-Boolean variable...")
458
459 # for Dirpath, Dirnames, Filenames in self.WalkTree():
460 # for F in Filenames:
461 # if os.path.splitext(F)[1] in ('.c'):
462 # FullName = os.path.join(Dirpath, F)
463 # EdkLogger.quiet("[NON-BOOLEAN]" + FullName)
464 # c.CheckNonBooleanValueComparison(FullName)
465 for FullName in EccGlobalData.gCFileList:
466 EdkLogger.quiet("[NON-BOOLEAN]" + FullName)
467 c.CheckNonBooleanValueComparison(FullName)
468
469 # Check whether a comparison of any pointer to zero must be done via the NULL type
470 def PredicateExpressionCheckComparisonNullType(self):
471 if EccGlobalData.gConfig.PredicateExpressionCheckComparisonNullType == '1' or EccGlobalData.gConfig.PredicateExpressionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
472 EdkLogger.quiet("Checking predicate expression NULL pointer ...")
473
474 # for Dirpath, Dirnames, Filenames in self.WalkTree():
475 # for F in Filenames:
476 # if os.path.splitext(F)[1] in ('.c'):
477 # FullName = os.path.join(Dirpath, F)
478 # EdkLogger.quiet("[POINTER]" + FullName)
479 # c.CheckPointerNullComparison(FullName)
480 for FullName in EccGlobalData.gCFileList:
481 EdkLogger.quiet("[POINTER]" + FullName)
482 c.CheckPointerNullComparison(FullName)
483
484 # Include file checking
485 def IncludeFileCheck(self):
486 self.IncludeFileCheckIfndef()
487 self.IncludeFileCheckData()
488 self.IncludeFileCheckSameName()
489
490 # Check whether having include files with same name
491 def IncludeFileCheckSameName(self):
492 if EccGlobalData.gConfig.IncludeFileCheckSameName == '1' or EccGlobalData.gConfig.IncludeFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
493 EdkLogger.quiet("Checking same header file name ...")
494 SqlCommand = """select ID, FullPath from File
495 where Model = 1002 order by Name """
496 RecordDict = {}
497 RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
498 for Record in RecordSet:
499 List = Record[1].replace('/', '\\').split('\\')
500 if len(List) >= 2:
501 Key = List[-2] + '\\' + List[-1]
502 else:
503 Key = List[0]
504 if Key not in RecordDict:
505 RecordDict[Key] = [Record]
506 else:
507 RecordDict[Key].append(Record)
508
509 for Key in RecordDict:
510 if len(RecordDict[Key]) > 1:
511 for Item in RecordDict[Key]:
512 Path = mws.relpath(Item[1], EccGlobalData.gWorkspace)
513 if not EccGlobalData.gException.IsException(ERROR_INCLUDE_FILE_CHECK_NAME, Path):
514 EccGlobalData.gDb.TblReport.Insert(ERROR_INCLUDE_FILE_CHECK_NAME, OtherMsg="The file name for [%s] is duplicate" % Path, BelongsToTable='File', BelongsToItem=Item[0])
515
516 # Check whether all include file contents is guarded by a #ifndef statement.
517 def IncludeFileCheckIfndef(self):
518 if EccGlobalData.gConfig.IncludeFileCheckIfndefStatement == '1' or EccGlobalData.gConfig.IncludeFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
519 EdkLogger.quiet("Checking header file ifndef ...")
520
521 # for Dirpath, Dirnames, Filenames in self.WalkTree():
522 # for F in Filenames:
523 # if os.path.splitext(F)[1] in ('.h'):
524 # FullName = os.path.join(Dirpath, F)
525 # MsgList = c.CheckHeaderFileIfndef(FullName)
526 for FullName in EccGlobalData.gHFileList:
527 MsgList = c.CheckHeaderFileIfndef(FullName)
528
529 # Check whether include files NOT contain code or define data variables
530 def IncludeFileCheckData(self):
531 if EccGlobalData.gConfig.IncludeFileCheckData == '1' or EccGlobalData.gConfig.IncludeFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
532 EdkLogger.quiet("Checking header file data ...")
533
534 # for Dirpath, Dirnames, Filenames in self.WalkTree():
535 # for F in Filenames:
536 # if os.path.splitext(F)[1] in ('.h'):
537 # FullName = os.path.join(Dirpath, F)
538 # MsgList = c.CheckHeaderFileData(FullName)
539 for FullName in EccGlobalData.gHFileList:
540 MsgList = c.CheckHeaderFileData(FullName)
541
542 # Doxygen document checking
543 def DoxygenCheck(self):
544 self.DoxygenCheckFileHeader()
545 self.DoxygenCheckFunctionHeader()
546 self.DoxygenCheckCommentDescription()
547 self.DoxygenCheckCommentFormat()
548 self.DoxygenCheckCommand()
549
550 # Check whether the file headers are followed Doxygen special documentation blocks in section 2.3.5
551 def DoxygenCheckFileHeader(self):
552 if EccGlobalData.gConfig.DoxygenCheckFileHeader == '1' or EccGlobalData.gConfig.DoxygenCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
553 EdkLogger.quiet("Checking Doxygen file header ...")
554
555 for Dirpath, Dirnames, Filenames in self.WalkTree():
556 for F in Filenames:
557 Ext = os.path.splitext(F)[1]
558 if Ext in ('.h', '.c'):
559 FullName = os.path.join(Dirpath, F)
560 MsgList = c.CheckFileHeaderDoxygenComments(FullName)
561 elif Ext in ('.inf', '.dec', '.dsc', '.fdf'):
562 FullName = os.path.join(Dirpath, F)
563 op = open(FullName).readlines()
564 FileLinesList = op
565 LineNo = 0
566 CurrentSection = MODEL_UNKNOWN
567 HeaderSectionLines = []
568 HeaderCommentStart = False
569 HeaderCommentEnd = False
570
571 for Line in FileLinesList:
572 LineNo = LineNo + 1
573 Line = Line.strip()
574 if (LineNo < len(FileLinesList) - 1):
575 NextLine = FileLinesList[LineNo].strip()
576
577 #
578 # blank line
579 #
580 if (Line == '' or not Line) and LineNo == len(FileLinesList):
581 LastSectionFalg = True
582
583 #
584 # check whether file header comment section started
585 #
586 if Line.startswith('#') and \
587 (Line.find('@file') > -1) and \
588 not HeaderCommentStart:
589 if CurrentSection != MODEL_UNKNOWN:
590 SqlStatement = """ select ID from File where FullPath like '%s'""" % FullName
591 ResultSet = EccGlobalData.gDb.TblFile.Exec(SqlStatement)
592 for Result in ResultSet:
593 Msg = 'INF/DEC/DSC/FDF file header comment should begin with ""## @file"" or ""# @file""at the very top file'
594 EccGlobalData.gDb.TblReport.Insert(ERROR_DOXYGEN_CHECK_FILE_HEADER, Msg, "File", Result[0])
595
596 else:
597 CurrentSection = MODEL_IDENTIFIER_FILE_HEADER
598 #
599 # Append the first line to section lines.
600 #
601 HeaderSectionLines.append((Line, LineNo))
602 HeaderCommentStart = True
603 continue
604
605 #
606 # Collect Header content.
607 #
608 if (Line.startswith('#') and CurrentSection == MODEL_IDENTIFIER_FILE_HEADER) and\
609 HeaderCommentStart and not Line.startswith('##') and not\
610 HeaderCommentEnd and NextLine != '':
611 HeaderSectionLines.append((Line, LineNo))
612 continue
613 #
614 # Header content end
615 #
616 if (Line.startswith('##') or not Line.strip().startswith("#")) and HeaderCommentStart \
617 and not HeaderCommentEnd:
618 if Line.startswith('##'):
619 HeaderCommentEnd = True
620 HeaderSectionLines.append((Line, LineNo))
621 ParseHeaderCommentSection(HeaderSectionLines, FullName)
622 break
623 if HeaderCommentStart == False:
624 SqlStatement = """ select ID from File where FullPath like '%s'""" % FullName
625 ResultSet = EccGlobalData.gDb.TblFile.Exec(SqlStatement)
626 for Result in ResultSet:
627 Msg = 'INF/DEC/DSC/FDF file header comment should begin with ""## @file"" or ""# @file"" at the very top file'
628 EccGlobalData.gDb.TblReport.Insert(ERROR_DOXYGEN_CHECK_FILE_HEADER, Msg, "File", Result[0])
629 if HeaderCommentEnd == False:
630 SqlStatement = """ select ID from File where FullPath like '%s'""" % FullName
631 ResultSet = EccGlobalData.gDb.TblFile.Exec(SqlStatement)
632 for Result in ResultSet:
633 Msg = 'INF/DEC/DSC/FDF file header comment should end with ""##"" at the end of file header comment block'
634 # Check whether File header Comment End with '##'
635 if EccGlobalData.gConfig.HeaderCheckFileCommentEnd == '1' or EccGlobalData.gConfig.HeaderCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
636 EccGlobalData.gDb.TblReport.Insert(ERROR_DOXYGEN_CHECK_FILE_HEADER, Msg, "File", Result[0])
637
638
639
640 # Check whether the function headers are followed Doxygen special documentation blocks in section 2.3.5
641 def DoxygenCheckFunctionHeader(self):
642 if EccGlobalData.gConfig.DoxygenCheckFunctionHeader == '1' or EccGlobalData.gConfig.DoxygenCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
643 EdkLogger.quiet("Checking Doxygen function header ...")
644
645 # for Dirpath, Dirnames, Filenames in self.WalkTree():
646 # for F in Filenames:
647 # if os.path.splitext(F)[1] in ('.h', '.c'):
648 # FullName = os.path.join(Dirpath, F)
649 # MsgList = c.CheckFuncHeaderDoxygenComments(FullName)
650 for FullName in EccGlobalData.gCFileList + EccGlobalData.gHFileList:
651 MsgList = c.CheckFuncHeaderDoxygenComments(FullName)
652
653
654 # Check whether the first line of text in a comment block is a brief description of the element being documented.
655 # The brief description must end with a period.
656 def DoxygenCheckCommentDescription(self):
657 if EccGlobalData.gConfig.DoxygenCheckCommentDescription == '1' or EccGlobalData.gConfig.DoxygenCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
658 pass
659
660 # Check whether comment lines with '///< ... text ...' format, if it is used, it should be after the code section.
661 def DoxygenCheckCommentFormat(self):
662 if EccGlobalData.gConfig.DoxygenCheckCommentFormat == '1' or EccGlobalData.gConfig.DoxygenCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
663 EdkLogger.quiet("Checking Doxygen comment ///< ...")
664
665 # for Dirpath, Dirnames, Filenames in self.WalkTree():
666 # for F in Filenames:
667 # if os.path.splitext(F)[1] in ('.h', '.c'):
668 # FullName = os.path.join(Dirpath, F)
669 # MsgList = c.CheckDoxygenTripleForwardSlash(FullName)
670 for FullName in EccGlobalData.gCFileList + EccGlobalData.gHFileList:
671 MsgList = c.CheckDoxygenTripleForwardSlash(FullName)
672
673 # Check whether only Doxygen commands allowed to mark the code are @bug and @todo.
674 def DoxygenCheckCommand(self):
675 if EccGlobalData.gConfig.DoxygenCheckCommand == '1' or EccGlobalData.gConfig.DoxygenCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
676 EdkLogger.quiet("Checking Doxygen command ...")
677
678 # for Dirpath, Dirnames, Filenames in self.WalkTree():
679 # for F in Filenames:
680 # if os.path.splitext(F)[1] in ('.h', '.c'):
681 # FullName = os.path.join(Dirpath, F)
682 # MsgList = c.CheckDoxygenCommand(FullName)
683 for FullName in EccGlobalData.gCFileList + EccGlobalData.gHFileList:
684 MsgList = c.CheckDoxygenCommand(FullName)
685
686 # Meta-Data File Processing Checking
687 def MetaDataFileCheck(self):
688 self.MetaDataFileCheckPathName()
689 self.MetaDataFileCheckGenerateFileList()
690 self.MetaDataFileCheckLibraryInstance()
691 self.MetaDataFileCheckLibraryInstanceDependent()
692 self.MetaDataFileCheckLibraryInstanceOrder()
693 self.MetaDataFileCheckLibraryNoUse()
694 self.MetaDataFileCheckLibraryDefinedInDec()
695 self.MetaDataFileCheckBinaryInfInFdf()
696 self.MetaDataFileCheckPcdDuplicate()
697 self.MetaDataFileCheckPcdFlash()
698 self.MetaDataFileCheckPcdNoUse()
699 self.MetaDataFileCheckGuidDuplicate()
700 self.MetaDataFileCheckModuleFileNoUse()
701 self.MetaDataFileCheckPcdType()
702 self.MetaDataFileCheckModuleFileGuidDuplication()
703 self.MetaDataFileCheckModuleFileGuidFormat()
704 self.MetaDataFileCheckModuleFileProtocolFormat()
705 self.MetaDataFileCheckModuleFilePpiFormat()
706 self.MetaDataFileCheckModuleFilePcdFormat()
707
708 # Check whether each file defined in meta-data exists
709 def MetaDataFileCheckPathName(self):
710 if EccGlobalData.gConfig.MetaDataFileCheckPathName == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
711 # This item is covered when parsing Inf/Dec/Dsc files
712 pass
713
714 # Generate a list for all files defined in meta-data files
715 def MetaDataFileCheckGenerateFileList(self):
716 if EccGlobalData.gConfig.MetaDataFileCheckGenerateFileList == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
717 # This item is covered when parsing Inf/Dec/Dsc files
718 pass
719
720 # Check whether all Library Instances defined for a given module (or dependent library instance) match the module's type.
721 # Each Library Instance must specify the Supported Module Types in its Inf file,
722 # and any module specifying the library instance must be one of the supported types.
723 def MetaDataFileCheckLibraryInstance(self):
724 if EccGlobalData.gConfig.MetaDataFileCheckLibraryInstance == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
725 EdkLogger.quiet("Checking for library instance type issue ...")
726 SqlCommand = """select A.ID, A.Value3, B.Value3 from Inf as A left join Inf as B
727 where A.Value2 = 'LIBRARY_CLASS' and A.Model = %s
728 and B.Value2 = 'MODULE_TYPE' and B.Model = %s and A.BelongsToFile = B.BelongsToFile
729 group by A.BelongsToFile""" % (MODEL_META_DATA_HEADER, MODEL_META_DATA_HEADER)
730 RecordSet = EccGlobalData.gDb.TblInf.Exec(SqlCommand)
731 LibraryClasses = {}
732 for Record in RecordSet:
733 List = Record[1].split('|', 1)
734 SupModType = []
735 if len(List) == 1:
736 SupModType = DT.SUP_MODULE_LIST_STRING.split(DT.TAB_VALUE_SPLIT)
737 elif len(List) == 2:
738 SupModType = List[1].split()
739
740 if List[0] not in LibraryClasses:
741 LibraryClasses[List[0]] = SupModType
742 else:
743 for Item in SupModType:
744 if Item not in LibraryClasses[List[0]]:
745 LibraryClasses[List[0]].append(Item)
746
747 if Record[2] != 'BASE' and Record[2] not in SupModType:
748 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_LIBRARY_INSTANCE_2, OtherMsg="The Library Class '%s' does not specify its supported module types" % (List[0]), BelongsToTable='Inf', BelongsToItem=Record[0])
749
750 SqlCommand = """select A.ID, A.Value1, B.Value3 from Inf as A left join Inf as B
751 where A.Model = %s and B.Value2 = '%s' and B.Model = %s
752 and B.BelongsToFile = A.BelongsToFile""" \
753 % (MODEL_EFI_LIBRARY_CLASS, 'MODULE_TYPE', MODEL_META_DATA_HEADER)
754 RecordSet = EccGlobalData.gDb.TblInf.Exec(SqlCommand)
755 # Merge all LibraryClasses' supmodlist
756 RecordDict = {}
757 for Record in RecordSet:
758 if Record[1] not in RecordDict:
759 RecordDict[Record[1]] = [str(Record[2])]
760 else:
761 if Record[2] not in RecordDict[Record[1]]:
762 RecordDict[Record[1]].append(Record[2])
763
764 for Record in RecordSet:
765 if Record[1] in LibraryClasses:
766 if Record[2] not in LibraryClasses[Record[1]] and 'BASE' not in RecordDict[Record[1]]:
767 if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_LIBRARY_INSTANCE_1, Record[1]):
768 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_LIBRARY_INSTANCE_1, OtherMsg="The type of Library Class [%s] defined in Inf file does not match the type of the module" % (Record[1]), BelongsToTable='Inf', BelongsToItem=Record[0])
769 else:
770 if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_LIBRARY_INSTANCE_1, Record[1]):
771 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_LIBRARY_INSTANCE_1, OtherMsg="The type of Library Class [%s] defined in Inf file does not match the type of the module" % (Record[1]), BelongsToTable='Inf', BelongsToItem=Record[0])
772
773 # Check whether a Library Instance has been defined for all dependent library classes
774 def MetaDataFileCheckLibraryInstanceDependent(self):
775 if EccGlobalData.gConfig.MetaDataFileCheckLibraryInstanceDependent == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
776 EdkLogger.quiet("Checking for library instance dependent issue ...")
777 SqlCommand = """select ID, Value1, Value2 from Dsc where Model = %s""" % MODEL_EFI_LIBRARY_CLASS
778 LibraryClasses = EccGlobalData.gDb.TblDsc.Exec(SqlCommand)
779 for LibraryClass in LibraryClasses:
780 if LibraryClass[1].upper() == 'NULL' or LibraryClass[1].startswith('!ifdef') or LibraryClass[1].startswith('!ifndef') or LibraryClass[1].endswith('!endif'):
781 continue
782 else:
783 LibraryIns = os.path.normpath(mws.join(EccGlobalData.gWorkspace, LibraryClass[2]))
784 SkipDirString = '|'.join(EccGlobalData.gConfig.SkipDirList)
785 p = re.compile(r'.*[\\/](?:%s^\S)[\\/]?.*' % SkipDirString)
786 if p.match(os.path.split(LibraryIns)[0].upper()):
787 continue
788 SqlCommand = """select Value3 from Inf where BelongsToFile =
789 (select ID from File where lower(FullPath) = lower('%s'))
790 and Value2 = '%s'""" % (LibraryIns, 'LIBRARY_CLASS')
791 RecordSet = EccGlobalData.gDb.TblInf.Exec(SqlCommand)
792 IsFound = False
793 for Record in RecordSet:
794 LibName = Record[0].split('|', 1)[0]
795 if LibraryClass[1] == LibName:
796 IsFound = True
797 if not IsFound:
798 if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_LIBRARY_INSTANCE_DEPENDENT, LibraryClass[1]):
799 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_LIBRARY_INSTANCE_DEPENDENT, OtherMsg="The Library Class [%s] is not specified in '%s'" % (LibraryClass[1], LibraryClass[2]), BelongsToTable='Dsc', BelongsToItem=LibraryClass[0])
800
801 # Check whether the Library Instances specified by the LibraryClasses sections are listed in order of dependencies
802 def MetaDataFileCheckLibraryInstanceOrder(self):
803 if EccGlobalData.gConfig.MetaDataFileCheckLibraryInstanceOrder == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
804 # This checkpoint is not necessary for Ecc check
805 pass
806
807 # Check whether the unnecessary inclusion of library classes in the Inf file
808 # Check whether the unnecessary duplication of library classe names in the DSC file
809 def MetaDataFileCheckLibraryNoUse(self):
810 if EccGlobalData.gConfig.MetaDataFileCheckLibraryNoUse == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
811 EdkLogger.quiet("Checking for library instance not used ...")
812 SqlCommand = """select ID, Value1 from Inf as A where A.Model = %s and A.Value1 not in (select B.Value1 from Dsc as B where Model = %s)""" % (MODEL_EFI_LIBRARY_CLASS, MODEL_EFI_LIBRARY_CLASS)
813 RecordSet = EccGlobalData.gDb.TblInf.Exec(SqlCommand)
814 for Record in RecordSet:
815 if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_LIBRARY_NO_USE, Record[1]):
816 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_LIBRARY_NO_USE, OtherMsg="The Library Class [%s] is not used in any platform" % (Record[1]), BelongsToTable='Inf', BelongsToItem=Record[0])
817 SqlCommand = """
818 select A.ID, A.Value1, A.BelongsToFile, A.StartLine, B.StartLine from Dsc as A left join Dsc as B
819 where A.Model = %s and B.Model = %s and A.Scope1 = B.Scope1 and A.Scope2 = B.Scope2 and A.ID <> B.ID
820 and A.Value1 = B.Value1 and A.Value2 <> B.Value2 and A.BelongsToItem = -1 and B.BelongsToItem = -1 and A.StartLine <> B.StartLine and B.BelongsToFile = A.BelongsToFile""" \
821 % (MODEL_EFI_LIBRARY_CLASS, MODEL_EFI_LIBRARY_CLASS)
822 RecordSet = EccGlobalData.gDb.TblDsc.Exec(SqlCommand)
823 for Record in RecordSet:
824 if Record[3] and Record[4] and Record[3] != Record[4] and Record[1] != 'NULL':
825 SqlCommand = """select FullPath from File where ID = %s""" % (Record[2])
826 FilePathList = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
827 for FilePath in FilePathList:
828 if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_LIBRARY_NAME_DUPLICATE, Record[1]):
829 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_LIBRARY_NAME_DUPLICATE, OtherMsg="The Library Class [%s] is duplicated in '%s' line %s and line %s." % (Record[1], FilePath, Record[3], Record[4]), BelongsToTable='Dsc', BelongsToItem=Record[0])
830
831 # Check the header file in Include\Library directory whether be defined in the package DEC file.
832 def MetaDataFileCheckLibraryDefinedInDec(self):
833 if EccGlobalData.gConfig.MetaDataFileCheckLibraryDefinedInDec == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
834 EdkLogger.quiet("Checking for library instance whether be defined in the package dec file ...")
835 SqlCommand = """
836 select A.Value1, A.StartLine, A.ID, B.Value1 from Inf as A left join Dec as B
837 on A.Model = B.Model and A.Value1 = B.Value1 where A.Model=%s
838 """ % MODEL_EFI_LIBRARY_CLASS
839 RecordSet = EccGlobalData.gDb.TblDsc.Exec(SqlCommand)
840 for Record in RecordSet:
841 LibraryInInf, Line, ID, LibraryDec = Record
842 if not LibraryDec:
843 if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_LIBRARY_NOT_DEFINED, LibraryInInf):
844 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_LIBRARY_NOT_DEFINED, \
845 OtherMsg="The Library Class [%s] in %s line is not defined in the associated package file." % (LibraryInInf, Line),
846 BelongsToTable='Inf', BelongsToItem=ID)
847
848 # Check whether an Inf file is specified in the FDF file, but not in the Dsc file, then the Inf file must be for a Binary module only
849 def MetaDataFileCheckBinaryInfInFdf(self):
850 if EccGlobalData.gConfig.MetaDataFileCheckBinaryInfInFdf == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
851 EdkLogger.quiet("Checking for non-binary modules defined in FDF files ...")
852 SqlCommand = """select A.ID, A.Value1 from Fdf as A
853 where A.Model = %s
854 and A.Enabled > -1
855 and A.Value1 not in
856 (select B.Value1 from Dsc as B
857 where B.Model = %s
858 and B.Enabled > -1)""" % (MODEL_META_DATA_COMPONENT, MODEL_META_DATA_COMPONENT)
859 RecordSet = EccGlobalData.gDb.TblFdf.Exec(SqlCommand)
860 for Record in RecordSet:
861 FdfID = Record[0]
862 FilePath = Record[1]
863 FilePath = os.path.normpath(mws.join(EccGlobalData.gWorkspace, FilePath))
864 SqlCommand = """select ID from Inf where Model = %s and BelongsToFile = (select ID from File where FullPath like '%s')
865 """ % (MODEL_EFI_SOURCE_FILE, FilePath)
866 NewRecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
867 if NewRecordSet != []:
868 if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_BINARY_INF_IN_FDF, FilePath):
869 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_BINARY_INF_IN_FDF, OtherMsg="File [%s] defined in FDF file and not in DSC file must be a binary module" % (FilePath), BelongsToTable='Fdf', BelongsToItem=FdfID)
870
871 # Check whether a PCD is set in a Dsc file or the FDF file, but not in both.
872 def MetaDataFileCheckPcdDuplicate(self):
873 if EccGlobalData.gConfig.MetaDataFileCheckPcdDuplicate == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
874 EdkLogger.quiet("Checking for duplicate PCDs defined in both DSC and FDF files ...")
875 SqlCommand = """
876 select A.ID, A.Value1, A.Value2, A.BelongsToFile, B.ID, B.Value1, B.Value2, B.BelongsToFile from Dsc as A, Fdf as B
877 where A.Model >= %s and A.Model < %s
878 and B.Model >= %s and B.Model < %s
879 and A.Value1 = B.Value1
880 and A.Value2 = B.Value2
881 and A.Enabled > -1
882 and B.Enabled > -1
883 group by A.ID
884 """ % (MODEL_PCD, MODEL_META_DATA_HEADER, MODEL_PCD, MODEL_META_DATA_HEADER)
885 RecordSet = EccGlobalData.gDb.TblDsc.Exec(SqlCommand)
886 for Record in RecordSet:
887 SqlCommand1 = """select Name from File where ID = %s""" % Record[3]
888 SqlCommand2 = """select Name from File where ID = %s""" % Record[7]
889 DscFileName = os.path.splitext(EccGlobalData.gDb.TblDsc.Exec(SqlCommand1)[0][0])[0]
890 FdfFileName = os.path.splitext(EccGlobalData.gDb.TblDsc.Exec(SqlCommand2)[0][0])[0]
891 if DscFileName != FdfFileName:
892 continue
893 if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_PCD_DUPLICATE, Record[1] + '.' + Record[2]):
894 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_PCD_DUPLICATE, OtherMsg="The PCD [%s] is defined in both FDF file and DSC file" % (Record[1] + '.' + Record[2]), BelongsToTable='Dsc', BelongsToItem=Record[0])
895 if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_PCD_DUPLICATE, Record[5] + '.' + Record[6]):
896 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_PCD_DUPLICATE, OtherMsg="The PCD [%s] is defined in both FDF file and DSC file" % (Record[5] + '.' + Record[6]), BelongsToTable='Fdf', BelongsToItem=Record[4])
897
898 EdkLogger.quiet("Checking for duplicate PCDs defined in DEC files ...")
899 SqlCommand = """
900 select A.ID, A.Value1, A.Value2, A.Model, B.Model from Dec as A left join Dec as B
901 where A.Model >= %s and A.Model < %s
902 and B.Model >= %s and B.Model < %s
903 and A.Value1 = B.Value1
904 and A.Value2 = B.Value2
905 and A.Scope1 = B.Scope1
906 and A.ID <> B.ID
907 and A.Model = B.Model
908 and A.Enabled > -1
909 and B.Enabled > -1
910 and A.BelongsToFile = B.BelongsToFile
911 group by A.ID
912 """ % (MODEL_PCD, MODEL_META_DATA_HEADER, MODEL_PCD, MODEL_META_DATA_HEADER)
913 RecordSet = EccGlobalData.gDb.TblDec.Exec(SqlCommand)
914 for Record in RecordSet:
915 RecordCat = Record[1] + '.' + Record[2]
916 if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_PCD_DUPLICATE, RecordCat):
917 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_PCD_DUPLICATE, OtherMsg="The PCD [%s] is defined duplicated in DEC file" % RecordCat, BelongsToTable='Dec', BelongsToItem=Record[0])
918
919 # Check whether PCD settings in the FDF file can only be related to flash.
920 def MetaDataFileCheckPcdFlash(self):
921 if EccGlobalData.gConfig.MetaDataFileCheckPcdFlash == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
922 EdkLogger.quiet("Checking only Flash related PCDs are used in FDF ...")
923 SqlCommand = """
924 select ID, Value1, Value2, BelongsToFile from Fdf as A
925 where A.Model >= %s and Model < %s
926 and A.Enabled > -1
927 and A.Value2 not like '%%Flash%%'
928 """ % (MODEL_PCD, MODEL_META_DATA_HEADER)
929 RecordSet = EccGlobalData.gDb.TblFdf.Exec(SqlCommand)
930 for Record in RecordSet:
931 if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_PCD_FLASH, Record[1] + '.' + Record[2]):
932 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_PCD_FLASH, OtherMsg="The PCD [%s] defined in FDF file is not related to Flash" % (Record[1] + '.' + Record[2]), BelongsToTable='Fdf', BelongsToItem=Record[0])
933
934 # Check whether PCDs used in Inf files but not specified in Dsc or FDF files
935 def MetaDataFileCheckPcdNoUse(self):
936 if EccGlobalData.gConfig.MetaDataFileCheckPcdNoUse == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
937 EdkLogger.quiet("Checking for non-specified PCDs ...")
938 SqlCommand = """
939 select ID, Value1, Value2, BelongsToFile from Inf as A
940 where A.Model >= %s and Model < %s
941 and A.Enabled > -1
942 and (A.Value1, A.Value2) not in
943 (select Value1, Value2 from Dsc as B
944 where B.Model >= %s and B.Model < %s
945 and B.Enabled > -1)
946 and (A.Value1, A.Value2) not in
947 (select Value1, Value2 from Fdf as C
948 where C.Model >= %s and C.Model < %s
949 and C.Enabled > -1)
950 """ % (MODEL_PCD, MODEL_META_DATA_HEADER, MODEL_PCD, MODEL_META_DATA_HEADER, MODEL_PCD, MODEL_META_DATA_HEADER)
951 RecordSet = EccGlobalData.gDb.TblInf.Exec(SqlCommand)
952 for Record in RecordSet:
953 if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_PCD_NO_USE, Record[1] + '.' + Record[2]):
954 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_PCD_NO_USE, OtherMsg="The PCD [%s] defined in INF file is not specified in either DSC or FDF files" % (Record[1] + '.' + Record[2]), BelongsToTable='Inf', BelongsToItem=Record[0])
955
956 # Check whether having duplicate guids defined for Guid/Protocol/Ppi
957 def MetaDataFileCheckGuidDuplicate(self):
958 if EccGlobalData.gConfig.MetaDataFileCheckGuidDuplicate == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
959 EdkLogger.quiet("Checking for duplicate GUID/PPI/PROTOCOL ...")
960 # Check Guid
961 self.CheckGuidProtocolPpi(ERROR_META_DATA_FILE_CHECK_DUPLICATE_GUID, MODEL_EFI_GUID, EccGlobalData.gDb.TblDec)
962 self.CheckGuidProtocolPpi(ERROR_META_DATA_FILE_CHECK_DUPLICATE_GUID, MODEL_EFI_GUID, EccGlobalData.gDb.TblDsc)
963 self.CheckGuidProtocolPpiValue(ERROR_META_DATA_FILE_CHECK_DUPLICATE_GUID, MODEL_EFI_GUID)
964 # Check protocol
965 self.CheckGuidProtocolPpi(ERROR_META_DATA_FILE_CHECK_DUPLICATE_PROTOCOL, MODEL_EFI_PROTOCOL, EccGlobalData.gDb.TblDec)
966 self.CheckGuidProtocolPpi(ERROR_META_DATA_FILE_CHECK_DUPLICATE_PROTOCOL, MODEL_EFI_PROTOCOL, EccGlobalData.gDb.TblDsc)
967 self.CheckGuidProtocolPpiValue(ERROR_META_DATA_FILE_CHECK_DUPLICATE_PROTOCOL, MODEL_EFI_PROTOCOL)
968 # Check ppi
969 self.CheckGuidProtocolPpi(ERROR_META_DATA_FILE_CHECK_DUPLICATE_PPI, MODEL_EFI_PPI, EccGlobalData.gDb.TblDec)
970 self.CheckGuidProtocolPpi(ERROR_META_DATA_FILE_CHECK_DUPLICATE_PPI, MODEL_EFI_PPI, EccGlobalData.gDb.TblDsc)
971 self.CheckGuidProtocolPpiValue(ERROR_META_DATA_FILE_CHECK_DUPLICATE_PPI, MODEL_EFI_PPI)
972
973 # Check whether all files under module directory are described in INF files
974 def MetaDataFileCheckModuleFileNoUse(self):
975 if EccGlobalData.gConfig.MetaDataFileCheckModuleFileNoUse == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
976 EdkLogger.quiet("Checking for no used module files ...")
977 SqlCommand = """
978 select upper(Path) from File where ID in (select BelongsToFile from Inf where BelongsToFile != -1)
979 """
980 InfPathSet = EccGlobalData.gDb.TblInf.Exec(SqlCommand)
981 InfPathList = []
982 for Item in InfPathSet:
983 if Item[0] not in InfPathList:
984 InfPathList.append(Item[0])
985 SqlCommand = """
986 select ID, Path, FullPath from File where upper(FullPath) not in
987 (select upper(A.Path) || '\\' || upper(B.Value1) from File as A, INF as B
988 where A.ID in (select BelongsToFile from INF where Model = %s group by BelongsToFile) and
989 B.BelongsToFile = A.ID and B.Model = %s)
990 and (Model = %s or Model = %s)
991 """ % (MODEL_EFI_SOURCE_FILE, MODEL_EFI_SOURCE_FILE, MODEL_FILE_C, MODEL_FILE_H)
992 RecordSet = EccGlobalData.gDb.TblInf.Exec(SqlCommand)
993 for Record in RecordSet:
994 Path = Record[1]
995 Path = Path.upper().replace('\X64', '').replace('\IA32', '').replace('\EBC', '').replace('\IPF', '').replace('\ARM', '')
996 if Path in InfPathList:
997 if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_MODULE_FILE_NO_USE, Record[2]):
998 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_MODULE_FILE_NO_USE, OtherMsg="The source file [%s] is existing in module directory but it is not described in INF file." % (Record[2]), BelongsToTable='File', BelongsToItem=Record[0])
999
1000 # Check whether the PCD is correctly used in C function via its type
1001 def MetaDataFileCheckPcdType(self):
1002 if EccGlobalData.gConfig.MetaDataFileCheckPcdType == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
1003 EdkLogger.quiet("Checking for pcd type in c code function usage ...")
1004 SqlCommand = """
1005 select ID, Model, Value1, Value2, BelongsToFile from INF where Model > %s and Model < %s
1006 """ % (MODEL_PCD, MODEL_META_DATA_HEADER)
1007 PcdSet = EccGlobalData.gDb.TblInf.Exec(SqlCommand)
1008 for Pcd in PcdSet:
1009 Model = Pcd[1]
1010 PcdName = Pcd[2]
1011 if Pcd[3]:
1012 PcdName = Pcd[3]
1013 BelongsToFile = Pcd[4]
1014 SqlCommand = """
1015 select ID from File where FullPath in
1016 (select B.Path || '\\' || A.Value1 from INF as A, File as B where A.Model = %s and A.BelongsToFile = %s
1017 and B.ID = %s and (B.Model = %s or B.Model = %s))
1018 """ % (MODEL_EFI_SOURCE_FILE, BelongsToFile, BelongsToFile, MODEL_FILE_C, MODEL_FILE_H)
1019 TableSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
1020 for Tbl in TableSet:
1021 TblName = 'Identifier' + str(Tbl[0])
1022 SqlCommand = """
1023 select Name, ID from %s where value like '%s' and Model = %s
1024 """ % (TblName, PcdName, MODEL_IDENTIFIER_FUNCTION_CALLING)
1025 RecordSet = EccGlobalData.gDb.TblInf.Exec(SqlCommand)
1026 TblNumber = TblName.replace('Identifier', '')
1027 for Record in RecordSet:
1028 FunName = Record[0]
1029 if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_PCD_TYPE, FunName):
1030 if Model in [MODEL_PCD_FIXED_AT_BUILD] and not FunName.startswith('FixedPcdGet'):
1031 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_PCD_TYPE, OtherMsg="The pcd '%s' is defined as a FixPcd but now it is called by c function [%s]" % (PcdName, FunName), BelongsToTable=TblName, BelongsToItem=Record[1])
1032 if Model in [MODEL_PCD_FEATURE_FLAG] and (not FunName.startswith('FeaturePcdGet') and not FunName.startswith('FeaturePcdSet')):
1033 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_PCD_TYPE, OtherMsg="The pcd '%s' is defined as a FeaturePcd but now it is called by c function [%s]" % (PcdName, FunName), BelongsToTable=TblName, BelongsToItem=Record[1])
1034 if Model in [MODEL_PCD_PATCHABLE_IN_MODULE] and (not FunName.startswith('PatchablePcdGet') and not FunName.startswith('PatchablePcdSet')):
1035 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_PCD_TYPE, OtherMsg="The pcd '%s' is defined as a PatchablePcd but now it is called by c function [%s]" % (PcdName, FunName), BelongsToTable=TblName, BelongsToItem=Record[1])
1036
1037 #ERROR_META_DATA_FILE_CHECK_PCD_TYPE
1038 pass
1039
1040 # Internal worker function to get the INF workspace relative path from FileID
1041 def GetInfFilePathFromID(self, FileID):
1042 Table = EccGlobalData.gDb.TblFile
1043 SqlCommand = """select A.FullPath from %s as A where A.ID = %s""" % (Table.Table, FileID)
1044 RecordSet = Table.Exec(SqlCommand)
1045 Path = ""
1046 for Record in RecordSet:
1047 Path = mws.relpath(Record[0], EccGlobalData.gWorkspace)
1048 return Path
1049
1050 # Check whether two module INFs under one workspace has the same FILE_GUID value
1051 def MetaDataFileCheckModuleFileGuidDuplication(self):
1052 if EccGlobalData.gConfig.MetaDataFileCheckModuleFileGuidDuplication == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
1053 EdkLogger.quiet("Checking for pcd type in c code function usage ...")
1054 Table = EccGlobalData.gDb.TblInf
1055 SqlCommand = """
1056 select A.ID, A.Value3, A.BelongsToFile, B.BelongsToFile from %s as A, %s as B
1057 where A.Value2 = 'FILE_GUID' and B.Value2 = 'FILE_GUID' and
1058 A.Value3 = B.Value3 and A.ID <> B.ID group by A.ID
1059 """ % (Table.Table, Table.Table)
1060 RecordSet = Table.Exec(SqlCommand)
1061 for Record in RecordSet:
1062 InfPath1 = self.GetInfFilePathFromID(Record[2])
1063 InfPath2 = self.GetInfFilePathFromID(Record[3])
1064 if InfPath1 and InfPath2:
1065 if not EccGlobalData.gException.IsException(ERROR_META_DATA_FILE_CHECK_MODULE_FILE_GUID_DUPLICATION, InfPath1):
1066 Msg = "The FILE_GUID of INF file [%s] is duplicated with that of %s" % (InfPath1, InfPath2)
1067 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_MODULE_FILE_GUID_DUPLICATION, OtherMsg=Msg, BelongsToTable=Table.Table, BelongsToItem=Record[0])
1068
1069
1070 # Check Guid Format in module INF
1071 def MetaDataFileCheckModuleFileGuidFormat(self):
1072 if EccGlobalData.gConfig.MetaDataFileCheckModuleFileGuidFormat == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
1073 EdkLogger.quiet("Check Guid Format in module INF ...")
1074 Table = EccGlobalData.gDb.TblInf
1075 SqlCommand = """
1076 select ID, Value1, Usage, BelongsToFile from %s where Model = %s group by ID
1077 """ % (Table.Table, MODEL_EFI_GUID)
1078 RecordSet = Table.Exec(SqlCommand)
1079 for Record in RecordSet:
1080 Value1 = Record[1]
1081 Value2 = Record[2]
1082 GuidCommentList = []
1083 InfPath = self.GetInfFilePathFromID(Record[3])
1084 Msg = "The GUID format of %s in INF file [%s] does not follow rules" % (Value1, InfPath)
1085 if Value2.startswith(DT.TAB_SPECIAL_COMMENT):
1086 GuidCommentList = Value2[2:].split(DT.TAB_SPECIAL_COMMENT)
1087 if GuidCommentList[0].strip().startswith(DT.TAB_INF_USAGE_UNDEFINED):
1088 continue
1089 elif len(GuidCommentList) > 1:
1090 if not GuidCommentList[0].strip().startswith((DT.TAB_INF_USAGE_PRO,
1091 DT.TAB_INF_USAGE_SOME_PRO,
1092 DT.TAB_INF_USAGE_CON,
1093 DT.TAB_INF_USAGE_SOME_CON)):
1094 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_FORMAT_GUID, OtherMsg=Msg, BelongsToTable=Table.Table, BelongsToItem=Record[0])
1095 if not (GuidCommentList[1].strip()).startswith(DT.TAB_INF_GUIDTYPE_VAR) and \
1096 not GuidCommentList[1].strip().startswith((DT.TAB_INF_GUIDTYPE_EVENT,
1097 DT.TAB_INF_GUIDTYPE_HII,
1098 DT.TAB_INF_GUIDTYPE_FILE,
1099 DT.TAB_INF_GUIDTYPE_HOB,
1100 DT.TAB_INF_GUIDTYPE_FV,
1101 DT.TAB_INF_GUIDTYPE_ST,
1102 DT.TAB_INF_GUIDTYPE_TSG,
1103 DT.TAB_INF_GUIDTYPE_GUID,
1104 DT.TAB_INF_GUIDTYPE_PROTOCOL,
1105 DT.TAB_INF_GUIDTYPE_PPI,
1106 DT.TAB_INF_USAGE_UNDEFINED)):
1107 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_FORMAT_GUID, OtherMsg=Msg, BelongsToTable=Table.Table, BelongsToItem=Record[0])
1108 else:
1109 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_FORMAT_GUID, OtherMsg=Msg, BelongsToTable=Table.Table, BelongsToItem=Record[0])
1110 else:
1111 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_FORMAT_GUID, OtherMsg=Msg, BelongsToTable=Table.Table, BelongsToItem=Record[0])
1112
1113 # Check Protocol Format in module INF
1114 def MetaDataFileCheckModuleFileProtocolFormat(self):
1115 if EccGlobalData.gConfig.MetaDataFileCheckModuleFileProtocolFormat == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
1116 EdkLogger.quiet("Check Protocol Format in module INF ...")
1117 Table = EccGlobalData.gDb.TblInf
1118 SqlCommand = """
1119 select ID, Value1, Usage, BelongsToFile from %s where Model = %s group by ID
1120 """ % (Table.Table, MODEL_EFI_PROTOCOL)
1121 RecordSet = Table.Exec(SqlCommand)
1122 for Record in RecordSet:
1123 Value1 = Record[1]
1124 Value2 = Record[2]
1125 GuidCommentList = []
1126 InfPath = self.GetInfFilePathFromID(Record[3])
1127 Msg = "The Protocol format of %s in INF file [%s] does not follow rules" % (Value1, InfPath)
1128 if Value2.startswith(DT.TAB_SPECIAL_COMMENT):
1129 GuidCommentList = Value2[2:].split(DT.TAB_SPECIAL_COMMENT)
1130 if len(GuidCommentList) >= 1:
1131 if not GuidCommentList[0].strip().startswith((DT.TAB_INF_USAGE_PRO,
1132 DT.TAB_INF_USAGE_SOME_PRO,
1133 DT.TAB_INF_USAGE_CON,
1134 DT.TAB_INF_USAGE_SOME_CON,
1135 DT.TAB_INF_USAGE_NOTIFY,
1136 DT.TAB_INF_USAGE_TO_START,
1137 DT.TAB_INF_USAGE_BY_START,
1138 DT.TAB_INF_USAGE_UNDEFINED)):
1139 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_FORMAT_PROTOCOL, OtherMsg=Msg, BelongsToTable=Table.Table, BelongsToItem=Record[0])
1140 else:
1141 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_FORMAT_PROTOCOL, OtherMsg=Msg, BelongsToTable=Table.Table, BelongsToItem=Record[0])
1142
1143
1144 # Check Ppi Format in module INF
1145 def MetaDataFileCheckModuleFilePpiFormat(self):
1146 if EccGlobalData.gConfig.MetaDataFileCheckModuleFilePpiFormat == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
1147 EdkLogger.quiet("Check Ppi Format in module INF ...")
1148 Table = EccGlobalData.gDb.TblInf
1149 SqlCommand = """
1150 select ID, Value1, Usage, BelongsToFile from %s where Model = %s group by ID
1151 """ % (Table.Table, MODEL_EFI_PPI)
1152 RecordSet = Table.Exec(SqlCommand)
1153 for Record in RecordSet:
1154 Value1 = Record[1]
1155 Value2 = Record[2]
1156 GuidCommentList = []
1157 InfPath = self.GetInfFilePathFromID(Record[3])
1158 Msg = "The Ppi format of %s in INF file [%s] does not follow rules" % (Value1, InfPath)
1159 if Value2.startswith(DT.TAB_SPECIAL_COMMENT):
1160 GuidCommentList = Value2[2:].split(DT.TAB_SPECIAL_COMMENT)
1161 if len(GuidCommentList) >= 1:
1162 if not GuidCommentList[0].strip().startswith((DT.TAB_INF_USAGE_PRO,
1163 DT.TAB_INF_USAGE_SOME_PRO,
1164 DT.TAB_INF_USAGE_CON,
1165 DT.TAB_INF_USAGE_SOME_CON,
1166 DT.TAB_INF_USAGE_NOTIFY,
1167 DT.TAB_INF_USAGE_UNDEFINED)):
1168 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_FORMAT_PPI, OtherMsg=Msg, BelongsToTable=Table.Table, BelongsToItem=Record[0])
1169 else:
1170 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_FORMAT_PPI, OtherMsg=Msg, BelongsToTable=Table.Table, BelongsToItem=Record[0])
1171
1172 # Check Pcd Format in module INF
1173 def MetaDataFileCheckModuleFilePcdFormat(self):
1174 if EccGlobalData.gConfig.MetaDataFileCheckModuleFilePcdFormat == '1' or EccGlobalData.gConfig.MetaDataFileCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
1175 EdkLogger.quiet("Check Pcd Format in module INF ...")
1176 Table = EccGlobalData.gDb.TblInf
1177 SqlCommand = """
1178 select ID, Model, Value1, Value2, Usage, BelongsToFile from %s where Model >= %s and Model < %s group by ID
1179 """ % (Table.Table, MODEL_PCD, MODEL_META_DATA_HEADER)
1180 RecordSet = Table.Exec(SqlCommand)
1181 for Record in RecordSet:
1182 Model = Record[1]
1183 PcdName = Record[2] + '.' + Record[3]
1184 Usage = Record[4]
1185 PcdCommentList = []
1186 InfPath = self.GetInfFilePathFromID(Record[5])
1187 Msg = "The Pcd format of %s in INF file [%s] does not follow rules" % (PcdName, InfPath)
1188 if Usage.startswith(DT.TAB_SPECIAL_COMMENT):
1189 PcdCommentList = Usage[2:].split(DT.TAB_SPECIAL_COMMENT)
1190 if len(PcdCommentList) >= 1:
1191 if Model in [MODEL_PCD_FIXED_AT_BUILD, MODEL_PCD_FEATURE_FLAG] \
1192 and not PcdCommentList[0].strip().startswith((DT.TAB_INF_USAGE_SOME_PRO,
1193 DT.TAB_INF_USAGE_CON,
1194 DT.TAB_INF_USAGE_UNDEFINED)):
1195 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_FORMAT_PCD, OtherMsg=Msg, BelongsToTable=Table.Table, BelongsToItem=Record[0])
1196 if Model in [MODEL_PCD_PATCHABLE_IN_MODULE, MODEL_PCD_DYNAMIC, MODEL_PCD_DYNAMIC_EX] \
1197 and not PcdCommentList[0].strip().startswith((DT.TAB_INF_USAGE_PRO,
1198 DT.TAB_INF_USAGE_SOME_PRO,
1199 DT.TAB_INF_USAGE_CON,
1200 DT.TAB_INF_USAGE_SOME_CON,
1201 DT.TAB_INF_USAGE_UNDEFINED)):
1202 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_FORMAT_PCD, OtherMsg=Msg, BelongsToTable=Table.Table, BelongsToItem=Record[0])
1203 else:
1204 EccGlobalData.gDb.TblReport.Insert(ERROR_META_DATA_FILE_CHECK_FORMAT_PCD, OtherMsg=Msg, BelongsToTable=Table.Table, BelongsToItem=Record[0])
1205
1206 # Check whether these is duplicate Guid/Ppi/Protocol name
1207 def CheckGuidProtocolPpi(self, ErrorID, Model, Table):
1208 Name = ''
1209 if Model == MODEL_EFI_GUID:
1210 Name = 'guid'
1211 if Model == MODEL_EFI_PROTOCOL:
1212 Name = 'protocol'
1213 if Model == MODEL_EFI_PPI:
1214 Name = 'ppi'
1215 SqlCommand = """
1216 select A.ID, A.Value1 from %s as A, %s as B
1217 where A.Model = %s and B.Model = %s
1218 and A.Value1 like B.Value1 and A.ID <> B.ID
1219 and A.Scope1 = B.Scope1
1220 and A.Enabled > -1
1221 and B.Enabled > -1
1222 group by A.ID
1223 """ % (Table.Table, Table.Table, Model, Model)
1224 RecordSet = Table.Exec(SqlCommand)
1225 for Record in RecordSet:
1226 if not EccGlobalData.gException.IsException(ErrorID, Record[1]):
1227 EccGlobalData.gDb.TblReport.Insert(ErrorID, OtherMsg="The %s name [%s] is defined more than one time" % (Name.upper(), Record[1]), BelongsToTable=Table.Table, BelongsToItem=Record[0])
1228
1229 # Check whether these is duplicate Guid/Ppi/Protocol value
1230 def CheckGuidProtocolPpiValue(self, ErrorID, Model):
1231 Name = ''
1232 Table = EccGlobalData.gDb.TblDec
1233 if Model == MODEL_EFI_GUID:
1234 Name = 'guid'
1235 if Model == MODEL_EFI_PROTOCOL:
1236 Name = 'protocol'
1237 if Model == MODEL_EFI_PPI:
1238 Name = 'ppi'
1239 SqlCommand = """
1240 select A.ID, A.Value1, A.Value2 from %s as A, %s as B
1241 where A.Model = %s and B.Model = %s
1242 and A.Value2 like B.Value2 and A.ID <> B.ID
1243 and A.Scope1 = B.Scope1 and A.Value1 <> B.Value1
1244 group by A.ID
1245 """ % (Table.Table, Table.Table, Model, Model)
1246 RecordSet = Table.Exec(SqlCommand)
1247 for Record in RecordSet:
1248 if not EccGlobalData.gException.IsException(ErrorID, Record[2]):
1249 EccGlobalData.gDb.TblReport.Insert(ErrorID, OtherMsg="The %s value [%s] is used more than one time" % (Name.upper(), Record[2]), BelongsToTable=Table.Table, BelongsToItem=Record[0])
1250
1251 # Naming Convention Check
1252 def NamingConventionCheck(self):
1253 if EccGlobalData.gConfig.NamingConventionCheckDefineStatement == '1' \
1254 or EccGlobalData.gConfig.NamingConventionCheckTypedefStatement == '1' \
1255 or EccGlobalData.gConfig.NamingConventionCheckIfndefStatement == '1' \
1256 or EccGlobalData.gConfig.NamingConventionCheckVariableName == '1' \
1257 or EccGlobalData.gConfig.NamingConventionCheckSingleCharacterVariable == '1' \
1258 or EccGlobalData.gConfig.NamingConventionCheckAll == '1'\
1259 or EccGlobalData.gConfig.CheckAll == '1':
1260 for Dirpath, Dirnames, Filenames in self.WalkTree():
1261 for F in Filenames:
1262 if os.path.splitext(F)[1] in ('.h', '.c'):
1263 FullName = os.path.join(Dirpath, F)
1264 Id = c.GetTableID(FullName)
1265 if Id < 0:
1266 continue
1267 FileTable = 'Identifier' + str(Id)
1268 self.NamingConventionCheckDefineStatement(FileTable)
1269 self.NamingConventionCheckTypedefStatement(FileTable)
1270 self.NamingConventionCheckVariableName(FileTable)
1271 self.NamingConventionCheckSingleCharacterVariable(FileTable)
1272 if os.path.splitext(F)[1] in ('.h'):
1273 self.NamingConventionCheckIfndefStatement(FileTable)
1274
1275 self.NamingConventionCheckPathName()
1276 self.NamingConventionCheckFunctionName()
1277
1278 # Check whether only capital letters are used for #define declarations
1279 def NamingConventionCheckDefineStatement(self, FileTable):
1280 if EccGlobalData.gConfig.NamingConventionCheckDefineStatement == '1' or EccGlobalData.gConfig.NamingConventionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
1281 EdkLogger.quiet("Checking naming covention of #define statement ...")
1282
1283 SqlCommand = """select ID, Value from %s where Model = %s""" % (FileTable, MODEL_IDENTIFIER_MACRO_DEFINE)
1284 RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
1285 for Record in RecordSet:
1286 Name = Record[1].strip().split()[1]
1287 if Name.find('(') != -1:
1288 Name = Name[0:Name.find('(')]
1289 if Name.upper() != Name:
1290 if not EccGlobalData.gException.IsException(ERROR_NAMING_CONVENTION_CHECK_DEFINE_STATEMENT, Name):
1291 EccGlobalData.gDb.TblReport.Insert(ERROR_NAMING_CONVENTION_CHECK_DEFINE_STATEMENT, OtherMsg="The #define name [%s] does not follow the rules" % (Name), BelongsToTable=FileTable, BelongsToItem=Record[0])
1292
1293 # Check whether only capital letters are used for typedef declarations
1294 def NamingConventionCheckTypedefStatement(self, FileTable):
1295 if EccGlobalData.gConfig.NamingConventionCheckTypedefStatement == '1' or EccGlobalData.gConfig.NamingConventionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
1296 EdkLogger.quiet("Checking naming covention of #typedef statement ...")
1297
1298 SqlCommand = """select ID, Name from %s where Model = %s""" % (FileTable, MODEL_IDENTIFIER_TYPEDEF)
1299 RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
1300 for Record in RecordSet:
1301 Name = Record[1].strip()
1302 if Name != '' and Name != None:
1303 if Name[0] == '(':
1304 Name = Name[1:Name.find(')')]
1305 if Name.find('(') > -1:
1306 Name = Name[Name.find('(') + 1 : Name.find(')')]
1307 Name = Name.replace('WINAPI', '')
1308 Name = Name.replace('*', '').strip()
1309 if Name.upper() != Name:
1310 if not EccGlobalData.gException.IsException(ERROR_NAMING_CONVENTION_CHECK_TYPEDEF_STATEMENT, Name):
1311 EccGlobalData.gDb.TblReport.Insert(ERROR_NAMING_CONVENTION_CHECK_TYPEDEF_STATEMENT, OtherMsg="The #typedef name [%s] does not follow the rules" % (Name), BelongsToTable=FileTable, BelongsToItem=Record[0])
1312
1313 # Check whether the #ifndef at the start of an include file uses both prefix and postfix underscore characters, '_'.
1314 def NamingConventionCheckIfndefStatement(self, FileTable):
1315 if EccGlobalData.gConfig.NamingConventionCheckIfndefStatement == '1' or EccGlobalData.gConfig.NamingConventionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
1316 EdkLogger.quiet("Checking naming covention of #ifndef statement ...")
1317
1318 SqlCommand = """select ID, Value from %s where Model = %s""" % (FileTable, MODEL_IDENTIFIER_MACRO_IFNDEF)
1319 RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
1320 for Record in RecordSet:
1321 Name = Record[1].replace('#ifndef', '').strip()
1322 if Name[0] != '_' or Name[-1] != '_':
1323 if not EccGlobalData.gException.IsException(ERROR_NAMING_CONVENTION_CHECK_IFNDEF_STATEMENT, Name):
1324 EccGlobalData.gDb.TblReport.Insert(ERROR_NAMING_CONVENTION_CHECK_IFNDEF_STATEMENT, OtherMsg="The #ifndef name [%s] does not follow the rules" % (Name), BelongsToTable=FileTable, BelongsToItem=Record[0])
1325
1326 # Rule for path name, variable name and function name
1327 # 1. First character should be upper case
1328 # 2. Existing lower case in a word
1329 # 3. No space existence
1330 # Check whether the path name followed the rule
1331 def NamingConventionCheckPathName(self):
1332 if EccGlobalData.gConfig.NamingConventionCheckPathName == '1' or EccGlobalData.gConfig.NamingConventionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
1333 EdkLogger.quiet("Checking naming covention of file path name ...")
1334 Pattern = re.compile(r'^[A-Z]+\S*[a-z]\S*$')
1335 SqlCommand = """select ID, Name from File"""
1336 RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
1337 for Record in RecordSet:
1338 if not Pattern.match(Record[1]):
1339 if not EccGlobalData.gException.IsException(ERROR_NAMING_CONVENTION_CHECK_PATH_NAME, Record[1]):
1340 EccGlobalData.gDb.TblReport.Insert(ERROR_NAMING_CONVENTION_CHECK_PATH_NAME, OtherMsg="The file path [%s] does not follow the rules" % (Record[1]), BelongsToTable='File', BelongsToItem=Record[0])
1341
1342 # Rule for path name, variable name and function name
1343 # 1. First character should be upper case
1344 # 2. Existing lower case in a word
1345 # 3. No space existence
1346 # 4. Global variable name must start with a 'g'
1347 # Check whether the variable name followed the rule
1348 def NamingConventionCheckVariableName(self, FileTable):
1349 if EccGlobalData.gConfig.NamingConventionCheckVariableName == '1' or EccGlobalData.gConfig.NamingConventionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
1350 EdkLogger.quiet("Checking naming covention of variable name ...")
1351 Pattern = re.compile(r'^[A-Zgm]+\S*[a-z]\S*$')
1352
1353 SqlCommand = """select ID, Name from %s where Model = %s""" % (FileTable, MODEL_IDENTIFIER_VARIABLE)
1354 RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
1355 for Record in RecordSet:
1356 Var = Record[1]
1357 if Var.startswith('CONST'):
1358 Var = Var[5:].lstrip()
1359 if not Pattern.match(Var):
1360 if not EccGlobalData.gException.IsException(ERROR_NAMING_CONVENTION_CHECK_VARIABLE_NAME, Record[1]):
1361 EccGlobalData.gDb.TblReport.Insert(ERROR_NAMING_CONVENTION_CHECK_VARIABLE_NAME, OtherMsg="The variable name [%s] does not follow the rules" % (Record[1]), BelongsToTable=FileTable, BelongsToItem=Record[0])
1362
1363 # Rule for path name, variable name and function name
1364 # 1. First character should be upper case
1365 # 2. Existing lower case in a word
1366 # 3. No space existence
1367 # Check whether the function name followed the rule
1368 def NamingConventionCheckFunctionName(self):
1369 if EccGlobalData.gConfig.NamingConventionCheckFunctionName == '1' or EccGlobalData.gConfig.NamingConventionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
1370 EdkLogger.quiet("Checking naming covention of function name ...")
1371 Pattern = re.compile(r'^[A-Z]+\S*[a-z]\S*$')
1372 SqlCommand = """select ID, Name from Function"""
1373 RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
1374 for Record in RecordSet:
1375 if not Pattern.match(Record[1]):
1376 if not EccGlobalData.gException.IsException(ERROR_NAMING_CONVENTION_CHECK_FUNCTION_NAME, Record[1]):
1377 EccGlobalData.gDb.TblReport.Insert(ERROR_NAMING_CONVENTION_CHECK_FUNCTION_NAME, OtherMsg="The function name [%s] does not follow the rules" % (Record[1]), BelongsToTable='Function', BelongsToItem=Record[0])
1378
1379 # Check whether NO use short variable name with single character
1380 def NamingConventionCheckSingleCharacterVariable(self, FileTable):
1381 if EccGlobalData.gConfig.NamingConventionCheckSingleCharacterVariable == '1' or EccGlobalData.gConfig.NamingConventionCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
1382 EdkLogger.quiet("Checking naming covention of single character variable name ...")
1383
1384 SqlCommand = """select ID, Name from %s where Model = %s""" % (FileTable, MODEL_IDENTIFIER_VARIABLE)
1385 RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
1386 for Record in RecordSet:
1387 Variable = Record[1].replace('*', '')
1388 if len(Variable) == 1:
1389 if not EccGlobalData.gException.IsException(ERROR_NAMING_CONVENTION_CHECK_SINGLE_CHARACTER_VARIABLE, Record[1]):
1390 EccGlobalData.gDb.TblReport.Insert(ERROR_NAMING_CONVENTION_CHECK_SINGLE_CHARACTER_VARIABLE, OtherMsg="The variable name [%s] does not follow the rules" % (Record[1]), BelongsToTable=FileTable, BelongsToItem=Record[0])
1391
1392 def FindPara(FilePath, Para, CallingLine):
1393 Lines = open(FilePath).readlines()
1394 Line = ''
1395 for Index in range(CallingLine - 1, 0, -1):
1396 # Find the nearest statement for Para
1397 Line = Lines[Index].strip()
1398 if Line.startswith('%s = ' % Para):
1399 Line = Line.strip()
1400 return Line
1401 break
1402
1403 return ''
1404
1405 ##
1406 #
1407 # This acts like the main() function for the script, unless it is 'import'ed into another
1408 # script.
1409 #
1410 if __name__ == '__main__':
1411 Check = Check()
1412 Check.Check()