Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use Collections.LIST | |
| 3 | ||
| 4 | use Source.LOCATION | |
| 5 | use Trees | |
| 6 | ||
| 7 | use Logging.Logger | |
| 8 | ||
| 9 | // Reports each use of a symbol whose declaration carries | |
| 10 | // System.ObsoleteAttribute: a warning, or an error where the attribute | |
| 11 | // says so. Runs over the uses compile-expressions recorded, once every | |
| 12 | // file's attributes are resolved, so a deprecation declared in one file | |
| 13 | // is seen at a use in any other. The declaration itself is not a use, | |
| 14 | // and nor is anything written inside a deprecated declaration: code | |
| 15 | // that is itself on its way out can go on using what it always did. | |
| 16 | class CHECK_DEPRECATED_USES( | |
| 17 | _logger: Logger, | |
| 18 | _symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS | |
| 19 | ) is | |
| 20 | // A constructor call reads as the type it constructs, so a | |
| 21 | // constructor with no deprecation of its own reports its type's. | |
| 22 | _reported(symbol: Semantic.Symbols.Symbol) -> Semantic.Symbols.Symbol is | |
| 23 | if !symbol.deprecation? /\ symbol.is_constructor then | |
| 24 | if let owner: Semantic.Symbols.Symbol = symbol.owner then | |
| 25 | return owner | |
| 26 | fi | |
| 27 | fi | |
| 28 | ||
| 29 | return symbol | |
| 30 | si | |
| 31 | ||
| 32 | check(source_file: Compiler.SOURCE_FILE) is | |
| 33 | let uses = _symbol_use_locations.symbol_uses_in_file(source_file.file_name) | |
| 34 | ||
| 35 | let deprecated_names = LIST[LOCATION]() | |
| 36 | ||
| 37 | for found in uses do | |
| 38 | let symbol = _reported(found.value) | |
| 39 | ||
| 40 | if symbol.deprecation? then | |
| 41 | deprecated_names.add(symbol.location) | |
| 42 | fi | |
| 43 | od | |
| 44 | ||
| 45 | if deprecated_names.count == 0 then | |
| 46 | return | |
| 47 | fi | |
| 48 | ||
| 49 | let regions = DEPRECATED_REGIONS(deprecated_names).collect(source_file.definition) | |
| 50 | ||
| 51 | for found in uses do | |
| 52 | if found.location =~ found.value.location then | |
| 53 | continue | |
| 54 | fi | |
| 55 | ||
| 56 | if _is_inside_any(found.location, regions) then | |
| 57 | continue | |
| 58 | fi | |
| 59 | ||
| 60 | let symbol = _reported(found.value) | |
| 61 | ||
| 62 | if let deprecation = symbol.deprecation then | |
| 63 | let message = | |
| 64 | if let text = deprecation.message then | |
| 65 | "{symbol.name} is deprecated: {text}" | |
| 66 | else | |
| 67 | "{symbol.name} is deprecated" | |
| 68 | fi | |
| 69 | ||
| 70 | if deprecation.is_error then | |
| 71 | _logger.error(found.location, message) | |
| 72 | else | |
| 73 | _logger.warn(found.location, "deprecated", message) | |
| 74 | fi | |
| 75 | fi | |
| 76 | od | |
| 77 | si | |
| 78 | ||
| 79 | _is_inside_any(location: LOCATION, regions: LIST[LOCATION]) -> bool is | |
| 80 | for region in regions do | |
| 81 | if region.contains(location) then | |
| 82 | return true | |
| 83 | fi | |
| 84 | od | |
| 85 | ||
| 86 | return false | |
| 87 | si | |
| 88 | si | |
| 89 | ||
| 90 | // The source ranges of the declarations in one file whose names sit | |
| 91 | // at the given locations: the whole declaration, body and members | |
| 92 | // included. | |
| 93 | class DEPRECATED_REGIONS: Visitor is | |
| 94 | _names: LIST[LOCATION] | |
| 95 | _regions: LIST[LOCATION] | |
| 96 | ||
| 97 | init(names: LIST[LOCATION]) is | |
| 98 | super.init() | |
| 99 | ||
| 100 | _names = names | |
| 101 | _regions = LIST[LOCATION]() | |
| 102 | si | |
| 103 | ||
| 104 | collect(definition: Definitions.Definition) -> LIST[LOCATION] is | |
| 105 | definition.walk(self) | |
| 106 | ||
| 107 | return _regions | |
| 108 | si | |
| 109 | ||
| 110 | _note(name: Identifiers.Identifier?, location: LOCATION) is | |
| 111 | if let n = name then | |
| 112 | for declared in _names do | |
| 113 | if declared =~ n.location then | |
| 114 | _regions.add(location) | |
| 115 | return | |
| 116 | fi | |
| 117 | od | |
| 118 | fi | |
| 119 | si | |
| 120 | ||
| 121 | pre(`class: Definitions.CLASS) -> bool is | |
| 122 | _note(`class.name, `class.location) | |
| 123 | return false | |
| 124 | si | |
| 125 | ||
| 126 | pre(`trait: Definitions.TRAIT) -> bool is | |
| 127 | _note(`trait.name, `trait.location) | |
| 128 | return false | |
| 129 | si | |
| 130 | ||
| 131 | pre(`struct: Definitions.STRUCT) -> bool is | |
| 132 | _note(`struct.name, `struct.location) | |
| 133 | return false | |
| 134 | si | |
| 135 | ||
| 136 | pre(`union: Definitions.UNION) -> bool is | |
| 137 | _note(`union.name, `union.location) | |
| 138 | return false | |
| 139 | si | |
| 140 | ||
| 141 | pre(variant: Definitions.VARIANT) -> bool is | |
| 142 | _note(variant.name, variant.location) | |
| 143 | return false | |
| 144 | si | |
| 145 | ||
| 146 | pre(`enum: Definitions.ENUM) -> bool is | |
| 147 | _note(`enum.name, `enum.location) | |
| 148 | return false | |
| 149 | si | |
| 150 | ||
| 151 | pre(function: Definitions.FUNCTION) -> bool is | |
| 152 | _note(function.name, function.location) | |
| 153 | return false | |
| 154 | si | |
| 155 | ||
| 156 | pre(property: Definitions.PROPERTY) -> bool is | |
| 157 | _note(property.name, property.location) | |
| 158 | return false | |
| 159 | si | |
| 160 | si | |
| 161 | si |