Explicitly assign constructed C++ classes

C++ style guides I am aware of recommend specifically preferring = syntax
for any classes with fairly obvious constructors[^0] that do not perform
any complicated logic in their constructor. I contend that all constructors
that the `rustc_llvm` code uses qualify. This has only become more common
since C++ 17 guaranteed many cases of copy initialization elision.

The other detail is that I tried to ask another contributor with
infinitely more C++ experience than me (i.e. any) what this constructor
syntax was, and they thought it was a macro. I know of no other language
that has adopted this same syntax. As the rustc codebase features many
contributors experienced in many other languages, using a less...
unique... style has many other benefits in making this code more
lucid and maintainable, which is something it direly needs.

[^0]: e.g. https://abseil.io/tips/88
This commit is contained in:
Jubilee Young 2024-03-05 20:04:24 -08:00
parent 3c029725f5
commit 23623a08d6
4 changed files with 53 additions and 52 deletions

View file

@ -109,7 +109,7 @@ extern "C" void LLVMRustSetNormalizedTarget(LLVMModuleRef M,
extern "C" const char *LLVMRustPrintPassTimings(size_t *Len) {
std::string buf;
raw_string_ostream SS(buf);
auto SS = raw_string_ostream(buf);
TimerGroup::printAll(SS);
SS.flush();
*Len = buf.length();
@ -120,7 +120,7 @@ extern "C" const char *LLVMRustPrintPassTimings(size_t *Len) {
extern "C" const char *LLVMRustPrintStatistics(size_t *Len) {
std::string buf;
raw_string_ostream SS(buf);
auto SS = raw_string_ostream(buf);
llvm::PrintStatistics(SS);
SS.flush();
*Len = buf.length();
@ -174,7 +174,7 @@ extern "C" LLVMValueRef LLVMRustGetOrInsertFunction(LLVMModuleRef M,
extern "C" LLVMValueRef
LLVMRustGetOrInsertGlobal(LLVMModuleRef M, const char *Name, size_t NameLen, LLVMTypeRef Ty) {
Module *Mod = unwrap(M);
StringRef NameRef(Name, NameLen);
auto NameRef = StringRef(Name, NameLen);
// We don't use Module::getOrInsertGlobal because that returns a Constant*,
// which may either be the real GlobalVariable*, or a constant bitcast of it
@ -285,7 +285,7 @@ static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
template<typename T> static inline void AddAttributes(T *t, unsigned Index,
LLVMAttributeRef *Attrs, size_t AttrsLen) {
AttributeList PAL = t->getAttributes();
AttrBuilder B(t->getContext());
auto B = AttrBuilder(t->getContext());
for (LLVMAttributeRef Attr : ArrayRef<LLVMAttributeRef>(Attrs, AttrsLen))
B.addAttribute(unwrap(Attr));
AttributeList PALNew = PAL.addAttributesAtIndex(t->getContext(), Index, B);
@ -1195,13 +1195,13 @@ extern "C" int64_t LLVMRustDIBuilderCreateOpLLVMFragment() {
}
extern "C" void LLVMRustWriteTypeToString(LLVMTypeRef Ty, RustStringRef Str) {
RawRustStringOstream OS(Str);
auto OS = RawRustStringOstream(Str);
unwrap<llvm::Type>(Ty)->print(OS);
}
extern "C" void LLVMRustWriteValueToString(LLVMValueRef V,
RustStringRef Str) {
RawRustStringOstream OS(Str);
auto OS = RawRustStringOstream(Str);
if (!V) {
OS << "(null)";
} else {
@ -1224,7 +1224,7 @@ extern "C" LLVMTypeRef LLVMRustArrayType(LLVMTypeRef ElementTy,
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Twine, LLVMTwineRef)
extern "C" void LLVMRustWriteTwineToString(LLVMTwineRef T, RustStringRef Str) {
RawRustStringOstream OS(Str);
auto OS = RawRustStringOstream(Str);
unwrap(T)->print(OS);
}
@ -1236,11 +1236,11 @@ extern "C" void LLVMRustUnpackOptimizationDiagnostic(
llvm::DiagnosticInfoOptimizationBase *Opt =
static_cast<llvm::DiagnosticInfoOptimizationBase *>(unwrap(DI));
RawRustStringOstream PassNameOS(PassNameOut);
auto PassNameOS = RawRustStringOstream(PassNameOut);
PassNameOS << Opt->getPassName();
*FunctionOut = wrap(&Opt->getFunction());
RawRustStringOstream FilenameOS(FilenameOut);
auto FilenameOS = RawRustStringOstream(FilenameOut);
DiagnosticLocation loc = Opt->getLocation();
if (loc.isValid()) {
*Line = loc.getLine();
@ -1248,7 +1248,7 @@ extern "C" void LLVMRustUnpackOptimizationDiagnostic(
FilenameOS << loc.getAbsolutePath();
}
RawRustStringOstream MessageOS(MessageOut);
auto MessageOS = RawRustStringOstream(MessageOut);
MessageOS << Opt->getMsg();
}
@ -1291,8 +1291,8 @@ LLVMRustUnpackInlineAsmDiagnostic(LLVMDiagnosticInfoRef DI,
extern "C" void LLVMRustWriteDiagnosticInfoToString(LLVMDiagnosticInfoRef DI,
RustStringRef Str) {
RawRustStringOstream OS(Str);
DiagnosticPrinterRawOStream DP(OS);
auto OS = RawRustStringOstream(Str);
auto DP = DiagnosticPrinterRawOStream(OS);
unwrap(DI)->print(DP);
}
@ -1406,7 +1406,7 @@ extern "C" LLVMTypeKind LLVMRustGetTypeKind(LLVMTypeRef Ty) {
default:
{
std::string error;
llvm::raw_string_ostream stream(error);
auto stream = llvm::raw_string_ostream(error);
stream << "Rust does not support the TypeID: " << unwrap(Ty)->getTypeID()
<< " for the type: " << *unwrap(Ty);
stream.flush();
@ -1432,7 +1432,7 @@ extern "C" bool LLVMRustUnpackSMDiagnostic(LLVMSMDiagnosticRef DRef,
unsigned* RangesOut,
size_t* NumRanges) {
SMDiagnostic& D = *unwrap(DRef);
RawRustStringOstream MessageOS(MessageOut);
auto MessageOS = RawRustStringOstream(MessageOut);
MessageOS << D.getMessage();
switch (D.getKind()) {
@ -1547,7 +1547,7 @@ extern "C" void LLVMRustPositionBuilderAtStart(LLVMBuilderRef B,
extern "C" void LLVMRustSetComdat(LLVMModuleRef M, LLVMValueRef V,
const char *Name, size_t NameLen) {
Triple TargetTriple(unwrap(M)->getTargetTriple());
Triple TargetTriple = Triple(unwrap(M)->getTargetTriple());
GlobalObject *GV = unwrap<GlobalObject>(V);
if (TargetTriple.supportsCOMDAT()) {
StringRef NameRef(Name, NameLen);
@ -1711,7 +1711,7 @@ extern "C" LLVMRustModuleBuffer*
LLVMRustModuleBufferCreate(LLVMModuleRef M) {
auto Ret = std::make_unique<LLVMRustModuleBuffer>();
{
raw_string_ostream OS(Ret->data);
auto OS = raw_string_ostream(Ret->data);
WriteBitcodeToFile(*unwrap(M), OS);
}
return Ret.release();
@ -1741,8 +1741,8 @@ LLVMRustModuleCost(LLVMModuleRef M) {
extern "C" void
LLVMRustModuleInstructionStats(LLVMModuleRef M, RustStringRef Str)
{
RawRustStringOstream OS(Str);
llvm::json::OStream JOS(OS);
auto OS = RawRustStringOstream(Str);
auto JOS = llvm::json::OStream(OS);
auto Module = unwrap(M);
JOS.object([&] {
@ -1857,7 +1857,7 @@ extern "C" LLVMRustResult LLVMRustWriteImportLibrary(
MinGW);
if (Error) {
std::string errorString;
llvm::raw_string_ostream stream(errorString);
auto stream = llvm::raw_string_ostream(errorString);
stream << Error;
stream.flush();
LLVMRustSetLastError(errorString.c_str());
@ -2041,7 +2041,7 @@ extern "C" void LLVMRustContextConfigureDiagnosticHandler(
}
extern "C" void LLVMRustGetMangledName(LLVMValueRef V, RustStringRef Str) {
RawRustStringOstream OS(Str);
auto OS = RawRustStringOstream(Str);
GlobalValue *GV = unwrap<GlobalValue>(V);
Mangler().getNameWithPrefix(OS, GV, true);
}