1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-12-07 01:21:00 +01:00

Apply clang-format universally.

* It is tough to contribute to a project that doesn't use a formatter,
* It is extra hard to contribute to a project which has configured the formatter, but ignores it for some files
* Code formatting makes it harder to hide obscure / weird bugs by accident or on purpose,

Let's rip the bandaid off?

Note that PRs currently in flight should be able to be merged relatively easily by applying `clang-format` to their tip prior to merge.
This commit is contained in:
Graham Christensen 2025-07-18 12:47:27 -04:00
parent 41bf87ec70
commit e4f62e4608
587 changed files with 23258 additions and 23135 deletions

View file

@ -45,5 +45,5 @@ int setEnvOs(const OsString & name, const OsString & value)
return -SetEnvironmentVariableW(name.c_str(), value.c_str());
}
}
} // namespace nix
#endif

View file

@ -6,12 +6,12 @@
#include "nix/util/file-path.hh"
#ifdef _WIN32
#include <fileapi.h>
#include <error.h>
#include <namedpipeapi.h>
#include <namedpipeapi.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
# include <fileapi.h>
# include <error.h>
# include <namedpipeapi.h>
# include <namedpipeapi.h>
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
namespace nix {
@ -26,7 +26,6 @@ std::string readFile(HANDLE handle)
return drainFD(handle, true, li.QuadPart);
}
void readFull(HANDLE handle, char * buf, size_t count)
{
while (count) {
@ -34,34 +33,34 @@ void readFull(HANDLE handle, char * buf, size_t count)
DWORD res;
if (!ReadFile(handle, (char *) buf, count, &res, NULL))
throw WinError("%s:%d reading from file", __FILE__, __LINE__);
if (res == 0) throw EndOfFile("unexpected end-of-file");
if (res == 0)
throw EndOfFile("unexpected end-of-file");
count -= res;
buf += res;
}
}
void writeFull(HANDLE handle, std::string_view s, bool allowInterrupts)
{
while (!s.empty()) {
if (allowInterrupts) checkInterrupt();
if (allowInterrupts)
checkInterrupt();
DWORD res;
#if _WIN32_WINNT >= 0x0600
# if _WIN32_WINNT >= 0x0600
auto path = handleToPath(handle); // debug; do it before because handleToPath changes lasterror
if (!WriteFile(handle, s.data(), s.size(), &res, NULL)) {
throw WinError("writing to file %1%:%2%", handle, path);
}
#else
# else
if (!WriteFile(handle, s.data(), s.size(), &res, NULL)) {
throw WinError("writing to file %1%", handle);
}
#endif
# endif
if (res > 0)
s.remove_prefix(res);
}
}
std::string readLine(HANDLE handle, bool eofOk)
{
std::string s;
@ -77,16 +76,15 @@ std::string readLine(HANDLE handle, bool eofOk)
return s;
else
throw EndOfFile("unexpected EOF reading a line");
}
else {
if (ch == '\n') return s;
} else {
if (ch == '\n')
return s;
s += ch;
}
}
}
void drainFD(HANDLE handle, Sink & sink/*, bool block*/)
void drainFD(HANDLE handle, Sink & sink /*, bool block*/)
{
std::vector<unsigned char> buf(64 * 1024);
while (1) {
@ -97,16 +95,14 @@ void drainFD(HANDLE handle, Sink & sink/*, bool block*/)
if (winError.lastError == ERROR_BROKEN_PIPE)
break;
throw winError;
}
else if (rd == 0) break;
} else if (rd == 0)
break;
sink({(char *) buf.data(), (size_t) rd});
}
}
//////////////////////////////////////////////////////////////////////
void Pipe::create()
{
SECURITY_ATTRIBUTES saAttr = {0};
@ -122,35 +118,38 @@ void Pipe::create()
writeSide = hWritePipe;
}
//////////////////////////////////////////////////////////////////////
#if _WIN32_WINNT >= 0x0600
# if _WIN32_WINNT >= 0x0600
std::wstring windows::handleToFileName(HANDLE handle) {
std::wstring windows::handleToFileName(HANDLE handle)
{
std::vector<wchar_t> buf(0x100);
DWORD dw = GetFinalPathNameByHandleW(handle, buf.data(), buf.size(), FILE_NAME_OPENED);
if (dw == 0) {
if (handle == GetStdHandle(STD_INPUT_HANDLE )) return L"<stdin>";
if (handle == GetStdHandle(STD_OUTPUT_HANDLE)) return L"<stdout>";
if (handle == GetStdHandle(STD_ERROR_HANDLE )) return L"<stderr>";
if (handle == GetStdHandle(STD_INPUT_HANDLE))
return L"<stdin>";
if (handle == GetStdHandle(STD_OUTPUT_HANDLE))
return L"<stdout>";
if (handle == GetStdHandle(STD_ERROR_HANDLE))
return L"<stderr>";
return (boost::wformat(L"<unnnamed handle %X>") % handle).str();
}
if (dw > buf.size()) {
buf.resize(dw);
if (GetFinalPathNameByHandleW(handle, buf.data(), buf.size(), FILE_NAME_OPENED) != dw-1)
if (GetFinalPathNameByHandleW(handle, buf.data(), buf.size(), FILE_NAME_OPENED) != dw - 1)
throw WinError("GetFinalPathNameByHandleW");
dw -= 1;
}
return std::wstring(buf.data(), dw);
}
Path windows::handleToPath(HANDLE handle) {
Path windows::handleToPath(HANDLE handle)
{
return os_string_to_string(handleToFileName(handle));
}
#endif
# endif
}
} // namespace nix
#endif

