|
| 1 | +//===--- UnnecessaryExternalLinkageCheck.cpp - clang-tidy |
| 2 | +//---------------------------------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#include "UnnecessaryExternalLinkageCheck.h" |
| 11 | +#include "clang/AST/Decl.h" |
| 12 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | +#include "clang/ASTMatchers/ASTMatchers.h" |
| 14 | +#include "clang/ASTMatchers/ASTMatchersMacros.h" |
| 15 | +#include "clang/Basic/Specifiers.h" |
| 16 | + |
| 17 | +using namespace clang::ast_matchers; |
| 18 | + |
| 19 | +namespace clang::tidy::readability { |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +AST_POLYMORPHIC_MATCHER(isFirstDecl, |
| 24 | + AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, |
| 25 | + VarDecl)) { |
| 26 | + return Node.isFirstDecl(); |
| 27 | +} |
| 28 | + |
| 29 | +AST_MATCHER(Decl, isInMainFile) { |
| 30 | + for (const Decl *D : Node.redecls()) |
| 31 | + if (!Finder->getASTContext().getSourceManager().isInMainFile( |
| 32 | + D->getLocation())) |
| 33 | + return false; |
| 34 | + return true; |
| 35 | +} |
| 36 | + |
| 37 | +AST_POLYMORPHIC_MATCHER(isExternStorageClass, |
| 38 | + AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, |
| 39 | + VarDecl)) { |
| 40 | + return Node.getStorageClass() == SC_Extern; |
| 41 | +} |
| 42 | + |
| 43 | +} // namespace |
| 44 | + |
| 45 | +void UnnecessaryExternalLinkageCheck::registerMatchers(MatchFinder *Finder) { |
| 46 | + auto Common = allOf(isFirstDecl(), isInMainFile(), |
| 47 | + unless(anyOf( |
| 48 | + // 1. internal linkage |
| 49 | + isStaticStorageClass(), isInAnonymousNamespace(), |
| 50 | + // 2. explicit external linkage |
| 51 | + isExternStorageClass(), isExternC(), |
| 52 | + // 3. template |
| 53 | + isExplicitTemplateSpecialization(), |
| 54 | + clang::ast_matchers::isTemplateInstantiation(), |
| 55 | + // 4. friend |
| 56 | + hasAncestor(friendDecl())))); |
| 57 | + Finder->addMatcher( |
| 58 | + functionDecl(Common, unless(cxxMethodDecl()), unless(isMain())) |
| 59 | + .bind("fn"), |
| 60 | + this); |
| 61 | + Finder->addMatcher(varDecl(Common, hasGlobalStorage()).bind("var"), this); |
| 62 | +} |
| 63 | + |
| 64 | +static constexpr StringRef Message = |
| 65 | + "%0 %1 can be internal linkage, " |
| 66 | + "marking as static or using anonymous namespace can avoid external " |
| 67 | + "linkage."; |
| 68 | + |
| 69 | +void UnnecessaryExternalLinkageCheck::check( |
| 70 | + const MatchFinder::MatchResult &Result) { |
| 71 | + if (const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("fn")) { |
| 72 | + diag(FD->getLocation(), Message) << "function" << FD; |
| 73 | + return; |
| 74 | + } |
| 75 | + if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>("var")) { |
| 76 | + diag(VD->getLocation(), Message) << "variable" << VD; |
| 77 | + return; |
| 78 | + } |
| 79 | + llvm_unreachable(""); |
| 80 | +} |
| 81 | + |
| 82 | +} // namespace clang::tidy::readability |
0 commit comments