systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
CallExprUtils.h
Go to the documentation of this file.
1#ifndef _CALL_EXPR_UTILS_H_
2#define _CALL_EXPR_UTILS_H_
3
4#undef DEBUG_TYPE
5#define DEBUG_TYPE "CallExprUtils"
6
7#include "clang/AST/DeclCXX.h"
8#include "clang/AST/ExprCXX.h"
9
10#include "clang/ASTMatchers/ASTMatchFinder.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
12#include "llvm/ADT/StringRef.h"
13#include <vector>
14
15using namespace clang::ast_matchers;
16
17/*
18namespace clang {
19class CallExpr;
20class Type;
21class FunctionDecl;
22class ValueDecl;
23class Expr;
24class CXXRecordDecl;
25class NamespaceDecl;
26
27}; // namespace clang
28*/
29
30namespace sc_ast_matchers {
31namespace utils {
32
33template <typename T>
34llvm::StringRef getClassNameFromDecl(T *decl) {
35 llvm::StringRef empty{"<NULL>"};
36 if (!decl) return empty;
37 const clang::DeclContext *dc = decl->getLexicalParent();
38 if (dc ) {
39 llvm::dbgs() << "@@@@@ Enter IF \n";
40 if (const auto *nd = llvm::dyn_cast<clang::NamespaceDecl>(dc)) {
41 clang::IdentifierInfo *iinfo = nd->getIdentifier();
42 llvm::dbgs() << "@@ name is " << iinfo->getName() << " for ";
43 return iinfo->getName();
44 }
45 } else {
46 llvm::dbgs() << "NOT NS\n";
47 }
48
49 return empty;
50}
51
52class NamespaceMatcher : public MatchFinder::MatchCallback {
53 private:
54 llvm::StringRef ns_name;
55 std::string func_name;
56
57 public:
58 llvm::StringRef getNamespaceName() { return ns_name; }
59 std::string getFunctionName() { return func_name; }
60
61 void registerMatchers(MatchFinder &finder) {
62 ns_name = "<NULL>";
63 func_name = "<NULL>";
64 // clang-format off
65 // auto member_matcher = cxxMemberCallExpr(callee(functionDecl().bind("ce_decl"))).bind("expr");
66 auto call_matcher = callExpr(callee(functionDecl().bind("ce_decl"))).bind("expr");
67 // clang-format on
68
69 // finder.addMatcher(member_matcher, this);
70 finder.addMatcher(call_matcher, this);
71 }
72
73 virtual void run(const MatchFinder::MatchResult &result) {
74 auto expr =
75 const_cast<clang::Expr *>(result.Nodes.getNodeAs<clang::Expr>("expr"));
76 auto ce_decl = const_cast<clang::FunctionDecl *>(
77 result.Nodes.getNodeAs<clang::FunctionDecl>("ce_decl"));
78
79 llvm::dbgs() << "@@@@@@## callExpr \n";
80
81 if (expr && ce_decl){
82 ce_decl->dump();
83 // wait() statements
84 if (auto mdecl = llvm::dyn_cast<clang::CXXMethodDecl>(ce_decl)) {
85 ns_name = getClassNameFromDecl(ce_decl->getParent());
86 llvm::dbgs() << "@@@@@ MDECL FUNC NAME " << mdecl->getNameAsString() << "\n";
87 func_name = mdecl->getNameAsString();
88 }
89 else {
90 // sc_min, sc_max, sc_abs
92 func_name = ce_decl->getNameAsString();
93 }
94 llvm::dbgs() << "@@@@@@ NAME " << ns_name << " func_name " << func_name << "\n";
95 }
96 }
97
98 void dump() {}
99};
100
101void dumpExprName(const clang::Expr *expr);
102
103/*
104 * \brief Check if a Type is declared within a given namespace.
105 */
106bool isInNamespace(const clang::Type *tp,
107 const std::vector<llvm::StringRef> &names);
108bool isInNamespace(const clang::Expr *expr,
109 const std::vector<llvm::StringRef> &names);
110
111bool isInNamespace(const clang::ValueDecl *fd,
112 const std::vector<llvm::StringRef> &names);
113
114// bool isInNamespace(const clang::CallExpr *cexpr, const
115// std::vector<llvm::StringRef>& names);
116void collect_sugar(const clang::Type *type,
117 std::vector<clang::Type *> &unwrapped_types);
118
119// llvm::StringRef getClassNameFromDecl( const clang::CXXRecordDecl* decl );
120// llvm::StringRef getClassNameFromDecl( const clang::FunctionDecl* decl );
121
122bool matchNames(llvm::StringRef str, const std::vector<llvm::StringRef> &names);
123/*
124 * \brief Check if a CallExpr is declared within a given namespace.
125 */
126bool isInNamespace(const clang::CallExpr *cexpr, llvm::StringRef name);
127
128} // namespace utils
129
130} // namespace sc_ast_matchers
131
132#endif
void registerMatchers(MatchFinder &finder)
virtual void run(const MatchFinder::MatchResult &result)
void dumpExprName(const Expr *expr)
void collect_sugar(const Type *type, std::vector< clang::Type * > &unwrapped_types)
bool matchNames(StringRef str, const std::vector< llvm::StringRef > &names)
bool isInNamespace(const clang::ValueDecl *fd, const std::vector< llvm::StringRef > &names)
llvm::StringRef getClassNameFromDecl(T *decl)