@@ -375,6 +375,19 @@ Optional<StringRef> CGDebugInfo::getSource(const SourceManager &SM,
375
375
return Source;
376
376
}
377
377
378
+ // Compute valid FID for FileName.
379
+ FileID ComputeValidFileID (SourceManager &SM, StringRef FileName) {
380
+ FileID MainFileID = SM.getMainFileID ();
381
+ // Find the filename FileName and load it.
382
+ llvm::Expected<FileEntryRef> ExpectedFileRef =
383
+ SM.getFileManager ().getFileRef (FileName);
384
+ if (ExpectedFileRef) {
385
+ MainFileID = SM.getOrCreateFileID (ExpectedFileRef.get (),
386
+ SrcMgr::CharacteristicKind::C_User);
387
+ }
388
+ return MainFileID;
389
+ }
390
+
378
391
llvm::DIFile *CGDebugInfo::getOrCreateFile (SourceLocation Loc) {
379
392
SourceManager &SM = CGM.getContext ().getSourceManager ();
380
393
StringRef FileName;
@@ -399,18 +412,6 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
399
412
400
413
// Cache the results.
401
414
auto It = DIFileCache.find (FileName.data ());
402
- if (SM.getFileEntryForID (SM.getMainFileID ()) &&
403
- CGM.getCodeGenOpts ().SYCLUseMainFileName && FID.isInvalid () &&
404
- llvm::sys::path::is_absolute (FileName)) {
405
- FileID MainFileID = SM.getMainFileID ();
406
- auto ExpectedFileRef = SM.getFileManager ().getFileRef (FileName);
407
- if (ExpectedFileRef) {
408
- MainFileID = SM.getOrCreateFileID (ExpectedFileRef.get (),
409
- SrcMgr::CharacteristicKind::C_User);
410
- FID = MainFileID;
411
- }
412
- }
413
-
414
415
if (It != DIFileCache.end ()) {
415
416
// Verify that the information still exists.
416
417
if (llvm::Metadata *V = It->second )
@@ -420,15 +421,11 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
420
421
SmallString<32 > Checksum;
421
422
422
423
if (SM.getFileEntryForID (SM.getMainFileID ()) &&
423
- CGM.getCodeGenOpts ().SYCLUseMainFileName && FID.isInvalid ()) {
424
- FileID MainFileID = SM.getMainFileID ();
425
- auto ExpectedFileRef = SM.getFileManager ().getFileRef (FileName);
426
- if (ExpectedFileRef) {
427
- MainFileID = SM.getOrCreateFileID (ExpectedFileRef.get (),
428
- SrcMgr::CharacteristicKind::C_User);
429
- FID = MainFileID;
430
- }
431
- }
424
+ CGM.getCodeGenOpts ().SYCLUseMainFileName && FID.isInvalid ())
425
+ // When an integration footer is involved, the main file is a temporary
426
+ // file generated by the compiler. FileName is pointing to original user
427
+ // source file. We use it here to properly calculate its checksum.
428
+ FID = ComputeValidFileID (SM, FileName);
432
429
433
430
Optional<llvm::DIFile::ChecksumKind> CSKind = computeChecksum (FID, Checksum);
434
431
Optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo;
@@ -560,14 +557,12 @@ void CGDebugInfo::CreateCompileUnit() {
560
557
MainFileName =
561
558
std::string (llvm::sys::path::remove_leading_dotslash (MainFileDirSS));
562
559
} else if (CGM.getCodeGenOpts ().SYCLUseMainFileName ) {
563
- // When integration footer is involved, main file is a temporary file
564
- // generated by the compiler, but -main-file-name is expected to contain
565
- // an absolute path to the original user-provided source file. We use it
566
- // here to properly calculate its checksum.
567
- auto ExpectedFileRef = SM.getFileManager ().getFileRef (FullMainFileName);
568
- if (ExpectedFileRef)
569
- MainFileID = SM.getOrCreateFileID (ExpectedFileRef.get (),
570
- SrcMgr::CharacteristicKind::C_User);
560
+ // When an integration footer is involved, the main file is a temporary
561
+ // file generated by the compiler. FullMainFileName is pointing to
562
+ // original user source file. We use it here to properly calculate its
563
+ // checksum.
564
+ MainFileID = ComputeValidFileID (SM, FullMainFileName);
565
+ // Make sure the filename points to the original user source filename.
571
566
MainFileName = FullMainFileName;
572
567
}
573
568
// If the main file name provided is identical to the input file name, and
0 commit comments