|
| 1 | +/** |
| 2 | + * A module for identifying guideline recategorizations specified in a `conding-standards.yml` file. |
| 3 | + */ |
| 4 | + |
| 5 | +import cpp |
| 6 | +import semmle.code.cpp.XML |
| 7 | +import codingstandards.cpp.exclusions.RuleMetadata |
| 8 | +import codingstandards.cpp.Config |
| 9 | + |
| 10 | +/** A container of guideline recategorizations. */ |
| 11 | +class GuidelineRecategorizations extends CodingStandardsConfigSection { |
| 12 | + GuidelineRecategorizations() { hasName("guideline-recategorizations") } |
| 13 | +} |
| 14 | + |
| 15 | +newtype TEffectiveCategory = |
| 16 | + TInvalid(string reason) { |
| 17 | + exists(GuidelineRecategorization gr | reason = gr.getAnInvalidReason()) |
| 18 | + } or |
| 19 | + TDisapplied() or |
| 20 | + TAdvisory() or |
| 21 | + TRequired() or |
| 22 | + TMandatory() |
| 23 | + |
| 24 | +class EffectiveCategory extends TEffectiveCategory { |
| 25 | + string toString() { |
| 26 | + this instanceof TInvalid and result = "invalid" |
| 27 | + or |
| 28 | + this instanceof TDisapplied and result = "disapplied" |
| 29 | + or |
| 30 | + this instanceof TAdvisory and result = "advisory" |
| 31 | + or |
| 32 | + this instanceof TRequired and result = "required" |
| 33 | + or |
| 34 | + this instanceof TMandatory and result = "mandatory" |
| 35 | + } |
| 36 | + |
| 37 | + /** Holds if the effective category permits a deviation */ |
| 38 | + predicate permitsDeviation() { not this instanceof TMandatory and not this instanceof TInvalid } |
| 39 | +} |
| 40 | + |
| 41 | +class GuidelineRecategorization extends XMLElement { |
| 42 | + GuidelineRecategorization() { |
| 43 | + getParent() instanceof GuidelineRecategorizations and |
| 44 | + hasName("guideline-recategorizations-entry") |
| 45 | + } |
| 46 | + |
| 47 | + string getRuleId() { result = getAChild("rule-id").getTextValue() } |
| 48 | + |
| 49 | + string getCategory() { result = getAChild("category").getTextValue() } |
| 50 | + |
| 51 | + /** Get a query for which a recategorization is specified. */ |
| 52 | + Query getQuery() { result.getRuleId() = getRuleId() } |
| 53 | + |
| 54 | + private EffectiveCategory getValidEffectiveCategory() { |
| 55 | + exists(string category, string recategorization | |
| 56 | + category = getQuery().getCategory() and |
| 57 | + recategorization = getCategory() |
| 58 | + | |
| 59 | + result = TMandatory() and |
| 60 | + category = ["advisory", "required"] and |
| 61 | + recategorization = "mandatory" |
| 62 | + or |
| 63 | + result = TRequired() and |
| 64 | + category = "advisory" and |
| 65 | + recategorization = "required" |
| 66 | + or |
| 67 | + result = TDisapplied() and |
| 68 | + category = "advisory" and |
| 69 | + recategorization = "disapplied" |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + private predicate isValidRecategorization(string category, string recategorization) { |
| 74 | + category = ["advisory", "required"] and |
| 75 | + recategorization = "mandatory" |
| 76 | + or |
| 77 | + category = "advisory" and |
| 78 | + recategorization = "required" |
| 79 | + or |
| 80 | + category = "advisory" and |
| 81 | + recategorization = "disapplied" |
| 82 | + } |
| 83 | + |
| 84 | + string getAnInvalidReason() { |
| 85 | + not isValidRecategorization(this.getQuery().getCategory(), this.getCategory()) and |
| 86 | + if exists(this.getQuery()) |
| 87 | + then |
| 88 | + result = |
| 89 | + "Invalid recategorization from '" + this.getQuery().getCategory() + "' to '" + |
| 90 | + this.getCategory() + "'." |
| 91 | + else result = "Unknown rule id '" + this.getRuleId() + "'." |
| 92 | + } |
| 93 | + |
| 94 | + predicate isValid() { not isInvalid() } |
| 95 | + |
| 96 | + predicate isInvalid() { getEffectiveCategory() = TInvalid(_) } |
| 97 | + |
| 98 | + EffectiveCategory getEffectiveCategory() { |
| 99 | + ( |
| 100 | + if exists(getValidEffectiveCategory()) |
| 101 | + then result = getValidEffectiveCategory() |
| 102 | + else result = TInvalid(getAnInvalidReason()) |
| 103 | + ) |
| 104 | + } |
| 105 | +} |
0 commit comments