Skip to content

[llvm] clang-format a few llvm/Support headers #138703

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

Merged
merged 2 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 45 additions & 46 deletions llvm/include/llvm/Support/ErrorHandling.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,46 @@
#include "llvm/Support/Compiler.h"

namespace llvm {
class StringRef;
class Twine;

/// An error handler callback.
typedef void (*fatal_error_handler_t)(void *user_data,
const char *reason,
bool gen_crash_diag);

/// install_fatal_error_handler - Installs a new error handler to be used
/// whenever a serious (non-recoverable) error is encountered by LLVM.
///
/// If no error handler is installed the default is to print the error message
/// to stderr, and call exit(1). If an error handler is installed then it is
/// the handler's responsibility to log the message, it will no longer be
/// printed to stderr. If the error handler returns, then exit(1) will be
/// called.
///
/// It is dangerous to naively use an error handler which throws an exception.
/// Even though some applications desire to gracefully recover from arbitrary
/// faults, blindly throwing exceptions through unfamiliar code isn't a way to
/// achieve this.
///
/// \param user_data - An argument which will be passed to the install error
/// handler.
void install_fatal_error_handler(fatal_error_handler_t handler,
void *user_data = nullptr);

/// Restores default error handling behaviour.
void remove_fatal_error_handler();

/// ScopedFatalErrorHandler - This is a simple helper class which just
/// calls install_fatal_error_handler in its constructor and
/// remove_fatal_error_handler in its destructor.
struct ScopedFatalErrorHandler {
explicit ScopedFatalErrorHandler(fatal_error_handler_t handler,
void *user_data = nullptr) {
install_fatal_error_handler(handler, user_data);
}

~ScopedFatalErrorHandler() { remove_fatal_error_handler(); }
};
class StringRef;
class Twine;

/// An error handler callback.
typedef void (*fatal_error_handler_t)(void *user_data, const char *reason,
bool gen_crash_diag);

/// install_fatal_error_handler - Installs a new error handler to be used
/// whenever a serious (non-recoverable) error is encountered by LLVM.
///
/// If no error handler is installed the default is to print the error message
/// to stderr, and call exit(1). If an error handler is installed then it is
/// the handler's responsibility to log the message, it will no longer be
/// printed to stderr. If the error handler returns, then exit(1) will be
/// called.
///
/// It is dangerous to naively use an error handler which throws an exception.
/// Even though some applications desire to gracefully recover from arbitrary
/// faults, blindly throwing exceptions through unfamiliar code isn't a way to
/// achieve this.
///
/// \param user_data - An argument which will be passed to the install error
/// handler.
void install_fatal_error_handler(fatal_error_handler_t handler,
void *user_data = nullptr);

/// Restores default error handling behaviour.
void remove_fatal_error_handler();

/// ScopedFatalErrorHandler - This is a simple helper class which just
/// calls install_fatal_error_handler in its constructor and
/// remove_fatal_error_handler in its destructor.
struct ScopedFatalErrorHandler {
explicit ScopedFatalErrorHandler(fatal_error_handler_t handler,
void *user_data = nullptr) {
install_fatal_error_handler(handler, user_data);
}

~ScopedFatalErrorHandler() { remove_fatal_error_handler(); }
};

/// @deprecated Use reportFatalInternalError() or reportFatalUsageError()
/// instead.
Expand Down Expand Up @@ -139,10 +138,10 @@ void install_out_of_memory_new_handler();
/// This function calls abort(), and prints the optional message to stderr.
/// Use the llvm_unreachable macro (that adds location info), instead of
/// calling this function directly.
[[noreturn]] void
llvm_unreachable_internal(const char *msg = nullptr, const char *file = nullptr,
unsigned line = 0);
}
[[noreturn]] void llvm_unreachable_internal(const char *msg = nullptr,
const char *file = nullptr,
unsigned line = 0);
} // namespace llvm

