clang-tools  10.0.0
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 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace google {
19 namespace build {
20 
21 void UsingNamespaceDirectiveCheck::registerMatchers(
22  ast_matchers::MatchFinder *Finder) {
23  // Only register the matchers for C++; the functionality currently does not
24  // provide any benefit to other languages, despite being benign.
25  if (getLangOpts().CPlusPlus)
26  Finder->addMatcher(usingDirectiveDecl().bind("usingNamespace"), this);
27 }
28 
29 void UsingNamespaceDirectiveCheck::check(
30  const MatchFinder::MatchResult &Result) {
31  const auto *U = Result.Nodes.getNodeAs<UsingDirectiveDecl>("usingNamespace");
32  SourceLocation Loc = U->getBeginLoc();
33  if (U->isImplicit() || !Loc.isValid())
34  return;
35 
36  // Do not warn if namespace is a std namespace with user-defined literals. The
37  // user-defined literals can only be used with a using directive.
38  if (isStdLiteralsNamespace(U->getNominatedNamespace()))
39  return;
40 
41  diag(Loc, "do not use namespace using-directives; "
42  "use using-declarations instead");
43  // TODO: We could suggest a list of using directives replacing the using
44  // namespace directive.
45 }
46 
47 bool UsingNamespaceDirectiveCheck::isStdLiteralsNamespace(
48  const NamespaceDecl *NS) {
49  if (!NS->getName().endswith("literals"))
50  return false;
51 
52  const auto *Parent = dyn_cast_or_null<NamespaceDecl>(NS->getParent());
53  if (!Parent)
54  return false;
55 
56  if (Parent->isStdNamespace())
57  return true;
58 
59  return Parent->getName() == "literals" && Parent->getParent() &&
60  Parent->getParent()->isStdNamespace();
61 }
62 } // namespace build
63 } // namespace google
64 } // namespace tidy
65 } // namespace clang
SourceLocation Loc
&#39;#&#39; location in the include directive
const Node * Parent
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//