clang-tools 22.0.0git
clang::reorder_fields Namespace Reference

Classes

class  Designator
 Represents a part of a designation in a C99/C++20 designated initializer. More...
class  Designators
 List of designators. More...
struct  ReorderedStruct
class  ReorderFieldsAction

Functions

static const RecordDecl * findDefinition (StringRef RecordName, ASTContext &Context)
 Finds the definition of a record by name.
static bool declaresMultipleFieldsInStatement (const RecordDecl *Decl)
static bool declaresMultipleFieldsInMacro (const RecordDecl *Decl, const SourceManager &SrcMgr)
static bool containsPreprocessorDirectives (const RecordDecl *Decl, const SourceManager &SrcMgr, const LangOptions &LangOpts)
static bool isSafeToRewrite (const RecordDecl *Decl, const ASTContext &Context)
static SmallVector< unsigned, 4 > getNewFieldsOrder (const RecordDecl *Definition, ArrayRef< std::string > DesiredFieldsOrder)
 Calculates the new order of fields.
static bool isOrderValid (const RecordDecl *RD, ArrayRef< unsigned > FieldOrder)
static void addReplacement (SourceRange Old, StringRef New, const ASTContext &Context, std::map< std::string, tooling::Replacements > &Replacements)
 Replaces a range of source code by the specified text.
