35void ObjCPropertyAttributeOrderFixer::sortPropertyAttributes(
43 if (BeginTok == EndTok || BeginTok->
Next == EndTok)
47 std::set<unsigned> Ordinals;
54 bool HasDuplicates =
false;
56 for (
auto Tok = BeginTok;
Tok != EndTok;
Tok =
Tok->Next) {
58 if (
Tok->is(tok::comma)) {
64 if (!
Tok->isOneOf(tok::identifier, tok::kw_class)) {
69 const StringRef Attribute(
Tok->TokenText);
76 if (
Tok->Next->is(tok::equal)) {
79 if (
Tok->Next->isNot(tok::identifier)) {
89 SortOrderMap.try_emplace(Attribute, SortOrderMap.size()).first->second;
90 if (!Ordinals.insert(Ordinal).second) {
95 if (Ordinal >= Indices.size())
96 Indices.resize(Ordinal + 1);
97 Indices[Ordinal] = Index++;
100 PropertyAttributes.push_back({Attribute,
Value});
103 if (!HasDuplicates) {
105 if (PropertyAttributes.size() < 2)
109 bool IsSorted =
true;
110 for (
const auto Ordinal : Ordinals) {
111 const auto Index = Indices[Ordinal];
112 if (Index < PrevIndex) {
116 assert(Index > PrevIndex);
128 for (
const auto Ordinal : Ordinals) {
134 const auto &PropertyEntry = PropertyAttributes[Indices[Ordinal]];
135 NewText += PropertyEntry.Attribute;
137 if (
const auto Value = PropertyEntry.Value; !
Value.empty()) {
145 auto Replacement = tooling::Replacement(SourceMgr, Range, NewText);
146 auto Err = Fixes.
add(Replacement);
148 llvm::errs() <<
"Error while reodering ObjC property attributes : "
149 << llvm::toString(std::move(Err)) <<
"\n";
153void ObjCPropertyAttributeOrderFixer::analyzeObjCPropertyDecl(
154 const SourceManager &SourceMgr,
const AdditionalKeywords &Keywords,
160 if (!PropertyTok || PropertyTok->isNot(Keywords.kw_property))
164 const FormatToken *
const LParenTok = PropertyTok->getNextNonComment();
165 if (!LParenTok || LParenTok->isNot(tok::l_paren))
169 const FormatToken *
const RParenTok = LParenTok->MatchingParen;
173 sortPropertyAttributes(SourceMgr, Fixes, LParenTok->Next, RParenTok);
176std::pair<tooling::Replacements, unsigned>
177ObjCPropertyAttributeOrderFixer::analyze(
181 tooling::Replacements Fixes;
182 const AdditionalKeywords &Keywords = Tokens.getKeywords();
183 const SourceManager &SourceMgr =
Env.getSourceManager();
186 for (AnnotatedLine *
Line : AnnotatedLines) {
192 if (
First->Finalized)
201 if (
Tok->isNot(TT_ObjCProperty))
204 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.