llvm-project/clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
Alexander Kornienko ab2d3ce47d [clang-tidy] Applied clang-tidy fixes. NFC
Applied fixes enabled by the LLVM's .clang-tidy configs. Reverted files where
fixes introduced compile errors:
  clang-tools-extra/clang-tidy/hicpp/NoAssemblerCheck.cpp
  clang-tools-extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp

$ clang-tools-extra/clang-tidy/tool/run-clang-tidy.py -fix clang-tools-extra/clang-tidy/
Enabled checks:
    llvm-else-after-return
    llvm-header-guard
    llvm-include-order
    llvm-namespace-comment
    llvm-prefer-isa-or-dyn-cast-in-conditionals
    llvm-prefer-register-over-unsigned
    llvm-qualified-auto
    llvm-twine-local
    misc-definitions-in-headers
    misc-misplaced-const
    misc-new-delete-overloads
    misc-no-recursion
    misc-non-copyable-objects
    misc-redundant-expression
    misc-static-assert
    misc-throw-by-value-catch-by-reference
    misc-unconventional-assign-operator
    misc-uniqueptr-reset-release
    misc-unused-alias-decls
    misc-unused-using-decls
    readability-identifier-naming

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D95614
2021-01-29 01:01:19 +01:00

92 lines
3.1 KiB
C++

//===--- RedundantDeclarationCheck.cpp - clang-tidy------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "RedundantDeclarationCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace readability {
AST_MATCHER(FunctionDecl, doesDeclarationForceExternallyVisibleDefinition) {
return Node.doesDeclarationForceExternallyVisibleDefinition();
}
RedundantDeclarationCheck::RedundantDeclarationCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
void RedundantDeclarationCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "IgnoreMacros", IgnoreMacros);
}
void RedundantDeclarationCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
namedDecl(anyOf(varDecl(unless(isDefinition())),
functionDecl(unless(anyOf(
isDefinition(), isDefaulted(),
doesDeclarationForceExternallyVisibleDefinition(),
hasParent(friendDecl()))))))
.bind("Decl"),
this);
}
void RedundantDeclarationCheck::check(const MatchFinder::MatchResult &Result) {
const auto *D = Result.Nodes.getNodeAs<NamedDecl>("Decl");
const auto *Prev = D->getPreviousDecl();
if (!Prev)
return;
if (!Prev->getLocation().isValid())
return;
if (Prev->getLocation() == D->getLocation())
return;
if (IgnoreMacros &&
(D->getLocation().isMacroID() || Prev->getLocation().isMacroID()))
return;
// Don't complain when the previous declaration is a friend declaration.
for (const auto &Parent : Result.Context->getParents(*Prev))
if (Parent.get<FriendDecl>())
return;
const SourceManager &SM = *Result.SourceManager;
const bool DifferentHeaders =
!SM.isInMainFile(D->getLocation()) &&
!SM.isWrittenInSameFile(Prev->getLocation(), D->getLocation());
bool MultiVar = false;
if (const auto *VD = dyn_cast<VarDecl>(D)) {
// Is this a multivariable declaration?
for (const auto *Other : VD->getDeclContext()->decls()) {
if (Other != D && Other->getBeginLoc() == VD->getBeginLoc()) {
MultiVar = true;
break;
}
}
}
SourceLocation EndLoc = Lexer::getLocForEndOfToken(
D->getSourceRange().getEnd(), 0, SM, Result.Context->getLangOpts());
{
auto Diag = diag(D->getLocation(), "redundant %0 declaration") << D;
if (!MultiVar && !DifferentHeaders)
Diag << FixItHint::CreateRemoval(
SourceRange(D->getSourceRange().getBegin(), EndLoc));
}
diag(Prev->getLocation(), "previously declared here", DiagnosticIDs::Note);
}
} // namespace readability
} // namespace tidy
} // namespace clang