[clang-tools-extra] r303085 - Make google-build-using-namespace skip std::.*literals
Alexander Kornienko via cfe-commits
cfe-commits at lists.llvm.org
Mon May 15 10:37:48 PDT 2017
Author: alexfh
Date: Mon May 15 12:37:48 2017
New Revision: 303085
URL: http://llvm.org/viewvc/llvm-project?rev=303085&view=rev
Log:
Make google-build-using-namespace skip std::.*literals
Summary:
C++14 added a couple of user-defined literals in the standard library. E.g.
std::chrono_literals and std::literals::chrono_literals . Using them
requires a using directive so do not warn in google-build-using-namespace
if namespace name starts with "std::" and ends with "literals".
Reviewers: alexfh
Reviewed By: alexfh
Subscribers: cfe-commits
Patch by Martin Ejdestig!
Differential Revision: https://reviews.llvm.org/D33010
Modified:
clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.cpp
clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.h
clang-tools-extra/trunk/test/clang-tidy/google-namespaces.cpp
Modified: clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.cpp?rev=303085&r1=303084&r2=303085&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.cpp Mon May 15 12:37:48 2017
@@ -34,12 +34,32 @@ void UsingNamespaceDirectiveCheck::check
if (U->isImplicit() || !Loc.isValid())
return;
+ // 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;
+
diag(Loc, "do not use namespace using-directives; "
"use using-declarations instead");
// TODO: We could suggest a list of using directives replacing the using
// namespace directive.
}
+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();
+}
} // namespace build
} // namespace google
} // namespace tidy
Modified: clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.h?rev=303085&r1=303084&r2=303085&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.h Mon May 15 12:37:48 2017
@@ -38,6 +38,9 @@ public:
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+
+private:
+ static bool isStdLiteralsNamespace(const NamespaceDecl *NS);
};
} // namespace build
Modified: clang-tools-extra/trunk/test/clang-tidy/google-namespaces.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/google-namespaces.cpp?rev=303085&r1=303084&r2=303085&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/google-namespaces.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/google-namespaces.cpp Mon May 15 12:37:48 2017
@@ -6,3 +6,47 @@ using namespace spaaaace;
// CHECK: :[[@LINE-1]]:1: warning: do not use namespace using-directives; use using-declarations instead [google-build-using-namespace]
using spaaaace::core; // no-warning
+
+namespace std {
+inline namespace literals {
+inline namespace chrono_literals {
+}
+inline namespace complex_literals {
+}
+inline namespace string_literals {
+}
+}
+}
+
+using namespace std::chrono_literals; // no-warning
+using namespace std::complex_literals; // no-warning
+using namespace std::literals; // no-warning
+using namespace std::literals::chrono_literals; // no-warning
+using namespace std::literals::complex_literals; // no-warning
+using namespace std::literals::string_literals; // no-warning
+using namespace std::string_literals; // no-warning
+
+namespace literals {}
+
+using namespace literals;
+// CHECK: :[[@LINE-1]]:1: warning: do not use namespace using-directives; use using-declarations instead [google-build-using-namespace]
+
+namespace foo {
+inline namespace literals {
+inline namespace bar_literals {}
+}
+}
+
+using namespace foo::literals;
+// CHECK: :[[@LINE-1]]:1: warning: do not use namespace using-directives; use using-declarations instead [google-build-using-namespace]
+
+using namespace foo::bar_literals;
+// CHECK: :[[@LINE-1]]:1: warning: do not use namespace using-directives; use using-declarations instead [google-build-using-namespace]
+
+using namespace foo::literals::bar_literals;
+// CHECK: :[[@LINE-1]]:1: warning: do not use namespace using-directives; use using-declarations instead [google-build-using-namespace]
+
+namespace foo_literals {}
+
+using namespace foo_literals;
+// CHECK: :[[@LINE-1]]:1: warning: do not use namespace using-directives; use using-declarations instead [google-build-using-namespace]
More information about the cfe-commits
mailing list