clang-tools 20.0.0git
ReorderFieldsAction.cpp
Go to the documentation of this file.
1//===-- tools/extra/clang-reorder-fields/ReorderFieldsAction.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 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"
24#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/SetVector.h"
26#include <string>
27
28namespace clang {
29namespace reorder_fields {
30using namespace clang::ast_matchers;
31using llvm::SmallSetVector;
32
33/// Finds the definition of a record by name.
34///
35/// \returns nullptr if the name is ambiguous or not found.
36static const RecordDecl *findDefinition(StringRef RecordName,
37 ASTContext &Context) {
38 auto Results =
39 match(recordDecl(hasName(RecordName), isDefinition()).bind("recordDecl"),
40 Context);
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 }
50 return selectFirst<RecordDecl>("recordDecl", Results);
51}
52
53/// Calculates the new order of fields.
54///
55/// \returns empty vector if the list of fields doesn't match the definition.
56static SmallVector<unsigned, 4>
57getNewFieldsOrder(const RecordDecl *Definition,
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 (" << DesiredFieldsOrder.size()
67 << ") doesn't match definition (" << NameToIndex.size()
68 << ").\n";
69 return {};
70 }
71 SmallVector<unsigned, 4> NewFieldsOrder;
72 for (const auto &Name : DesiredFieldsOrder) {
73 if (!NameToIndex.count(Name)) {
74 llvm::errs() << "Field " << Name << " not found in definition.\n";
75 return {};
76 }
77 NewFieldsOrder.push_back(NameToIndex[Name]);
78 }
79 assert(NewFieldsOrder.size() == NameToIndex.size());
80 return NewFieldsOrder;
81}
82
83// FIXME: error-handling
84/// Replaces one range of source code by another.
85static void
86addReplacement(SourceRange Old, SourceRange New, const ASTContext &Context,
87 std::map<std::string, tooling::Replacements> &Replacements) {
88 StringRef NewText =
89 Lexer::getSourceText(CharSourceRange::getTokenRange(New),
90 Context.getSourceManager(), Context.getLangOpts());
91 tooling::Replacement R(Context.getSourceManager(),
92 CharSourceRange::getTokenRange(Old), NewText,
93 Context.getLangOpts());
94 consumeError(Replacements[std::string(R.getFilePath())].add(R));
95}
96
97/// Find all member fields used in the given init-list initializer expr
98/// that belong to the same record
99///
100/// \returns a set of field declarations, empty if none were present
101static SmallSetVector<FieldDecl *, 1>
102findMembersUsedInInitExpr(const CXXCtorInitializer *Initializer,
103 ASTContext &Context) {
104 SmallSetVector<FieldDecl *, 1> Results;
105 // Note that this does not pick up member fields of base classes since
106 // for those accesses Sema::PerformObjectMemberConversion always inserts an
107 // UncheckedDerivedToBase ImplicitCastExpr between the this expr and the
108 // object expression
109 auto FoundExprs = match(
110 traverse(
111 TK_AsIs,
112 findAll(memberExpr(hasObjectExpression(cxxThisExpr())).bind("ME"))),
113 *Initializer->getInit(), Context);
114 for (BoundNodes &BN : FoundExprs)
115 if (auto *MemExpr = BN.getNodeAs<MemberExpr>("ME"))
116 if (auto *FD = dyn_cast<FieldDecl>(MemExpr->getMemberDecl()))
117 Results.insert(FD);
118 return Results;
119}
120
121/// Returns the next token after `Loc` (including comment tokens).
122static std::optional<Token> getTokenAfter(SourceLocation Loc,
123 const SourceManager &SM,
124 const LangOptions &LangOpts) {
125 if (Loc.isMacroID()) {
126 return std::nullopt;
127 }
128 Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, LangOpts);
129
130 // Break down the source location.
131 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
132
133 // Try to load the file buffer.
134 bool InvalidTemp = false;
135 StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp);
136 if (InvalidTemp)
137 return std::nullopt;
138
139 const char *TokenBegin = File.data() + LocInfo.second;
140
141 Lexer lexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(),
142 TokenBegin, File.end());
143 lexer.SetCommentRetentionState(true);
144 // Find the token.
145 Token Tok;
146 lexer.LexFromRawLexer(Tok);
147 return Tok;
148}
149
150/// Returns the end of the trailing comments after `Loc`.
151static SourceLocation getEndOfTrailingComment(SourceLocation Loc,
152 const SourceManager &SM,
153 const LangOptions &LangOpts) {
154 // We consider any following comment token that is indented more than the
155 // first comment to be part of the trailing comment.
156 const unsigned Column = SM.getPresumedColumnNumber(Loc);
157 std::optional<Token> Tok = getTokenAfter(Loc, SM, LangOpts);
158 while (Tok && Tok->is(tok::comment) &&
159 SM.getPresumedColumnNumber(Tok->getLocation()) > Column) {
160 Loc = Tok->getEndLoc();
161 Tok = getTokenAfter(Loc, SM, LangOpts);
162 }
163 return Loc;
164}
165
166/// Returns the full source range for the field declaration up to (including)
167/// the trailing semicolumn, including potential macro invocations,
168/// e.g. `int a GUARDED_BY(mu);`. If there is a trailing comment, include it.
169static SourceRange getFullFieldSourceRange(const FieldDecl &Field,
170 const ASTContext &Context) {
171 const SourceRange Range = Field.getSourceRange();
172 SourceLocation Begin = Range.getBegin();
173 SourceLocation End = Range.getEnd();
174 const SourceManager &SM = Context.getSourceManager();
175 const LangOptions &LangOpts = Context.getLangOpts();
176 while (true) {
177 std::optional<Token> CurrentToken = Lexer::findNextToken(End, SM, LangOpts);
178
179 if (!CurrentToken)
180 return SourceRange(Begin, End);
181
182 if (CurrentToken->is(tok::eof))
183 return Range; // Something is wrong, return the original range.
184
185 End = CurrentToken->getLastLoc();
186
187 if (CurrentToken->is(tok::semi))
188 break;
189 }
190 End = getEndOfTrailingComment(End, SM, LangOpts);
191 return SourceRange(Begin, End);
192}
193
194/// Reorders fields in the definition of a struct/class.
195///
196/// At the moment reordering of fields with
197/// different accesses (public/protected/private) is not supported.
198/// \returns true on success.
200 const RecordDecl *Definition, ArrayRef<unsigned> NewFieldsOrder,
201 const ASTContext &Context,
202 std::map<std::string, tooling::Replacements> &Replacements) {
203 assert(Definition && "Definition is null");
204
205 SmallVector<const FieldDecl *, 10> Fields;
206 for (const auto *Field : Definition->fields())
207 Fields.push_back(Field);
208
209 // Check that the permutation of the fields doesn't change the accesses
210 for (const auto *Field : Definition->fields()) {
211 const auto FieldIndex = Field->getFieldIndex();
212 if (Field->getAccess() != Fields[NewFieldsOrder[FieldIndex]]->getAccess()) {
213 llvm::errs() << "Currently reordering of fields with different accesses "
214 "is not supported\n";
215 return false;
216 }
217 }
218
219 for (const auto *Field : Definition->fields()) {
220 const auto FieldIndex = Field->getFieldIndex();
221 if (FieldIndex == NewFieldsOrder[FieldIndex])
222 continue;
225 getFullFieldSourceRange(*Fields[NewFieldsOrder[FieldIndex]], Context),
226 Context, Replacements);
227 }
228 return true;
229}
230
231/// Reorders initializers in a C++ struct/class constructor.
232///
233/// A constructor can have initializers for an arbitrary subset of the class's
234/// fields. Thus, we need to ensure that we reorder just the initializers that
235/// are present.
237 const CXXConstructorDecl *CtorDecl, ArrayRef<unsigned> NewFieldsOrder,
238 ASTContext &Context,
239 std::map<std::string, tooling::Replacements> &Replacements) {
240 assert(CtorDecl && "Constructor declaration is null");
241 if (CtorDecl->isImplicit() || CtorDecl->getNumCtorInitializers() <= 1)
242 return;
243
244 // The method FunctionDecl::isThisDeclarationADefinition returns false
245 // for a defaulted function unless that function has been implicitly defined.
246 // Thus this assert needs to be after the previous checks.
247 assert(CtorDecl->isThisDeclarationADefinition() && "Not a definition");
248
249 SmallVector<unsigned, 10> NewFieldsPositions(NewFieldsOrder.size());
250 for (unsigned i = 0, e = NewFieldsOrder.size(); i < e; ++i)
251 NewFieldsPositions[NewFieldsOrder[i]] = i;
252
253 SmallVector<const CXXCtorInitializer *, 10> OldWrittenInitializersOrder;
254 SmallVector<const CXXCtorInitializer *, 10> NewWrittenInitializersOrder;
255 for (const auto *Initializer : CtorDecl->inits()) {
256 if (!Initializer->isMemberInitializer() || !Initializer->isWritten())
257 continue;
258
259 // Warn if this reordering violates initialization expr dependencies.
260 const FieldDecl *ThisM = Initializer->getMember();
261 const auto UsedMembers = findMembersUsedInInitExpr(Initializer, Context);
262 for (const FieldDecl *UM : UsedMembers) {
263 if (NewFieldsPositions[UM->getFieldIndex()] >
264 NewFieldsPositions[ThisM->getFieldIndex()]) {
265 DiagnosticsEngine &DiagEngine = Context.getDiagnostics();
266 auto Description = ("reordering field " + UM->getName() + " after " +
267 ThisM->getName() + " makes " + UM->getName() +
268 " uninitialized when used in init expression")
269 .str();
270 unsigned ID = DiagEngine.getDiagnosticIDs()->getCustomDiagID(
271 DiagnosticIDs::Warning, Description);
272 DiagEngine.Report(Initializer->getSourceLocation(), ID);
273 }
274 }
275
276 OldWrittenInitializersOrder.push_back(Initializer);
277 NewWrittenInitializersOrder.push_back(Initializer);
278 }
279 auto ByFieldNewPosition = [&](const CXXCtorInitializer *LHS,
280 const CXXCtorInitializer *RHS) {
281 assert(LHS && RHS);
282 return NewFieldsPositions[LHS->getMember()->getFieldIndex()] <
283 NewFieldsPositions[RHS->getMember()->getFieldIndex()];
284 };
285 llvm::sort(NewWrittenInitializersOrder, ByFieldNewPosition);
286 assert(OldWrittenInitializersOrder.size() ==
287 NewWrittenInitializersOrder.size());
288 for (unsigned i = 0, e = NewWrittenInitializersOrder.size(); i < e; ++i)
289 if (OldWrittenInitializersOrder[i] != NewWrittenInitializersOrder[i])
290 addReplacement(OldWrittenInitializersOrder[i]->getSourceRange(),
291 NewWrittenInitializersOrder[i]->getSourceRange(), Context,
292 Replacements);
293}
294
295/// Reorders initializers in the brace initialization of an aggregate.
296///
297/// At the moment partial initialization is not supported.
298/// \returns true on success
300 const InitListExpr *InitListEx, ArrayRef<unsigned> NewFieldsOrder,
301 const ASTContext &Context,
302 std::map<std::string, tooling::Replacements> &Replacements) {
303 assert(InitListEx && "Init list expression is null");
304 // We care only about InitListExprs which originate from source code.
305 // Implicit InitListExprs are created by the semantic analyzer.
306 if (!InitListEx->isExplicit())
307 return true;
308 // The method InitListExpr::getSyntacticForm may return nullptr indicating
309 // that the current initializer list also serves as its syntactic form.
310 if (const auto *SyntacticForm = InitListEx->getSyntacticForm())
311 InitListEx = SyntacticForm;
312 // If there are no initializers we do not need to change anything.
313 if (!InitListEx->getNumInits())
314 return true;
315 if (InitListEx->getNumInits() != NewFieldsOrder.size()) {
316 llvm::errs() << "Currently only full initialization is supported\n";
317 return false;
318 }
319 for (unsigned i = 0, e = InitListEx->getNumInits(); i < e; ++i)
320 if (i != NewFieldsOrder[i])
321 addReplacement(InitListEx->getInit(i)->getSourceRange(),
322 InitListEx->getInit(NewFieldsOrder[i])->getSourceRange(),
323 Context, Replacements);
324 return true;
325}
326
327namespace {
328class ReorderingConsumer : public ASTConsumer {
329 StringRef RecordName;
330 ArrayRef<std::string> DesiredFieldsOrder;
331 std::map<std::string, tooling::Replacements> &Replacements;
332
333public:
334 ReorderingConsumer(StringRef RecordName,
335 ArrayRef<std::string> DesiredFieldsOrder,
336 std::map<std::string, tooling::Replacements> &Replacements)
337 : RecordName(RecordName), DesiredFieldsOrder(DesiredFieldsOrder),
338 Replacements(Replacements) {}
339
340 ReorderingConsumer(const ReorderingConsumer &) = delete;
341 ReorderingConsumer &operator=(const ReorderingConsumer &) = delete;
342
343 void HandleTranslationUnit(ASTContext &Context) override {
344 const RecordDecl *RD = findDefinition(RecordName, Context);
345 if (!RD)
346 return;
347 SmallVector<unsigned, 4> NewFieldsOrder =
348 getNewFieldsOrder(RD, DesiredFieldsOrder);
349 if (NewFieldsOrder.empty())
350 return;
351 if (!reorderFieldsInDefinition(RD, NewFieldsOrder, Context, Replacements))
352 return;
353
354 // CXXRD will be nullptr if C code (not C++) is being processed.
355 const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD);
356 if (CXXRD)
357 for (const auto *C : CXXRD->ctors())
358 if (const auto *D = dyn_cast<CXXConstructorDecl>(C->getDefinition()))
359 reorderFieldsInConstructor(cast<const CXXConstructorDecl>(D),
360 NewFieldsOrder, Context, Replacements);
361
362 // We only need to reorder init list expressions for
363 // plain C structs or C++ aggregate types.
364 // For other types the order of constructor parameters is used,
365 // which we don't change at the moment.
366 // Now (v0) partial initialization is not supported.
367 if (!CXXRD || CXXRD->isAggregate())
368 for (auto Result :
369 match(initListExpr(hasType(equalsNode(RD))).bind("initListExpr"),
370 Context))
372 Result.getNodeAs<InitListExpr>("initListExpr"), NewFieldsOrder,
373 Context, Replacements)) {
374 Replacements.clear();
375 return;
376 }
377 }
378};
379} // end anonymous namespace
380
381std::unique_ptr<ASTConsumer> ReorderFieldsAction::newASTConsumer() {
382 return std::make_unique<ReorderingConsumer>(RecordName, DesiredFieldsOrder,
383 Replacements);
384}
385
386} // namespace reorder_fields
387} // namespace clang
llvm::SmallString< 256U > Name
std::vector< CodeCompletionResult > Results
const Criteria C
CharSourceRange Range
SourceRange for the file name.
SourceLocation Loc
const FieldDecl * Field
int Column
This file contains the declarations of the ReorderFieldsAction class and the FieldPosition struct.
std::unique_ptr< ASTConsumer > newASTConsumer()
static void reorderFieldsInConstructor(const CXXConstructorDecl *CtorDecl, ArrayRef< unsigned > NewFieldsOrder, ASTContext &Context, std::map< std::string, tooling::Replacements > &Replacements)
Reorders initializers in a C++ struct/class constructor.
static const RecordDecl * findDefinition(StringRef RecordName, ASTContext &Context)
Finds the definition of a record by name.
static bool reorderFieldsInInitListExpr(const InitListExpr *InitListEx, ArrayRef< unsigned > NewFieldsOrder, const ASTContext &Context, std::map< std::string, tooling::Replacements > &Replacements)
Reorders initializers in the brace initialization of an aggregate.
static void addReplacement(SourceRange Old, SourceRange New, const ASTContext &Context, std::map< std::string, tooling::Replacements > &Replacements)
Replaces one range of source code by another.
static SourceRange getFullFieldSourceRange(const FieldDecl &Field, const ASTContext &Context)
Returns the full source range for the field declaration up to (including) the trailing semicolumn,...
static SourceLocation getEndOfTrailingComment(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Returns the end of the trailing comments after Loc.
static SmallSetVector< FieldDecl *, 1 > findMembersUsedInInitExpr(const CXXCtorInitializer *Initializer, ASTContext &Context)
Find all member fields used in the given init-list initializer expr that belong to the same record.
static bool reorderFieldsInDefinition(const RecordDecl *Definition, ArrayRef< unsigned > NewFieldsOrder, const ASTContext &Context, std::map< std::string, tooling::Replacements > &Replacements)
Reorders fields in the definition of a struct/class.
static std::optional< Token > getTokenAfter(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Returns the next token after Loc (including comment tokens).
static SmallVector< unsigned, 4 > getNewFieldsOrder(const RecordDecl *Definition, ArrayRef< std::string > DesiredFieldsOrder)
Calculates the new order of fields.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//