9
9
#include " UserDefinedRules/PatternRewriter.h"
10
10
#include " AnalysisInfo.h"
11
11
#include " Diagnostics/Diagnostics.h"
12
+ #include " FileGenerator/GenFiles.h"
12
13
#include " MigrateScript/MigrateCmakeScript.h"
13
14
#include " MigrateScript/MigratePythonBuildScript.h"
14
15
#include " UserDefinedRules/UserDefinedRules.h"
15
- #include " FileGenerator/GenFiles.h"
16
16
17
17
#include " llvm/ADT/StringRef.h"
18
18
#include " llvm/Support/Path.h"
25
25
#include < variant>
26
26
#include < vector>
27
27
28
-
29
28
namespace clang {
30
29
namespace dpct {
31
30
@@ -62,10 +61,28 @@ static bool isWhitespace(char Character) {
62
61
63
62
static bool isNotWhitespace (char Character) { return !isWhitespace (Character); }
64
63
64
+ static bool isLeftDelimiter (char Character) {
65
+ return Character == ' {' || Character == ' [' || Character == ' (' ;
66
+ }
67
+
65
68
static bool isRightDelimiter (char Character) {
66
69
return Character == ' }' || Character == ' ]' || Character == ' )' ;
67
70
}
68
71
72
+ static char getRightDelimiter (char Character) {
73
+ char rightChar = ' \0 ' ;
74
+
75
+ if (Character == ' {' ) {
76
+ rightChar = ' }' ;
77
+ } else if (Character == ' [' ) {
78
+ rightChar = ' ]' ;
79
+ } else if (Character == ' (' ) {
80
+ rightChar = ' )' ;
81
+ }
82
+
83
+ return rightChar;
84
+ }
85
+
69
86
static int detectIndentation (const std::string &Input, int Start) {
70
87
int Indentation = 0 ;
71
88
int Index = Start - 1 ;
@@ -239,11 +256,11 @@ findMatch(const MatchPattern &Pattern, const std::string &Input,
239
256
240
257
static int parseCodeElement (const MatchPattern &Suffix,
241
258
const std::string &Input, const int Start,
242
- RuleMatchMode Mode);
259
+ RuleMatchMode Mode, bool IsInsideBlock = false );
243
260
244
261
static int parseBlock (char LeftDelimiter, char RightDelimiter,
245
262
const std::string &Input, const int Start,
246
- RuleMatchMode Mode) {
263
+ RuleMatchMode Mode, const MatchPattern &Suffix = {} ) {
247
264
const int Size = Input.size ();
248
265
int Index = Start;
249
266
@@ -252,25 +269,21 @@ static int parseBlock(char LeftDelimiter, char RightDelimiter,
252
269
}
253
270
Index++;
254
271
255
- Index = parseCodeElement ({}, Input, Index, Mode);
256
- if (Index == -1 ) {
257
- return -1 ;
258
- }
272
+ Index = parseCodeElement (Suffix, Input, Index, Mode, true );
259
273
260
- if (Index >= Size || Input[ Index] != RightDelimiter ) {
274
+ if (Index == - 1 || Index >= Size ) {
261
275
return -1 ;
262
276
}
263
- Index++;
277
+
264
278
return Index;
265
279
}
266
280
267
281
static int parseCodeElement (const MatchPattern &Suffix,
268
282
const std::string &Input, const int Start,
269
- RuleMatchMode Mode) {
283
+ RuleMatchMode Mode, bool IsInsideBlock ) {
270
284
int Index = Start;
271
285
const int Size = Input.size ();
272
286
while (Index >= 0 && Index < Size) {
273
-
274
287
if (SrcFileType == SourceFileType::SFT_CMakeScript ||
275
288
SrcFileType == SourceFileType::SFT_PySetupScript) {
276
289
if (Input[Index] == ' #' ) {
@@ -281,11 +294,10 @@ static int parseCodeElement(const MatchPattern &Suffix,
281
294
}
282
295
283
296
const auto Character = Input[Index];
284
- if (SrcFileType != SourceFileType::SFT_PySetupScript) {
285
- if (Suffix.size () == 0 && Character == ' "' ) {
286
- return Index;
287
- }
297
+ if (Suffix.size () == 0 && Character == ' "' ) {
298
+ return Index;
288
299
}
300
+
289
301
if (Suffix.size () > 0 ) {
290
302
std::optional<MatchResult> SuffixMatch;
291
303
@@ -295,24 +307,29 @@ static int parseCodeElement(const MatchPattern &Suffix,
295
307
return Index;
296
308
}
297
309
298
- if (isRightDelimiter (Character) || Index == Size - 1 ) {
310
+ if (Index == Size - 1 ) {
299
311
return -1 ;
300
312
}
301
- }
302
313
303
- if (Character == ' { ' ) {
304
- Index = parseBlock ( ' { ' , ' } ' , Input, Index, Mode) ;
305
- continue ;
314
+ if (isRightDelimiter ( Character) ) {
315
+ return IsInsideBlock ? Index : - 1 ;
316
+ }
306
317
}
307
318
308
- if (Character == ' [' ) {
309
- Index = parseBlock (' [' , ' ]' , Input, Index, Mode);
310
- continue ;
311
- }
319
+ if (isLeftDelimiter (Character)) {
320
+ char RightDelimiter = getRightDelimiter (Character);
312
321
313
- if (Character == ' (' ) {
314
- Index = parseBlock (' (' , ' )' , Input, Index, Mode);
315
- continue ;
322
+ if (SrcFileType == SourceFileType::SFT_PySetupScript) {
323
+ Index =
324
+ parseBlock (Character, RightDelimiter, Input, Index, Mode, Suffix);
325
+ } else {
326
+ Index = parseBlock (Character, RightDelimiter, Input, Index, Mode);
327
+ }
328
+ if (Index != -1 && isRightDelimiter (Input[Index])) {
329
+ Index++;
330
+ continue ;
331
+ } else
332
+ return Index;
316
333
}
317
334
318
335
if (isRightDelimiter (Input[Index])) {
@@ -394,6 +411,7 @@ static int parseCodeElement(const MatchPattern &Suffix,
394
411
395
412
Index++;
396
413
}
414
+
397
415
return Suffix.size () == 0 ? Index : -1 ;
398
416
}
399
417
@@ -402,24 +420,43 @@ static int parseCodeElement(const MatchPattern &Suffix,
402
420
static bool isIdentifiedChar (char Char) {
403
421
404
422
if ((Char >= ' a' && Char <= ' z' ) || (Char >= ' A' && Char <= ' Z' ) ||
405
- (Char >= ' 0' && Char <= ' 9' ) || (Char == ' _' ) || (Char == ' - ' ) ) {
423
+ (Char >= ' 0' && Char <= ' 9' ) || (Char == ' _' )) {
406
424
return true ;
425
+ } else if (SrcFileType == SourceFileType::SFT_CMakeScript) {
426
+ if (Char == ' -' )
427
+ return true ;
407
428
}
408
429
409
430
return false ;
410
431
}
411
432
433
+ static bool isValidFilePrefix (char Char) {
434
+ return isIdentifiedChar (Char) || Char == ' .' || Char == ' /' || Char == ' \\ ' ;
435
+ }
436
+
437
+ static bool isValidFilePostfix (char Char) {
438
+ return !(isIdentifiedChar (Char) || Char == ' .' );
439
+ }
440
+
412
441
static void applyExtenstionNameChange (
413
442
const std::string &Input, size_t Next,
414
443
std::unordered_map<std::string, std::string> &Bindings,
415
444
const std::string &FileName, const clang::tooling::UnifiedPath &OutRoot,
416
445
std::string ExtensionType) {
446
+
447
+ // Check for valid postfix for a file name
448
+ if (!isValidFilePostfix (Input[Next + ExtensionType.length () + 1 ])) {
449
+ Bindings[" rewrite_extention_name" ] = std::move (ExtensionType);
450
+ return ;
451
+ }
452
+
417
453
size_t Pos = Next - 1 ;
418
- for (; Pos > 0 && !isWhitespace (Input[Pos]); Pos--) {
454
+ // Find the starting position of the file name
455
+ for (; Pos > 0 && isValidFilePrefix (Input[Pos]); Pos--) {
419
456
}
420
- Pos = Pos == 0 ? 0 : Pos + 1 ;
421
- if (Input[Pos] == ' " ' || Input[ Pos] == ' \' ' )
422
- Pos += 1 ;
457
+ if (! isValidFilePrefix (Input[ Pos]))
458
+ Pos++;
459
+
423
460
std::string SrcFile = Input.substr (Pos, Next + ExtensionType.length () +
424
461
1 /* strlen of "."*/ - Pos);
425
462
bool HasCudaSyntax = false ;
@@ -480,6 +517,14 @@ static void applyExtenstionNameChange(
480
517
HasCudaSyntax = true ;
481
518
break ;
482
519
}
520
+ } else if (llvm::sys::path::filename (FileName).ends_with (" .py" )) {
521
+ llvm::SmallString<512 > _SrcFile (SrcFile);
522
+ llvm::sys::path::native (_SrcFile);
523
+
524
+ if (llvm::StringRef (File).ends_with (_SrcFile)) {
525
+ HasCudaSyntax = true ;
526
+ break ;
527
+ }
483
528
} else {
484
529
// For other module files (e.g., .cmake files), just check the
485
530
// file names.
@@ -883,4 +928,4 @@ std::string applyPatternRewriter(const MetaRuleObject::PatternRewriter &PP,
883
928
}
884
929
885
930
} // namespace dpct
886
- } // namespace clang
931
+ } // namespace clang
0 commit comments