|
| 1 | +//===--- UseInternalLinkageCheck.cpp - clang-tidy--------------------------===// |
| 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-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "UseInternalLinkageCheck.h" |
| 10 | +#include "../utils/FileExtensionsUtils.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/SourceLocation.h" |
| 16 | +#include "clang/Basic/Specifiers.h" |
| 17 | +#include "llvm/ADT/STLExtras.h" |
| 18 | + |
| 19 | +using namespace clang::ast_matchers; |
| 20 | + |
| 21 | +namespace clang::tidy::misc { |
| 22 | + |
| 23 | +namespace { |
| 24 | + |
| 25 | +AST_MATCHER(Decl, isFirstDecl) { return Node.isFirstDecl(); } |
| 26 | + |
| 27 | +static bool isInMainFile(SourceLocation L, SourceManager &SM, |
| 28 | + const FileExtensionsSet &HeaderFileExtensions) { |
| 29 | + for (;;) { |
| 30 | + if (utils::isSpellingLocInHeaderFile(L, SM, HeaderFileExtensions)) |
| 31 | + return false; |
| 32 | + if (SM.isInMainFile(L)) |
| 33 | + return true; |
| 34 | + // not in header file but not in main file |
| 35 | + L = SM.getIncludeLoc(SM.getFileID(L)); |
| 36 | + if (L.isValid()) |
| 37 | + continue; |
| 38 | + // Conservative about the unknown |
| 39 | + return false; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +AST_MATCHER_P(Decl, isAllRedeclsInMainFile, FileExtensionsSet, |
| 44 | + HeaderFileExtensions) { |
| 45 | + return llvm::all_of(Node.redecls(), [&](const Decl *D) { |
| 46 | + return isInMainFile(D->getLocation(), |
| 47 | + Finder->getASTContext().getSourceManager(), |
| 48 | + HeaderFileExtensions); |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +AST_POLYMORPHIC_MATCHER(isExternStorageClass, |
| 53 | + AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, |
| 54 | + VarDecl)) { |
| 55 | + return Node.getStorageClass() == SC_Extern; |
| 56 | +} |
| 57 | + |
| 58 | +} // namespace |
| 59 | + |
| 60 | +void UseInternalLinkageCheck::registerMatchers(MatchFinder *Finder) { |
| 61 | + auto Common = |
| 62 | + allOf(isFirstDecl(), isAllRedeclsInMainFile(HeaderFileExtensions), |
| 63 | + unless(anyOf( |
| 64 | + // 1. internal linkage |
| 65 | + isStaticStorageClass(), isInAnonymousNamespace(), |
| 66 | + // 2. explicit external linkage |
| 67 | + isExternStorageClass(), isExternC(), |
| 68 | + // 3. template |
| 69 | + isExplicitTemplateSpecialization(), |
| 70 | + // 4. friend |
| 71 | + hasAncestor(friendDecl())))); |
| 72 | + Finder->addMatcher( |
| 73 | + functionDecl(Common, unless(cxxMethodDecl()), unless(isMain())) |
| 74 | + .bind("fn"), |
| 75 | + this); |
| 76 | + Finder->addMatcher(varDecl(Common, hasGlobalStorage()).bind("var"), this); |
| 77 | +} |
| 78 | + |
| 79 | +static constexpr StringRef Message = |
| 80 | + "%0 %1 can be made static or moved into an anonymous namespace " |
| 81 | + "to enforce internal linkage"; |
| 82 | + |
| 83 | +void UseInternalLinkageCheck::check(const MatchFinder::MatchResult &Result) { |
| 84 | + if (const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("fn")) { |
| 85 | + diag(FD->getLocation(), Message) << "function" << FD; |
| 86 | + return; |
| 87 | + } |
| 88 | + if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>("var")) { |
| 89 | + diag(VD->getLocation(), Message) << "variable" << VD; |
| 90 | + return; |
| 91 | + } |
| 92 | + llvm_unreachable(""); |
| 93 | +} |
| 94 | + |
| 95 | +} // namespace clang::tidy::misc |
0 commit comments