Benjamin Kramer | 2252cbf | 2014-07-16 14:16:56 +0000 | [diff] [blame] | 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 | |
| 10 | #include "UsingNamespaceDirectiveCheck.h" |
Chandler Carruth | 3cbd71c | 2015-01-14 11:24:38 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
Benjamin Kramer | 2252cbf | 2014-07-16 14:16:56 +0000 | [diff] [blame] | 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | #include "clang/ASTMatchers/ASTMatchers.h" |
Benjamin Kramer | 2252cbf | 2014-07-16 14:16:56 +0000 | [diff] [blame] | 14 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace tidy { |
Alexander Kornienko | ed824e0 | 2015-03-05 13:46:14 +0000 | [diff] [blame] | 19 | namespace google { |
Benjamin Kramer | 2252cbf | 2014-07-16 14:16:56 +0000 | [diff] [blame] | 20 | namespace build { |
| 21 | |
| 22 | void UsingNamespaceDirectiveCheck::registerMatchers( |
| 23 | ast_matchers::MatchFinder *Finder) { |
Aaron Ballman | ec3e5d6 | 2015-09-02 16:20:42 +0000 | [diff] [blame] | 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); |
Benjamin Kramer | 2252cbf | 2014-07-16 14:16:56 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 30 | void UsingNamespaceDirectiveCheck::check( |
| 31 | const MatchFinder::MatchResult &Result) { |
Benjamin Kramer | 2252cbf | 2014-07-16 14:16:56 +0000 | [diff] [blame] | 32 | const auto *U = Result.Nodes.getNodeAs<UsingDirectiveDecl>("usingNamespace"); |
| 33 | SourceLocation Loc = U->getLocStart(); |
| 34 | if (U->isImplicit() || !Loc.isValid()) |
| 35 | return; |
| 36 | |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 37 | diag(Loc, "do not use namespace using-directives; " |
| 38 | "use using-declarations instead"); |
Benjamin Kramer | 2252cbf | 2014-07-16 14:16:56 +0000 | [diff] [blame] | 39 | // TODO: We could suggest a list of using directives replacing the using |
| 40 | // namespace directive. |
| 41 | } |
| 42 | |
| 43 | } // namespace build |
Alexander Kornienko | ed824e0 | 2015-03-05 13:46:14 +0000 | [diff] [blame] | 44 | } // namespace google |
Benjamin Kramer | 2252cbf | 2014-07-16 14:16:56 +0000 | [diff] [blame] | 45 | } // namespace tidy |
| 46 | } // namespace clang |