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;
89 auto It = SortOrderMap.find(Attribute);
90 if (It == SortOrderMap.end())
91 It = SortOrderMap.insert({Attribute, SortOrderMap.size()}).first;
94 const auto Ordinal = It->second;
95 if (!Ordinals.insert(Ordinal).second) {
100 if (Ordinal >= Indices.size())
101 Indices.resize(Ordinal + 1);
102 Indices[Ordinal] = Index++;
105 PropertyAttributes.push_back({Attribute,
Value});
108 if (!HasDuplicates) {
110 if (PropertyAttributes.size() < 2)
114 bool IsSorted =
true;
115 for (
const auto Ordinal : Ordinals) {
116 const auto Index = Indices[Ordinal];
117 if (Index < PrevIndex) {
121 assert(Index > PrevIndex);
133 for (
const auto Ordinal : Ordinals) {
139 const auto &PropertyEntry = PropertyAttributes[Indices[Ordinal]];
140 NewText += PropertyEntry.Attribute;
142 if (
const auto Value = PropertyEntry.Value; !
Value.empty()) {
150 auto Replacement = tooling::Replacement(SourceMgr,
Range, NewText);
151 auto Err = Fixes.
add(Replacement);
153 llvm::errs() <<
"Error while reodering ObjC property attributes : "
154 << llvm::toString(std::move(Err)) <<
"\n";
158void ObjCPropertyAttributeOrderFixer::analyzeObjCPropertyDecl(
159 const SourceManager &SourceMgr,
const AdditionalKeywords &Keywords,
160 tooling::Replacements &Fixes,
const FormatToken *Tok) {
164 const FormatToken *
const PropertyTok = Tok->Next;
165 if (!PropertyTok || PropertyTok->isNot(Keywords.kw_property))
169 const FormatToken *
const LParenTok = PropertyTok->getNextNonComment();
170 if (!LParenTok || LParenTok->isNot(tok::l_paren))
174 const FormatToken *
const RParenTok = LParenTok->MatchingParen;
178 sortPropertyAttributes(SourceMgr, Fixes, LParenTok->Next, RParenTok);
181std::pair<tooling::Replacements, unsigned>
182ObjCPropertyAttributeOrderFixer::analyze(
184 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
185 FormatTokenLexer &Tokens) {
186 tooling::Replacements Fixes;
187 const AdditionalKeywords &Keywords = Tokens.getKeywords();
191 for (AnnotatedLine *
Line : AnnotatedLines) {
197 if (
First->Finalized)
202 for (
const auto *Tok =
First; Tok !=
Last; Tok = Tok->Next) {
206 if (Tok->isNot(TT_ObjCProperty))
209 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.