[PATCH] D115936: [Clang] Add isInNamespace() to check if a Decl in a specific namespace

Jun Zhang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 17 06:03:31 PST 2021


junaire created this revision.
junaire added reviewers: rtrieu, ddunbar, CornedBee, gribozavr.
junaire requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Currently we can only check whether a Decl in namespace `std` or not.
However, it will very useful if we can have a API to check if a Decl in
a specific namespace. With this, we can implement clang-tidy checks to
replace some old third-party library component to std, like boost::shared_ptr.

Signed-off-by: Jun Zhang <jun at junz.org>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115936

Files:
  clang/include/clang/AST/DeclBase.h
  clang/lib/AST/DeclBase.cpp


Index: clang/lib/AST/DeclBase.cpp
===================================================================
--- clang/lib/AST/DeclBase.cpp
+++ clang/lib/AST/DeclBase.cpp
@@ -392,8 +392,12 @@
 }
 
 bool Decl::isInStdNamespace() const {
+	return isInNamespace("std");
+}
+
+bool Decl::isInNamespace(llvm::StringRef Namespace) const {
   const DeclContext *DC = getDeclContext();
-  return DC && DC->isStdNamespace();
+  return DC && DC->isNamespace(Namespace);
 }
 
 TranslationUnitDecl *Decl::getTranslationUnitDecl() {
@@ -1124,6 +1128,10 @@
 }
 
 bool DeclContext::isStdNamespace() const {
+	return isNamespace("std");
+}
+
+bool DeclContext::isNamespace(llvm::StringRef Namespace) const {
   if (!isNamespace())
     return false;
 
@@ -1136,7 +1144,7 @@
     return false;
 
   const IdentifierInfo *II = ND->getIdentifier();
-  return II && II->isStr("std");
+  return II && II->isStr(Namespace);
 }
 
 bool DeclContext::isDependentContext() const {
Index: clang/include/clang/AST/DeclBase.h
===================================================================
--- clang/include/clang/AST/DeclBase.h
+++ clang/include/clang/AST/DeclBase.h
@@ -463,6 +463,7 @@
   bool isInAnonymousNamespace() const;
 
   bool isInStdNamespace() const;
+  bool isInNamespace(llvm::StringRef Namespace) const;
 
   ASTContext &getASTContext() const LLVM_READONLY;
 
@@ -1945,6 +1946,8 @@
 
   bool isStdNamespace() const;
 
+  bool isNamespace(llvm::StringRef Namespace) const;
+
   bool isInlineNamespace() const;
 
   /// Determines whether this context is dependent on a


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115936.395108.patch
Type: text/x-patch
Size: 1552 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211217/22e8643a/attachment.bin>


More information about the cfe-commits mailing list