blob: 60a46f88f5f6a165cb8d9ec2b9ef5a503d1d1477 [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 Kornienko1ca0e322017-05-15 17:37:48 +000037 // 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
Alexander Kornienkob1b2f872016-01-08 15:21:40 +000042 diag(Loc, "do not use namespace using-directives; "
43 "use using-declarations instead");
Benjamin Kramer2252cbf2014-07-16 14:16:56 +000044 // TODO: We could suggest a list of using directives replacing the using
45 // namespace directive.
46}
47
Alexander Kornienko1ca0e322017-05-15 17:37:48 +000048bool 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}
Benjamin Kramer2252cbf2014-07-16 14:16:56 +000063} // namespace build
Alexander Kornienkoed824e02015-03-05 13:46:14 +000064} // namespace google
Benjamin Kramer2252cbf2014-07-16 14:16:56 +000065} // namespace tidy
66} // namespace clang