/// Marks that the current location is not supposed to be reachable.
/// In !NDEBUG builds, prints the message and location info to stderr.
Expand All @@ -162,7 +161,7 @@ llvm_unreachable_internal(const char *msg = nullptr, const char *file = nullptr,
/// diagnostics for unreachable code paths, and allows compilers to omit
/// unnecessary code.
#ifndef NDEBUG
#define llvm_unreachable(msg) \
#define llvm_unreachable(msg) \
::llvm::llvm_unreachable_internal(msg, __FILE__, __LINE__)
#elif !defined(LLVM_BUILTIN_UNREACHABLE)
#define llvm_unreachable(msg) ::llvm::llvm_unreachable_internal()
Expand Down
140 changes: 69 additions & 71 deletions llvm/include/llvm/Support/FileUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,81 +22,79 @@

namespace llvm {

/// DiffFilesWithTolerance - Compare the two files specified, returning 0 if
/// the files match, 1 if they are different, and 2 if there is a file error.
/// This function allows you to specify an absolute and relative FP error that
/// is allowed to exist. If you specify a string to fill in for the error
/// option, it will set the string to an error message if an error occurs, or
/// if the files are different.
///
int DiffFilesWithTolerance(StringRef FileA,
StringRef FileB,
double AbsTol, double RelTol,
std::string *Error = nullptr);


/// FileRemover - This class is a simple object meant to be stack allocated.
/// If an exception is thrown from a region, the object removes the filename
/// specified (if deleteIt is true).
///
class FileRemover {
SmallString<128> Filename;
bool DeleteIt;
public:
FileRemover() : DeleteIt(false) {}

explicit FileRemover(const Twine& filename, bool deleteIt = true)
/// DiffFilesWithTolerance - Compare the two files specified, returning 0 if
/// the files match, 1 if they are different, and 2 if there is a file error.
/// This function allows you to specify an absolute and relative FP error that
/// is allowed to exist. If you specify a string to fill in for the error
/// option, it will set the string to an error message if an error occurs, or
/// if the files are different.
///
int DiffFilesWithTolerance(StringRef FileA, StringRef FileB, double AbsTol,
double RelTol, std::string *Error = nullptr);

/// FileRemover - This class is a simple object meant to be stack allocated.
/// If an exception is thrown from a region, the object removes the filename
/// specified (if deleteIt is true).
///
class FileRemover {
SmallString<128> Filename;
bool DeleteIt;

public:
FileRemover() : DeleteIt(false) {}

explicit FileRemover(const Twine &filename, bool deleteIt = true)
: DeleteIt(deleteIt) {
filename.toVector(Filename);
}
filename.toVector(Filename);
}

~FileRemover() {
if (DeleteIt) {
// Ignore problems deleting the file.
sys::fs::remove(Filename);
}
~FileRemover() {
if (DeleteIt) {
// Ignore problems deleting the file.
sys::fs::remove(Filename);
}

/// setFile - Give ownership of the file to the FileRemover so it will
/// be removed when the object is destroyed. If the FileRemover already
/// had ownership of a file, remove it first.
void setFile(const Twine& filename, bool deleteIt = true) {
if (DeleteIt) {
// Ignore problems deleting the file.
sys::fs::remove(Filename);
}

Filename.clear();
filename.toVector(Filename);
DeleteIt = deleteIt;
}

/// setFile - Give ownership of the file to the FileRemover so it will
/// be removed when the object is destroyed. If the FileRemover already
/// had ownership of a file, remove it first.
void setFile(const Twine &filename, bool deleteIt = true) {
if (DeleteIt) {
// Ignore problems deleting the file.
sys::fs::remove(Filename);
}

/// releaseFile - Take ownership of the file away from the FileRemover so it
/// will not be removed when the object is destroyed.
void releaseFile() { DeleteIt = false; }
};

/// FilePermssionsApplier helps to copy permissions from an input file to
/// an output one. It memorizes the status of the input file and can apply
/// permissions and dates to the output file.
class FilePermissionsApplier {
public:
static Expected<FilePermissionsApplier> create(StringRef InputFilename);

/// Apply stored permissions to the \p OutputFilename.
/// Copy LastAccess and ModificationTime if \p CopyDates is true.
/// Overwrite stored permissions if \p OverwritePermissions is specified.
Error
apply(StringRef OutputFilename, bool CopyDates = false,
std::optional<sys::fs::perms> OverwritePermissions = std::nullopt);

private:
FilePermissionsApplier(StringRef InputFilename, sys::fs::file_status Status)
: InputFilename(InputFilename), InputStatus(Status) {}

StringRef InputFilename;
sys::fs::file_status InputStatus;
};
} // End llvm namespace
Filename.clear();
filename.toVector(Filename);
DeleteIt = deleteIt;
}

/// releaseFile - Take ownership of the file away from the FileRemover so it
/// will not be removed when the object is destroyed.
void releaseFile() { DeleteIt = false; }
};

/// FilePermssionsApplier helps to copy permissions from an input file to
/// an output one. It memorizes the status of the input file and can apply
/// permissions and dates to the output file.
class FilePermissionsApplier {
public:
static Expected<FilePermissionsApplier> create(StringRef InputFilename);

/// Apply stored permissions to the \p OutputFilename.
/// Copy LastAccess and ModificationTime if \p CopyDates is true.
/// Overwrite stored permissions if \p OverwritePermissions is specified.
Error
apply(StringRef OutputFilename, bool CopyDates = false,
std::optional<sys::fs::perms> OverwritePermissions = std::nullopt);

private:
FilePermissionsApplier(StringRef InputFilename, sys::fs::file_status Status)
: InputFilename(InputFilename), InputStatus(Status) {}

StringRef InputFilename;
sys::fs::file_status InputStatus;
};
} // namespace llvm

#endif
Loading