@@ -375,6 +375,19 @@ Optional<StringRef> CGDebugInfo::getSource(const SourceManager &SM,
375375 return Source;
376376}
377377
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+
378391llvm::DIFile *CGDebugInfo::getOrCreateFile (SourceLocation Loc) {
379392 SourceManager &SM = CGM.getContext ().getSourceManager ();
380393 StringRef FileName;
@@ -399,18 +412,6 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
399412
400413 // Cache the results.
401414 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-
414415 if (It != DIFileCache.end ()) {
415416 // Verify that the information still exists.
416417 if (llvm::Metadata *V = It->second )
@@ -420,15 +421,11 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
420421 SmallString<32 > Checksum;
421422
422423 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);
432429
433430 Optional<llvm::DIFile::ChecksumKind> CSKind = computeChecksum (FID, Checksum);
434431 Optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo;
@@ -560,14 +557,12 @@ void CGDebugInfo::CreateCompileUnit() {
560557 MainFileName =
561558 std::string (llvm::sys::path::remove_leading_dotslash (MainFileDirSS));
562559 } 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.
571566 MainFileName = FullMainFileName;
572567 }
573568 // If the main file name provided is identical to the input file name, and
0 commit comments