Skip to content

Commit 869529a

Browse files
authored
Rollup merge of rust-lang#122062 - workingjubilee:initialize-my-fist, r=cuviper
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
2 parents 8dc49e1 + 23623a0 commit 869529a

File tree

4 files changed

+53
-52
lines changed

4 files changed

+53
-52
lines changed

compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ extern "C" void LLVMRustCoverageWriteFilenamesSectionToBuffer(
120120
}
121121
auto FilenamesWriter =
122122
coverage::CoverageFilenamesSectionWriter(ArrayRef<std::string>(FilenameRefs));
123-
RawRustStringOstream OS(BufferOut);
123+
auto OS = RawRustStringOstream(BufferOut);
124124
FilenamesWriter.write(OS);
125125
}
126126

@@ -160,31 +160,31 @@ extern "C" void LLVMRustCoverageWriteMappingToBuffer(
160160
ArrayRef<unsigned>(VirtualFileMappingIDs, NumVirtualFileMappingIDs),
161161
Expressions,
162162
MappingRegions);
163-
RawRustStringOstream OS(BufferOut);
163+
auto OS = RawRustStringOstream(BufferOut);
164164
CoverageMappingWriter.write(OS);
165165
}
166166

167167
extern "C" LLVMValueRef LLVMRustCoverageCreatePGOFuncNameVar(
168168
LLVMValueRef F,
169169
const char *FuncName,
170170
size_t FuncNameLen) {
171-
StringRef FuncNameRef(FuncName, FuncNameLen);
171+
auto FuncNameRef = StringRef(FuncName, FuncNameLen);
172172
return wrap(createPGOFuncNameVar(*cast<Function>(unwrap(F)), FuncNameRef));
173173
}
174174

175175
extern "C" uint64_t LLVMRustCoverageHashByteArray(
176176
const char *Bytes,
177177
size_t NumBytes) {
178-
StringRef StrRef(Bytes, NumBytes);
178+
auto StrRef = StringRef(Bytes, NumBytes);
179179
return IndexedInstrProf::ComputeHash(StrRef);
180180
}
181181

182182
static void WriteSectionNameToString(LLVMModuleRef M,
183183
InstrProfSectKind SK,
184184
RustStringRef Str) {
185-
Triple TargetTriple(unwrap(M)->getTargetTriple());
185+
auto TargetTriple = Triple(unwrap(M)->getTargetTriple());
186186
auto name = getInstrProfSectionName(SK, TargetTriple.getObjectFormat());
187-
RawRustStringOstream OS(Str);
187+
auto OS = RawRustStringOstream(Str);
188188
OS << name;
189189
}
190190