View file

@ -11,14 +11,15 @@ namespace nix {
std::optional<std::filesystem::path> maybePath(PathView path)
{
if (path.length() >= 3 && (('A' <= path[0] && path[0] <= 'Z') || ('a' <= path[0] && path[0] <= 'z')) && path[1] == ':' && WindowsPathTrait<char>::isPathSep(path[2])) {
std::filesystem::path::string_type sw = string_to_os_string(
std::string { "\\\\?\\" } + path);
if (path.length() >= 3 && (('A' <= path[0] && path[0] <= 'Z') || ('a' <= path[0] && path[0] <= 'z'))
&& path[1] == ':' && WindowsPathTrait<char>::isPathSep(path[2])) {
std::filesystem::path::string_type sw = string_to_os_string(std::string{"\\\\?\\"} + path);
std::replace(sw.begin(), sw.end(), '/', '\\');
return sw;
}
if (path.length() >= 7 && path[0] == '\\' && path[1] == '\\' && (path[2] == '.' || path[2] == '?') && path[3] == '\\' &&
('A' <= path[4] && path[4] <= 'Z') && path[5] == ':' && WindowsPathTrait<char>::isPathSep(path[6])) {
if (path.length() >= 7 && path[0] == '\\' && path[1] == '\\' && (path[2] == '.' || path[2] == '?')
&& path[3] == '\\' && ('A' <= path[4] && path[4] <= 'Z') && path[5] == ':'
&& WindowsPathTrait<char>::isPathSep(path[6])) {
std::filesystem::path::string_type sw = string_to_os_string(path);
std::replace(sw.begin(), sw.end(), '/', '\\');
return sw;
@ -31,10 +32,10 @@ std::filesystem::path pathNG(PathView path)
std::optional<std::filesystem::path::string_type> sw = maybePath(path);
if (!sw) {
// FIXME why are we not using the regular error handling?
std::cerr << "invalid path for WinAPI call ["<<path<<"]"<<std::endl;
std::cerr << "invalid path for WinAPI call [" << path << "]" << std::endl;
_exit(111);
}
return *sw;
}
}
} // namespace nix

View file

@ -28,5 +28,5 @@ Descriptor openDirectory(const std::filesystem::path & path)
NULL);
}
}
} // namespace nix
#endif

View file

@ -44,4 +44,4 @@ struct ReceiveInterrupts
~ReceiveInterrupts() {}
};
}
} // namespace nix

View file

@ -25,5 +25,5 @@ public:
void close();
};
}
} // namespace nix::windows
#endif

View file

