================
@@ -9,6 +9,105 @@
 #include "SSAFAnalysesCommon.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Decl.h"
+#include "clang/AST/DeclObjC.h"
+#include "clang/AST/DynamicRecursiveASTVisitor.h"
+
+using namespace clang;
+
+namespace {
+// Traverses the AST and finds contributors.
+class ContributorFinder : public DynamicRecursiveASTVisitor {
+public:
+  std::vector<const NamedDecl *> Contributors;
+
+  bool VisitFunctionDecl(FunctionDecl *D) override {
+    Contributors.push_back(D);
+    return true;
+  }
+
+  bool VisitRecordDecl(RecordDecl *D) override {
+    Contributors.push_back(D);
+    return true;
+  }
+
+  bool VisitVarDecl(VarDecl *D) override {
+    DeclContext *DC = D->getDeclContext();
+
+    if (DC->isFileContext() || DC->isNamespace())
+      Contributors.push_back(D);
+    return true;
+  }
+};
+
+// An AST visitor that skips the root node's strict-descendants that are
+// callable Decls and record Decls, because those are separate contributors.
+//
+// Clients need to implement their own `MatchAction`, which is a function that
+// takes a `DynTypedNode`, decides if it matches and performs any further
+// callback actions.
+class ContributorFactFinder : public DynamicRecursiveASTVisitor {
+  llvm::function_ref<void(const DynTypedNode &)> MatchAction;
+  const NamedDecl *RootDecl = nullptr;
+
+  template <typename NodeTy> void match(const NodeTy &Node) {
+    MatchAction(DynTypedNode::create(Node));
+  }
+
+public:
+  ContributorFactFinder(
+      llvm::function_ref<void(const DynTypedNode &)> MatchAction)
+      : MatchAction(MatchAction) {
+    ShouldVisitTemplateInstantiations = true;
+    ShouldVisitImplicitCode = false;
+  }
+
+  // The entry point:
+  void findMatches(const NamedDecl *Contributor) {
+    RootDecl = Contributor;
+    TraverseDecl(const_cast<NamedDecl *>(Contributor));
+  }
+
+  bool TraverseDecl(Decl *Node) override {
+    if (!Node)
+      return true;
----------------
steakhal wrote:

I'm surprised that any of these can be null, but I guess you must have checked.
Same for Traversing Stmts.

https://github.com/llvm/llvm-project/pull/191933
_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to