clang-tools 22.0.0git
Classes | Functions
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 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]

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

References addReplacement().

◆ addReplacement() [2/3]

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

References addReplacement().

◆ addReplacement() [3/3]

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

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

◆ containsPreprocessorDirectives()

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

Definition at line 83 of file ReorderFieldsAction.cpp.

References Decl.

Referenced by isSafeToRewrite().

◆ declaresMultipleFieldsInMacro()

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

Definition at line 69 of file ReorderFieldsAction.cpp.

References Decl, and Field.

Referenced by isSafeToRewrite().

◆ declaresMultipleFieldsInStatement()

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

Definition at line 57 of file ReorderFieldsAction.cpp.

References Decl, and Field.

Referenced by isSafeToRewrite().

◆ 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 40 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 271 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 314 of file ReorderFieldsAction.cpp.

References Column, and Loc.

Referenced by getFullFieldSourceRange().

◆ getExplicitILE()

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

References isImplicitILE().

Referenced by reorderFieldsInInitListExpr().

◆ 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 333 of file ReorderFieldsAction.cpp.

References Field, getEndOfTrailingComment(), getStartOfLeadingComment(), 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 140 of file ReorderFieldsAction.cpp.

References Field, and Name.

◆ getStartOfLeadingComment()

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

References Column, Line, and Loc.

Referenced by getFullFieldSourceRange().

◆ isImplicitILE()

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

Referenced by getExplicitILE(), and reorderFieldsInInitListExpr().

◆ isSafeToRewrite()

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

◆ reorderFieldsInConstructor()

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

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

◆ reorderFieldsInDefinition()

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

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

◆ reorderFieldsInInitListExpr()

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

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

◆ reportError()

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

Definition at line 495 of file ReorderFieldsAction.cpp.

References Loc.

Referenced by reorderFieldsInInitListExpr().