@@ -813,16 +813,18 @@ bool DpctFileInfo::isInAnalysisScope() {
813813 return DpctGlobalInfo::isInAnalysisScope (FilePath);
814814}
815815void DpctFileInfo::setFileEnterOffset (unsigned Offset) {
816- if (!HasInclusionDirective) {
817- FirstIncludeOffset = Offset;
816+ auto MF = DpctGlobalInfo::getInstance ().getMainFile ();
817+ if (!HasInclusionDirectiveSet.count (MF)) {
818+ FirstIncludeOffset[MF] = Offset;
818819 LastIncludeOffset = Offset;
819820 }
820821}
821822void DpctFileInfo::setFirstIncludeOffset (unsigned Offset) {
822- if (!HasInclusionDirective) {
823- FirstIncludeOffset = Offset;
823+ auto MF = DpctGlobalInfo::getInstance ().getMainFile ();
824+ if (!HasInclusionDirectiveSet.count (MF)) {
825+ FirstIncludeOffset[MF] = Offset;
824826 LastIncludeOffset = Offset;
825- HasInclusionDirective = true ;
827+ HasInclusionDirectiveSet. insert (MF) ;
826828 }
827829}
828830void DpctFileInfo::concatHeader (llvm::raw_string_ostream &OS) {}
@@ -853,9 +855,10 @@ void DpctFileInfo::insertHeader(HeaderType Type, unsigned Offset,
853855 ReplacementType IsForCodePin) {
854856 if (Type == HT_DPL_Algorithm || Type == HT_DPL_Execution || Type == HT_SYCL) {
855857 if (auto MF = DpctGlobalInfo::getInstance ().getMainFile ())
856- if (this != MF.get ())
858+ if (this != MF.get () && FirstIncludeOffset. count (MF)) {
857859 DpctGlobalInfo::getInstance ().getMainFile ()->insertHeader (
858- Type, FirstIncludeOffset);
860+ Type, FirstIncludeOffset.at (MF));
861+ }
859862 }
860863 if (HeaderInsertedBitMap[Type])
861864 return ;
@@ -877,8 +880,11 @@ void DpctFileInfo::insertHeader(HeaderType Type, unsigned Offset,
877880 case HT_DPL_Algorithm:
878881 case HT_DPL_Execution:
879882 concatHeader (OS, getHeaderSpelling (Type));
880- return insertHeader (OS.str (), FirstIncludeOffset,
881- InsertPosition::IP_AlwaysLeft);
883+ if (auto Iter = FirstIncludeOffset.find (
884+ DpctGlobalInfo::getInstance ().getMainFile ());
885+ Iter != FirstIncludeOffset.end ())
886+ insertHeader (OS.str (), Iter->second , InsertPosition::IP_AlwaysLeft);
887+ return ;
882888 case HT_SYCL:
883889 // Add the label for profiling macro "DPCT_PROFILING_ENABLED", which will be
884890 // replaced by "#define DPCT_PROFILING_ENABLED" or not in the post
@@ -924,7 +930,10 @@ void DpctFileInfo::insertHeader(HeaderType Type, unsigned Offset,
924930 << DpctGlobalInfo::getGlobalQueueName () << " ;" << getNL ();
925931 }
926932 }
927- insertHeader (OS.str (), FirstIncludeOffset, InsertPosition::IP_Left);
933+ if (auto Iter = FirstIncludeOffset.find (
934+ DpctGlobalInfo::getInstance ().getMainFile ());
935+ Iter != FirstIncludeOffset.end ())
936+ insertHeader (OS.str (), Iter->second , InsertPosition::IP_Left);
928937 if (!RTVersionValue.empty ())
929938 MigratedMacroDefinitionOS << " #define DPCT_COMPAT_RT_VERSION "
930939 << RTVersionValue << getNL ();
@@ -941,8 +950,11 @@ void DpctFileInfo::insertHeader(HeaderType Type, unsigned Offset,
941950 InsertPosition::IP_AlwaysLeft);
942951 for (const auto &File :
943952 DpctGlobalInfo::getCustomHelperFunctionAddtionalIncludes ()) {
944- insertHeader (" #include \" " + File + +" \" " + getNL (), FirstIncludeOffset,
945- InsertPosition::IP_Right);
953+ if (auto Iter = FirstIncludeOffset.find (
954+ DpctGlobalInfo::getInstance ().getMainFile ());
955+ Iter != FirstIncludeOffset.end ())
956+ insertHeader (" #include \" " + File + +" \" " + getNL (), Iter->second ,
957+ InsertPosition::IP_Right);
946958 }
947959 return ;
948960
@@ -994,14 +1006,20 @@ void DpctFileInfo::insertHeader(HeaderType Type, unsigned Offset,
9941006 }
9951007 SchemaRelativePath += " codepin_autogen_util.hpp\" " ;
9961008 concatHeader (OS, SchemaRelativePath);
997- return insertHeader (OS.str (), FirstIncludeOffset, InsertPosition::IP_Right,
998- IsForCodePin);
1009+ if (auto Iter = FirstIncludeOffset.find (
1010+ DpctGlobalInfo::getInstance ().getMainFile ());
1011+ Iter != FirstIncludeOffset.end ())
1012+ insertHeader (OS.str (), Iter->second , InsertPosition::IP_Right,
1013+ IsForCodePin);
1014+ return ;
9991015 } break ;
10001016 default :
10011017 break ;
10021018 }
10031019
1004- if (Offset != FirstIncludeOffset)
1020+ if (FirstIncludeOffset.count (DpctGlobalInfo::getInstance ().getMainFile ()) &&
1021+ Offset !=
1022+ FirstIncludeOffset.at (DpctGlobalInfo::getInstance ().getMainFile ()))
10051023 OS << getNL ();
10061024 concatHeader (OS, getHeaderSpelling (Type));
10071025 return insertHeader (OS.str (), LastIncludeOffset, InsertPosition::IP_Right);
0 commit comments