[clang] [Clang] Optimize -Wunsafe-buffer-usage. (PR #124554)

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 30 01:38:43 PST 2025


================
@@ -68,32 +70,60 @@ static std::string getDREAncestorString(const DeclRefExpr *DRE,
 
     if (StParents.size() > 1)
       return "unavailable due to multiple parents";
-    if (StParents.size() == 0)
+    if (StParents.empty())
       break;
     St = StParents.begin()->get<Stmt>();
     if (St)
       SS << " ==> ";
   } while (St);
   return SS.str();
 }
+
 } // namespace
 #endif /* NDEBUG */
 
-namespace clang::ast_matchers {
+namespace {
+// Using a custom matcher instead of ASTMatchers to achieve better performance.
+class FastMatcher {
+public:
+  virtual bool matches(const DynTypedNode &DynNode, ASTContext &Ctx,
+                       const UnsafeBufferUsageHandler &Handler) = 0;
+  virtual ~FastMatcher() = default;
+};
+
+class MatchResult {
+
+public:
+  template <typename T> const T *getNodeAs(StringRef ID) const {
+    auto It = Nodes.find(std::string(ID));
+    if (It == Nodes.end()) {
+      return nullptr;
+    }
+    return It->second.get<T>();
+  }
+
+  void addNode(StringRef ID, const DynTypedNode &Node) {
+    Nodes[std::string(ID)] = Node;
+  }
+
+private:
+  llvm::StringMap<DynTypedNode>
+      Nodes; // DynTypedNode needed to store different types of Nodes, not
----------------
ilya-biryukov wrote:

NIT: the comment is probably redundant, it describes what `DynTypedNode` is and folks unfamiliar with it would just follow the code to its definition and read about it there.

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


More information about the cfe-commits mailing list