33#include < clang/Frontend/CompilerInstance.h>
44#include " core/transpiler_session/session_stage.h"
55#include " core/transpiler_session/transpiler_session.h"
6+ #include " oklt/util/io_helper.h"
67#include " util/string_utils.hpp"
78
89#include < algorithm>
@@ -73,10 +74,9 @@ std::optional<fs::path> getExternalInstrincisInclude(TranspilerSession& session,
7374 return std::nullopt ;
7475}
7576
76- tl::expected<std::unique_ptr<llvm::MemoryBuffer>, std::string> getExternalIntrinsicSource (
77- TargetBackend backend,
78- const fs::path& intrinsicPath,
79- clang::SourceManager& sm) {
77+ tl::expected<std::string, std::string> getExternalIntrinsicSource (TargetBackend backend,
78+ const fs::path& intrinsicPath,
79+ clang::SourceManager& sm) {
8080 auto implPathResult = getIntrincisImplSourcePath (backend, intrinsicPath);
8181 if (!implPathResult) {
8282 return tl::make_unexpected (implPathResult.error ());
@@ -93,119 +93,85 @@ tl::expected<std::unique_ptr<llvm::MemoryBuffer>, std::string> getExternalIntrin
9393 }
9494
9595 auto it = std::find_if (files.cbegin (), files.cend (), [](const fs::path& p) -> bool {
96- return p.extension ().string () == std::string (" .h " );
96+ return p.extension ().string () == std::string (" .cpp " );
9797 });
9898
9999 if (it == files.cend ()) {
100100 std::string error = " Can't' find implementation file with path: " + sourceFolder.string ();
101101 return tl::make_unexpected (error);
102102 }
103103
104- auto & fm = sm.getFileManager ();
105- auto backendFilePath = it->string ();
106- auto maybeReplacedFile = fm.getFile (backendFilePath);
107- if (!maybeReplacedFile) {
108- std::string error = " Can't open file: " + backendFilePath;
104+ auto contentResult = util::readFileAsStr (*it);
105+ if (!contentResult) {
106+ std::string error = " Can't get memory buffer for: " + it->string ();
109107 return tl::make_unexpected (error);
110108 }
111- auto maybeBuffer = fm.getBufferForFile (maybeReplacedFile.get ());
112- if (!maybeBuffer) {
113- std::string error = " Can't get memory buffer for: " + backendFilePath;
114- return tl::make_unexpected (error);
115- }
116- return std::move (maybeBuffer.get ());
109+ return contentResult.value ();
117110}
118111
119- bool overrideExternalIntrinsic (TranspilerSession& session,
112+ bool overrideExternalIntrinsic (SessionStage& stage,
113+ HeaderDepsInfo& deps,
120114 const std::string& includedFileName,
121- clang::OptionalFileEntryRef includedFile,
122- clang::SourceManager& sourceManager) {
115+ clang::OptionalFileEntryRef includedFile) {
116+ auto & session = stage.getSession ();
117+ auto & sourceManager = stage.getCompiler ().getSourceManager ();
123118 const auto & userIntrinsics = session.getInput ().userIntrinsics ;
124119 if (!userIntrinsics.empty ()) {
125120 auto maybeIntrinsicPath = getExternalInstrincisInclude (session, includedFileName);
126121 if (!maybeIntrinsicPath) {
127122 return false ;
128123 }
129124 auto intrinsicPath = maybeIntrinsicPath.value ();
130- auto infoResult =
131- getExternalIntrinsicSource (session. getInput (). backend , intrinsicPath, sourceManager);
132- if (!infoResult ) {
133- session.pushError (std::error_code (), infoResult .error ());
125+ auto intrinsicResult =
126+ getExternalIntrinsicSource (stage. getBackend () , intrinsicPath, sourceManager);
127+ if (!intrinsicResult ) {
128+ session.pushError (std::error_code (), intrinsicResult .error ());
134129 return false ;
135130 }
136- auto buffer = std::move (infoResult.value ());
137- auto & fm = sourceManager.getFileManager ();
131+ deps.externalIntrinsicsSources [includedFileName] = std::move (intrinsicResult.value ());
132+
133+ auto emptyExternalIntrinsic = MemoryBuffer::getMemBuffer (" " );
138134 if (includedFile) {
139135 auto fileRef = includedFile;
140136 const auto & fileEntry = fileRef->getFileEntry ();
141- sourceManager.overrideFileContents (&fileEntry, std::move (buffer ));
137+ sourceManager.overrideFileContents (&fileEntry, std::move (emptyExternalIntrinsic ));
142138 } else {
143139 // INFO: case when the file can be found by relative path
144140 // it happens when the include path is relative to WORKING DIR path
141+ auto & fm = sourceManager.getFileManager ();
145142 auto maybeFileRef = fm.getFileRef (includedFileName);
146143 if (maybeFileRef) {
147144 auto foundFileRef = maybeFileRef.get ();
148- sourceManager.overrideFileContents (foundFileRef, std::move (buffer ));
145+ sourceManager.overrideFileContents (foundFileRef, std::move (emptyExternalIntrinsic ));
149146 }
150147 }
151148 return true ;
152149 }
153150 return false ;
154151}
155152
156- void nullyLauncherExternalIntrinsics (TransformedFiles& inputs, SessionStage& stage) {
157- if (stage.getBackend () != TargetBackend::_LAUNCHER) {
158- return ;
159- }
160-
161- auto & session = stage.getSession ();
162- const auto & intrinsics = session.getInput ().userIntrinsics ;
163- if (intrinsics.empty ()) {
153+ void updateExternalIntrinsicMap (SessionStage& stage, HeaderDepsInfo& deps) {
154+ if (deps.externalIntrinsicsSources .empty ()) {
164155 return ;
165156 }
166157
167- for (auto & mappedFile : inputs.fileMap ) {
168- auto fileName = normalizedFileName (mappedFile.first );
169- for (const auto & includePath : intrinsics) {
170- auto folderPrefix = includePath.filename ().string ();
171- if (util::startsWith (fileName, folderPrefix)) {
172- mappedFile.second .clear ();
173- }
174- }
175- }
176- }
177-
178- void embedLauncherExternalIntrinsics (std::string& input,
179- const HeaderDepsInfo& info,
180- SessionStage& stage) {
181158 auto backend = stage.getBackend ();
182- if (backend != TargetBackend::_LAUNCHER) {
183- return ;
184- }
185-
186- const auto & usedIntrinsics = info.externalIntrinsics ;
187- if (usedIntrinsics.empty ()) {
188- return ;
189- }
190-
191- auto session = stage.getSession ();
159+ auto & session = stage.getSession ();
192160 auto & sm = stage.getCompiler ().getSourceManager ();
193- for (const auto & includeIntrinsic : usedIntrinsics ) {
194- auto maybeIntrinsicPath = getExternalInstrincisInclude (session, includeIntrinsic );
161+ for (auto & mappedIntrinsic : deps. externalIntrinsicsSources ) {
162+ auto maybeIntrinsicPath = getExternalInstrincisInclude (session, mappedIntrinsic. first );
195163 if (!maybeIntrinsicPath) {
196- std::string error = " Count not find implementation for " + includeIntrinsic ;
164+ std::string error = " Count not find implementation for " + mappedIntrinsic. first ;
197165 session.pushError (std::error_code (), error);
198166 return ;
199167 }
200168 auto intrinsicPath = maybeIntrinsicPath.value ();
201- auto infoResult = getExternalIntrinsicSource (backend, intrinsicPath, sm);
202- if (!infoResult ) {
203- session.pushError (std::error_code (), infoResult .error ());
169+ auto intrinsicResult = getExternalIntrinsicSource (backend, intrinsicPath, sm);
170+ if (!intrinsicResult ) {
171+ session.pushError (std::error_code (), intrinsicResult .error ());
204172 return ;
205173 }
206- auto buffer = std::move (infoResult.value ());
207- // INFO: make temporary buffer because pointer could be not null terminated
208- input.insert (0 , buffer->getBuffer ().str ());
174+ mappedIntrinsic.second = std::move (intrinsicResult.value ());
209175 }
210176}
211177} // namespace oklt
0 commit comments