[clang] [clang-tools-extra] [clang-tidy] Avoid processing declarations in system headers (PR #128150)

Balazs Benics via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 12 02:46:53 PDT 2025


================
@@ -35,19 +35,41 @@ AST_POLYMORPHIC_MATCHER_P(
                              Builder) != Args.end();
 }
 
+bool isStdOrPosixImpl(const DeclContext *Ctx) {
+  if (!Ctx->isNamespace())
+    return false;
+
+  const auto *ND = cast<NamespaceDecl>(Ctx);
+  if (ND->isInline()) {
+    return isStdOrPosixImpl(ND->getParent());
+  }
+
+  if (!ND->getParent()->getRedeclContext()->isTranslationUnit())
+    return false;
+
+  const IdentifierInfo *II = ND->getIdentifier();
+  return II && (II->isStr("std") || II->isStr("posix"));
+}
+
+AST_MATCHER(Decl, isInStdOrPosixNS) {
+  for (const auto *Ctx = Node.getDeclContext(); Ctx; Ctx = Ctx->getParent()) {
+    if (isStdOrPosixImpl(Ctx))
+      return true;
+  }
+  return false;
+}
+
----------------
steakhal wrote:

Wouldn't this matcher match decls under `namespace my::std::foo {...}`?

https://github.com/llvm/llvm-project/pull/128150


More information about the cfe-commits mailing list