clang-tools  4.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->getLocStart();
34  if (U->isImplicit() || !Loc.isValid())
35  return;
36 
37  diag(Loc, "do not use namespace using-directives; "
38  "use using-declarations instead");
39  // TODO: We could suggest a list of using directives replacing the using
40  // namespace directive.
41 }
42 
43 } // namespace build
44 } // namespace google
45 } // namespace tidy
46 } // namespace clang
SourceLocation Loc
'#' location in the include directive
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:262
const NamedDecl * Result
Definition: USRFinder.cpp:162