clang-tools 20.0.0git
Classes | Functions
clang::reorder_fields Namespace Reference

Classes

class  ReorderFieldsAction
 

Functions

static const RecordDecl * findDefinition (StringRef RecordName, ASTContext &Context)
 Finds the definition of a record by name.
 
static SmallVector< unsigned, 4 > getNewFieldsOrder (const RecordDecl *Definition, ArrayRef< std::string > DesiredFieldsOrder)
 Calculates the new order of fields.
 
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 std::optional< Token > getTokenAfter (SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
 Returns the next token after Loc (including comment tokens).
 
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 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 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 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.
 

Function Documentation

◆ addReplacement()

static 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 86 of file ReorderFieldsAction.cpp.

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

◆ findDefinition()

static 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 36 of file ReorderFieldsAction.cpp.

References Results.

◆ findMembersUsedInInitExpr()

static 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 102 of file ReorderFieldsAction.cpp.

References Results.

Referenced by reorderFieldsInConstructor().

◆ getEndOfTrailingComment()

static 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 151 of file ReorderFieldsAction.cpp.

References Column, getTokenAfter(), and Loc.

Referenced by getFullFieldSourceRange().

◆ getFullFieldSourceRange()

static 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 169 of file ReorderFieldsAction.cpp.

References Field, getEndOfTrailingComment(), and Range.

Referenced by reorderFieldsInDefinition().

◆ getNewFieldsOrder()

static 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 57 of file ReorderFieldsAction.cpp.

References Field, and Name.

◆ getTokenAfter()

static std::optional< Token > clang::reorder_fields::getTokenAfter ( SourceLocation  Loc,
const SourceManager &  SM,
const LangOptions &  LangOpts 
)
static

Returns the next token after Loc (including comment tokens).

Definition at line 122 of file ReorderFieldsAction.cpp.

References Loc.

Referenced by getEndOfTrailingComment().

◆ reorderFieldsInConstructor()

static void clang::reorder_fields::reorderFieldsInConstructor ( const CXXConstructorDecl *  CtorDecl,
ArrayRef< unsigned >  NewFieldsOrder,
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 236 of file ReorderFieldsAction.cpp.

References addReplacement(), findMembersUsedInInitExpr(), and ID.

◆ reorderFieldsInDefinition()

static bool clang::reorder_fields::reorderFieldsInDefinition ( const RecordDecl *  Definition,
ArrayRef< unsigned >  NewFieldsOrder,
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 199 of file ReorderFieldsAction.cpp.

References addReplacement(), Field, and getFullFieldSourceRange().

◆ reorderFieldsInInitListExpr()

static bool clang::reorder_fields::reorderFieldsInInitListExpr ( const InitListExpr *  InitListEx,
ArrayRef< unsigned >  NewFieldsOrder,
const 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 299 of file ReorderFieldsAction.cpp.

References addReplacement().