10#include "../utils/LexerUtils.h"
11#include "../utils/Matchers.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Lex/Lexer.h"
24 const LangOptions &LangOpts) {
25 const Token PrevToken =
27 if (PrevToken.is(tok::unknown))
30 if (PrevToken.isNot(tok::equal))
31 return {PrevToken.getEndLoc(),
Range.getEnd()};
34 {PrevToken.getLocation(),
Range.getEnd()}, SM, LangOpts);
39 IgnoreBaseInCopyConstructors);
43 auto ConstructorMatcher =
44 cxxConstructExpr(argumentCountIs(0),
45 hasDeclaration(cxxConstructorDecl(ofClass(cxxRecordDecl(
46 unless(isTriviallyDefaultConstructible()))))))
51 unless(isDelegatingConstructor()), ofClass(unless(isUnion())),
52 forEachConstructorInitializer(
53 cxxCtorInitializer(withInitializer(ConstructorMatcher),
54 unless(forField(fieldDecl(
55 anyOf(hasType(isConstQualified()),
56 hasParent(recordDecl(isUnion())))))))
61 Finder->addMatcher(fieldDecl(hasInClassInitializer(ConstructorMatcher),
62 unless(hasParent(recordDecl(isUnion()))))
68 const auto *Construct = Result.Nodes.getNodeAs<CXXConstructExpr>(
"construct");
70 if (
const auto *
Field = Result.Nodes.getNodeAs<FieldDecl>(
"field")) {
71 const Expr *Init =
Field->getInClassInitializer();
72 diag(Construct->getExprLoc(),
"initializer for member %0 is redundant")
75 Init->getSourceRange(), *Result.SourceManager,
getLangOpts()));
79 const auto *Init = Result.Nodes.getNodeAs<CXXCtorInitializer>(
"init");
80 const auto *ConstructorDecl =
81 Result.Nodes.getNodeAs<CXXConstructorDecl>(
"constructor");
83 if (IgnoreBaseInCopyConstructors && ConstructorDecl->isCopyConstructor() &&
84 Init->isBaseInitializer())
87 if (Init->isAnyMemberInitializer()) {
88 diag(Init->getSourceLocation(),
"initializer for member %0 is redundant")
89 << Init->getAnyMember()
90 << FixItHint::CreateRemoval(Init->getSourceRange());
92 diag(Init->getSourceLocation(),
93 "initializer for base class %0 is redundant")
94 << Construct->getType()
95 << FixItHint::CreateRemoval(Init->getSourceRange());
CharSourceRange Range
SourceRange for the file name.
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.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
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...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static SourceRange getFullInitRangeInclWhitespaces(SourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Token getPreviousToken(SourceLocation Location, const SourceManager &SM, const LangOptions &LangOpts, bool SkipComments)
Returns previous token or tok::unknown if not found.
llvm::StringMap< ClangTidyValue > OptionMap