Skip to content

Commit 23623a0

Browse files
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
1 parent 3c02972 commit 23623a0

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
@@ -109,7 +109,7 @@ extern "C" void LLVMRustSetNormalizedTarget(LLVMModuleRef M,
109109

110110
extern "C" const char *LLVMRustPrintPassTimings(size_t *Len) {
111111
std::string buf;
112-
raw_string_ostream SS(buf);
112+
auto SS = raw_string_ostream(buf);
113113
TimerGroup::printAll(SS);
114114
SS.flush();
115115
*Len = buf.length();
@@ -120,7 +120,7 @@ extern "C" const char *LLVMRustPrintPassTimings(size_t *Len) {
120120

121121
extern "C" const char *LLVMRustPrintStatistics(size_t *Len) {
122122
std::string buf;
123-
raw_string_ostream SS(buf);
123+
auto SS = raw_string_ostream(buf);
124124
llvm::PrintStatistics(SS);
125125
SS.flush();
126126
*Len = buf.length();
@@ -174,7 +174,7 @@ extern "C" LLVMValueRef LLVMRustGetOrInsertFunction(LLVMModuleRef M,
174174
extern "C" LLVMValueRef
175175
LLVMRustGetOrInsertGlobal(LLVMModuleRef M, const char *Name, size_t NameLen, LLVMTypeRef Ty) {
176176
Module *Mod = unwrap(M);
177-
StringRef NameRef(Name, NameLen);
177+
auto NameRef = StringRef(Name, NameLen);
178178

179179
// We don't use Module::getOrInsertGlobal because that returns a Constant*,
180180
// which may either be the real GlobalVariable*, or a constant bitcast of it
@@ -285,7 +285,7 @@ static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
285285
template<typename T> static inline void AddAttributes(T *t, unsigned Index,
286286
LLVMAttributeRef *Attrs, size_t AttrsLen) {
287287
AttributeList PAL = t->getAttributes();
288-
AttrBuilder B(t->getContext());
288+
auto B = AttrBuilder(t->getContext());
289289
for (LLVMAttributeRef Attr : ArrayRef<LLVMAttributeRef>(Attrs, AttrsLen))
290290
B.addAttribute(unwrap(Attr));
291291
AttributeList PALNew = PAL.addAttributesAtIndex(t->getContext(), Index, B);
@@ -1195,13 +1195,13 @@ extern "C" int64_t LLVMRustDIBuilderCreateOpLLVMFragment() {
11951195
}
11961196

11971197
extern "C" void LLVMRustWriteTypeToString(LLVMTypeRef Ty, RustStringRef Str) {
1198-
RawRustStringOstream OS(Str);
1198+
auto OS = RawRustStringOstream(Str);
11991199
unwrap<llvm::Type>(Ty)->print(OS);
12001200
}
12011201

12021202
extern "C" void LLVMRustWriteValueToString(LLVMValueRef V,
12031203
RustStringRef Str) {
1204-
RawRustStringOstream OS(Str);
1204+
auto OS = RawRustStringOstream(Str);
12051205
if (!V) {
12061206
OS << "(null)";
12071207
} else {
@@ -1224,7 +1224,7 @@ extern "C" LLVMTypeRef LLVMRustArrayType(LLVMTypeRef ElementTy,
12241224
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Twine, LLVMTwineRef)
12251225

12261226
extern "C" void LLVMRustWriteTwineToString(LLVMTwineRef T, RustStringRef Str) {
1227-
RawRustStringOstream OS(Str);
1227+
auto OS = RawRustStringOstream(Str);
12281228
unwrap(T)->print(OS);
12291229
}
12301230

@@ -1236,19 +1236,19 @@ extern "C" void LLVMRustUnpackOptimizationDiagnostic(
12361236
llvm::DiagnosticInfoOptimizationBase *Opt =
12371237
static_cast<llvm::DiagnosticInfoOptimizationBase *>(unwrap(DI));
12381238

1239-
RawRustStringOstream PassNameOS(PassNameOut);
1239+
auto PassNameOS = RawRustStringOstream(PassNameOut);
12401240
PassNameOS << Opt->getPassName();
12411241
*FunctionOut = wrap(&Opt->getFunction());
12421242

1243-
RawRustStringOstream FilenameOS(FilenameOut);
1243+
auto FilenameOS = RawRustStringOstream(FilenameOut);
12441244
DiagnosticLocation loc = Opt->getLocation();
12451245
if (loc.isValid()) {
12461246
*Line = loc.getLine();
12471247
*Column = loc.getColumn();
12481248
FilenameOS << loc.getAbsolutePath();
12491249
}
12501250

1251-
RawRustStringOstream MessageOS(MessageOut);
1251+
auto MessageOS = RawRustStringOstream(MessageOut);
12521252
MessageOS << Opt->getMsg();
12531253
}
12541254

@@ -1291,8 +1291,8 @@ LLVMRustUnpackInlineAsmDiagnostic(LLVMDiagnosticInfoRef DI,
12911291

12921292
extern "C" void LLVMRustWriteDiagnosticInfoToString(LLVMDiagnosticInfoRef DI,
12931293
RustStringRef Str) {
1294-
RawRustStringOstream OS(Str);
1295-
DiagnosticPrinterRawOStream DP(OS);
1294+
auto OS = RawRustStringOstream(Str);
1295+
auto DP = DiagnosticPrinterRawOStream(OS);
12961296
unwrap(DI)->print(DP);
12971297
}
12981298

@@ -1406,7 +1406,7 @@ extern "C" LLVMTypeKind LLVMRustGetTypeKind(LLVMTypeRef Ty) {
14061406
default:
14071407
{
14081408
std::string error;
1409-
llvm::raw_string_ostream stream(error);
1409+
auto stream = llvm::raw_string_ostream(error);
14101410
stream << "Rust does not support the TypeID: " << unwrap(Ty)->getTypeID()
14111411
<< " for the type: " << *unwrap(Ty);
14121412
stream.flush();
@@ -1432,7 +1432,7 @@ extern "C" bool LLVMRustUnpackSMDiagnostic(LLVMSMDiagnosticRef DRef,
14321432
unsigned* RangesOut,
14331433
size_t* NumRanges) {
14341434
SMDiagnostic& D = *unwrap(DRef);
1435-
RawRustStringOstream MessageOS(MessageOut);
1435+
auto MessageOS = RawRustStringOstream(MessageOut);
14361436
MessageOS << D.getMessage();
14371437

14381438
switch (D.getKind()) {
@@ -1547,7 +1547,7 @@ extern "C" void LLVMRustPositionBuilderAtStart(LLVMBuilderRef B,
15471547

15481548
extern "C" void LLVMRustSetComdat(LLVMModuleRef M, LLVMValueRef V,
15491549
const char *Name, size_t NameLen) {
1550-
Triple TargetTriple(unwrap(M)->getTargetTriple());
1550+
Triple TargetTriple = Triple(unwrap(M)->getTargetTriple());
15511551
GlobalObject *GV = unwrap<GlobalObject>(V);
15521552
if (TargetTriple.supportsCOMDAT()) {
15531553
StringRef NameRef(Name, NameLen);
@@ -1711,7 +1711,7 @@ extern "C" LLVMRustModuleBuffer*
17111711
LLVMRustModuleBufferCreate(LLVMModuleRef M) {
17121712
auto Ret = std::make_unique<LLVMRustModuleBuffer>();
17131713
{
1714-
raw_string_ostream OS(Ret->data);
1714+
auto OS = raw_string_ostream(Ret->data);
17151715
WriteBitcodeToFile(*unwrap(M), OS);
17161716
}
17171717
return Ret.release();
@@ -1741,8 +1741,8 @@ LLVMRustModuleCost(LLVMModuleRef M) {
17411741
extern "C" void
17421742
LLVMRustModuleInstructionStats(LLVMModuleRef M, RustStringRef Str)
17431743
{
1744-
RawRustStringOstream OS(Str);
1745-
llvm::json::OStream JOS(OS);
1744+
auto OS = RawRustStringOstream(Str);
1745+
auto JOS = llvm::json::OStream(OS);
17461746
auto Module = unwrap(M);
17471747

17481748
JOS.object([&] {
@@ -1857,7 +1857,7 @@ extern "C" LLVMRustResult LLVMRustWriteImportLibrary(
18571857
MinGW);
18581858
if (Error) {
18591859
std::string errorString;
1860-
llvm::raw_string_ostream stream(errorString);
1860+
auto stream = llvm::raw_string_ostream(errorString);
18611861
stream << Error;
18621862
stream.flush();
18631863
LLVMRustSetLastError(errorString.c_str());
@@ -2041,7 +2041,7 @@ extern "C" void LLVMRustContextConfigureDiagnosticHandler(
20412041
}
20422042

20432043
extern "C" void LLVMRustGetMangledName(LLVMValueRef V, RustStringRef Str) {
2044-
RawRustStringOstream OS(Str);
2044+
auto OS = RawRustStringOstream(Str);
20452045
GlobalValue *GV = unwrap<GlobalValue>(V);
20462046
Mangler().getNameWithPrefix(OS, GV, true);
20472047
}

0 commit comments

Comments
 (0)