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);
88 if (
const InitListExpr *SyntacticForm =
89 Node.isSyntacticForm() ? &
Node :
Node.getSyntacticForm())
90 return getNumberOfDesignated(SyntacticForm) == SyntacticForm->getNumInits();
95 return Node.getNumInits() > 1;
111 StrictCppStandardCompliance(
116 const auto HasBaseWithFields =
117 hasAnyBase(hasType(cxxRecordDecl(has(fieldDecl()))));
120 hasType(cxxRecordDecl(RestrictToPODTypes ? isPOD() : isAggregate(),
121 unless(HasBaseWithFields))
123 IgnoreSingleElementAggregates ? hasMoreThanOneElement() : anything(),
124 unless(isFullyDesignated()))
130 const MatchFinder::MatchResult &Result) {
131 const auto *InitList = Result.Nodes.getNodeAs<InitListExpr>(
"init");
132 const auto *
Type = Result.Nodes.getNodeAs<CXXRecordDecl>(
"type");
133 if (!
Type || !InitList)
135 const auto *SyntacticInitList = InitList->getSyntacticForm();
136 if (!SyntacticInitList)
138 Designators Designators{SyntacticInitList};
139 const unsigned NumberOfDesignated = getNumberOfDesignated(SyntacticInitList);
140 if (SyntacticInitList->getNumInits() - NumberOfDesignated >
146 if (0 == NumberOfDesignated) {
147 if (IgnoreMacros && InitList->getBeginLoc().isMacroID())
150 DiagnosticBuilder Diag =
151 diag(InitList->getLBraceLoc(),
152 "use designated initializer list to initialize %0");
153 Diag <<
Type << InitList->getSourceRange();
154 for (
const Stmt *InitExpr : *SyntacticInitList) {
155 const auto Designator = Designators[InitExpr->getBeginLoc()];
156 if (Designator && !Designator->empty())
157 Diag << FixItHint::CreateInsertion(InitExpr->getBeginLoc(),
158 (
"." + *Designator +
"=").str());
161 diag(
Type->getBeginLoc(),
"aggregate type is defined here",
162 DiagnosticIDs::Note);
168 for (
const auto *InitExpr : *SyntacticInitList) {
169 if (isa<DesignatedInitExpr>(InitExpr))
171 if (IgnoreMacros && InitExpr->getBeginLoc().isMacroID())
173 const auto Designator = Designators[InitExpr->getBeginLoc()];
174 if (!Designator || Designator->empty()) {
177 diag(InitExpr->getBeginLoc(),
"use designated init expression")
178 << InitExpr->getSourceRange();
180 diag(InitExpr->getBeginLoc(),
181 "use designated init expression to initialize field '%0'")
182 << InitExpr->getSourceRange() << *Designator
183 << FixItHint::CreateInsertion(InitExpr->getBeginLoc(),
184 (
"." + *Designator +
"=").str());
192 IgnoreSingleElementAggregates);
197 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(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 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