@ -2,9 +2,9 @@
///@file
#ifdef _WIN32
#include <errhandlingapi.h>
# include <errhandlingapi.h>
#include "nix/util/error.hh"
# include "nix/util/error.hh"
namespace nix::windows {
@ -25,8 +25,9 @@ public:
* information to the message.
*/
template<typename... Args>
WinError(DWORD lastError, const Args & ... args)
: SystemError(""), lastError(lastError)
WinError(DWORD lastError, const Args &... args)
: SystemError("")
, lastError(lastError)
{
auto hf = HintFmt(args...);
err.msg = HintFmt("%1%: %2%", Uncolored(hf.str()), renderError(lastError));
@ -39,8 +40,8 @@ public:
* before calling this constructor!
*/
template<typename... Args>
WinError(const Args & ... args)
: WinError(GetLastError(), args ...)
WinError(const Args &... args)
: WinError(GetLastError(), args...)
{
}
@ -49,5 +50,5 @@ private:
std::string renderError(DWORD lastError);
};
}
} // namespace nix::windows
#endif

View file

@ -68,5 +68,5 @@ void MuxablePipePollState::iterate(
}
}
}
} // namespace nix
#endif

View file

@ -23,6 +23,6 @@ std::filesystem::path::string_type string_to_os_string(std::string_view s)
return converter.from_bytes(std::string{s});
}
}
} // namespace nix
#endif

View file

@ -25,8 +25,8 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
namespace nix {
@ -84,8 +84,13 @@ int Pid::wait()
std::string runProgram(
Path program, bool lookupPath, const Strings & args, const std::optional<std::string> & input, bool isInteractive)
{
auto res = runProgram(RunOptions{
.program = program, .lookupPath = lookupPath, .args = args, .input = input, .isInteractive = isInteractive});
auto res = runProgram(
RunOptions{
.program = program,
.lookupPath = lookupPath,
.args = args,
.input = input,
.isInteractive = isInteractive});
if (!statusOk(res.first))
throw ExecError(res.first, "program '%1%' %2%", program, statusToString(res.first));
@ -383,6 +388,6 @@ int execvpe(const wchar_t * file0, const wchar_t * const argv[], const wchar_t *
return _wexecve(file.c_str(), argv, envp);
}
}
} // namespace nix
#endif

View file

@ -5,8 +5,8 @@
#include "nix/util/windows-error.hh"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
namespace nix {
@ -37,8 +37,7 @@ std::string getUserName()
Path getHome()
{
static Path homeDir = []()
{
static Path homeDir = []() {
Path homeDir = getEnv("USERPROFILE").value_or("C:\\Users\\Default");
assert(!homeDir.empty());
return canonPath(homeDir);
@ -46,9 +45,10 @@ Path getHome()
return homeDir;
}
bool isRootUser() {
bool isRootUser()
{
return false;
}
}
} // namespace nix
#endif

View file

@ -49,6 +49,6 @@ void AsyncPipe::close()
writeSide.close();
}
}
} // namespace nix::windows
#endif

View file

@ -1,8 +1,8 @@
#ifdef _WIN32
#include "nix/util/windows-error.hh"
#include <error.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
# include "nix/util/windows-error.hh"
# include <error.h>
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
namespace nix::windows {
@ -10,23 +10,25 @@ std::string WinError::renderError(DWORD lastError)
{
LPSTR errorText = NULL;
FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM // use system message tables to retrieve error text
|FORMAT_MESSAGE_ALLOCATE_BUFFER // allocate buffer on local heap for error text
|FORMAT_MESSAGE_IGNORE_INSERTS, // Important! will fail otherwise, since we're not (and CANNOT) pass insertion parameters
NULL, // unused with FORMAT_MESSAGE_FROM_SYSTEM
lastError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&errorText, // output
0, // minimum size for output buffer
NULL); // arguments - see note
FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM // use system message tables to retrieve error text
| FORMAT_MESSAGE_ALLOCATE_BUFFER // allocate buffer on local heap for error text
| FORMAT_MESSAGE_IGNORE_INSERTS, // Important! will fail otherwise, since we're not (and CANNOT) pass
// insertion parameters
NULL, // unused with FORMAT_MESSAGE_FROM_SYSTEM
lastError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &errorText, // output
0, // minimum size for output buffer
NULL); // arguments - see note
if (NULL != errorText ) {
std::string s2 { errorText };
if (NULL != errorText) {
std::string s2{errorText};
LocalFree(errorText);
return s2;
}
return fmt("CODE=%d", lastError);
}
}
} // namespace nix::windows
#endif