CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
110 lines
3.8 KiB
C++
110 lines
3.8 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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 "PreferStaticOverAnonymousNamespaceCheck.h"
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
namespace clang::tidy::llvm_check {
|
|
|
|
namespace {
|
|
|
|
AST_MATCHER(NamedDecl, isInMacro) {
|
|
return Node.getBeginLoc().isMacroID() || Node.getEndLoc().isMacroID();
|
|
}
|
|
|
|
AST_MATCHER(VarDecl, isLocalVariable) { return Node.isLocalVarDecl(); }
|
|
|
|
AST_MATCHER(Decl, isLexicallyInAnonymousNamespace) {
|
|
for (const DeclContext *DC = Node.getLexicalDeclContext(); DC != nullptr;
|
|
DC = DC->getLexicalParent()) {
|
|
if (const auto *ND = dyn_cast<NamespaceDecl>(DC))
|
|
if (ND->isAnonymousNamespace())
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
PreferStaticOverAnonymousNamespaceCheck::
|
|
PreferStaticOverAnonymousNamespaceCheck(StringRef Name,
|
|
ClangTidyContext *Context)
|
|
: ClangTidyCheck(Name, Context),
|
|
AllowVariableDeclarations(Options.get("AllowVariableDeclarations", true)),
|
|
AllowMemberFunctionsInClass(
|
|
Options.get("AllowMemberFunctionsInClass", true)) {}
|
|
|
|
void PreferStaticOverAnonymousNamespaceCheck::storeOptions(
|
|
ClangTidyOptions::OptionMap &Opts) {
|
|
Options.store(Opts, "AllowVariableDeclarations", AllowVariableDeclarations);
|
|
Options.store(Opts, "AllowMemberFunctionsInClass",
|
|
AllowMemberFunctionsInClass);
|
|
}
|
|
|
|
void PreferStaticOverAnonymousNamespaceCheck::registerMatchers(
|
|
MatchFinder *Finder) {
|
|
const auto IsDefinitionInAnonymousNamespace = allOf(
|
|
isLexicallyInAnonymousNamespace(), unless(isInMacro()), isDefinition());
|
|
|
|
if (AllowMemberFunctionsInClass) {
|
|
Finder->addMatcher(
|
|
functionDecl(IsDefinitionInAnonymousNamespace,
|
|
unless(anyOf(hasParent(cxxRecordDecl()),
|
|
hasParent(functionTemplateDecl(
|
|
hasParent(cxxRecordDecl()))))))
|
|
.bind("function"),
|
|
this);
|
|
} else {
|
|
Finder->addMatcher(
|
|
functionDecl(IsDefinitionInAnonymousNamespace).bind("function"), this);
|
|
}
|
|
|
|
if (!AllowVariableDeclarations)
|
|
Finder->addMatcher(varDecl(IsDefinitionInAnonymousNamespace,
|
|
unless(isLocalVariable()), unless(parmVarDecl()))
|
|
.bind("var"),
|
|
this);
|
|
}
|
|
|
|
void PreferStaticOverAnonymousNamespaceCheck::check(
|
|
const MatchFinder::MatchResult &Result) {
|
|
if (const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("function")) {
|
|
if (Func->isCXXClassMember())
|
|
diag(Func->getLocation(),
|
|
"place definition of method %0 outside of an anonymous namespace")
|
|
<< Func;
|
|
else if (Func->isStatic())
|
|
diag(Func->getLocation(),
|
|
"place static function %0 outside of an anonymous namespace")
|
|
<< Func;
|
|
else
|
|
diag(Func->getLocation(),
|
|
"function %0 is declared in an anonymous namespace; "
|
|
"prefer using 'static' for restricting visibility")
|
|
<< Func;
|
|
return;
|
|
}
|
|
|
|
if (const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var")) {
|
|
if (Var->getStorageClass() == SC_Static)
|
|
diag(Var->getLocation(),
|
|
"place static variable %0 outside of an anonymous namespace")
|
|
<< Var;
|
|
else
|
|
diag(Var->getLocation(),
|
|
"variable %0 is declared in an anonymous namespace; "
|
|
"prefer using 'static' for restricting visibility")
|
|
<< Var;
|
|
}
|
|
}
|
|
|
|
} // namespace clang::tidy::llvm_check
|