brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.4 KiB · ed36d48 Raw
64 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "UseAnonymousNamespaceCheck.h"10#include "clang/AST/ASTContext.h"11#include "clang/ASTMatchers/ASTMatchFinder.h"12 13using namespace clang::ast_matchers;14 15namespace clang::tidy::misc {16namespace {17AST_POLYMORPHIC_MATCHER_P(isInHeaderFile,18                          AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,19                                                          VarDecl),20                          FileExtensionsSet, HeaderFileExtensions) {21  return utils::isExpansionLocInHeaderFile(22      Node.getBeginLoc(), Finder->getASTContext().getSourceManager(),23      HeaderFileExtensions);24}25 26AST_MATCHER(FunctionDecl, isMemberFunction) {27  return llvm::isa<CXXMethodDecl>(&Node);28}29AST_MATCHER(VarDecl, isStaticDataMember) { return Node.isStaticDataMember(); }30} // namespace31 32UseAnonymousNamespaceCheck::UseAnonymousNamespaceCheck(33    StringRef Name, ClangTidyContext *Context)34    : ClangTidyCheck(Name, Context),35      HeaderFileExtensions(Context->getHeaderFileExtensions()) {}36 37void UseAnonymousNamespaceCheck::registerMatchers(MatchFinder *Finder) {38  Finder->addMatcher(39      functionDecl(isStaticStorageClass(),40                   unless(anyOf(isInHeaderFile(HeaderFileExtensions),41                                isInAnonymousNamespace(), isMemberFunction())))42          .bind("x"),43      this);44  Finder->addMatcher(45      varDecl(isStaticStorageClass(),46              unless(anyOf(isInHeaderFile(HeaderFileExtensions),47                           isInAnonymousNamespace(), isStaticLocal(),48                           isStaticDataMember(), hasType(isConstQualified()))))49          .bind("x"),50      this);51}52 53void UseAnonymousNamespaceCheck::check(const MatchFinder::MatchResult &Result) {54  if (const auto *MatchedDecl = Result.Nodes.getNodeAs<NamedDecl>("x")) {55    const StringRef Type =56        llvm::isa<VarDecl>(MatchedDecl) ? "variable" : "function";57    diag(MatchedDecl->getLocation(),58         "%0 %1 declared 'static', move to anonymous namespace instead")59        << Type << MatchedDecl;60  }61}62 63} // namespace clang::tidy::misc64