From d4fd0c7eca99ed2c22400d819388168e830ce529 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 3 Oct 2020 15:39:33 +0200 Subject: [PATCH] windows/mkwinsyscall: simplify generated code for booleans Signed-off-by: Sebastiaan van Stijn --- windows/mkwinsyscall/mkwinsyscall.go | 22 ++++------ windows/zsyscall_windows.go | 62 ---------------------------- 2 files changed, 8 insertions(+), 76 deletions(-) diff --git a/windows/mkwinsyscall/mkwinsyscall.go b/windows/mkwinsyscall/mkwinsyscall.go index 392ce1bb..65bb0368 100644 --- a/windows/mkwinsyscall/mkwinsyscall.go +++ b/windows/mkwinsyscall/mkwinsyscall.go @@ -105,26 +105,20 @@ func (p *Param) tmpVar() string { // BoolTmpVarCode returns source code for bool temp variable. func (p *Param) BoolTmpVarCode() string { - const code = `var %s uint32 - if %s { - %s = 1 - } else { - %s = 0 + const code = `var %[1]s uint32 + if %[2]s { + %[1]s = 1 }` - tmp := p.tmpVar() - return fmt.Sprintf(code, tmp, p.Name, tmp, tmp) + return fmt.Sprintf(code, p.tmpVar(), p.Name) } // BoolPointerTmpVarCode returns source code for bool temp variable. func (p *Param) BoolPointerTmpVarCode() string { - const code = `var %s uint32 - if *%s { - %s = 1 - } else { - %s = 0 + const code = `var %[1]s uint32 + if *%[2]s { + %[1]s = 1 }` - tmp := p.tmpVar() - return fmt.Sprintf(code, tmp, p.Name, tmp, tmp) + return fmt.Sprintf(code, p.tmpVar(), p.Name) } // SliceTmpVarCode returns source code for slice temp variable. diff --git a/windows/zsyscall_windows.go b/windows/zsyscall_windows.go index 347f13db..8d585a87 100644 --- a/windows/zsyscall_windows.go +++ b/windows/zsyscall_windows.go @@ -761,8 +761,6 @@ func IsWow64Process(handle Handle, isWow64 *bool) (err error) { var _p0 uint32 if *isWow64 { _p0 = 1 - } else { - _p0 = 0 } r1, _, e1 := syscall.Syscall(procIsWow64Process.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(&_p0)), 0) *isWow64 = _p0 != 0 @@ -825,8 +823,6 @@ func GetOverlappedResult(handle Handle, overlapped *Overlapped, done *uint32, wa var _p0 uint32 if wait { _p0 = 1 - } else { - _p0 = 0 } r1, _, e1 := syscall.Syscall6(procGetOverlappedResult.Addr(), 4, uintptr(handle), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(done)), uintptr(_p0), 0, 0) if r1 == 0 { @@ -1183,8 +1179,6 @@ func CreateProcess(appName *uint16, commandLine *uint16, procSecurity *SecurityA var _p0 uint32 if inheritHandles { _p0 = 1 - } else { - _p0 = 0 } r1, _, e1 := syscall.Syscall12(procCreateProcessW.Addr(), 10, uintptr(unsafe.Pointer(appName)), uintptr(unsafe.Pointer(commandLine)), uintptr(unsafe.Pointer(procSecurity)), uintptr(unsafe.Pointer(threadSecurity)), uintptr(_p0), uintptr(creationFlags), uintptr(unsafe.Pointer(env)), uintptr(unsafe.Pointer(currentDir)), uintptr(unsafe.Pointer(startupInfo)), uintptr(unsafe.Pointer(outProcInfo)), 0, 0) if r1 == 0 { @@ -1201,8 +1195,6 @@ func OpenProcess(desiredAccess uint32, inheritHandle bool, processId uint32) (ha var _p0 uint32 if inheritHandle { _p0 = 1 - } else { - _p0 = 0 } r0, _, e1 := syscall.Syscall(procOpenProcess.Addr(), 3, uintptr(desiredAccess), uintptr(_p0), uintptr(processId)) handle = Handle(r0) @@ -1288,8 +1280,6 @@ func DuplicateHandle(hSourceProcessHandle Handle, hSourceHandle Handle, hTargetP var _p0 uint32 if bInheritHandle { _p0 = 1 - } else { - _p0 = 0 } r1, _, e1 := syscall.Syscall9(procDuplicateHandle.Addr(), 7, uintptr(hSourceProcessHandle), uintptr(hSourceHandle), uintptr(hTargetProcessHandle), uintptr(unsafe.Pointer(lpTargetHandle)), uintptr(dwDesiredAccess), uintptr(_p0), uintptr(dwOptions), 0, 0) if r1 == 0 { @@ -1319,8 +1309,6 @@ func waitForMultipleObjects(count uint32, handles uintptr, waitAll bool, waitMil var _p0 uint32 if waitAll { _p0 = 1 - } else { - _p0 = 0 } r0, _, e1 := syscall.Syscall6(procWaitForMultipleObjects.Addr(), 4, uintptr(count), uintptr(handles), uintptr(_p0), uintptr(waitMilliseconds), 0, 0) event = uint32(r0) @@ -1462,8 +1450,6 @@ func CreateEnvironmentBlock(block **uint16, token Token, inheritExisting bool) ( var _p0 uint32 if inheritExisting { _p0 = 1 - } else { - _p0 = 0 } r1, _, e1 := syscall.Syscall(procCreateEnvironmentBlock.Addr(), 3, uintptr(unsafe.Pointer(block)), uintptr(token), uintptr(_p0)) if r1 == 0 { @@ -1765,8 +1751,6 @@ func ReadDirectoryChanges(handle Handle, buf *byte, buflen uint32, watchSubTree var _p0 uint32 if watchSubTree { _p0 = 1 - } else { - _p0 = 0 } r1, _, e1 := syscall.Syscall9(procReadDirectoryChangesW.Addr(), 8, uintptr(handle), uintptr(unsafe.Pointer(buf)), uintptr(buflen), uintptr(_p0), uintptr(mask), uintptr(unsafe.Pointer(retlen)), uintptr(unsafe.Pointer(overlapped)), uintptr(completionRoutine), 0) if r1 == 0 { @@ -2135,8 +2119,6 @@ func OpenEvent(desiredAccess uint32, inheritHandle bool, name *uint16) (handle H var _p0 uint32 if inheritHandle { _p0 = 1 - } else { - _p0 = 0 } r0, _, e1 := syscall.Syscall(procOpenEventW.Addr(), 3, uintptr(desiredAccess), uintptr(_p0), uintptr(unsafe.Pointer(name))) handle = Handle(r0) @@ -2190,8 +2172,6 @@ func CreateMutex(mutexAttrs *SecurityAttributes, initialOwner bool, name *uint16 var _p0 uint32 if initialOwner { _p0 = 1 - } else { - _p0 = 0 } r0, _, e1 := syscall.Syscall(procCreateMutexW.Addr(), 3, uintptr(unsafe.Pointer(mutexAttrs)), uintptr(_p0), uintptr(unsafe.Pointer(name))) handle = Handle(r0) @@ -2222,8 +2202,6 @@ func OpenMutex(desiredAccess uint32, inheritHandle bool, name *uint16) (handle H var _p0 uint32 if inheritHandle { _p0 = 1 - } else { - _p0 = 0 } r0, _, e1 := syscall.Syscall(procOpenMutexW.Addr(), 3, uintptr(desiredAccess), uintptr(_p0), uintptr(unsafe.Pointer(name))) handle = Handle(r0) @@ -2253,8 +2231,6 @@ func SleepEx(milliseconds uint32, alertable bool) (ret uint32) { var _p0 uint32 if alertable { _p0 = 1 - } else { - _p0 = 0 } r0, _, _ := syscall.Syscall(procSleepEx.Addr(), 2, uintptr(milliseconds), uintptr(_p0), 0) ret = uint32(r0) @@ -2396,8 +2372,6 @@ func OpenThread(desiredAccess uint32, inheritHandle bool, threadId uint32) (hand var _p0 uint32 if inheritHandle { _p0 = 1 - } else { - _p0 = 0 } r0, _, e1 := syscall.Syscall(procOpenThread.Addr(), 3, uintptr(desiredAccess), uintptr(_p0), uintptr(threadId)) handle = Handle(r0) @@ -2415,8 +2389,6 @@ func SetProcessPriorityBoost(process Handle, disable bool) (err error) { var _p0 uint32 if disable { _p0 = 1 - } else { - _p0 = 0 } r1, _, e1 := syscall.Syscall(procSetProcessPriorityBoost.Addr(), 2, uintptr(process), uintptr(_p0), 0) if r1 == 0 { @@ -2714,14 +2686,10 @@ func InitiateSystemShutdownEx(machineName *uint16, message *uint16, timeout uint var _p0 uint32 if forceAppsClosed { _p0 = 1 - } else { - _p0 = 0 } var _p1 uint32 if rebootAfterShutdown { _p1 = 1 - } else { - _p1 = 0 } r1, _, e1 := syscall.Syscall6(procInitiateSystemShutdownExW.Addr(), 6, uintptr(unsafe.Pointer(machineName)), uintptr(unsafe.Pointer(message)), uintptr(timeout), uintptr(_p0), uintptr(_p1), uintptr(reason)) if r1 == 0 { @@ -3513,8 +3481,6 @@ func OpenThreadToken(thread Handle, access uint32, openAsSelf bool, token *Token var _p0 uint32 if openAsSelf { _p0 = 1 - } else { - _p0 = 0 } r1, _, e1 := syscall.Syscall6(procOpenThreadToken.Addr(), 4, uintptr(thread), uintptr(access), uintptr(_p0), uintptr(unsafe.Pointer(token)), 0, 0) if r1 == 0 { @@ -3579,8 +3545,6 @@ func AdjustTokenPrivileges(token Token, disableAllPrivileges bool, newstate *Tok var _p0 uint32 if disableAllPrivileges { _p0 = 1 - } else { - _p0 = 0 } r1, _, e1 := syscall.Syscall6(procAdjustTokenPrivileges.Addr(), 6, uintptr(token), uintptr(_p0), uintptr(unsafe.Pointer(newstate)), uintptr(buflen), uintptr(unsafe.Pointer(prevstate)), uintptr(unsafe.Pointer(returnlen))) if r1 == 0 { @@ -3597,8 +3561,6 @@ func AdjustTokenGroups(token Token, resetToDefault bool, newstate *Tokengroups, var _p0 uint32 if resetToDefault { _p0 = 1 - } else { - _p0 = 0 } r1, _, e1 := syscall.Syscall6(procAdjustTokenGroups.Addr(), 6, uintptr(token), uintptr(_p0), uintptr(unsafe.Pointer(newstate)), uintptr(buflen), uintptr(unsafe.Pointer(prevstate)), uintptr(unsafe.Pointer(returnlen))) if r1 == 0 { @@ -3810,14 +3772,10 @@ func getSecurityDescriptorDacl(sd *SECURITY_DESCRIPTOR, daclPresent *bool, dacl var _p0 uint32 if *daclPresent { _p0 = 1 - } else { - _p0 = 0 } var _p1 uint32 if *daclDefaulted { _p1 = 1 - } else { - _p1 = 0 } r1, _, e1 := syscall.Syscall6(procGetSecurityDescriptorDacl.Addr(), 4, uintptr(unsafe.Pointer(sd)), uintptr(unsafe.Pointer(&_p0)), uintptr(unsafe.Pointer(dacl)), uintptr(unsafe.Pointer(&_p1)), 0, 0) *daclPresent = _p0 != 0 @@ -3836,14 +3794,10 @@ func getSecurityDescriptorSacl(sd *SECURITY_DESCRIPTOR, saclPresent *bool, sacl var _p0 uint32 if *saclPresent { _p0 = 1 - } else { - _p0 = 0 } var _p1 uint32 if *saclDefaulted { _p1 = 1 - } else { - _p1 = 0 } r1, _, e1 := syscall.Syscall6(procGetSecurityDescriptorSacl.Addr(), 4, uintptr(unsafe.Pointer(sd)), uintptr(unsafe.Pointer(&_p0)), uintptr(unsafe.Pointer(sacl)), uintptr(unsafe.Pointer(&_p1)), 0, 0) *saclPresent = _p0 != 0 @@ -3862,8 +3816,6 @@ func getSecurityDescriptorOwner(sd *SECURITY_DESCRIPTOR, owner **SID, ownerDefau var _p0 uint32 if *ownerDefaulted { _p0 = 1 - } else { - _p0 = 0 } r1, _, e1 := syscall.Syscall(procGetSecurityDescriptorOwner.Addr(), 3, uintptr(unsafe.Pointer(sd)), uintptr(unsafe.Pointer(owner)), uintptr(unsafe.Pointer(&_p0))) *ownerDefaulted = _p0 != 0 @@ -3881,8 +3833,6 @@ func getSecurityDescriptorGroup(sd *SECURITY_DESCRIPTOR, group **SID, groupDefau var _p0 uint32 if *groupDefaulted { _p0 = 1 - } else { - _p0 = 0 } r1, _, e1 := syscall.Syscall(procGetSecurityDescriptorGroup.Addr(), 3, uintptr(unsafe.Pointer(sd)), uintptr(unsafe.Pointer(group)), uintptr(unsafe.Pointer(&_p0))) *groupDefaulted = _p0 != 0 @@ -3932,14 +3882,10 @@ func setSecurityDescriptorDacl(sd *SECURITY_DESCRIPTOR, daclPresent bool, dacl * var _p0 uint32 if daclPresent { _p0 = 1 - } else { - _p0 = 0 } var _p1 uint32 if daclDefaulted { _p1 = 1 - } else { - _p1 = 0 } r1, _, e1 := syscall.Syscall6(procSetSecurityDescriptorDacl.Addr(), 4, uintptr(unsafe.Pointer(sd)), uintptr(_p0), uintptr(unsafe.Pointer(dacl)), uintptr(_p1), 0, 0) if r1 == 0 { @@ -3956,14 +3902,10 @@ func setSecurityDescriptorSacl(sd *SECURITY_DESCRIPTOR, saclPresent bool, sacl * var _p0 uint32 if saclPresent { _p0 = 1 - } else { - _p0 = 0 } var _p1 uint32 if saclDefaulted { _p1 = 1 - } else { - _p1 = 0 } r1, _, e1 := syscall.Syscall6(procSetSecurityDescriptorSacl.Addr(), 4, uintptr(unsafe.Pointer(sd)), uintptr(_p0), uintptr(unsafe.Pointer(sacl)), uintptr(_p1), 0, 0) if r1 == 0 { @@ -3980,8 +3922,6 @@ func setSecurityDescriptorOwner(sd *SECURITY_DESCRIPTOR, owner *SID, ownerDefaul var _p0 uint32 if ownerDefaulted { _p0 = 1 - } else { - _p0 = 0 } r1, _, e1 := syscall.Syscall(procSetSecurityDescriptorOwner.Addr(), 3, uintptr(unsafe.Pointer(sd)), uintptr(unsafe.Pointer(owner)), uintptr(_p0)) if r1 == 0 { @@ -3998,8 +3938,6 @@ func setSecurityDescriptorGroup(sd *SECURITY_DESCRIPTOR, group *SID, groupDefaul var _p0 uint32 if groupDefaulted { _p0 = 1 - } else { - _p0 = 0 } r1, _, e1 := syscall.Syscall(procSetSecurityDescriptorGroup.Addr(), 3, uintptr(unsafe.Pointer(sd)), uintptr(unsafe.Pointer(group)), uintptr(_p0)) if r1 == 0 {