clang-tools  8.0.0
UsingNamespaceDirectiveCheck.cpp
Go to the documentation of this file.
1 //===--- UsingNamespaceDirectiveCheck.cpp - clang-tidy ----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace google {
20 namespace build {
21 
22 void UsingNamespaceDirectiveCheck::registerMatchers(
23  ast_matchers::MatchFinder *Finder) {
24  // Only register the matchers for C++; the functionality currently does not
25  // provide any benefit to other languages, despite being benign.
26  if (getLangOpts().CPlusPlus)
27  Finder->addMatcher(usingDirectiveDecl().bind("usingNamespace"), this);
28 }
29 
30 void UsingNamespaceDirectiveCheck::check(
31  const MatchFinder::MatchResult &Result) {
32  const auto *U = Result.Nodes.getNodeAs<UsingDirectiveDecl>("usingNamespace");
33  SourceLocation Loc = U->getBeginLoc();
34  if (U->isImplicit() || !Loc.isValid())
35  return;
36 
37  // Do not warn if namespace is a std namespace with user-defined literals. The
38  // user-defined literals can only be used with a using directive.
39  if (isStdLiteralsNamespace(U->getNominatedNamespace()))
40  return;
41 
42  diag(Loc, "do not use namespace using-directives; "
43  "use using-declarations instead");
44  // TODO: We could suggest a list of using directives replacing the using
45  // namespace directive.
46 }
47 
48 bool UsingNamespaceDirectiveCheck::isStdLiteralsNamespace(
49  const NamespaceDecl *NS) {
50  if (!NS->getName().endswith("literals"))
51  return false;
52 
53  const auto *Parent = dyn_cast_or_null<NamespaceDecl>(NS->getParent());
54  if (!Parent)
55  return false;
56 
57  if (Parent->isStdNamespace())
58  return true;
59 
60  return Parent->getName() == "literals" && Parent->getParent() &&
61  Parent->getParent()->isStdNamespace();
62 }
63 } // namespace build
64 } // namespace google
65 } // namespace tidy
66 } // namespace clang
SourceLocation Loc
&#39;#&#39; location in the include directive
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//