blob: 6fc6fd3f605a82d87246ce1deed99b8da3615cdb [file] [log] [blame]
Benjamin Kramer2252cbf2014-07-16 14:16:56 +00001//===--- 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 Carruth3cbd71c2015-01-14 11:24:38 +000011#include "clang/AST/ASTContext.h"
Benjamin Kramer2252cbf2014-07-16 14:16:56 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
Benjamin Kramer2252cbf2014-07-16 14:16:56 +000014
15using namespace clang::ast_matchers;
16
17namespace clang {
18namespace tidy {
Alexander Kornienkoed824e02015-03-05 13:46:14 +000019namespace google {
Benjamin Kramer2252cbf2014-07-16 14:16:56 +000020namespace build {
21
22void UsingNamespaceDirectiveCheck::registerMatchers(
23 ast_matchers::MatchFinder *Finder) {
Aaron Ballmanec3e5d62015-09-02 16:20:42 +000024 // 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 Kramer2252cbf2014-07-16 14:16:56 +000028}
29
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000030void UsingNamespaceDirectiveCheck::check(
31 const MatchFinder::MatchResult &Result) {
Benjamin Kramer2252cbf2014-07-16 14:16:56 +000032 const auto *U = Result.Nodes.getNodeAs<UsingDirectiveDecl>("usingNamespace");
33 SourceLocation Loc = U->getLocStart();
34 if (U->isImplicit() || !Loc.isValid())
35 return;
36
Alexander Kornienkob1b2f872016-01-08 15:21:40 +000037 diag(Loc, "do not use namespace using-directives; "
38 "use using-declarations instead");
Benjamin Kramer2252cbf2014-07-16 14:16:56 +000039 // TODO: We could suggest a list of using directives replacing the using
40 // namespace directive.
41}
42
43} // namespace build
Alexander Kornienkoed824e02015-03-05 13:46:14 +000044} // namespace google
Benjamin Kramer2252cbf2014-07-16 14:16:56 +000045} // namespace tidy
46} // namespace clang