[PATCH] D33010: Make google-build-using-namespace skip std::.*literals

Martin Ejdestig via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat May 13 07:02:14 PDT 2017


marejde updated this revision to Diff 98893.
marejde added a comment.

Removed use of getQualifiedNameAsString().


https://reviews.llvm.org/D33010

Files:
  clang-tidy/google/UsingNamespaceDirectiveCheck.cpp
  clang-tidy/google/UsingNamespaceDirectiveCheck.h
  test/clang-tidy/google-namespaces.cpp


Index: test/clang-tidy/google-namespaces.cpp
===================================================================
--- test/clang-tidy/google-namespaces.cpp
+++ test/clang-tidy/google-namespaces.cpp
@@ -6,3 +6,31 @@
 // 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 {}
+namespace foo::literals {}
+
+using namespace literals;
+// CHECK: :[[@LINE-1]]:1: warning: do not use namespace using-directives; use using-declarations instead [google-build-using-namespace]
+
+using namespace foo::literals;
+// CHECK: :[[@LINE-1]]:1: warning: do not use namespace using-directives; use using-declarations instead [google-build-using-namespace]
Index: clang-tidy/google/UsingNamespaceDirectiveCheck.h
===================================================================
--- clang-tidy/google/UsingNamespaceDirectiveCheck.h
+++ clang-tidy/google/UsingNamespaceDirectiveCheck.h
@@ -38,6 +38,9 @@
       : ClangTidyCheck(Name, Context) {}
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+
+private:
+  bool isStdLiteralsNamespace(const NamespaceDecl *NS);
 };
 
 } // namespace build
Index: clang-tidy/google/UsingNamespaceDirectiveCheck.cpp
===================================================================
--- clang-tidy/google/UsingNamespaceDirectiveCheck.cpp
+++ clang-tidy/google/UsingNamespaceDirectiveCheck.cpp
@@ -34,12 +34,32 @@
   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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33010.98893.patch
Type: text/x-patch
Size: 3199 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170513/79078f7b/attachment.bin>


More information about the cfe-commits mailing list