1212#include " Diagnostics/Diagnostics.h"
1313#include " RuleInfra/ExprAnalysis.h"
1414
15+ #include " llvm/Support/SaveAndRestore.h"
16+
1517namespace clang {
1618namespace dpct {
1719
@@ -154,7 +156,9 @@ class CallExprRewriter {
154156 // factories. As a result, the access modifiers of the constructors are
155157 // supposed to be protected instead of public.
156158 CallExprRewriter (const CallExpr *Call, StringRef SourceCalleeName)
157- : Call(Call), SourceCalleeName(SourceCalleeName) {}
159+ : Call(Call), SourceCalleeName(SourceCalleeName) {
160+ Analyzer.setCallSpelling (Call);
161+ }
158162 bool NoRewrite = false ;
159163
160164public:
@@ -163,7 +167,7 @@ class CallExprRewriter {
163167
164168 // / This function should be overwritten to implement call expression
165169 // / rewriting.
166- virtual std::optional<std::string> rewrite () = 0;
170+ virtual std::optional<std::string> rewrite (ExprAnalysis *Parent ) = 0;
167171 // Emits a warning/error/note and/or comment depending on MsgID. For details
168172 // see Diagnostics.inc, Diagnostics.h and Diagnostics.cpp
169173 template <typename IDTy, typename ... Ts>
@@ -183,13 +187,21 @@ class CallExprRewriter {
183187 return BlockLevelFormatFlag;
184188 }
185189
190+ static ExprAnalysis *getParentAnalysis () { return ParentAnalysis; }
191+
186192protected:
193+ struct ParentAnalysisGuard : llvm::SaveAndRestore<ExprAnalysis *> {
194+ ParentAnalysisGuard (ExprAnalysis *Parent)
195+ : llvm::SaveAndRestore<ExprAnalysis *>(ParentAnalysis, Parent) {}
196+ };
187197 bool BlockLevelFormatFlag = false ;
188198 std::vector<std::string> getMigratedArgs ();
189199 std::string getMigratedArg (unsigned Index);
190200 std::string getMigratedArgWithExtraParens (unsigned Index);
191201
192202 StringRef getSourceCalleeName () { return SourceCalleeName; }
203+
204+ static ExprAnalysis *ParentAnalysis;
193205};
194206
195207class ConditionalRewriterFactory : public CallExprRewriterFactoryBase {
@@ -339,8 +351,8 @@ class AssignableRewriter : public CallExprRewriter {
339351 requestFeature (HelperFeatureEnum::device_ext);
340352 }
341353
342- std::optional<std::string> rewrite () override {
343- std::optional<std::string> &&Result = Inner->rewrite ();
354+ std::optional<std::string> rewrite (ExprAnalysis *Analysis ) override {
355+ std::optional<std::string> &&Result = Inner->rewrite (Analysis );
344356 if (Result.has_value ()) {
345357 if ((CheckAssigned && IsAssigned) || (CheckInRetStmt && IsInRetStmt)) {
346358 if (UseDpctCheckError) {
@@ -372,8 +384,8 @@ class InsertAroundRewriter : public CallExprRewriter {
372384 : CallExprRewriter(C, " " ), Prefix(Prefix), Suffix(Suffix),
373385 Inner (InnerRewriter) {}
374386
375- std::optional<std::string> rewrite () override {
376- std::optional<std::string> &&Result = Inner->rewrite ();
387+ std::optional<std::string> rewrite (ExprAnalysis *Analysis ) override {
388+ std::optional<std::string> &&Result = Inner->rewrite (Analysis );
377389 if (Result.has_value ())
378390 return Prefix + Result.value () + Suffix;
379391 return Result;
@@ -391,7 +403,7 @@ class RemoveAPIRewriter : public CallExprRewriter {
391403 : CallExprRewriter(C, CalleeName), IsAssigned(isAssigned(C)),
392404 CalleeName (CalleeName), Message(Message) {}
393405
394- std::optional<std::string> rewrite () override {
406+ std::optional<std::string> rewrite (ExprAnalysis *Analysis ) override {
395407 std::string Msg =
396408 Message.empty () ? " this functionality is redundant in SYCL." : Message;
397409 if (IsAssigned) {
@@ -424,10 +436,10 @@ class IfElseRewriter : public CallExprRewriter {
424436 Indent = getIndent (getStmtExpansionSourceRange (C).getBegin (), SM);
425437 }
426438
427- std::optional<std::string> rewrite () override {
428- std::optional<std::string> &&PredStr = Pred->rewrite ();
429- std::optional<std::string> &&IfBlockStr = IfBlock->rewrite ();
430- std::optional<std::string> &&ElseBlockStr = ElseBlock->rewrite ();
439+ std::optional<std::string> rewrite (ExprAnalysis *Analysis ) override {
440+ std::optional<std::string> &&PredStr = Pred->rewrite (Analysis );
441+ std::optional<std::string> &&IfBlockStr = IfBlock->rewrite (Analysis );
442+ std::optional<std::string> &&ElseBlockStr = ElseBlock->rewrite (Analysis );
431443 return " if(" + PredStr.value () + " ){" + NL.str () + Indent.str () +
432444 Indent.str () + IfBlockStr.value () + " ;" + NL.str () +
433445 Indent.str () + " } else {" + NL.str () + Indent.str () + Indent.str () +
@@ -555,7 +567,7 @@ class FuncCallExprRewriter : public CallExprRewriter {
555567public:
556568 virtual ~FuncCallExprRewriter () {}
557569
558- virtual std::optional<std::string> rewrite () override ;
570+ virtual std::optional<std::string> rewrite (ExprAnalysis *Analysis ) override ;
559571
560572 friend FuncCallExprRewriterFactory;
561573
@@ -581,7 +593,7 @@ class NoRewriteFuncNameRewriter : public CallExprRewriter {
581593 NoRewrite = true ;
582594 }
583595
584- std::optional<std::string> rewrite () override { return NewFuncName; }
596+ std::optional<std::string> rewrite (ExprAnalysis *Analysis ) override { return NewFuncName; }
585597};
586598
587599struct ThrustFunctor {
@@ -1175,7 +1187,8 @@ template <class ArgT> class DeleterCallExprRewriter : public CallExprRewriter {
11751187 DeleterCallExprRewriter (const CallExpr *C, StringRef Source,
11761188 std::function<ArgT(const CallExpr *)> ArgCreator)
11771189 : CallExprRewriter(C, Source), Arg(ArgCreator(C)) {}
1178- std::optional<std::string> rewrite () override {
1190+ std::optional<std::string> rewrite (ExprAnalysis *Analysis) override {
1191+ ParentAnalysisGuard Guard (Analysis);
11791192 std::string Result;
11801193 llvm::raw_string_ostream OS (Result);
11811194 OS << " delete " ;
@@ -1191,7 +1204,8 @@ template <class ArgT> class ToStringExprRewriter : public CallExprRewriter {
11911204 ToStringExprRewriter (const CallExpr *C, StringRef Source,
11921205 std::function<ArgT(const CallExpr *)> ArgCreator)
11931206 : CallExprRewriter(C, Source), Arg(ArgCreator(C)) {}
1194- std::optional<std::string> rewrite () override {
1207+ std::optional<std::string> rewrite (ExprAnalysis *Analysis) override {
1208+ ParentAnalysisGuard Guard (Analysis);
11951209 std::string Result;
11961210 llvm::raw_string_ostream OS (Result);
11971211 print (OS, Arg);
@@ -1375,7 +1389,8 @@ class PrinterRewriter : Printer, public CallExprRewriter {
13751389 PrinterRewriter (const CallExpr *C, StringRef Source,
13761390 const std::function<ArgsT(const CallExpr *)> &...ArgCreators)
13771391 : PrinterRewriter(C, Source, ArgCreators(C)...) {}
1378- std::optional<std::string> rewrite () override {
1392+ std::optional<std::string> rewrite (ExprAnalysis *Analysis) override {
1393+ ParentAnalysisGuard Guard (Analysis);
13791394 std::string Result;
13801395 llvm::raw_string_ostream OS (Result);
13811396 Printer::print (OS);
@@ -1398,7 +1413,8 @@ class PrinterRewriter<MultiStmtsPrinter<StmtPrinters...>>
13981413 const CallExpr *C, StringRef Source,
13991414 const std::function<StmtPrinters(const CallExpr *)> &...PrinterCreators)
14001415 : PrinterRewriter(C, Source, PrinterCreators(C)...) {}
1401- std::optional<std::string> rewrite () override {
1416+ std::optional<std::string> rewrite (ExprAnalysis *Analysis) override {
1417+ ParentAnalysisGuard Guard (Analysis);
14021418 std::string Result;
14031419 llvm::raw_string_ostream OS (Result);
14041420 Base::print (OS);
@@ -1479,7 +1495,8 @@ class SimpleCallExprRewriter : public CallExprRewriter {
14791495 const std::function<CallExprPrinter<CalleeT, ArgsT...>(const CallExpr *)>
14801496 &PrinterFunctor)
14811497 : CallExprRewriter(C, Source), Printer(PrinterFunctor(C)) {}
1482- std::optional<std::string> rewrite () override {
1498+ std::optional<std::string> rewrite (ExprAnalysis *Analysis) override {
1499+ ParentAnalysisGuard Guard (Analysis);
14831500 std::string Result;
14841501 llvm::raw_string_ostream OS (Result);
14851502 Printer.print (OS);
@@ -1582,7 +1599,7 @@ class UnsupportFunctionRewriter : public CallExprRewriter {
15821599 report (MsgID, false , getMsgArg (Args, CE)...);
15831600 }
15841601
1585- std::optional<std::string> rewrite () override { return std::nullopt ; }
1602+ std::optional<std::string> rewrite (ExprAnalysis *Analysis ) override { return std::nullopt ; }
15861603
15871604 friend UnsupportFunctionRewriterFactory<MsgArgs...>;
15881605};
@@ -1609,7 +1626,7 @@ class UserDefinedRewriter : public CallExprRewriter {
16091626 buildRewriterStr (Call, OS, OB);
16101627 OS.flush ();
16111628 }
1612- std::optional<std::string> rewrite () override {
1629+ std::optional<std::string> rewrite (ExprAnalysis *Analysis ) override {
16131630 return ResultStr;
16141631 }
16151632
@@ -1701,7 +1718,7 @@ class UserDefinedRewriter : public CallExprRewriter {
17011718struct NullRewriter : public CallExprRewriter {
17021719 NullRewriter (const CallExpr *C, StringRef Name) : CallExprRewriter(C, Name) {}
17031720
1704- std::optional<std::string> rewrite () override { return std::nullopt ; }
1721+ std::optional<std::string> rewrite (ExprAnalysis *Analysis ) override { return std::nullopt ; }
17051722};
17061723
17071724struct NullRewriterFactory : public CallExprRewriterFactoryBase {
0 commit comments