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";
36 "StrictCStandardCompliance";
40 "StrictCppStandardCompliance";
47 Designators(
const InitListExpr *InitList) : InitList(InitList) {
48 assert(InitList->isSyntacticForm());
51 unsigned size() {
return getCached().size(); }
53 std::optional<llvm::StringRef> operator[](
const SourceLocation &
Location) {
54 const auto &Designators = getCached();
55 const auto Result = Designators.find(
Location);
56 if (Result == Designators.end())
58 const llvm::StringRef Designator = Result->getSecond();
59 return (Designator.front() ==
'.' ? Designator.substr(1) : Designator)
65 using LocationToNameMap = llvm::DenseMap<clang::SourceLocation, std::string>;
67 std::optional<LocationToNameMap> CachedDesignators;
68 const InitListExpr *InitList;
70 LocationToNameMap &getCached() {
71 return CachedDesignators ? *CachedDesignators
72 : CachedDesignators.emplace(
77unsigned getNumberOfDesignated(
const InitListExpr *SyntacticInitList) {
78 return llvm::count_if(*SyntacticInitList, [](
auto *InitExpr) {
79 return isa<DesignatedInitExpr>(InitExpr);
84 return Node.hasDefinition() &&
Node.isAggregate();
88 return Node.hasDefinition() &&
Node.isPOD();
92 if (
const InitListExpr *SyntacticForm =
93 Node.isSyntacticForm() ? &
Node :
Node.getSyntacticForm())
94 return getNumberOfDesignated(SyntacticForm) == SyntacticForm->getNumInits();
99 return Node.getNumInits() > 1;
115 StrictCppStandardCompliance(
120 const auto HasBaseWithFields =
121 hasAnyBase(hasType(cxxRecordDecl(has(fieldDecl()))));
124 hasType(cxxRecordDecl(RestrictToPODTypes ? isPOD() : isAggregate(),
125 unless(HasBaseWithFields))
127 IgnoreSingleElementAggregates ? hasMoreThanOneElement() : anything(),
128 unless(isFullyDesignated()))
134 const MatchFinder::MatchResult &Result) {
135 const auto *InitList = Result.Nodes.getNodeAs<InitListExpr>(
"init");
136 const auto *
Type = Result.Nodes.getNodeAs<CXXRecordDecl>(
"type");
137 if (!
Type || !InitList)
139 const auto *SyntacticInitList = InitList->getSyntacticForm();
140 if (!SyntacticInitList)
142 Designators Designators{SyntacticInitList};
143 const unsigned NumberOfDesignated = getNumberOfDesignated(SyntacticInitList);
144 if (SyntacticInitList->getNumInits() - NumberOfDesignated >
150 if (0 == NumberOfDesignated) {
151 if (IgnoreMacros && InitList->getBeginLoc().isMacroID())
154 DiagnosticBuilder Diag =
155 diag(InitList->getLBraceLoc(),
156 "use designated initializer list to initialize %0");
157 Diag <<
Type << InitList->getSourceRange();
158 for (
const Stmt *InitExpr : *SyntacticInitList) {
159 const auto Designator = Designators[InitExpr->getBeginLoc()];
160 if (Designator && !Designator->empty())
161 Diag << FixItHint::CreateInsertion(InitExpr->getBeginLoc(),
162 (
"." + *Designator +
"=").str());
165 diag(
Type->getBeginLoc(),
"aggregate type is defined here",
166 DiagnosticIDs::Note);
172 for (
const auto *InitExpr : *SyntacticInitList) {
173 if (isa<DesignatedInitExpr>(InitExpr))
175 if (IgnoreMacros && InitExpr->getBeginLoc().isMacroID())
177 const auto Designator = Designators[InitExpr->getBeginLoc()];
178 if (!Designator || Designator->empty()) {
181 diag(InitExpr->getBeginLoc(),
"use designated init expression")
182 << InitExpr->getSourceRange();
184 diag(InitExpr->getBeginLoc(),
185 "use designated init expression to initialize field '%0'")
186 << InitExpr->getSourceRange() << *Designator
187 << FixItHint::CreateInsertion(InitExpr->getBeginLoc(),
188 (
"." + *Designator +
"=").str());
196 IgnoreSingleElementAggregates);
201 StrictCppStandardCompliance);
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(Expr, isMacroID)
static constexpr char IgnoreMacrosName[]
static constexpr bool RestrictToPODTypesDefault
static constexpr char IgnoreSingleElementAggregatesName[]
static constexpr bool IgnoreMacrosDefault
static constexpr bool StrictCppStandardComplianceDefault
static constexpr bool IgnoreSingleElementAggregatesDefault
static constexpr char StrictCppStandardComplianceName[]
static constexpr bool StrictCStandardComplianceDefault
static constexpr char RestrictToPODTypesName[]
static constexpr char StrictCStandardComplianceName[]
llvm::DenseMap< SourceLocation, std::string > getUnwrittenDesignators(const InitListExpr *Syn)
llvm::StringMap< ClangTidyValue > OptionMap