@@ -200,7 +200,7 @@ extern "C" void LLVMRustCoverageWriteFuncSectionNameToString(LLVMModuleRef M,
200200

201201
extern "C" void LLVMRustCoverageWriteMappingVarNameToString(RustStringRef Str) {
202202
auto name = getCoverageMappingVarName();
203-
RawRustStringOstream OS(Str);
203+
auto OS = RawRustStringOstream(Str);
204204
OS << name;
205205
}
206206

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+21-21
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ extern "C" void LLVMRustTimeTraceProfilerFinishThread() {
7777
}
7878

7979
extern "C" void LLVMRustTimeTraceProfilerFinish(const char* FileName) {
80-
StringRef FN(FileName);
80+
auto FN = StringRef(FileName);
8181
std::error_code EC;
82-
raw_fd_ostream OS(FN, EC, sys::fs::CD_CreateAlways);
82+
auto OS = raw_fd_ostream(FN, EC, sys::fs::CD_CreateAlways);
8383

8484
timeTraceProfilerWrite(OS);
8585
timeTraceProfilerCleanup();
@@ -424,7 +424,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
424424
auto CM = fromRust(RustCM);
425425

426426
std::string Error;
427-
Triple Trip(Triple::normalize(TripleStr));
427+
auto Trip = Triple(Triple::normalize(TripleStr));
428428
const llvm::Target *TheTarget =
429429
TargetRegistry::lookupTarget(Trip.getTriple(), Error);
430430
if (TheTarget == nullptr) {
@@ -537,8 +537,8 @@ extern "C" void LLVMRustDisposeTargetMachine(LLVMTargetMachineRef TM) {
537537
// TargetLibraryInfo pass, so we use this method to do so.
538538
extern "C" void LLVMRustAddLibraryInfo(LLVMPassManagerRef PMR, LLVMModuleRef M,
539539
bool DisableSimplifyLibCalls) {
540-
Triple TargetTriple(unwrap(M)->getTargetTriple());
541-
TargetLibraryInfoImpl TLII(TargetTriple);
540+
auto TargetTriple = Triple(unwrap(M)->getTargetTriple());
541+
auto TLII = TargetLibraryInfoImpl(TargetTriple);
542542
if (DisableSimplifyLibCalls)
543543
TLII.disableAllFunctions();
544544
unwrap(PMR)->add(new TargetLibraryInfoWrapperPass(TLII));
@@ -589,25 +589,25 @@ LLVMRustWriteOutputFile(LLVMTargetMachineRef Target, LLVMPassManagerRef PMR,
589589

590590
std::string ErrorInfo;
591591
std::error_code EC;
592-
raw_fd_ostream OS(Path, EC, sys::fs::OF_None);
592+
auto OS = raw_fd_ostream(Path, EC, sys::fs::OF_None);
593593
if (EC)
594594
ErrorInfo = EC.message();
595595
if (ErrorInfo != "") {
596596
LLVMRustSetLastError(ErrorInfo.c_str());
597597
return LLVMRustResult::Failure;
598598
}
599599

600-
buffer_ostream BOS(OS);
600+
auto BOS = buffer_ostream(OS);
601601
if (DwoPath) {
602-
raw_fd_ostream DOS(DwoPath, EC, sys::fs::OF_None);
602+
auto DOS = raw_fd_ostream(DwoPath, EC, sys::fs::OF_None);
603603
EC.clear();
604604
if (EC)
605605
ErrorInfo = EC.message();
606606
if (ErrorInfo != "") {
607607
LLVMRustSetLastError(ErrorInfo.c_str());
608608
return LLVMRustResult::Failure;
609609
}
610-
buffer_ostream DBOS(DOS);
610+
auto DBOS = buffer_ostream(DOS);
611611
unwrap(Target)->addPassesToEmitFile(*PM, BOS, &DBOS, FileType, false);
612612
PM->run(*unwrap(M));
613613
} else {
@@ -796,7 +796,7 @@ LLVMRustOptimize(
796796
DebugInfoForProfiling);
797797
}
798798

799-
PassBuilder PB(TM, PTO, PGOOpt, &PIC);
799+
auto PB = PassBuilder(TM, PTO, PGOOpt, &PIC);
800800
LoopAnalysisManager LAM;
801801
FunctionAnalysisManager FAM;
802802
CGSCCAnalysisManager CGAM;
@@ -1112,16 +1112,16 @@ extern "C" LLVMRustResult
11121112
LLVMRustPrintModule(LLVMModuleRef M, const char *Path, DemangleFn Demangle) {
11131113
std::string ErrorInfo;
11141114
std::error_code EC;
1115-
raw_fd_ostream OS(Path, EC, sys::fs::OF_None);
1115+
auto OS = raw_fd_ostream(Path, EC, sys::fs::OF_None);
11161116
if (EC)
11171117
ErrorInfo = EC.message();
11181118
if (ErrorInfo != "") {
11191119
LLVMRustSetLastError(ErrorInfo.c_str());
11201120
return LLVMRustResult::Failure;
11211121
}
11221122

1123-
RustAssemblyAnnotationWriter AAW(Demangle);
1124-
formatted_raw_ostream FOS(OS);
1123+
auto AAW = RustAssemblyAnnotationWriter(Demangle);
1124+
auto FOS = formatted_raw_ostream(OS);
11251125
unwrap(M)->print(FOS, &AAW);
11261126

11271127
return LLVMRustResult::Success;
@@ -1281,8 +1281,8 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
12811281
// Load each module's summary and merge it into one combined index
12821282
for (int i = 0; i < num_modules; i++) {
12831283
auto module = &modules[i];
1284-
StringRef buffer(module->data, module->len);
1285-
MemoryBufferRef mem_buffer(buffer, module->identifier);
1284+
auto buffer = StringRef(module->data, module->len);
1285+
auto mem_buffer = MemoryBufferRef(buffer, module->identifier);
12861286

12871287
Ret->ModuleMap[module->identifier] = mem_buffer;
12881288

@@ -1485,7 +1485,7 @@ LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data, LLVMModuleRef M,
14851485
return MOrErr;
14861486
};
14871487
bool ClearDSOLocal = clearDSOLocalOnDeclarations(Mod, Target);
1488-
FunctionImporter Importer(Data->Index, Loader, ClearDSOLocal);
1488+
auto Importer = FunctionImporter(Data->Index, Loader, ClearDSOLocal);
14891489
Expected<bool> Result = Importer.importFunctions(Mod, ImportList);
14901490
if (!Result) {
14911491
LLVMRustSetLastError(toString(Result.takeError()).c_str());
@@ -1510,7 +1510,7 @@ extern "C" LLVMRustThinLTOBuffer*
15101510
LLVMRustThinLTOBufferCreate(LLVMModuleRef M, bool is_thin) {
15111511
auto Ret = std::make_unique<LLVMRustThinLTOBuffer>();
15121512
{
1513-
raw_string_ostream OS(Ret->data);
1513+
auto OS = raw_string_ostream(Ret->data);
15141514
{
15151515
if (is_thin) {
15161516
PassBuilder PB;
@@ -1557,8 +1557,8 @@ LLVMRustParseBitcodeForLTO(LLVMContextRef Context,
15571557
const char *data,
15581558
size_t len,
15591559
const char *identifier) {
1560-
StringRef Data(data, len);
1561-
MemoryBufferRef Buffer(Data, identifier);
1560+
auto Data = StringRef(data, len);
1561+
auto Buffer = MemoryBufferRef(Data, identifier);
15621562
unwrap(Context)->enableDebugTypeODRUniquing();
15631563
Expected<std::unique_ptr<Module>> SrcOrError =
15641564
parseBitcodeFile(Buffer, *unwrap(Context));
@@ -1576,8 +1576,8 @@ extern "C" const char *LLVMRustGetSliceFromObjectDataByName(const char *data,
15761576
const char *name,
15771577
size_t *out_len) {
15781578
*out_len = 0;
1579-
StringRef Data(data, len);
1580-
MemoryBufferRef Buffer(Data, ""); // The id is unused.
1579+
auto Data = StringRef(data, len);
1580+
auto Buffer = MemoryBufferRef(Data, ""); // The id is unused.
15811581
file_magic Type = identify_magic(Buffer.getBuffer());
15821582
Expected<std::unique_ptr<object::ObjectFile>> ObjFileOrError =
15831583
object::ObjectFile::createObjectFile(Buffer, Type);

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+20-20
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ extern "C" void LLVMRustSetNormalizedTarget(LLVMModuleRef M,
119119

120120
extern "C" const char *LLVMRustPrintPassTimings(size_t *Len) {
121121
std::string buf;
122-
raw_string_ostream SS(buf);
122+
auto SS = raw_string_ostream(buf);
123123
TimerGroup::printAll(SS);
124124
SS.flush();
125125
*Len = buf.length();
@@ -130,7 +130,7 @@ extern "C" const char *LLVMRustPrintPassTimings(size_t *Len) {
130130

131131
extern "C" const char *LLVMRustPrintStatistics(size_t *Len) {
132132
std::string buf;
133-
raw_string_ostream SS(buf);
133+
auto SS = raw_string_ostream(buf);
134134
llvm::PrintStatistics(SS);
135135
SS.flush();
136136
*Len = buf.length();
@@ -184,7 +184,7 @@ extern "C" LLVMValueRef LLVMRustGetOrInsertFunction(LLVMModuleRef M,
184184
extern "C" LLVMValueRef
185185
LLVMRustGetOrInsertGlobal(LLVMModuleRef M, const char *Name, size_t NameLen, LLVMTypeRef Ty) {
186186
Module *Mod = unwrap(M);
187-
StringRef NameRef(Name, NameLen);
187+
auto NameRef = StringRef(Name, NameLen);
188188

189189
// We don't use Module::getOrInsertGlobal because that returns a Constant*,
190190
// which may either be the real GlobalVariable*, or a constant bitcast of it
@@ -295,7 +295,7 @@ static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
295295
template<typename T> static inline void AddAttributes(T *t, unsigned Index,
296296
LLVMAttributeRef *Attrs, size_t AttrsLen) {
297297
AttributeList PAL = t->getAttributes();
298-
AttrBuilder B(t->getContext());
298+
auto B = AttrBuilder(t->getContext());
299299
for (LLVMAttributeRef Attr : ArrayRef<LLVMAttributeRef>(Attrs, AttrsLen))
300300
B.addAttribute(unwrap(Attr));
301301
AttributeList PALNew = PAL.addAttributesAtIndex(t->getContext(), Index, B);
@@ -1205,13 +1205,13 @@ extern "C" int64_t LLVMRustDIBuilderCreateOpLLVMFragment() {
12051205
}
12061206

12071207
extern "C" void LLVMRustWriteTypeToString(LLVMTypeRef Ty, RustStringRef Str) {
1208-
RawRustStringOstream OS(Str);
1208+
auto OS = RawRustStringOstream(Str);
12091209
unwrap<llvm::Type>(Ty)->print(OS);
12101210
}
12111211

12121212
extern "C" void LLVMRustWriteValueToString(LLVMValueRef V,
12131213
RustStringRef Str) {
1214-
RawRustStringOstream OS(Str);
1214+
auto OS = RawRustStringOstream(Str);
12151215
if (!V) {
12161216
OS << "(null)";
12171217
} else {
@@ -1234,7 +1234,7 @@ extern "C" LLVMTypeRef LLVMRustArrayType(LLVMTypeRef ElementTy,
12341234
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Twine, LLVMTwineRef)
12351235

12361236
extern "C" void LLVMRustWriteTwineToString(LLVMTwineRef T, RustStringRef Str) {
1237-
RawRustStringOstream OS(Str);
1237+
auto OS = RawRustStringOstream(Str);
12381238
unwrap(T)->print(OS);
12391239
}
12401240

@@ -1246,19 +1246,19 @@ extern "C" void LLVMRustUnpackOptimizationDiagnostic(
12461246
llvm::DiagnosticInfoOptimizationBase *Opt =
12471247
static_cast<llvm::DiagnosticInfoOptimizationBase *>(unwrap(DI));
12481248

1249-
RawRustStringOstream PassNameOS(PassNameOut);
1249+
auto PassNameOS = RawRustStringOstream(PassNameOut);
12501250
PassNameOS << Opt->getPassName();
12511251
*FunctionOut = wrap(&Opt->getFunction());
12521252

1253-
RawRustStringOstream FilenameOS(FilenameOut);
1253+
auto FilenameOS = RawRustStringOstream(FilenameOut);
12541254
DiagnosticLocation loc = Opt->getLocation();
12551255
if (loc.isValid()) {
12561256
*Line = loc.getLine();
12571257
*Column = loc.getColumn();
12581258
FilenameOS << loc.getAbsolutePath();
12591259
}
12601260

1261-
RawRustStringOstream MessageOS(MessageOut);
1261+
auto MessageOS = RawRustStringOstream(MessageOut);
12621262
MessageOS << Opt->getMsg();
12631263
}
12641264

@@ -1301,8 +1301,8 @@ LLVMRustUnpackInlineAsmDiagnostic(LLVMDiagnosticInfoRef DI,
13011301

13021302
extern "C" void LLVMRustWriteDiagnosticInfoToString(LLVMDiagnosticInfoRef DI,
13031303
RustStringRef Str) {
1304-
RawRustStringOstream OS(Str);
1305-
DiagnosticPrinterRawOStream DP(OS);
1304+
auto OS = RawRustStringOstream(Str);
1305+
auto DP = DiagnosticPrinterRawOStream(OS);
13061306
unwrap(DI)->print(DP);
13071307
}
13081308

@@ -1416,7 +1416,7 @@ extern "C" LLVMTypeKind LLVMRustGetTypeKind(LLVMTypeRef Ty) {
14161416
default:
14171417
{
14181418
std::string error;
1419-
llvm::raw_string_ostream stream(error);
1419+
auto stream = llvm::raw_string_ostream(error);
14201420
stream << "Rust does not support the TypeID: " << unwrap(Ty)->getTypeID()
14211421
<< " for the type: " << *unwrap(Ty);
14221422
stream.flush();
@@ -1442,7 +1442,7 @@ extern "C" bool LLVMRustUnpackSMDiagnostic(LLVMSMDiagnosticRef DRef,
14421442
unsigned* RangesOut,
14431443
size_t* NumRanges) {
14441444
SMDiagnostic& D = *unwrap(DRef);
1445-
RawRustStringOstream MessageOS(MessageOut);
1445+
auto MessageOS = RawRustStringOstream(MessageOut);
14461446
MessageOS << D.getMessage();
14471447

14481448
switch (D.getKind()) {
@@ -1557,7 +1557,7 @@ extern "C" void LLVMRustPositionBuilderAtStart(LLVMBuilderRef B,
15571557

15581558
extern "C" void LLVMRustSetComdat(LLVMModuleRef M, LLVMValueRef V,
15591559
const char *Name, size_t NameLen) {
1560-
Triple TargetTriple(unwrap(M)->getTargetTriple());
1560+
Triple TargetTriple = Triple(unwrap(M)->getTargetTriple());
15611561
GlobalObject *GV = unwrap<GlobalObject>(V);
15621562
if (TargetTriple.supportsCOMDAT()) {
15631563
StringRef NameRef(Name, NameLen);
@@ -1721,7 +1721,7 @@ extern "C" LLVMRustModuleBuffer*
17211721
LLVMRustModuleBufferCreate(LLVMModuleRef M) {
17221722
auto Ret = std::make_unique<LLVMRustModuleBuffer>();
17231723
{
1724-
raw_string_ostream OS(Ret->data);
1724+
auto OS = raw_string_ostream(Ret->data);
17251725
WriteBitcodeToFile(*unwrap(M), OS);
17261726
}
17271727
return Ret.release();
@@ -1751,8 +1751,8 @@ LLVMRustModuleCost(LLVMModuleRef M) {
17511751
extern "C" void
17521752
LLVMRustModuleInstructionStats(LLVMModuleRef M, RustStringRef Str)
17531753
{
1754-
RawRustStringOstream OS(Str);
1755-
llvm::json::OStream JOS(OS);
1754+
auto OS = RawRustStringOstream(Str);
1755+
auto JOS = llvm::json::OStream(OS);
17561756
auto Module = unwrap(M);
17571757

17581758
JOS.object([&] {
@@ -1867,7 +1867,7 @@ extern "C" LLVMRustResult LLVMRustWriteImportLibrary(
18671867
MinGW);
18681868
if (Error) {
18691869
std::string errorString;
1870-
llvm::raw_string_ostream stream(errorString);
1870+
auto stream = llvm::raw_string_ostream(errorString);
18711871
stream << Error;
18721872
stream.flush();
18731873
LLVMRustSetLastError(errorString.c_str());
@@ -2051,7 +2051,7 @@ extern "C" void LLVMRustContextConfigureDiagnosticHandler(
20512051
}
20522052

20532053
extern "C" void LLVMRustGetMangledName(LLVMValueRef V, RustStringRef Str) {
2054-
RawRustStringOstream OS(Str);
2054+
auto OS = RawRustStringOstream(Str);
20552055
GlobalValue *GV = unwrap<GlobalValue>(V);
20562056
Mangler().getNameWithPrefix(OS, GV, true);
20572057
}

0 commit comments

Comments
 (0)