-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[Flang][runtime] Fix RENAME intrinsic, remove trailing blanks #159123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
664be1d
b20d0ab
6827079
8b60514
8e9305e
58cbad6
b5101da
14cbc44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,6 +15,8 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <cstdio> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <cstring> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include "../../include/flang-rt/runtime/descriptor.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace Fortran::runtime { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static RT_API_ATTRS void TransferImpl(Descriptor &result, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -60,13 +62,34 @@ void RTDEF(Rename)(const Descriptor &path1, const Descriptor &path2, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const Descriptor *status, const char *sourceFile, int line) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Terminator terminator{sourceFile, line}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #if !defined(RT_DEVICE_COMPILATION) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Get the raw strings (null-terminated) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| char *pathSrc{EnsureNullTerminated( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| path1.OffsetElement(), path1.ElementBytes(), terminator)}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| char *pathDst{EnsureNullTerminated( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| path2.OffsetElement(), path2.ElementBytes(), terminator)}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| char *srcFilePath = pathSrc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| char *dstFilePath = pathDst; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Trim trailing blanks (if string have not been null-terminated) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!IsNullTerminated(path1.OffsetElement(), path1.ElementBytes())) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auto srcTrimPos{TrimTrailingSpaces(pathSrc, path1.ElementBytes())}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| char *srcPathTrim{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static_cast<char *>(alloca((srcTrimPos + 1)))}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::memcpy(srcPathTrim, pathSrc, srcTrimPos); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| srcPathTrim[srcTrimPos] = '\0'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| srcFilePath = srcPathTrim; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!IsNullTerminated(path2.OffsetElement(), path2.ElementBytes())) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auto dstTrimPos{TrimTrailingSpaces(pathDst, path2.ElementBytes())}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| char *dstPathTrim{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static_cast<char *>(alloca((dstTrimPos + 1)))}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::memcpy(dstPathTrim, pathDst, dstTrimPos); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dstPathTrim[dstTrimPos] = '\0'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dstFilePath = dstPathTrim; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Get the raw strings (null-terminated) | |
| char *pathSrc{EnsureNullTerminated( | |
| path1.OffsetElement(), path1.ElementBytes(), terminator)}; | |
| char *pathDst{EnsureNullTerminated( | |
| path2.OffsetElement(), path2.ElementBytes(), terminator)}; | |
| char *srcFilePath = pathSrc; | |
| char *dstFilePath = pathDst; | |
| // Trim trailing blanks (if string have not been null-terminated) | |
| if (!IsNullTerminated(path1.OffsetElement(), path1.ElementBytes())) { | |
| auto srcTrimPos{TrimTrailingSpaces(pathSrc, path1.ElementBytes())}; | |
| char *srcPathTrim{ | |
| static_cast<char *>(alloca((srcTrimPos + 1)))}; | |
| std::memcpy(srcPathTrim, pathSrc, srcTrimPos); | |
| srcPathTrim[srcTrimPos] = '\0'; | |
| srcFilePath = srcPathTrim; | |
| } | |
| if (!IsNullTerminated(path2.OffsetElement(), path2.ElementBytes())) { | |
| auto dstTrimPos{TrimTrailingSpaces(pathDst, path2.ElementBytes())}; | |
| char *dstPathTrim{ | |
| static_cast<char *>(alloca((dstTrimPos + 1)))}; | |
| std::memcpy(dstPathTrim, pathDst, dstTrimPos); | |
| dstPathTrim[dstTrimPos] = '\0'; | |
| dstFilePath = dstPathTrim; | |
| } | |
| auto srcFilePath{SaveDefaultCharacter(path1.OffsetElement(), TrimTrailingSpaces(path1.OffsetElement(), path1.ElementBytes()), terminator)}; | |
| auto dstFilePath{SaveDefaultCharacter(path2.OffsetElement(), TrimTrailingSpaces(path2.OffsetElement(), path2.ElementBytes()), terminator)}; |
(then use srcFilePath.get() and dstFilePath.get() in rename call below).
Unformatted/untested.
You should get the same behavior because CHAR(0) is not a blank, so trimming will have no effect when this is the last character and no special handling for this case is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion! That is much better than what I had :-) and it seems to work for what I have tested.
Uh oh!
There was an error while loading. Please reload this page.