Skip to content

Commit c8e2e00

Browse files
Yi Wuyiwu0b11
authored andcommitted
add test fixes on Windows and remove address_of in testing
1 parent f7133e4 commit c8e2e00

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

flang/runtime/execute.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ int TerminationCheck(int status, const Descriptor *command,
120120
if (exitStatusVal == 127 || exitStatusVal == 126) {
121121
#endif
122122
if (!cmdstat) {
123-
terminator.Crash("\'%s\' not found with exit status code: %d",
124-
command->OffsetElement(), exitStatusVal);
123+
terminator.Crash(
124+
"Invalid command quit with exit status code: %d", exitStatusVal);
125125
} else {
126126
CheckAndStoreIntToDescriptor(cmdstat, INVALID_CL_ERR, terminator);
127127
CopyToDescriptor(*cmdmsg, "Invalid command line", 20);
@@ -186,27 +186,25 @@ void RTNAME(ExecuteCommandLine)(const Descriptor *command, bool wait,
186186
STARTUPINFO si;
187187
PROCESS_INFORMATION pi;
188188
ZeroMemory(&si, sizeof(si));
189-
si.cb{sizeof(si)};
189+
si.cb = sizeof(si);
190190
ZeroMemory(&pi, sizeof(pi));
191191

192192
// append "cmd.exe /c " to the beginning of command
193193
const char *cmd{command->OffsetElement()};
194194
const char *prefix{"cmd.exe /c "};
195-
char *newCmd{(char *)malloc(strlen(prefix) + strlen(cmd) + 1)};
195+
char *newCmd{(char *)malloc(std::strlen(prefix) + std::strlen(cmd) + 1)};
196196
if (newCmd != NULL) {
197197
std::strcpy(newCmd, prefix);
198198
std::strcat(newCmd, cmd);
199199
} else {
200200
terminator.Crash("Memory allocation failed for newCmd");
201201
}
202202

203-
// Convert the narrow string to a wide string
204-
int sizeNeede{MultiByteToWideChar(CP_UTF8, 0, newCmd, -1, NULL, 0)};
203+
// Convert the char to wide char
204+
const size_t sizeNeeded{mbstowcs(NULL, newCmd, 0) + 1};
205205
wchar_t *wcmd{new wchar_t[sizeNeeded]};
206-
if (MultiByteToWideChar(CP_UTF8, 0, newCmd, -1, wcmd, sizeNeeded) == 0) {
207-
terminator.Crash(
208-
"Char to wider char conversion failed with error code: %lu.",
209-
GetLastError());
206+
if (std::mbstowcs(wcmd, newCmd, sizeNeeded) == static_cast<size_t>(-1)) {
207+
terminator.Crash("Char to wide char failed for newCmd");
210208
}
211209
free(newCmd);
212210

@@ -220,7 +218,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor *command, bool wait,
220218
"CreateProcess failed with error code: %lu.", GetLastError());
221219
} else {
222220
StoreIntToDescriptor(cmdstat, (uint32_t)GetLastError(), terminator);
223-
CheckAndCopyToDescriptor(*cmdmsg, "CreateProcess failed.", 21);
221+
CheckAndCopyToDescriptor(cmdmsg, "CreateProcess failed.", 21);
224222
}
225223
}
226224
delete[] wcmd;

flang/test/Lower/Intrinsics/execute_command_line-optional.f90

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,11 @@ subroutine all_args_optional(command, isWait, exitVal, cmdVal, msg)
4141
! CHECK-NEXT: %20 = fir.embox %7 typeparams %6#1 : (!fir.ref<!fir.char<1,?>>, index) -> !fir.box<!fir.char<1,?>>
4242
! CHECK-NEXT: %21 = fir.absent !fir.box<!fir.char<1,?>>
4343
! CHECK-NEXT: %22 = arith.select %12, %20, %21 : !fir.box<!fir.char<1,?>>
44-
! CHECK-NEXT: %23 = fir.address_of(@_QQclX76c8fd75e0e20222cfcde5fe9055bcbe) : !fir.ref<!fir.char<1,96>>
45-
! CHECK-NEXT: %24 = fir.convert %13 : (!fir.box<!fir.char<1,?>>) -> !fir.box<none>
44+
! CHECK: %24 = fir.convert %13 : (!fir.box<!fir.char<1,?>>) -> !fir.box<none>
4645
! CHECK-NEXT: %25 = fir.convert %14 : (!fir.logical<4>) -> i1
4746
! CHECK-NEXT: %26 = fir.convert %17 : (!fir.box<i32>) -> !fir.box<none>
4847
! CHECK-NEXT: %27 = fir.convert %19 : (!fir.box<i32>) -> !fir.box<none>
4948
! CHECK-NEXT: %28 = fir.convert %22 : (!fir.box<!fir.char<1,?>>) -> !fir.box<none>
50-
! CHECK-NEXT: %29 = fir.convert %23 : (!fir.ref<!fir.char<1,96>>) -> !fir.ref<i8>
51-
! CHECK-NEXT: %30 = fir.call @_FortranAExecuteCommandLine(%24, %25, %26, %27, %28, %29, %c14_i32) fastmath<contract> : (!fir.box<none>, i1, !fir.box<none>, !fir.box<none>, !fir.box<none>, !fir.ref<i8>, i32) -> none
49+
! CHECK: %30 = fir.call @_FortranAExecuteCommandLine(%24, %25, %26, %27, %28, %29, %c14_i32) fastmath<contract> : (!fir.box<none>, i1, !fir.box<none>, !fir.box<none>, !fir.box<none>, !fir.ref<i8>, i32) -> none
5250
! CHECK-NEXT: return
5351
end subroutine all_args_optional

flang/test/Lower/Intrinsics/execute_command_line.f90

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@ subroutine all_args(command, isWait, exitVal, cmdVal, msg)
2525
! CHECK-NEXT: %[[exitstatBox:.*]] = fir.embox %4 : (!fir.ref<i32>) -> !fir.box<i32>
2626
! CHECK-NEXT: %[[cmdstatBox:.*]] = fir.embox %0 : (!fir.ref<i32>) -> !fir.box<i32>
2727
! CHECK-NEXT: %[[cmdmsgBox:.*]] = fir.embox %8 : (!fir.ref<!fir.char<1,30>>) -> !fir.box<!fir.char<1,30>>
28-
! CHECK-NEXT: %14 = fir.address_of(@_QQclX05b779b120ed6fd4f4d4144c6c9136d6) : !fir.ref<!fir.char<1,87>>
29-
! CHECK-NEXT: %[[command:.*]] = fir.convert %[[commandBox]] : (!fir.box<!fir.char<1,30>>) -> !fir.box<none>
28+
! CHECK: %[[command:.*]] = fir.convert %[[commandBox]] : (!fir.box<!fir.char<1,30>>) -> !fir.box<none>
3029
! CHECK-NEXT: %[[wait:.*]] = fir.convert %[[waitLoaded]] : (!fir.logical<4>) -> i1
3130
! CHECK-NEXT: %[[exitstat:.*]] = fir.convert %[[exitstatBox]] : (!fir.box<i32>) -> !fir.box<none>
3231
! CHECK-NEXT: %[[cmdstat:.*]] = fir.convert %[[cmdstatBox]] : (!fir.box<i32>) -> !fir.box<none>
3332
! CHECK-NEXT: %[[cmdmsg:.*]] = fir.convert %[[cmdmsgBox]] : (!fir.box<!fir.char<1,30>>) -> !fir.box<none>
34-
! CHECK-NEXT: %20 = fir.convert %14 : (!fir.ref<!fir.char<1,87>>) -> !fir.ref<i8>
35-
! CHECK-NEXT: %21 = fir.call @_FortranAExecuteCommandLine(%[[command]], %[[wait]], %[[exitstat]], %[[cmdstat]], %[[cmdmsg]], %20, %c13_i32) fastmath<contract> : (!fir.box<none>, i1, !fir.box<none>, !fir.box<none>, !fir.box<none>, !fir.ref<i8>, i32) -> none
33+
! CHECK: %21 = fir.call @_FortranAExecuteCommandLine(%[[command]], %[[wait]], %[[exitstat]], %[[cmdstat]], %[[cmdmsg]], %20, %c13_i32) fastmath<contract> : (!fir.box<none>, i1, !fir.box<none>, !fir.box<none>, !fir.box<none>, !fir.ref<i8>, i32) -> none
3634
! CHECK-NEXT: return
3735
end subroutine all_args
3836

@@ -46,9 +44,7 @@ subroutine only_command_default_wait_true(command)
4644
! CHECK-NEXT: %2 = fir.declare %1 typeparams %c30 {uniq_name = "_QFonly_command_default_wait_trueEcommand"} : (!fir.ref<!fir.char<1,30>>, index) -> !fir.ref<!fir.char<1,30>>
4745
! CHECK-NEXT: %3 = fir.embox %2 : (!fir.ref<!fir.char<1,30>>) -> !fir.box<!fir.char<1,30>>
4846
! CHECK-NEXT: %4 = fir.absent !fir.box<none>
49-
! CHECK-NEXT: %5 = fir.address_of(@_QQclX05b779b120ed6fd4f4d4144c6c9136d6) : !fir.ref<!fir.char<1,87>>
50-
! CHECK-NEXT: %6 = fir.convert %3 : (!fir.box<!fir.char<1,30>>) -> !fir.box<none>
51-
! CHECK-NEXT: %7 = fir.convert %5 : (!fir.ref<!fir.char<1,87>>) -> !fir.ref<i8>
52-
! CHECK-NEXT: %8 = fir.call @_FortranAExecuteCommandLine(%6, %true, %4, %4, %4, %7, %c43_i32) fastmath<contract> : (!fir.box<none>, i1, !fir.box<none>, !fir.box<none>, !fir.box<none>, !fir.ref<i8>, i32) -> none
47+
! CHECK: %6 = fir.convert %3 : (!fir.box<!fir.char<1,30>>) -> !fir.box<none>
48+
! CHECK: %8 = fir.call @_FortranAExecuteCommandLine(%6, %true, %4, %4, %4, %7, %c41_i32) fastmath<contract> : (!fir.box<none>, i1, !fir.box<none>, !fir.box<none>, !fir.box<none>, !fir.ref<i8>, i32) -> none
5349
! CHECK-NEXT: return
5450
end subroutine only_command_default_wait_true

flang/unittests/Runtime/CommandTest.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,23 +279,31 @@ TEST_F(ZeroArguments, ECLInvalidCommandErrorSync) {
279279

280280
RTNAME(ExecuteCommandLine)
281281
(command.get(), wait, exitStat.get(), cmdStat.get(), cmdMsg.get());
282-
282+
#ifdef _WIN32
283+
CheckDescriptorEqInt(exitStat.get(), 1);
284+
#else
283285
CheckDescriptorEqInt(exitStat.get(), 127);
286+
#endif
284287
CheckDescriptorEqInt(cmdStat.get(), 3);
285288
CheckDescriptorEqStr(cmdMsg.get(), "Invalid command lineXXXX");
286289
}
287290

288291
TEST_F(ZeroArguments, ECLInvalidCommandTerminatedSync) {
289292
OwningPtr<Descriptor> command{CharDescriptor("InvalidCommand")};
290293
bool wait{true};
291-
OwningPtr<Descriptor> exitStat{EmptyIntDescriptor()};
294+
OwningPtr<Descriptor> exitStat{IntDescriptor(404)};
292295
OwningPtr<Descriptor> cmdMsg{CharDescriptor("No Change")};
293296

297+
#ifdef _WIN32
294298
EXPECT_DEATH(RTNAME(ExecuteCommandLine)(
295299
command.get(), wait, exitStat.get(), nullptr, cmdMsg.get()),
296-
"'InvalidCommand' not found with exit status code: 127");
297-
298-
CheckDescriptorEqInt(exitStat.get(), 0);
300+
"Invalid command quit with exit status code: 1");
301+
#else
302+
EXPECT_DEATH(RTNAME(ExecuteCommandLine)(
303+
command.get(), wait, exitStat.get(), nullptr, cmdMsg.get()),
304+
"Invalid command quit with exit status code: 127");
305+
#endif
306+
CheckDescriptorEqInt(exitStat.get(), 404);
299307
CheckDescriptorEqStr(cmdMsg.get(), "No Change");
300308
}
301309

0 commit comments

Comments
 (0)