12
12
#include " Diagnostics/Diagnostics.h"
13
13
#include " RuleInfra/ExprAnalysis.h"
14
14
15
+ #include " llvm/Support/SaveAndRestore.h"
16
+
15
17
namespace clang {
16
18
namespace dpct {
17
19
@@ -154,7 +156,9 @@ class CallExprRewriter {
154
156
// factories. As a result, the access modifiers of the constructors are
155
157
// supposed to be protected instead of public.
156
158
CallExprRewriter (const CallExpr *Call, StringRef SourceCalleeName)
157
- : Call(Call), SourceCalleeName(SourceCalleeName) {}
159
+ : Call(Call), SourceCalleeName(SourceCalleeName) {
160
+ Analyzer.setCallSpelling (Call);
161
+ }
158
162
bool NoRewrite = false ;
159
163
160
164
public:
@@ -163,7 +167,7 @@ class CallExprRewriter {
163
167
164
168
// / This function should be overwritten to implement call expression
165
169
// / rewriting.
166
- virtual std::optional<std::string> rewrite () = 0;
170
+ virtual std::optional<std::string> rewrite (ExprAnalysis *Parent ) = 0;
167
171
// Emits a warning/error/note and/or comment depending on MsgID. For details
168
172
// see Diagnostics.inc, Diagnostics.h and Diagnostics.cpp
169
173
template <typename IDTy, typename ... Ts>
@@ -183,13 +187,21 @@ class CallExprRewriter {
183
187
return BlockLevelFormatFlag;
184
188
}
185
189
190
+ static ExprAnalysis *getParentAnalysis () { return ParentAnalysis; }
191
+
186
192
protected:
193
+ struct ParentAnalysisGuard : llvm::SaveAndRestore<ExprAnalysis *> {
194
+ ParentAnalysisGuard (ExprAnalysis *Parent)
195
+ : llvm::SaveAndRestore<ExprAnalysis *>(ParentAnalysis, Parent) {}
196
+ };
187
197
bool BlockLevelFormatFlag = false ;
188
198
std::vector<std::string> getMigratedArgs ();
189
199
std::string getMigratedArg (unsigned Index);
190
200
std::string getMigratedArgWithExtraParens (unsigned Index);
191
201
192
202
StringRef getSourceCalleeName () { return SourceCalleeName; }
203
+
204
+ static ExprAnalysis *ParentAnalysis;
193
205
};
194
206
195
207
class ConditionalRewriterFactory : public CallExprRewriterFactoryBase {
@@ -339,8 +351,8 @@ class AssignableRewriter : public CallExprRewriter {
339
351
requestFeature (HelperFeatureEnum::device_ext);
340
352
}
341
353
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 );
344
356
if (Result.has_value ()) {
345
357
if ((CheckAssigned && IsAssigned) || (CheckInRetStmt && IsInRetStmt)) {
346
358
if (UseDpctCheckError) {
@@ -372,8 +384,8 @@ class InsertAroundRewriter : public CallExprRewriter {
372
384
: CallExprRewriter(C, " " ), Prefix(Prefix), Suffix(Suffix),
373
385
Inner (InnerRewriter) {}
374
386
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 );
377
389
if (Result.has_value ())
378
390
return Prefix + Result.value () + Suffix;
379
391
return Result;
@@ -391,7 +403,7 @@ class RemoveAPIRewriter : public CallExprRewriter {
391
403
: CallExprRewriter(C, CalleeName), IsAssigned(isAssigned(C)),
392
404
CalleeName (CalleeName), Message(Message) {}
393
405
394
- std::optional<std::string> rewrite () override {
406
+ std::optional<std::string> rewrite (ExprAnalysis *Analysis ) override {
395
407
std::string Msg =
396
408
Message.empty () ? " this functionality is redundant in SYCL." : Message;
397
409
if (IsAssigned) {
@@ -424,10 +436,10 @@ class IfElseRewriter : public CallExprRewriter {
424
436
Indent = getIndent (getStmtExpansionSourceRange (C).getBegin (), SM);
425
437
}
426
438
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 );
431
443
return " if(" + PredStr.value () + " ){" + NL.str () + Indent.str () +
432
444
Indent.str () + IfBlockStr.value () + " ;" + NL.str () +
433
445
Indent.str () + " } else {" + NL.str () + Indent.str () + Indent.str () +
@@ -555,7 +567,7 @@ class FuncCallExprRewriter : public CallExprRewriter {
555
567
public:
556
568
virtual ~FuncCallExprRewriter () {}
557
569
558
- virtual std::optional<std::string> rewrite () override ;
570
+ virtual std::optional<std::string> rewrite (ExprAnalysis *Analysis ) override ;
559
571
560
572
friend FuncCallExprRewriterFactory;
561
573
@@ -581,7 +593,7 @@ class NoRewriteFuncNameRewriter : public CallExprRewriter {
581
593
NoRewrite = true ;
582
594
}
583
595
584
- std::optional<std::string> rewrite () override { return NewFuncName; }
596
+ std::optional<std::string> rewrite (ExprAnalysis *Analysis ) override { return NewFuncName; }
585
597
};
586
598
587
599
struct ThrustFunctor {
@@ -1175,7 +1187,8 @@ template <class ArgT> class DeleterCallExprRewriter : public CallExprRewriter {
1175
1187
DeleterCallExprRewriter (const CallExpr *C, StringRef Source,
1176
1188
std::function<ArgT(const CallExpr *)> ArgCreator)
1177
1189
: 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);
1179
1192
std::string Result;
1180
1193
llvm::raw_string_ostream OS (Result);
1181
1194
OS << " delete " ;
@@ -1191,7 +1204,8 @@ template <class ArgT> class ToStringExprRewriter : public CallExprRewriter {
1191
1204
ToStringExprRewriter (const CallExpr *C, StringRef Source,
1192
1205
std::function<ArgT(const CallExpr *)> ArgCreator)
1193
1206
: 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);
1195
1209
std::string Result;
1196
1210
llvm::raw_string_ostream OS (Result);
1197
1211
print (OS, Arg);
@@ -1375,7 +1389,8 @@ class PrinterRewriter : Printer, public CallExprRewriter {
1375
1389
PrinterRewriter (const CallExpr *C, StringRef Source,
1376
1390
const std::function<ArgsT(const CallExpr *)> &...ArgCreators)
1377
1391
: 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);
1379
1394
std::string Result;
1380
1395
llvm::raw_string_ostream OS (Result);
1381
1396
Printer::print (OS);
@@ -1398,7 +1413,8 @@ class PrinterRewriter<MultiStmtsPrinter<StmtPrinters...>>
1398
1413
const CallExpr *C, StringRef Source,
1399
1414
const std::function<StmtPrinters(const CallExpr *)> &...PrinterCreators)
1400
1415
: 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);
1402
1418
std::string Result;
1403
1419
llvm::raw_string_ostream OS (Result);
1404
1420
Base::print (OS);
@@ -1479,7 +1495,8 @@ class SimpleCallExprRewriter : public CallExprRewriter {
1479
1495
const std::function<CallExprPrinter<CalleeT, ArgsT...>(const CallExpr *)>
1480
1496
&PrinterFunctor)
1481
1497
: 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);
1483
1500
std::string Result;
1484
1501
llvm::raw_string_ostream OS (Result);
1485
1502
Printer.print (OS);
@@ -1582,7 +1599,7 @@ class UnsupportFunctionRewriter : public CallExprRewriter {
1582
1599
report (MsgID, false , getMsgArg (Args, CE)...);
1583
1600
}
1584
1601
1585
- std::optional<std::string> rewrite () override { return std::nullopt; }
1602
+ std::optional<std::string> rewrite (ExprAnalysis *Analysis ) override { return std::nullopt; }
1586
1603
1587
1604
friend UnsupportFunctionRewriterFactory<MsgArgs...>;
1588
1605
};
@@ -1609,7 +1626,7 @@ class UserDefinedRewriter : public CallExprRewriter {
1609
1626
buildRewriterStr (Call, OS, OB);
1610
1627
OS.flush ();
1611
1628
}
1612
- std::optional<std::string> rewrite () override {
1629
+ std::optional<std::string> rewrite (ExprAnalysis *Analysis ) override {
1613
1630
return ResultStr;
1614
1631
}
1615
1632
@@ -1701,7 +1718,7 @@ class UserDefinedRewriter : public CallExprRewriter {
1701
1718
struct NullRewriter : public CallExprRewriter {
1702
1719
NullRewriter (const CallExpr *C, StringRef Name) : CallExprRewriter(C, Name) {}
1703
1720
1704
- std::optional<std::string> rewrite () override { return std::nullopt; }
1721
+ std::optional<std::string> rewrite (ExprAnalysis *Analysis ) override { return std::nullopt; }
1705
1722
};
1706
1723
1707
1724
struct NullRewriterFactory : public CallExprRewriterFactoryBase {
0 commit comments