10#include "../utils/DesignatedInitializers.h"
11#include "clang/AST/APValue.h"
12#include "clang/AST/Decl.h"
13#include "clang/AST/Expr.h"
14#include "clang/AST/Stmt.h"
15#include "clang/ASTMatchers/ASTMatchFinder.h"
16#include "clang/ASTMatchers/ASTMatchers.h"
17#include "clang/ASTMatchers/ASTMatchersMacros.h"
18#include "clang/Basic/Diagnostic.h"
19#include "clang/Lex/Lexer.h"
26 "IgnoreSingleElementAggregates";
39 Designators(
const InitListExpr *InitList) : InitList(InitList) {
40 assert(InitList->isSyntacticForm());
43 unsigned size() {
return getCached().size(); }
45 std::optional<llvm::StringRef> operator[](
const SourceLocation &
Location) {
46 const auto &Designators = getCached();
47 const auto Result = Designators.find(
Location);
48 if (Result == Designators.end())
50 const llvm::StringRef Designator = Result->getSecond();
51 return (Designator.front() ==
'.' ? Designator.substr(1) : Designator)
57 using LocationToNameMap = llvm::DenseMap<clang::SourceLocation, std::string>;
59 std::optional<LocationToNameMap> CachedDesignators;
60 const InitListExpr *InitList;
62 LocationToNameMap &getCached() {
63 return CachedDesignators ? *CachedDesignators
64 : CachedDesignators.emplace(
69unsigned getNumberOfDesignated(
const InitListExpr *SyntacticInitList) {
70 return llvm::count_if(*SyntacticInitList, [](
auto *InitExpr) {
71 return isa<DesignatedInitExpr>(InitExpr);
80 if (
const InitListExpr *SyntacticForm =
81 Node.isSyntacticForm() ? &
Node :
Node.getSyntacticForm())
82 return getNumberOfDesignated(SyntacticForm) == SyntacticForm->getNumInits();
87 return Node.getNumInits() > 1;
103 const auto HasBaseWithFields =
104 hasAnyBase(hasType(cxxRecordDecl(has(fieldDecl()))));
107 hasType(cxxRecordDecl(RestrictToPODTypes ? isPOD() : isAggregate(),
108 unless(HasBaseWithFields))
110 IgnoreSingleElementAggregates ? hasMoreThanOneElement() : anything(),
111 unless(isFullyDesignated()))
117 const MatchFinder::MatchResult &Result) {
118 const auto *InitList = Result.Nodes.getNodeAs<InitListExpr>(
"init");
119 const auto *
Type = Result.Nodes.getNodeAs<CXXRecordDecl>(
"type");
120 if (!
Type || !InitList)
122 const auto *SyntacticInitList = InitList->getSyntacticForm();
123 if (!SyntacticInitList)
125 Designators Designators{SyntacticInitList};
126 const unsigned NumberOfDesignated = getNumberOfDesignated(SyntacticInitList);
127 if (SyntacticInitList->getNumInits() - NumberOfDesignated >
133 if (0 == NumberOfDesignated) {
134 if (IgnoreMacros && InitList->getBeginLoc().isMacroID())
137 DiagnosticBuilder Diag =
138 diag(InitList->getLBraceLoc(),
139 "use designated initializer list to initialize %0");
140 Diag <<
Type << InitList->getSourceRange();
141 for (
const Stmt *InitExpr : *SyntacticInitList) {
142 const auto Designator = Designators[InitExpr->getBeginLoc()];
143 if (Designator && !Designator->empty())
144 Diag << FixItHint::CreateInsertion(InitExpr->getBeginLoc(),
145 (
"." + *Designator +
"=").str());
148 diag(
Type->getBeginLoc(),
"aggregate type is defined here",
149 DiagnosticIDs::Note);
155 for (
const auto *InitExpr : *SyntacticInitList) {
156 if (isa<DesignatedInitExpr>(InitExpr))
158 if (IgnoreMacros && InitExpr->getBeginLoc().isMacroID())
160 const auto Designator = Designators[InitExpr->getBeginLoc()];
161 if (!Designator || Designator->empty()) {
164 diag(InitExpr->getBeginLoc(),
"use designated init expression")
165 << InitExpr->getSourceRange();
167 diag(InitExpr->getBeginLoc(),
168 "use designated init expression to initialize field '%0'")
169 << InitExpr->getSourceRange() << *Designator
170 << FixItHint::CreateInsertion(InitExpr->getBeginLoc(),
171 (
"." + *Designator +
"=").str());
179 IgnoreSingleElementAggregates);
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
UseDesignatedInitializersCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
AST_MATCHER(Decl, declHasNoReturnAttr)
matches a Decl if it has a "no return" attribute of any kind
static constexpr char IgnoreMacrosName[]
static constexpr bool RestrictToPODTypesDefault
static constexpr char IgnoreSingleElementAggregatesName[]
static constexpr bool IgnoreMacrosDefault
static constexpr bool IgnoreSingleElementAggregatesDefault
static constexpr char RestrictToPODTypesName[]
llvm::DenseMap< SourceLocation, std::string > getUnwrittenDesignators(const InitListExpr *Syn)
llvm::StringMap< ClangTidyValue > OptionMap