summaryrefslogtreecommitdiffstats
path: root/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
diff options
context:
space:
mode:
authorManuel Klimek <klimek@google.com>2012-07-06 05:48:52 +0000
committerManuel Klimek <klimek@google.com>2012-07-06 05:48:52 +0000
commit04616e47769af372649ec2c16e89a2fbc1ddce8b (patch)
treee98bd8afbde2e8fb332a727f07d1544bfb6bebd3 /clang/lib/ASTMatchers/ASTMatchersInternal.cpp
parent5e6e6264f43b03c0c83610245b79da1f998551ee (diff)
downloadbcm5719-llvm-04616e47769af372649ec2c16e89a2fbc1ddce8b.tar.gz
bcm5719-llvm-04616e47769af372649ec2c16e89a2fbc1ddce8b.zip
Adds the AST Matcher library, which provides a in-C++ DSL to express
matches on interesting parts of the AST, and callback mechanisms to act on them. llvm-svn: 159805
Diffstat (limited to 'clang/lib/ASTMatchers/ASTMatchersInternal.cpp')
-rw-r--r--clang/lib/ASTMatchers/ASTMatchersInternal.cpp102
1 files changed, 102 insertions, 0 deletions
diff --git a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
new file mode 100644
index 00000000000..c864e31cb14
--- /dev/null
+++ b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -0,0 +1,102 @@
+//===--- ASTMatchersInternal.cpp - Structural query framework -------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Implements the base layer of the matcher framework.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersInternal.h"
+
+namespace clang {
+namespace ast_matchers {
+namespace internal {
+
+BoundNodesTree::BoundNodesTree() {}
+
+BoundNodesTree::BoundNodesTree(
+ const std::map<std::string, const clang::Decl*>& DeclBindings,
+ const std::map<std::string, const clang::Stmt*>& StmtBindings,
+ const std::vector<BoundNodesTree> RecursiveBindings)
+ : DeclBindings(DeclBindings), StmtBindings(StmtBindings),
+ RecursiveBindings(RecursiveBindings) {}
+
+void BoundNodesTree::copyTo(BoundNodesTreeBuilder* Builder) const {
+ copyBindingsTo(DeclBindings, Builder);
+ copyBindingsTo(StmtBindings, Builder);
+ for (std::vector<BoundNodesTree>::const_iterator
+ I = RecursiveBindings.begin(),
+ E = RecursiveBindings.end();
+ I != E; ++I) {
+ Builder->addMatch(*I);
+ }
+}
+
+template <typename T>
+void BoundNodesTree::copyBindingsTo(
+ const T& Bindings, BoundNodesTreeBuilder* Builder) const {
+ for (typename T::const_iterator I = Bindings.begin(),
+ E = Bindings.end();
+ I != E; ++I) {
+ Builder->setBinding(*I);
+ }
+}
+
+void BoundNodesTree::visitMatches(Visitor* ResultVisitor) {
+ std::map<std::string, const clang::Decl*> AggregatedDeclBindings;
+ std::map<std::string, const clang::Stmt*> AggregatedStmtBindings;
+ visitMatchesRecursively(ResultVisitor, AggregatedDeclBindings,
+ AggregatedStmtBindings);
+}
+
+void BoundNodesTree::
+visitMatchesRecursively(Visitor* ResultVisitor,
+ std::map<std::string, const clang::Decl*>
+ AggregatedDeclBindings,
+ std::map<std::string, const clang::Stmt*>
+ AggregatedStmtBindings) {
+ copy(DeclBindings.begin(), DeclBindings.end(),
+ inserter(AggregatedDeclBindings, AggregatedDeclBindings.begin()));
+ copy(StmtBindings.begin(), StmtBindings.end(),
+ inserter(AggregatedStmtBindings, AggregatedStmtBindings.begin()));
+ if (RecursiveBindings.empty()) {
+ ResultVisitor->visitMatch(BoundNodes(AggregatedDeclBindings,
+ AggregatedStmtBindings));
+ } else {
+ for (unsigned I = 0; I < RecursiveBindings.size(); ++I) {
+ RecursiveBindings[I].visitMatchesRecursively(ResultVisitor,
+ AggregatedDeclBindings,
+ AggregatedStmtBindings);
+ }
+ }
+}
+
+BoundNodesTreeBuilder::BoundNodesTreeBuilder() {}
+
+void BoundNodesTreeBuilder::
+setBinding(const std::pair<const std::string, const clang::Decl*>& Binding) {
+ DeclBindings.insert(Binding);
+}
+
+void BoundNodesTreeBuilder::
+setBinding(const std::pair<const std::string, const clang::Stmt*>& Binding) {
+ StmtBindings.insert(Binding);
+}
+
+void BoundNodesTreeBuilder::addMatch(const BoundNodesTree& Bindings) {
+ RecursiveBindings.push_back(Bindings);
+}
+
+BoundNodesTree BoundNodesTreeBuilder::build() const {
+ return BoundNodesTree(DeclBindings, StmtBindings, RecursiveBindings);
+}
+
+} // end namespace internal
+} // end namespace ast_matchers
+} // end namespace clang
OpenPOWER on IntegriCloud