Skip to content

Commit a3da58f

Browse files
authored
[SYCLomatic] Add ${method_base} keyword in user-defined rule (#2434)
In "${method_base} member_function", ${method_base} is used to represent the Object or pointer to the Object for the cxx member_function. Signed-off-by: Huang, Andy <[email protected]>
1 parent 739cce7 commit a3da58f

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

clang/lib/DPCT/RuleInfra/CallExprRewriter.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "Diagnostics/Diagnostics.h"
1313

14-
1514
namespace clang {
1615
namespace dpct {
1716

@@ -1683,6 +1682,34 @@ class UserDefinedRewriter : public CallExprRewriter {
16831682
OS << getTemplateArg(OB.ArgIndex)(Call);
16841683
return;
16851684
}
1685+
case (OutputBuilder::Kind::MethodBase): {
1686+
if (auto *MCE = llvm::dyn_cast<CXXMemberCallExpr>(Call)) {
1687+
if (auto *Callee = llvm::dyn_cast<MemberExpr>(MCE->getCallee())) {
1688+
auto &SM = DpctGlobalInfo::getSourceManager();
1689+
auto &Context = DpctGlobalInfo::getContext();
1690+
auto CallRange =
1691+
getDefinitionRange(Call->getBeginLoc(), Call->getEndLoc());
1692+
auto LastTokenLength = Lexer::MeasureTokenLength(
1693+
CallRange.getEnd(), SM, Context.getLangOpts());
1694+
auto Base = Callee->getBase();
1695+
std::string BaseStr;
1696+
if (isa<CXXThisExpr>(Callee->getBase())) {
1697+
BaseStr = "this";
1698+
} else {
1699+
BaseStr = getStringInRange(
1700+
Base->getSourceRange(), CallRange.getBegin(),
1701+
CallRange.getEnd().getLocWithOffset(LastTokenLength));
1702+
}
1703+
OS << BaseStr;
1704+
if (Callee->isArrow()) {
1705+
OS << "->";
1706+
} else {
1707+
OS << ".";
1708+
}
1709+
}
1710+
}
1711+
return;
1712+
}
16861713
}
16871714
DpctDebugs() << "[OutputBuilder::Kind] Unexpected value: " << OB.Kind
16881715
<< "\n";

clang/lib/DPCT/UserDefinedRules/UserDefinedRules.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "MigrationRuleManager.h"
1717
#include "RuleInfra/TypeLocRewriters.h"
1818
#include "Utility.h"
19+
#include "clang/ASTMatchers/ASTMatchers.h"
1920
#include "llvm/Support/YAMLTraits.h"
2021

2122
using namespace clang::ast_matchers;
@@ -591,6 +592,9 @@ OutputBuilder::consumeKeyword(std::string &OutStr, size_t &Idx) {
591592
ResultBuilder->Kind = Kind::TemplateArg;
592593
ResultBuilder->ArgIndex = consumeArgIndex(OutStr, Idx, "$template_arg");
593594
consumeRParen(OutStr, Idx, "$template_arg");
595+
} else if (OutStr.substr(Idx, 12) == "$method_base") {
596+
Idx += 12;
597+
ResultBuilder->Kind = Kind::MethodBase;
594598
} else {
595599
ResultBuilder->Kind = Kind::Arg;
596600
ResultBuilder->ArgIndex = consumeArgIndex(OutStr, Idx, "$");
@@ -738,12 +742,16 @@ void clang::dpct::UserDefinedClassFieldRule::runRule(
738742

739743
void clang::dpct::UserDefinedClassMethodRule::registerMatcher(
740744
clang::ast_matchers::MatchFinder &MF) {
741-
MF.addMatcher(cxxMemberCallExpr(
742-
allOf(on(hasType(hasCanonicalType(qualType(
743-
hasDeclaration(namedDecl(hasName(BaseName))))))),
744-
callee(cxxMethodDecl(hasName(MethodName)))))
745-
.bind("memberCallExpr"),
746-
this);
745+
MF.addMatcher(
746+
cxxMemberCallExpr(
747+
anyOf(allOf(on(hasType(hasCanonicalType(qualType(
748+
hasDeclaration(namedDecl(hasName(BaseName))))))),
749+
callee(cxxMethodDecl(hasName(MethodName)))),
750+
allOf(on(hasType(pointerType(pointee(hasCanonicalType(qualType(
751+
hasDeclaration(namedDecl(hasName(BaseName))))))))),
752+
callee(cxxMethodDecl(hasName(MethodName))))))
753+
.bind("memberCallExpr"),
754+
this);
747755
}
748756

749757
void clang::dpct::UserDefinedClassMethodRule::runRule(

clang/lib/DPCT/UserDefinedRules/UserDefinedRules.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ class OutputBuilder {
347347
TypeName,
348348
AddrOf,
349349
DerefedTypeName,
350-
TemplateArg
350+
TemplateArg,
351+
MethodBase
351352
};
352353
std::string RuleName;
353354
clang::tooling::UnifiedPath RuleFile;

clang/test/dpct/user_defined_rule.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
Out: fieldD
6666
Methods:
6767
- In: methodA
68-
Out: a.methodB($2)
68+
Out: $method_base methodB($2)
6969
- In: methodC
7070
Out: methodD
7171
- Rule: rule_Fruit

0 commit comments

Comments
 (0)