clang-tools 19.0.0git
UsingNamespaceDirectiveCheck.cpp
Go to the documentation of this file.
1//===--- UsingNamespaceDirectiveCheck.cpp - clang-tidy ----------*- C++ -*-===//
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
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13
14using namespace clang::ast_matchers;
15
17
19 ast_matchers::MatchFinder *Finder) {
20 Finder->addMatcher(usingDirectiveDecl().bind("usingNamespace"), this);
21}
22
24 const MatchFinder::MatchResult &Result) {
25 const auto *U = Result.Nodes.getNodeAs<UsingDirectiveDecl>("usingNamespace");
26 SourceLocation Loc = U->getBeginLoc();
27 if (U->isImplicit() || !Loc.isValid())
28 return;
29
30 // Do not warn if namespace is a std namespace with user-defined literals. The
31 // user-defined literals can only be used with a using directive.
32 if (isStdLiteralsNamespace(U->getNominatedNamespace()))
33 return;
34
35 diag(Loc, "do not use namespace using-directives; "
36 "use using-declarations instead");
37 // TODO: We could suggest a list of using directives replacing the using
38 // namespace directive.
39}
40
41bool UsingNamespaceDirectiveCheck::isStdLiteralsNamespace(
42 const NamespaceDecl *NS) {
43 if (!NS->getName().ends_with("literals"))
44 return false;
45
46 const auto *Parent = dyn_cast_or_null<NamespaceDecl>(NS->getParent());
47 if (!Parent)
48 return false;
49
50 if (Parent->isStdNamespace())
51 return true;
52
53 return Parent->getName() == "literals" && Parent->getParent() &&
54 Parent->getParent()->isStdNamespace();
55}
56} // namespace clang::tidy::google::build
const Node * Parent
SourceLocation Loc
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.