2014-07-16 22:16:56 +08:00
|
|
|
//===--- UsingNamespaceDirectiveCheck.cpp - clang-tidy ----------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-07-16 22:16:56 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "UsingNamespaceDirectiveCheck.h"
|
2015-01-14 19:24:38 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2014-07-16 22:16:56 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2015-03-05 21:46:14 +08:00
|
|
|
namespace google {
|
2014-07-16 22:16:56 +08:00
|
|
|
namespace build {
|
|
|
|
|
|
|
|
void UsingNamespaceDirectiveCheck::registerMatchers(
|
|
|
|
ast_matchers::MatchFinder *Finder) {
|
2015-09-03 00:20:42 +08:00
|
|
|
// Only register the matchers for C++; the functionality currently does not
|
|
|
|
// provide any benefit to other languages, despite being benign.
|
|
|
|
if (getLangOpts().CPlusPlus)
|
|
|
|
Finder->addMatcher(usingDirectiveDecl().bind("usingNamespace"), this);
|
2014-07-16 22:16:56 +08:00
|
|
|
}
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
void UsingNamespaceDirectiveCheck::check(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
2014-07-16 22:16:56 +08:00
|
|
|
const auto *U = Result.Nodes.getNodeAs<UsingDirectiveDecl>("usingNamespace");
|
2018-08-10 06:42:26 +08:00
|
|
|
SourceLocation Loc = U->getBeginLoc();
|
2014-07-16 22:16:56 +08:00
|
|
|
if (U->isImplicit() || !Loc.isValid())
|
|
|
|
return;
|
|
|
|
|
2017-05-16 01:37:48 +08:00
|
|
|
// Do not warn if namespace is a std namespace with user-defined literals. The
|
|
|
|
// user-defined literals can only be used with a using directive.
|
|
|
|
if (isStdLiteralsNamespace(U->getNominatedNamespace()))
|
|
|
|
return;
|
|
|
|
|
2016-01-08 23:21:40 +08:00
|
|
|
diag(Loc, "do not use namespace using-directives; "
|
|
|
|
"use using-declarations instead");
|
2014-07-16 22:16:56 +08:00
|
|
|
// TODO: We could suggest a list of using directives replacing the using
|
|
|
|
// namespace directive.
|
|
|
|
}
|
|
|
|
|
2017-05-16 01:37:48 +08:00
|
|
|
bool UsingNamespaceDirectiveCheck::isStdLiteralsNamespace(
|
|
|
|
const NamespaceDecl *NS) {
|
|
|
|
if (!NS->getName().endswith("literals"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto *Parent = dyn_cast_or_null<NamespaceDecl>(NS->getParent());
|
|
|
|
if (!Parent)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Parent->isStdNamespace())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return Parent->getName() == "literals" && Parent->getParent() &&
|
|
|
|
Parent->getParent()->isStdNamespace();
|
|
|
|
}
|
2014-07-16 22:16:56 +08:00
|
|
|
} // namespace build
|
2015-03-05 21:46:14 +08:00
|
|
|
} // namespace google
|
2014-07-16 22:16:56 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|