@@ -346,51 +346,41 @@ void writeModule(llvm::Module *m, std::string filename) {
346346 global.params .targetTriple .getOS () == llvm::Triple::AIX);
347347
348348 // eventually do our own path stuff, dmd's is a bit strange.
349- typedef llvm::SmallString<128 > LLPath;
349+ using LLPath = llvm::SmallString<128 >;
350+
351+ #if LDC_LLVM_VER >= 306
352+ using ErrorInfo = std::error_code;
353+ #define ERRORINFO_STRING (errinfo ) errinfo.message().c_str()
354+ #else
355+ using ErrorInfo = std::string;
356+ #define ERRORINFO_STRING (errinfo ) errinfo.c_str()
357+ #endif
350358
351359 // write LLVM bitcode
352360 if (global.params .output_bc ) {
353- LLPath bcpath = LLPath (filename);
361+ LLPath bcpath (filename);
354362 llvm::sys::path::replace_extension (bcpath, global.bc_ext );
355363 Logger::println (" Writing LLVM bitcode to: %s\n " , bcpath.c_str ());
356- #if LDC_LLVM_VER >= 306
357- std::error_code errinfo;
358- #else
359- std::string errinfo;
360- #endif
364+ ErrorInfo errinfo;
361365 llvm::raw_fd_ostream bos (bcpath.c_str (), errinfo, llvm::sys::fs::F_None);
362366 if (bos.has_error ()) {
363367 error (Loc (), " cannot write LLVM bitcode file '%s': %s" , bcpath.c_str (),
364- #if LDC_LLVM_VER >= 306
365- errinfo
366- #else
367- errinfo.c_str ()
368- #endif
369- );
368+ ERRORINFO_STRING (errinfo));
370369 fatal ();
371370 }
372371 llvm::WriteBitcodeToFile (m, bos);
373372 }
374373
375374 // write LLVM IR
376375 if (global.params .output_ll ) {
377- LLPath llpath = LLPath (filename);
376+ LLPath llpath (filename);
378377 llvm::sys::path::replace_extension (llpath, global.ll_ext );
379378 Logger::println (" Writing LLVM asm to: %s\n " , llpath.c_str ());
380- #if LDC_LLVM_VER >= 306
381- std::error_code errinfo;
382- #else
383- std::string errinfo;
384- #endif
379+ ErrorInfo errinfo;
385380 llvm::raw_fd_ostream aos (llpath.c_str (), errinfo, llvm::sys::fs::F_None);
386381 if (aos.has_error ()) {
387382 error (Loc (), " cannot write LLVM asm file '%s': %s" , llpath.c_str (),
388- #if LDC_LLVM_VER >= 306
389- errinfo
390- #else
391- errinfo.c_str ()
392- #endif
393- );
383+ ERRORINFO_STRING (errinfo));
394384 fatal ();
395385 }
396386 AssemblyAnnotator annotator;
@@ -399,18 +389,14 @@ void writeModule(llvm::Module *m, std::string filename) {
399389
400390 // write native assembly
401391 if (global.params .output_s || assembleExternally) {
402- LLPath spath = LLPath (filename);
392+ LLPath spath (filename);
403393 llvm::sys::path::replace_extension (spath, global.s_ext );
404394 if (!global.params .output_s ) {
405395 llvm::sys::fs::createUniqueFile (" ldc-%%%%%%%.s" , spath);
406396 }
407397
408398 Logger::println (" Writing native asm to: %s\n " , spath.c_str ());
409- #if LDC_LLVM_VER >= 306
410- std::error_code errinfo;
411- #else
412- std::string errinfo;
413- #endif
399+ ErrorInfo errinfo;
414400 {
415401 llvm::raw_fd_ostream out (spath.c_str (), errinfo, llvm::sys::fs::F_None);
416402#if LDC_LLVM_VER >= 306
@@ -422,20 +408,13 @@ void writeModule(llvm::Module *m, std::string filename) {
422408 codegenModule (*gTargetMachine , *m, out,
423409 llvm::TargetMachine::CGFT_AssemblyFile);
424410 } else {
425- error (Loc (), " cannot write native asm: %s" ,
426- #if LDC_LLVM_VER >= 306
427- errinfo
428- #else
429- errinfo.c_str ()
430- #endif
431- );
411+ error (Loc (), " cannot write native asm: %s" , ERRORINFO_STRING (errinfo));
432412 fatal ();
433413 }
434414 }
435415
436416 if (assembleExternally) {
437- LLPath objpath (filename);
438- assemble (spath.str (), objpath.str ());
417+ assemble (spath.str (), filename);
439418 }
440419
441420 if (!global.params .output_s ) {
@@ -444,15 +423,11 @@ void writeModule(llvm::Module *m, std::string filename) {
444423 }
445424
446425 if (global.params .output_o && !assembleExternally) {
447- LLPath objpath = LLPath (filename);
448- Logger::println (" Writing object file to: %s\n " , objpath.c_str ());
449- #if LDC_LLVM_VER >= 306
450- std::error_code errinfo;
451- #else
452- std::string errinfo;
453- #endif
426+ Logger::println (" Writing object file to: %s\n " , filename.c_str ());
427+ ErrorInfo errinfo;
454428 {
455- llvm::raw_fd_ostream out (objpath.c_str (), errinfo, llvm::sys::fs::F_None);
429+ llvm::raw_fd_ostream out (filename.c_str (), errinfo,
430+ llvm::sys::fs::F_None);
456431#if LDC_LLVM_VER >= 306
457432 if (!errinfo)
458433#else
@@ -462,15 +437,11 @@ void writeModule(llvm::Module *m, std::string filename) {
462437 codegenModule (*gTargetMachine , *m, out,
463438 llvm::TargetMachine::CGFT_ObjectFile);
464439 } else {
465- error (Loc (), " cannot write object file: %s" ,
466- #if LDC_LLVM_VER >= 306
467- errinfo
468- #else
469- errinfo.c_str ()
470- #endif
471- );
440+ error (Loc (), " cannot write object file: %s" , ERRORINFO_STRING (errinfo));
472441 fatal ();
473442 }
474443 }
475444 }
445+
446+ #undef ERRORINFO_STRING
476447}
0 commit comments