Check enumerated values for duplicates. - enums - list of ` ` Elements returns the list with duplicates stripped
(self, enums)
| 531 | return [None, None] |
| 532 | |
| 533 | def checkDuplicateEnums(self, enums): |
| 534 | """Check enumerated values for duplicates. |
| 535 | |
| 536 | - enums - list of `<enum>` Elements |
| 537 | |
| 538 | returns the list with duplicates stripped""" |
| 539 | # Dictionaries indexed by name and numeric value. |
| 540 | # Entries are [ Element, numVal, strVal ] matching name or value |
| 541 | |
| 542 | nameMap = {} |
| 543 | valueMap = {} |
| 544 | |
| 545 | stripped = [] |
| 546 | for elem in enums: |
| 547 | name = elem.get('name') |
| 548 | alias = elem.get('alias') |
| 549 | (numVal, strVal) = self.enumToValue(elem, True) |
| 550 | |
| 551 | if name in nameMap: |
| 552 | # Duplicate name found; check values |
| 553 | (name2, numVal2, strVal2) = nameMap[name] |
| 554 | |
| 555 | # Duplicate enum values for the same name are benign. This |
| 556 | # happens when defining the same enum conditionally in |
| 557 | # several extension blocks. |
| 558 | if (strVal2 == strVal or (numVal is not None |
| 559 | and numVal == numVal2)): |
| 560 | True |
| 561 | # self.logMsg('info', 'checkDuplicateEnums: Duplicate enum (' + name + |
| 562 | # ') found with the same value:' + strVal) |
| 563 | else: |
| 564 | self.logMsg('warn', 'checkDuplicateEnums: Duplicate enum (' + name |
| 565 | + ') found with different values:' + strVal |
| 566 | + ' and ' + strVal2) |
| 567 | |
| 568 | # Do not add the duplicate to the returned list |
| 569 | continue |
| 570 | elif numVal in valueMap: |
| 571 | # Duplicate value found (such as an alias); report it, but |
| 572 | # still add this enum to the list. |
| 573 | (name2, numVal2, strVal2) = valueMap[numVal] |
| 574 | |
| 575 | # If an enum is tagged with both a value and an alias then this is deliberate - no error |
| 576 | if alias != name2.get('name'): |
| 577 | msg = 'Two enums found with the same value: {} = {} = {}. '.format(name, name2.get('name'), strVal) |
| 578 | msg += 'If this is deliberate, tagging one as an `alias` of the other will fix this error.' |
| 579 | self.logMsg('error', msg) |
| 580 | |
| 581 | # Track this enum to detect followon duplicates |
| 582 | nameMap[name] = [elem, numVal, strVal] |
| 583 | if alias is None and numVal is not None: |
| 584 | valueMap[numVal] = [elem, numVal, strVal] |
| 585 | |
| 586 | # Add this enum to the list |
| 587 | stripped.append(elem) |
| 588 | |
| 589 | # Return the list |
| 590 | return stripped |
no test coverage detected