|
| 1 | +//===--- ObjCPropertyAttributeOrderFixer.cpp -------------------*- C++--*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +/// |
| 9 | +/// \file |
| 10 | +/// This file implements ObjCPropertyAttributeOrderFixer, a TokenAnalyzer that |
| 11 | +/// adjusts the order of attributes in an ObjC `@property(...)` declaration, |
| 12 | +/// depending on the style. |
| 13 | +/// |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +#include "ObjCPropertyAttributeOrderFixer.h" |
| 17 | + |
| 18 | +#include "llvm/ADT/Sequence.h" |
| 19 | + |
| 20 | +#include <algorithm> |
| 21 | + |
| 22 | +namespace clang { |
| 23 | +namespace format { |
| 24 | + |
| 25 | +ObjCPropertyAttributeOrderFixer::ObjCPropertyAttributeOrderFixer( |
| 26 | + const Environment &Env, const FormatStyle &Style) |
| 27 | + : TokenAnalyzer(Env, Style) { |
| 28 | + |
| 29 | + // Create an "order priority" map to use to sort properties. |
| 30 | + unsigned index = 0; |
| 31 | + for (const auto &Property : Style.ObjCPropertyAttributeOrder) |
| 32 | + SortOrderMap[Property] = index++; |
| 33 | +} |
| 34 | + |
| 35 | +struct ObjCPropertyEntry { |
| 36 | + StringRef Attribute; // eg, "readwrite" |
| 37 | + StringRef Value; // eg, the "foo" of the attribute "getter=foo" |
| 38 | +}; |
| 39 | + |
| 40 | +static bool isObjCPropertyAttribute(const FormatToken *Tok) { |
| 41 | + // Most attributes look like identifiers, but `class` is a keyword. |
| 42 | + return Tok->isOneOf(tok::identifier, tok::kw_class); |
| 43 | +} |
| 44 | + |
| 45 | +void ObjCPropertyAttributeOrderFixer::sortPropertyAttributes( |
| 46 | + const SourceManager &SourceMgr, tooling::Replacements &Fixes, |
| 47 | + const FormatToken *BeginTok, const FormatToken *EndTok) const { |
| 48 | + assert(BeginTok); |
| 49 | + assert(EndTok); |
| 50 | + assert(EndTok->Previous); |
| 51 | + |
| 52 | + // If there are zero or one tokens, nothing to do. |
| 53 | + if (BeginTok == EndTok || BeginTok->Next == EndTok) |
| 54 | + return; |
| 55 | + |
| 56 | + // Collect the attributes. |
| 57 | + SmallVector<ObjCPropertyEntry, 8> PropertyAttributes; |
| 58 | + for (auto Tok = BeginTok; Tok != EndTok; Tok = Tok->Next) { |
| 59 | + assert(Tok); |
| 60 | + if (Tok->is(tok::comma)) { |
| 61 | + // Ignore the comma separators. |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + if (!isObjCPropertyAttribute(Tok)) { |
| 66 | + // If we hit any other kind of token, just bail. |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // Memoize the attribute. (Note that 'class' is a legal attribute!) |
| 71 | + PropertyAttributes.push_back({Tok->TokenText, StringRef{}}); |
| 72 | + |
| 73 | + // Also handle `getter=getFoo` attributes. |
| 74 | + // (Note: no check needed against `EndTok`, since its type is not |
| 75 | + // BinaryOperator or Identifier) |
| 76 | + assert(Tok->Next); |
| 77 | + if (Tok->Next->is(tok::equal)) { |
| 78 | + Tok = Tok->Next; |
| 79 | + assert(Tok->Next); |
| 80 | + if (Tok->Next->isNot(tok::identifier)) { |
| 81 | + // If we hit any other kind of token, just bail. It's unusual/illegal. |
| 82 | + return; |
| 83 | + } |
| 84 | + Tok = Tok->Next; |
| 85 | + PropertyAttributes.back().Value = Tok->TokenText; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // There's nothing to do unless there's more than one attribute. |
| 90 | + if (PropertyAttributes.size() < 2) |
| 91 | + return; |
| 92 | + |
| 93 | + // Create a "remapping index" on how to reorder the attributes. |
| 94 | + SmallVector<unsigned, 8> Indices = |
| 95 | + llvm::to_vector<8>(llvm::seq<unsigned>(0, PropertyAttributes.size())); |
| 96 | + |
| 97 | + // Sort the indices based on the priority stored in 'SortOrderMap'; use Max |
| 98 | + // for missing values. |
| 99 | + const auto SortOrderMax = Style.ObjCPropertyAttributeOrder.size(); |
| 100 | + auto SortIndex = [&](const StringRef &Needle) -> unsigned { |
| 101 | + auto I = SortOrderMap.find(Needle); |
| 102 | + return (I == SortOrderMap.end()) ? SortOrderMax : I->getValue(); |
| 103 | + }; |
| 104 | + llvm::stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) { |
| 105 | + return SortIndex(PropertyAttributes[LHSI].Attribute) < |
| 106 | + SortIndex(PropertyAttributes[RHSI].Attribute); |
| 107 | + }); |
| 108 | + |
| 109 | + // If the property order is already correct, then no fix-up is needed. |
| 110 | + if (llvm::is_sorted(Indices)) |
| 111 | + return; |
| 112 | + |
| 113 | + // Generate the replacement text. |
| 114 | + std::string NewText; |
| 115 | + const auto AppendAttribute = [&](const ObjCPropertyEntry &PropertyEntry) { |
| 116 | + NewText += PropertyEntry.Attribute; |
| 117 | + |
| 118 | + if (!PropertyEntry.Value.empty()) { |
| 119 | + NewText += "="; |
| 120 | + NewText += PropertyEntry.Value; |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + AppendAttribute(PropertyAttributes[Indices[0]]); |
| 125 | + for (unsigned Index : llvm::drop_begin(Indices)) { |
| 126 | + NewText += ", "; |
| 127 | + AppendAttribute(PropertyAttributes[Index]); |
| 128 | + } |
| 129 | + |
| 130 | + auto Range = CharSourceRange::getCharRange( |
| 131 | + BeginTok->getStartOfNonWhitespace(), EndTok->Previous->Tok.getEndLoc()); |
| 132 | + auto Replacement = tooling::Replacement(SourceMgr, Range, NewText); |
| 133 | + auto Err = Fixes.add(Replacement); |
| 134 | + if (Err) { |
| 135 | + llvm::errs() << "Error while reodering ObjC property attributes : " |
| 136 | + << llvm::toString(std::move(Err)) << "\n"; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +void ObjCPropertyAttributeOrderFixer::analyzeObjCPropertyDecl( |
| 141 | + const SourceManager &SourceMgr, const AdditionalKeywords &Keywords, |
| 142 | + tooling::Replacements &Fixes, const FormatToken *Tok) const { |
| 143 | + assert(Tok); |
| 144 | + |
| 145 | + // Expect `property` to be the very next token or else just bail early. |
| 146 | + const FormatToken *const PropertyTok = Tok->Next; |
| 147 | + if (!PropertyTok || PropertyTok->isNot(Keywords.kw_property)) |
| 148 | + return; |
| 149 | + |
| 150 | + // Expect the opening paren to be the next token or else just bail early. |
| 151 | + const FormatToken *const LParenTok = PropertyTok->getNextNonComment(); |
| 152 | + if (!LParenTok || LParenTok->isNot(tok::l_paren)) |
| 153 | + return; |
| 154 | + |
| 155 | + // Get the matching right-paren, the bounds for property attributes. |
| 156 | + const FormatToken *const RParenTok = LParenTok->MatchingParen; |
| 157 | + if (!RParenTok) |
| 158 | + return; |
| 159 | + |
| 160 | + sortPropertyAttributes(SourceMgr, Fixes, LParenTok->Next, RParenTok); |
| 161 | +} |
| 162 | + |
| 163 | +std::pair<tooling::Replacements, unsigned> |
| 164 | +ObjCPropertyAttributeOrderFixer::analyze( |
| 165 | + TokenAnnotator & /*Annotator*/, |
| 166 | + SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, |
| 167 | + FormatTokenLexer &Tokens) { |
| 168 | + tooling::Replacements Fixes; |
| 169 | + const AdditionalKeywords &Keywords = Tokens.getKeywords(); |
| 170 | + const SourceManager &SourceMgr = Env.getSourceManager(); |
| 171 | + AffectedRangeMgr.computeAffectedLines(AnnotatedLines); |
| 172 | + |
| 173 | + for (AnnotatedLine *Line : AnnotatedLines) { |
| 174 | + assert(Line); |
| 175 | + if (!Line->Affected || Line->Type != LT_ObjCProperty) |
| 176 | + continue; |
| 177 | + FormatToken *First = Line->First; |
| 178 | + assert(First); |
| 179 | + if (First->Finalized) |
| 180 | + continue; |
| 181 | + |
| 182 | + const auto *Last = Line->Last; |
| 183 | + |
| 184 | + for (const auto *Tok = First; Tok != Last; Tok = Tok->Next) { |
| 185 | + assert(Tok); |
| 186 | + |
| 187 | + // Skip until the `@` of a `@property` declaration. |
| 188 | + if (Tok->isNot(TT_ObjCProperty)) |
| 189 | + continue; |
| 190 | + |
| 191 | + analyzeObjCPropertyDecl(SourceMgr, Keywords, Fixes, Tok); |
| 192 | + |
| 193 | + // There are never two `@property` in a line (they are split |
| 194 | + // by other passes), so this pass can break after just one. |
| 195 | + break; |
| 196 | + } |
| 197 | + } |
| 198 | + return {Fixes, 0}; |
| 199 | +} |
| 200 | + |
| 201 | +} // namespace format |
| 202 | +} // namespace clang |
0 commit comments