10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
18AST_MATCHER(FunctionDecl, doesDeclarationForceExternallyVisibleDefinition) {
19 return Node.doesDeclarationForceExternallyVisibleDefinition();
25 IgnoreMacros(Options.getLocalOrGlobal(
"IgnoreMacros", true)) {}
34 namedDecl(anyOf(varDecl(unless(isDefinition())),
35 functionDecl(unless(anyOf(
36 isDefinition(), isDefaulted(),
37 doesDeclarationForceExternallyVisibleDefinition(),
38 hasAncestor(friendDecl()))))),
39 optionally(hasParent(linkageSpecDecl().bind(
"extern"))))
45 const auto *D = Result.Nodes.getNodeAs<NamedDecl>(
"Decl");
46 const auto *Prev = D->getPreviousDecl();
49 if (!Prev->getLocation().isValid())
51 if (Prev->getLocation() == D->getLocation())
54 (D->getLocation().isMacroID() || Prev->getLocation().isMacroID()))
57 for (
const auto &
Parent : Result.Context->getParents(*Prev))
58 if (
Parent.get<FriendDecl>())
61 const SourceManager &SM = *Result.SourceManager;
63 const bool DifferentHeaders =
64 !SM.isInMainFile(D->getLocation()) &&
65 !SM.isWrittenInSameFile(Prev->getLocation(), D->getLocation());
67 bool MultiVar =
false;
68 if (
const auto *VD = dyn_cast<VarDecl>(D)) {
70 for (
const auto *Other : VD->getDeclContext()->decls()) {
71 if (Other != D && Other->getBeginLoc() == VD->getBeginLoc()) {
78 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
79 D->getSourceRange().getEnd(), 0, SM, Result.Context->getLangOpts());
81 auto Diag =
diag(D->getLocation(),
"redundant %0 declaration") << D;
82 if (!MultiVar && !DifferentHeaders) {
83 SourceLocation BeginLoc;
84 if (
const auto *Extern =
85 Result.Nodes.getNodeAs<LinkageSpecDecl>(
"extern");
86 Extern && !Extern->hasBraces())
87 BeginLoc = Extern->getExternLoc();
89 BeginLoc = D->getSourceRange().getBegin();
91 Diag << FixItHint::CreateRemoval(SourceRange(BeginLoc, EndLoc));
94 diag(Prev->getLocation(),
"previously declared here", DiagnosticIDs::Note);
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.
RedundantDeclarationCheck(StringRef Name, ClangTidyContext *Context)
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.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
AST_MATCHER(CXXMethodDecl, isStatic)
llvm::StringMap< ClangTidyValue > OptionMap