static void addReplacement (SourceRange Old, SourceRange New, StringRef Prefix, const ASTContext &Context, std::map< std::string, tooling::Replacements > &Replacements)
 Replaces one range of source code by another and adds a prefix.
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 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 SourceLocation getStartOfLeadingComment (SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
 Returns the start of the leading comments before Loc.
static SourceLocation getEndOfTrailingComment (SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
 Returns the end of the trailing comments after Loc.
static SourceRange getFullFieldSourceRange (const FieldDecl &Field, const ASTContext &Context)
 Returns the full source range for the field declaration up to (including) the trailing semicolumn, including potential macro invocations, e.g.
static bool reorderFieldsInDefinition (const ReorderedStruct &RS, const ASTContext &Context, std::map< std::string, tooling::Replacements > &Replacements)
 Reorders fields in the definition of a struct/class.
static void reorderFieldsInConstructor (const CXXConstructorDecl *CtorDecl, const ReorderedStruct &RS, ASTContext &Context, std::map< std::string, tooling::Replacements > &Replacements)
 Reorders initializers in a C++ struct/class constructor.
static bool isImplicitILE (const InitListExpr *ILE, const ASTContext &Context)
 Replacement for broken InitListExpr::isExplicit function.
static const InitListExpr * getExplicitILE (const InitListExpr *ILE, ASTContext &Context)
 Finds the semantic form of the first explicit ancestor of the given initializer list including itself.
static void reportError (const Twine &Message, SourceLocation Loc, const SourceManager &SM)
static bool reorderFieldsInInitListExpr (const InitListExpr *InitListEx, const ReorderedStruct &RS, ASTContext &Context, std::map< std::string, tooling::Replacements > &Replacements)
 Reorders initializers in the brace initialization of an aggregate.

Function Documentation

◆ addReplacement() [1/3]

void clang::reorder_fields::addReplacement ( SourceRange Old,
SourceRange New,
const ASTContext & Context,
std::map< std::string, tooling::Replacements > & Replacements )
static

Replaces one range of source code by another.

Definition at line 270 of file ReorderFieldsAction.cpp.

References addReplacement().

◆ addReplacement() [2/3]

void clang::reorder_fields::addReplacement ( SourceRange Old,
SourceRange New,
StringRef Prefix,
const ASTContext & Context,
std::map< std::string, tooling::Replacements > & Replacements )
static

Replaces one range of source code by another and adds a prefix.

Definition at line 257 of file ReorderFieldsAction.cpp.

References addReplacement().

◆ addReplacement() [3/3]

void clang::reorder_fields::addReplacement ( SourceRange Old,
StringRef New,
const ASTContext & Context,
std::map< std::string, tooling::Replacements > & Replacements )
static

Replaces a range of source code by the specified text.

Definition at line 247 of file ReorderFieldsAction.cpp.

Referenced by addReplacement(), addReplacement(), reorderFieldsInConstructor(), reorderFieldsInDefinition(), and reorderFieldsInInitListExpr().

◆ containsPreprocessorDirectives()

bool clang::reorder_fields::containsPreprocessorDirectives ( const RecordDecl * Decl,
const SourceManager & SrcMgr,
const LangOptions & LangOpts )
static

Definition at line 83 of file ReorderFieldsAction.cpp.

Referenced by isSafeToRewrite().

◆ declaresMultipleFieldsInMacro()

bool clang::reorder_fields::declaresMultipleFieldsInMacro ( const RecordDecl * Decl,
const SourceManager & SrcMgr )
static

Definition at line 69 of file ReorderFieldsAction.cpp.

Referenced by isSafeToRewrite().

◆ declaresMultipleFieldsInStatement()

bool clang::reorder_fields::declaresMultipleFieldsInStatement ( const RecordDecl * Decl)
static

Definition at line 57 of file ReorderFieldsAction.cpp.

Referenced by isSafeToRewrite().

◆ findDefinition()

const RecordDecl * clang::reorder_fields::findDefinition ( StringRef RecordName,
ASTContext & Context )
static

Finds the definition of a record by name.

Returns
nullptr if the name is ambiguous or not found.

Definition at line 40 of file ReorderFieldsAction.cpp.

References RecordName().

◆ findMembersUsedInInitExpr()

SmallSetVector< FieldDecl *, 1 > clang::reorder_fields::findMembersUsedInInitExpr ( const CXXCtorInitializer * Initializer,
ASTContext & Context )
static

Find all member fields used in the given init-list initializer expr that belong to the same record.

Returns
a set of field declarations, empty if none were present

Definition at line 287 of file ReorderFieldsAction.cpp.

Referenced by reorderFieldsInConstructor().

◆ getEndOfTrailingComment()

SourceLocation clang::reorder_fields::getEndOfTrailingComment ( SourceLocation Loc,
const SourceManager & SM,
const LangOptions & LangOpts )
static

Returns the end of the trailing comments after Loc.

Definition at line 330 of file ReorderFieldsAction.cpp.

Referenced by getFullFieldSourceRange().

◆ getExplicitILE()

const InitListExpr * clang::reorder_fields::getExplicitILE ( const InitListExpr * ILE,
ASTContext & Context )
static

Finds the semantic form of the first explicit ancestor of the given initializer list including itself.

Definition at line 493 of file ReorderFieldsAction.cpp.

References isImplicitILE().

Referenced by reorderFieldsInInitListExpr().

◆ getFullFieldSourceRange()

SourceRange clang::reorder_fields::getFullFieldSourceRange ( const FieldDecl & Field,
const ASTContext & Context )
static

Returns the full source range for the field declaration up to (including) the trailing semicolumn, including potential macro invocations, e.g.

int a GUARDED_BY(mu);. If there is a trailing comment, include it.

Definition at line 349 of file ReorderFieldsAction.cpp.

References getEndOfTrailingComment(), and getStartOfLeadingComment().

Referenced by reorderFieldsInDefinition().

◆ getNewFieldsOrder()

SmallVector< unsigned, 4 > clang::reorder_fields::getNewFieldsOrder ( const RecordDecl * Definition,
ArrayRef< std::string > DesiredFieldsOrder )
static

Calculates the new order of fields.

Returns
empty vector if the list of fields doesn't match the definition.

Definition at line 140 of file ReorderFieldsAction.cpp.

◆ getStartOfLeadingComment()

SourceLocation clang::reorder_fields::getStartOfLeadingComment ( SourceLocation Loc,
const SourceManager & SM,
const LangOptions & LangOpts )
static

Returns the start of the leading comments before Loc.

Definition at line 307 of file ReorderFieldsAction.cpp.

Referenced by getFullFieldSourceRange().

◆ isImplicitILE()

bool clang::reorder_fields::isImplicitILE ( const InitListExpr * ILE,
const ASTContext & Context )
static

Replacement for broken InitListExpr::isExplicit function.

FIXME: Remove when InitListExpr::isExplicit is fixed.

Definition at line 474 of file ReorderFieldsAction.cpp.

Referenced by getExplicitILE().

◆ isOrderValid()

bool clang::reorder_fields::isOrderValid ( const RecordDecl * RD,
ArrayRef< unsigned > FieldOrder )
static

Definition at line 167 of file ReorderFieldsAction.cpp.

◆ isSafeToRewrite()

bool clang::reorder_fields::isSafeToRewrite ( const RecordDecl * Decl,
const ASTContext & Context )
static

◆ reorderFieldsInConstructor()

void clang::reorder_fields::reorderFieldsInConstructor ( const CXXConstructorDecl * CtorDecl,
const ReorderedStruct & RS,
ASTContext & Context,
std::map< std::string, tooling::Replacements > & Replacements )
static

Reorders initializers in a C++ struct/class constructor.

A constructor can have initializers for an arbitrary subset of the class's fields. Thus, we need to ensure that we reorder just the initializers that are present.

Definition at line 417 of file ReorderFieldsAction.cpp.

References addReplacement(), findMembersUsedInInitExpr(), and clang::reorder_fields::ReorderedStruct::NewFieldsPositions.

◆ reorderFieldsInDefinition()

bool clang::reorder_fields::reorderFieldsInDefinition ( const ReorderedStruct & RS,
const ASTContext & Context,
std::map< std::string, tooling::Replacements > & Replacements )
static

Reorders fields in the definition of a struct/class.

At the moment reordering of fields with different accesses (public/protected/private) is not supported.

Returns
true on success.

Definition at line 380 of file ReorderFieldsAction.cpp.

References addReplacement(), clang::reorder_fields::ReorderedStruct::Definition, getFullFieldSourceRange(), and clang::reorder_fields::ReorderedStruct::NewFieldsOrder.

◆ reorderFieldsInInitListExpr()

bool clang::reorder_fields::reorderFieldsInInitListExpr ( const InitListExpr * InitListEx,
const ReorderedStruct & RS,
ASTContext & Context,
std::map< std::string, tooling::Replacements > & Replacements )
static

Reorders initializers in the brace initialization of an aggregate.

At the moment partial initialization is not supported.

Returns
true on success

Definition at line 524 of file ReorderFieldsAction.cpp.

References addReplacement(), getExplicitILE(), clang::reorder_fields::ReorderedStruct::NewFieldsOrder, and reportError().

◆ reportError()

void clang::reorder_fields::reportError ( const Twine & Message,
SourceLocation Loc,
const SourceManager & SM )
static

Definition at line 511 of file ReorderFieldsAction.cpp.

Referenced by reorderFieldsInInitListExpr().