37void ObjCPropertyAttributeOrderFixer::sortPropertyAttributes(
45 if (BeginTok == EndTok || BeginTok->
Next == EndTok)
49 std::set<unsigned> Ordinals;
56 bool HasDuplicates =
false;
58 for (
auto Tok = BeginTok; Tok != EndTok; Tok = Tok->
Next) {
60 if (Tok->is(tok::comma)) {
66 if (!Tok->isOneOf(tok::identifier, tok::kw_class)) {
71 const StringRef Attribute{Tok->TokenText};
78 if (Tok->Next->is(tok::equal)) {
81 if (Tok->Next->isNot(tok::identifier)) {
86 Value = Tok->TokenText;
91 SortOrderMap.try_emplace(Attribute, SortOrderMap.size()).first->second;
92 if (!Ordinals.insert(Ordinal).second) {
97 if (Ordinal >= Indices.size())
98 Indices.resize(Ordinal + 1);
99 Indices[Ordinal] = Index++;
102 PropertyAttributes.push_back({Attribute,
Value});
105 if (!HasDuplicates) {
107 if (PropertyAttributes.size() < 2)
111 bool IsSorted =
true;
112 for (
const auto Ordinal : Ordinals) {
113 const auto Index = Indices[Ordinal];
114 if (Index < PrevIndex) {
118 assert(Index > PrevIndex);
130 for (
const auto Ordinal : Ordinals) {
136 const auto &PropertyEntry = PropertyAttributes[Indices[Ordinal]];
137 NewText += PropertyEntry.Attribute;
139 if (
const auto Value = PropertyEntry.Value; !
Value.empty()) {
147 auto Replacement = tooling::Replacement(SourceMgr,
Range, NewText);
148 auto Err = Fixes.
add(Replacement);
150 llvm::errs() <<
"Error while reodering ObjC property attributes : "
151 << llvm::toString(std::move(Err)) <<
"\n";
155void ObjCPropertyAttributeOrderFixer::analyzeObjCPropertyDecl(
156 const SourceManager &SourceMgr,
const AdditionalKeywords &Keywords,
157 tooling::Replacements &Fixes,
const FormatToken *Tok) {
161 const FormatToken *
const PropertyTok = Tok->Next;
162 if (!PropertyTok || PropertyTok->isNot(Keywords.kw_property))
166 const FormatToken *
const LParenTok = PropertyTok->getNextNonComment();
167 if (!LParenTok || LParenTok->isNot(tok::l_paren))
171 const FormatToken *
const RParenTok = LParenTok->MatchingParen;
175 sortPropertyAttributes(SourceMgr, Fixes, LParenTok->Next, RParenTok);
178std::pair<tooling::Replacements, unsigned>
179ObjCPropertyAttributeOrderFixer::analyze(
181 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
182 FormatTokenLexer &Tokens) {
183 tooling::Replacements Fixes;
184 const AdditionalKeywords &Keywords = Tokens.getKeywords();
188 for (AnnotatedLine *
Line : AnnotatedLines) {
194 if (
First->Finalized)
199 for (
const auto *Tok =
First; Tok !=
Last; Tok = Tok->Next) {
203 if (Tok->isNot(TT_ObjCProperty))
206 analyzeObjCPropertyDecl(SourceMgr, Keywords, Fixes, Tok);
This file declares ObjCPropertyAttributeOrderFixer, a TokenAnalyzer that adjusts the order of attribu...
static CharSourceRange getCharRange(SourceRange R)
This class handles loading and caching of source files into memory.
SourceLocation getEndLoc() const
The JSON file list parser is used to communicate input to InstallAPI.
@ Property
The type of a property.