Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 1 | //===-- tools/extra/clang-reorder-fields/ReorderFieldsAction.cpp -*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
| 10 | /// This file contains the definition of the |
| 11 | /// ReorderFieldsAction::newASTConsumer method |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "ReorderFieldsAction.h" |
| 16 | #include "clang/AST/AST.h" |
| 17 | #include "clang/AST/ASTConsumer.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/AST/RecursiveASTVisitor.h" |
| 21 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 22 | #include "clang/Lex/Lexer.h" |
| 23 | #include "clang/Tooling/Refactoring.h" |
Alexander Shaposhnikov | b687fdd | 2017-07-30 06:43:03 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SetVector.h" |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 25 | #include <algorithm> |
| 26 | #include <string> |
| 27 | |
| 28 | namespace clang { |
| 29 | namespace reorder_fields { |
| 30 | using namespace clang::ast_matchers; |
Alexander Shaposhnikov | b687fdd | 2017-07-30 06:43:03 +0000 | [diff] [blame] | 31 | using llvm::SmallSetVector; |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 32 | |
Dmitri Gribenko | 282dc72 | 2019-08-22 11:32:57 +0000 | [diff] [blame] | 33 | /// Finds the definition of a record by name. |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 34 | /// |
| 35 | /// \returns nullptr if the name is ambiguous or not found. |
Alexander Shaposhnikov | eaf833c | 2017-07-20 21:41:20 +0000 | [diff] [blame] | 36 | static const RecordDecl *findDefinition(StringRef RecordName, |
| 37 | ASTContext &Context) { |
Alexander Shaposhnikov | b687fdd | 2017-07-30 06:43:03 +0000 | [diff] [blame] | 38 | auto Results = |
Benjamin Kramer | 4e3f4f0 | 2020-01-29 10:52:25 +0100 | [diff] [blame] | 39 | match(recordDecl(hasName(RecordName), isDefinition()).bind("recordDecl"), |
Alexander Shaposhnikov | b687fdd | 2017-07-30 06:43:03 +0000 | [diff] [blame] | 40 | Context); |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 41 | if (Results.empty()) { |
| 42 | llvm::errs() << "Definition of " << RecordName << " not found\n"; |
| 43 | return nullptr; |
| 44 | } |
| 45 | if (Results.size() > 1) { |
| 46 | llvm::errs() << "The name " << RecordName |
| 47 | << " is ambiguous, several definitions found\n"; |
| 48 | return nullptr; |
| 49 | } |
Alexander Shaposhnikov | eaf833c | 2017-07-20 21:41:20 +0000 | [diff] [blame] | 50 | return selectFirst<RecordDecl>("recordDecl", Results); |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Dmitri Gribenko | 282dc72 | 2019-08-22 11:32:57 +0000 | [diff] [blame] | 53 | /// Calculates the new order of fields. |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 54 | /// |
| 55 | /// \returns empty vector if the list of fields doesn't match the definition. |
| 56 | static SmallVector<unsigned, 4> |
Alexander Shaposhnikov | eaf833c | 2017-07-20 21:41:20 +0000 | [diff] [blame] | 57 | getNewFieldsOrder(const RecordDecl *Definition, |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 58 | ArrayRef<std::string> DesiredFieldsOrder) { |
| 59 | assert(Definition && "Definition is null"); |
| 60 | |
| 61 | llvm::StringMap<unsigned> NameToIndex; |
| 62 | for (const auto *Field : Definition->fields()) |
| 63 | NameToIndex[Field->getName()] = Field->getFieldIndex(); |
| 64 | |
| 65 | if (DesiredFieldsOrder.size() != NameToIndex.size()) { |
| 66 | llvm::errs() << "Number of provided fields doesn't match definition.\n"; |
| 67 | return {}; |
| 68 | } |
| 69 | SmallVector<unsigned, 4> NewFieldsOrder; |
| 70 | for (const auto &Name : DesiredFieldsOrder) { |
| 71 | if (!NameToIndex.count(Name)) { |
| 72 | llvm::errs() << "Field " << Name << " not found in definition.\n"; |
| 73 | return {}; |
| 74 | } |
| 75 | NewFieldsOrder.push_back(NameToIndex[Name]); |
| 76 | } |
| 77 | assert(NewFieldsOrder.size() == NameToIndex.size()); |
| 78 | return NewFieldsOrder; |
| 79 | } |
| 80 | |
| 81 | // FIXME: error-handling |
Dmitri Gribenko | 282dc72 | 2019-08-22 11:32:57 +0000 | [diff] [blame] | 82 | /// Replaces one range of source code by another. |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 83 | static void |
| 84 | addReplacement(SourceRange Old, SourceRange New, const ASTContext &Context, |
| 85 | std::map<std::string, tooling::Replacements> &Replacements) { |
| 86 | StringRef NewText = |
| 87 | Lexer::getSourceText(CharSourceRange::getTokenRange(New), |
| 88 | Context.getSourceManager(), Context.getLangOpts()); |
| 89 | tooling::Replacement R(Context.getSourceManager(), |
| 90 | CharSourceRange::getTokenRange(Old), NewText, |
| 91 | Context.getLangOpts()); |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 92 | consumeError(Replacements[std::string(R.getFilePath())].add(R)); |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Dmitri Gribenko | 282dc72 | 2019-08-22 11:32:57 +0000 | [diff] [blame] | 95 | /// Find all member fields used in the given init-list initializer expr |
Alexander Shaposhnikov | b687fdd | 2017-07-30 06:43:03 +0000 | [diff] [blame] | 96 | /// that belong to the same record |
| 97 | /// |
| 98 | /// \returns a set of field declarations, empty if none were present |
| 99 | static SmallSetVector<FieldDecl *, 1> |
| 100 | findMembersUsedInInitExpr(const CXXCtorInitializer *Initializer, |
| 101 | ASTContext &Context) { |
| 102 | SmallSetVector<FieldDecl *, 1> Results; |
| 103 | // Note that this does not pick up member fields of base classes since |
| 104 | // for those accesses Sema::PerformObjectMemberConversion always inserts an |
| 105 | // UncheckedDerivedToBase ImplicitCastExpr between the this expr and the |
| 106 | // object expression |
| 107 | auto FoundExprs = |
| 108 | match(findAll(memberExpr(hasObjectExpression(cxxThisExpr())).bind("ME")), |
| 109 | *Initializer->getInit(), Context); |
| 110 | for (BoundNodes &BN : FoundExprs) |
| 111 | if (auto *MemExpr = BN.getNodeAs<MemberExpr>("ME")) |
| 112 | if (auto *FD = dyn_cast<FieldDecl>(MemExpr->getMemberDecl())) |
| 113 | Results.insert(FD); |
| 114 | return Results; |
| 115 | } |
| 116 | |
Dmitri Gribenko | 282dc72 | 2019-08-22 11:32:57 +0000 | [diff] [blame] | 117 | /// Reorders fields in the definition of a struct/class. |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 118 | /// |
Kazuaki Ishizaki | dd5571d | 2020-04-05 15:28:11 +0900 | [diff] [blame] | 119 | /// At the moment reordering of fields with |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 120 | /// different accesses (public/protected/private) is not supported. |
| 121 | /// \returns true on success. |
| 122 | static bool reorderFieldsInDefinition( |
Alexander Shaposhnikov | eaf833c | 2017-07-20 21:41:20 +0000 | [diff] [blame] | 123 | const RecordDecl *Definition, ArrayRef<unsigned> NewFieldsOrder, |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 124 | const ASTContext &Context, |
| 125 | std::map<std::string, tooling::Replacements> &Replacements) { |
| 126 | assert(Definition && "Definition is null"); |
| 127 | |
| 128 | SmallVector<const FieldDecl *, 10> Fields; |
| 129 | for (const auto *Field : Definition->fields()) |
| 130 | Fields.push_back(Field); |
| 131 | |
| 132 | // Check that the permutation of the fields doesn't change the accesses |
| 133 | for (const auto *Field : Definition->fields()) { |
| 134 | const auto FieldIndex = Field->getFieldIndex(); |
| 135 | if (Field->getAccess() != Fields[NewFieldsOrder[FieldIndex]]->getAccess()) { |
Kazuaki Ishizaki | dd5571d | 2020-04-05 15:28:11 +0900 | [diff] [blame] | 136 | llvm::errs() << "Currently reordering of fields with different accesses " |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 137 | "is not supported\n"; |
| 138 | return false; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | for (const auto *Field : Definition->fields()) { |
| 143 | const auto FieldIndex = Field->getFieldIndex(); |
| 144 | if (FieldIndex == NewFieldsOrder[FieldIndex]) |
| 145 | continue; |
| 146 | addReplacement(Field->getSourceRange(), |
| 147 | Fields[NewFieldsOrder[FieldIndex]]->getSourceRange(), |
| 148 | Context, Replacements); |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
Dmitri Gribenko | 282dc72 | 2019-08-22 11:32:57 +0000 | [diff] [blame] | 153 | /// Reorders initializers in a C++ struct/class constructor. |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 154 | /// |
Alexander Shaposhnikov | b687fdd | 2017-07-30 06:43:03 +0000 | [diff] [blame] | 155 | /// A constructor can have initializers for an arbitrary subset of the class's |
| 156 | /// fields. Thus, we need to ensure that we reorder just the initializers that |
| 157 | /// are present. |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 158 | static void reorderFieldsInConstructor( |
| 159 | const CXXConstructorDecl *CtorDecl, ArrayRef<unsigned> NewFieldsOrder, |
Alexander Shaposhnikov | b687fdd | 2017-07-30 06:43:03 +0000 | [diff] [blame] | 160 | ASTContext &Context, |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 161 | std::map<std::string, tooling::Replacements> &Replacements) { |
| 162 | assert(CtorDecl && "Constructor declaration is null"); |
| 163 | if (CtorDecl->isImplicit() || CtorDecl->getNumCtorInitializers() <= 1) |
| 164 | return; |
| 165 | |
| 166 | // The method FunctionDecl::isThisDeclarationADefinition returns false |
| 167 | // for a defaulted function unless that function has been implicitly defined. |
| 168 | // Thus this assert needs to be after the previous checks. |
| 169 | assert(CtorDecl->isThisDeclarationADefinition() && "Not a definition"); |
| 170 | |
| 171 | SmallVector<unsigned, 10> NewFieldsPositions(NewFieldsOrder.size()); |
| 172 | for (unsigned i = 0, e = NewFieldsOrder.size(); i < e; ++i) |
| 173 | NewFieldsPositions[NewFieldsOrder[i]] = i; |
| 174 | |
| 175 | SmallVector<const CXXCtorInitializer *, 10> OldWrittenInitializersOrder; |
| 176 | SmallVector<const CXXCtorInitializer *, 10> NewWrittenInitializersOrder; |
| 177 | for (const auto *Initializer : CtorDecl->inits()) { |
Alexander Shaposhnikov | b687fdd | 2017-07-30 06:43:03 +0000 | [diff] [blame] | 178 | if (!Initializer->isMemberInitializer() || !Initializer->isWritten()) |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 179 | continue; |
Alexander Shaposhnikov | b687fdd | 2017-07-30 06:43:03 +0000 | [diff] [blame] | 180 | |
| 181 | // Warn if this reordering violates initialization expr dependencies. |
| 182 | const FieldDecl *ThisM = Initializer->getMember(); |
| 183 | const auto UsedMembers = findMembersUsedInInitExpr(Initializer, Context); |
| 184 | for (const FieldDecl *UM : UsedMembers) { |
| 185 | if (NewFieldsPositions[UM->getFieldIndex()] > |
| 186 | NewFieldsPositions[ThisM->getFieldIndex()]) { |
| 187 | DiagnosticsEngine &DiagEngine = Context.getDiagnostics(); |
| 188 | auto Description = ("reordering field " + UM->getName() + " after " + |
| 189 | ThisM->getName() + " makes " + UM->getName() + |
| 190 | " uninitialized when used in init expression") |
| 191 | .str(); |
| 192 | unsigned ID = DiagEngine.getDiagnosticIDs()->getCustomDiagID( |
| 193 | DiagnosticIDs::Warning, Description); |
| 194 | DiagEngine.Report(Initializer->getSourceLocation(), ID); |
| 195 | } |
| 196 | } |
| 197 | |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 198 | OldWrittenInitializersOrder.push_back(Initializer); |
| 199 | NewWrittenInitializersOrder.push_back(Initializer); |
| 200 | } |
| 201 | auto ByFieldNewPosition = [&](const CXXCtorInitializer *LHS, |
| 202 | const CXXCtorInitializer *RHS) { |
| 203 | assert(LHS && RHS); |
| 204 | return NewFieldsPositions[LHS->getMember()->getFieldIndex()] < |
| 205 | NewFieldsPositions[RHS->getMember()->getFieldIndex()]; |
| 206 | }; |
| 207 | std::sort(std::begin(NewWrittenInitializersOrder), |
| 208 | std::end(NewWrittenInitializersOrder), ByFieldNewPosition); |
| 209 | assert(OldWrittenInitializersOrder.size() == |
| 210 | NewWrittenInitializersOrder.size()); |
| 211 | for (unsigned i = 0, e = NewWrittenInitializersOrder.size(); i < e; ++i) |
| 212 | if (OldWrittenInitializersOrder[i] != NewWrittenInitializersOrder[i]) |
| 213 | addReplacement(OldWrittenInitializersOrder[i]->getSourceRange(), |
| 214 | NewWrittenInitializersOrder[i]->getSourceRange(), Context, |
| 215 | Replacements); |
| 216 | } |
| 217 | |
Dmitri Gribenko | 282dc72 | 2019-08-22 11:32:57 +0000 | [diff] [blame] | 218 | /// Reorders initializers in the brace initialization of an aggregate. |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 219 | /// |
| 220 | /// At the moment partial initialization is not supported. |
| 221 | /// \returns true on success |
| 222 | static bool reorderFieldsInInitListExpr( |
| 223 | const InitListExpr *InitListEx, ArrayRef<unsigned> NewFieldsOrder, |
| 224 | const ASTContext &Context, |
| 225 | std::map<std::string, tooling::Replacements> &Replacements) { |
| 226 | assert(InitListEx && "Init list expression is null"); |
Alexander Shaposhnikov | b687fdd | 2017-07-30 06:43:03 +0000 | [diff] [blame] | 227 | // We care only about InitListExprs which originate from source code. |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 228 | // Implicit InitListExprs are created by the semantic analyzer. |
| 229 | if (!InitListEx->isExplicit()) |
| 230 | return true; |
Alexander Shaposhnikov | b687fdd | 2017-07-30 06:43:03 +0000 | [diff] [blame] | 231 | // The method InitListExpr::getSyntacticForm may return nullptr indicating |
| 232 | // that the current initializer list also serves as its syntactic form. |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 233 | if (const auto *SyntacticForm = InitListEx->getSyntacticForm()) |
| 234 | InitListEx = SyntacticForm; |
| 235 | // If there are no initializers we do not need to change anything. |
| 236 | if (!InitListEx->getNumInits()) |
| 237 | return true; |
| 238 | if (InitListEx->getNumInits() != NewFieldsOrder.size()) { |
| 239 | llvm::errs() << "Currently only full initialization is supported\n"; |
| 240 | return false; |
| 241 | } |
| 242 | for (unsigned i = 0, e = InitListEx->getNumInits(); i < e; ++i) |
| 243 | if (i != NewFieldsOrder[i]) |
Alexander Shaposhnikov | b687fdd | 2017-07-30 06:43:03 +0000 | [diff] [blame] | 244 | addReplacement(InitListEx->getInit(i)->getSourceRange(), |
| 245 | InitListEx->getInit(NewFieldsOrder[i])->getSourceRange(), |
| 246 | Context, Replacements); |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 247 | return true; |
| 248 | } |
| 249 | |
| 250 | namespace { |
| 251 | class ReorderingConsumer : public ASTConsumer { |
| 252 | StringRef RecordName; |
| 253 | ArrayRef<std::string> DesiredFieldsOrder; |
| 254 | std::map<std::string, tooling::Replacements> &Replacements; |
| 255 | |
| 256 | public: |
| 257 | ReorderingConsumer(StringRef RecordName, |
| 258 | ArrayRef<std::string> DesiredFieldsOrder, |
| 259 | std::map<std::string, tooling::Replacements> &Replacements) |
| 260 | : RecordName(RecordName), DesiredFieldsOrder(DesiredFieldsOrder), |
| 261 | Replacements(Replacements) {} |
| 262 | |
| 263 | ReorderingConsumer(const ReorderingConsumer &) = delete; |
| 264 | ReorderingConsumer &operator=(const ReorderingConsumer &) = delete; |
| 265 | |
| 266 | void HandleTranslationUnit(ASTContext &Context) override { |
Alexander Shaposhnikov | eaf833c | 2017-07-20 21:41:20 +0000 | [diff] [blame] | 267 | const RecordDecl *RD = findDefinition(RecordName, Context); |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 268 | if (!RD) |
| 269 | return; |
| 270 | SmallVector<unsigned, 4> NewFieldsOrder = |
| 271 | getNewFieldsOrder(RD, DesiredFieldsOrder); |
| 272 | if (NewFieldsOrder.empty()) |
| 273 | return; |
| 274 | if (!reorderFieldsInDefinition(RD, NewFieldsOrder, Context, Replacements)) |
| 275 | return; |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 276 | |
Alexander Shaposhnikov | eaf833c | 2017-07-20 21:41:20 +0000 | [diff] [blame] | 277 | // CXXRD will be nullptr if C code (not C++) is being processed. |
| 278 | const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD); |
| 279 | if (CXXRD) |
| 280 | for (const auto *C : CXXRD->ctors()) |
| 281 | if (const auto *D = dyn_cast<CXXConstructorDecl>(C->getDefinition())) |
| 282 | reorderFieldsInConstructor(cast<const CXXConstructorDecl>(D), |
Alexander Shaposhnikov | b687fdd | 2017-07-30 06:43:03 +0000 | [diff] [blame] | 283 | NewFieldsOrder, Context, Replacements); |
Alexander Shaposhnikov | eaf833c | 2017-07-20 21:41:20 +0000 | [diff] [blame] | 284 | |
Alexander Shaposhnikov | b687fdd | 2017-07-30 06:43:03 +0000 | [diff] [blame] | 285 | // We only need to reorder init list expressions for |
Alexander Shaposhnikov | eaf833c | 2017-07-20 21:41:20 +0000 | [diff] [blame] | 286 | // plain C structs or C++ aggregate types. |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 287 | // For other types the order of constructor parameters is used, |
| 288 | // which we don't change at the moment. |
| 289 | // Now (v0) partial initialization is not supported. |
Alexander Shaposhnikov | eaf833c | 2017-07-20 21:41:20 +0000 | [diff] [blame] | 290 | if (!CXXRD || CXXRD->isAggregate()) |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 291 | for (auto Result : |
| 292 | match(initListExpr(hasType(equalsNode(RD))).bind("initListExpr"), |
| 293 | Context)) |
| 294 | if (!reorderFieldsInInitListExpr( |
| 295 | Result.getNodeAs<InitListExpr>("initListExpr"), NewFieldsOrder, |
| 296 | Context, Replacements)) { |
| 297 | Replacements.clear(); |
| 298 | return; |
| 299 | } |
| 300 | } |
| 301 | }; |
| 302 | } // end anonymous namespace |
| 303 | |
| 304 | std::unique_ptr<ASTConsumer> ReorderFieldsAction::newASTConsumer() { |
Jonas Devlieghere | 1c705d9 | 2019-08-14 23:52:23 +0000 | [diff] [blame] | 305 | return std::make_unique<ReorderingConsumer>(RecordName, DesiredFieldsOrder, |
Alexander Shaposhnikov | bf3c84c | 2016-09-02 02:56:07 +0000 | [diff] [blame] | 306 | Replacements); |
| 307 | } |
| 308 | |
| 309 | } // namespace reorder_fields |
| 310 | } // namespace clang |