From: Mehdi Amini Date: Fri, 7 Oct 2016 19:05:14 +0000 (+0000) Subject: Recommit "Use StringRef in LTOModule implementation (NFC)"" X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c16b74e39e3d158e015079a46f3de573b6df92fa;p=llvm Recommit "Use StringRef in LTOModule implementation (NFC)"" This reverts commit r283456 and reapply r282997, with explicitly zeroing the struct member to workaround a bug in MSVC2013 with zero-initialization: https://connect.microsoft.com/VisualStudio/feedback/details/802160 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283581 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/LTO/legacy/LTOModule.h b/include/llvm/LTO/legacy/LTOModule.h index 2e46219be19..fc9e36cdaea 100644 --- a/include/llvm/LTO/legacy/LTOModule.h +++ b/include/llvm/LTO/legacy/LTOModule.h @@ -37,10 +37,10 @@ namespace llvm { struct LTOModule { private: struct NameAndAttributes { - const char *name; - uint32_t attributes; - bool isFunction; - const GlobalValue *symbol; + StringRef name; + uint32_t attributes = 0; + bool isFunction = 0; + const GlobalValue *symbol = 0; }; std::unique_ptr OwnedContext; @@ -54,7 +54,7 @@ private: // _defines and _undefines only needed to disambiguate tentative definitions StringSet<> _defines; StringMap _undefines; - std::vector _asm_undefines; + std::vector _asm_undefines; LTOModule(std::unique_ptr Obj, TargetMachine *TM); @@ -63,7 +63,7 @@ public: /// Returns 'true' if the file or memory contents is LLVM bitcode. static bool isBitcodeFile(const void *mem, size_t length); - static bool isBitcodeFile(const char *path); + static bool isBitcodeFile(StringRef path); /// Returns 'true' if the Module is produced for ThinLTO. bool isThinLTO(); @@ -91,13 +91,13 @@ public: /// InitializeAllAsmPrinters(); /// InitializeAllAsmParsers(); static ErrorOr> - createFromFile(LLVMContext &Context, const char *path, + createFromFile(LLVMContext &Context, StringRef path, const TargetOptions &options); static ErrorOr> - createFromOpenFile(LLVMContext &Context, int fd, const char *path, - size_t size, const TargetOptions &options); + createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size, + const TargetOptions &options); static ErrorOr> - createFromOpenFileSlice(LLVMContext &Context, int fd, const char *path, + createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path, size_t map_size, off_t offset, const TargetOptions &options); static ErrorOr> @@ -140,10 +140,10 @@ public: } /// Get the name of the symbol at the specified index. - const char *getSymbolName(uint32_t index) { + StringRef getSymbolName(uint32_t index) { if (index < _symbols.size()) return _symbols[index].name; - return nullptr; + return StringRef(); } const GlobalValue *getSymbolGV(uint32_t index) { @@ -152,13 +152,9 @@ public: return nullptr; } - const char *getLinkerOpts() { - return LinkerOpts.c_str(); - } + StringRef getLinkerOpts() { return LinkerOpts; } - const std::vector &getAsmUndefinedRefs() { - return _asm_undefines; - } + const std::vector &getAsmUndefinedRefs() { return _asm_undefines; } private: /// Parse metadata from the module @@ -174,22 +170,22 @@ private: bool isFunc); /// Add a defined symbol to the list. - void addDefinedSymbol(const char *Name, const GlobalValue *def, + void addDefinedSymbol(StringRef Name, const GlobalValue *def, bool isFunction); /// Add a data symbol as defined to the list. void addDefinedDataSymbol(const object::BasicSymbolRef &Sym); - void addDefinedDataSymbol(const char*Name, const GlobalValue *v); + void addDefinedDataSymbol(StringRef Name, const GlobalValue *v); /// Add a function symbol as defined to the list. void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym); - void addDefinedFunctionSymbol(const char *Name, const Function *F); + void addDefinedFunctionSymbol(StringRef Name, const Function *F); /// Add a global symbol from module-level ASM to the defined list. - void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope); + void addAsmGlobalSymbol(StringRef, lto_symbol_attributes scope); /// Add a global symbol from module-level ASM to the undefined list. - void addAsmGlobalSymbolUndef(const char *); + void addAsmGlobalSymbolUndef(StringRef); /// Parse i386/ppc ObjC class data structure. void addObjCClass(const GlobalVariable *clgv); diff --git a/lib/LTO/LTOCodeGenerator.cpp b/lib/LTO/LTOCodeGenerator.cpp index 1ade0fb7530..81fb58a4e0a 100644 --- a/lib/LTO/LTOCodeGenerator.cpp +++ b/lib/LTO/LTOCodeGenerator.cpp @@ -131,7 +131,7 @@ void LTOCodeGenerator::initializeLTOPasses() { } void LTOCodeGenerator::setAsmUndefinedRefs(LTOModule *Mod) { - const std::vector &undefs = Mod->getAsmUndefinedRefs(); + const std::vector &undefs = Mod->getAsmUndefinedRefs(); for (int i = 0, e = undefs.size(); i != e; ++i) AsmUndefinedRefs[undefs[i]] = 1; } diff --git a/lib/LTO/LTOModule.cpp b/lib/LTO/LTOModule.cpp index cca33546a43..5b46feb15b9 100644 --- a/lib/LTO/LTOModule.cpp +++ b/lib/LTO/LTOModule.cpp @@ -62,7 +62,7 @@ bool LTOModule::isBitcodeFile(const void *Mem, size_t Length) { return bool(BCData); } -bool LTOModule::isBitcodeFile(const char *Path) { +bool LTOModule::isBitcodeFile(StringRef Path) { ErrorOr> BufferOrErr = MemoryBuffer::getFile(Path); if (!BufferOrErr) @@ -106,7 +106,7 @@ std::string LTOModule::getProducerString(MemoryBuffer *Buffer) { } ErrorOr> -LTOModule::createFromFile(LLVMContext &Context, const char *path, +LTOModule::createFromFile(LLVMContext &Context, StringRef path, const TargetOptions &options) { ErrorOr> BufferOrErr = MemoryBuffer::getFile(path); @@ -120,15 +120,15 @@ LTOModule::createFromFile(LLVMContext &Context, const char *path, } ErrorOr> -LTOModule::createFromOpenFile(LLVMContext &Context, int fd, const char *path, +LTOModule::createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size, const TargetOptions &options) { return createFromOpenFileSlice(Context, fd, path, size, 0, options); } ErrorOr> -LTOModule::createFromOpenFileSlice(LLVMContext &Context, int fd, - const char *path, size_t map_size, - off_t offset, const TargetOptions &options) { +LTOModule::createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path, + size_t map_size, off_t offset, + const TargetOptions &options) { ErrorOr> BufferOrErr = MemoryBuffer::getOpenFileSlice(fd, path, map_size, offset); if (std::error_code EC = BufferOrErr.getError()) { @@ -280,7 +280,7 @@ void LTOModule::addObjCClass(const GlobalVariable *clgv) { _undefines.insert(std::make_pair(superclassName, NameAndAttributes())); if (IterBool.second) { NameAndAttributes &info = IterBool.first->second; - info.name = IterBool.first->first().data(); + info.name = IterBool.first->first(); info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; info.isFunction = false; info.symbol = clgv; @@ -293,7 +293,7 @@ void LTOModule::addObjCClass(const GlobalVariable *clgv) { auto Iter = _defines.insert(className).first; NameAndAttributes info; - info.name = Iter->first().data(); + info.name = Iter->first(); info.attributes = LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT; info.isFunction = false; @@ -319,7 +319,7 @@ void LTOModule::addObjCCategory(const GlobalVariable *clgv) { return; NameAndAttributes &info = IterBool.first->second; - info.name = IterBool.first->first().data(); + info.name = IterBool.first->first(); info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; info.isFunction = false; info.symbol = clgv; @@ -338,7 +338,7 @@ void LTOModule::addObjCClassRef(const GlobalVariable *clgv) { return; NameAndAttributes &info = IterBool.first->second; - info.name = IterBool.first->first().data(); + info.name = IterBool.first->first(); info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; info.isFunction = false; info.symbol = clgv; @@ -349,13 +349,14 @@ void LTOModule::addDefinedDataSymbol(const object::BasicSymbolRef &Sym) { { raw_svector_ostream OS(Buffer); Sym.printName(OS); + Buffer.c_str(); } const GlobalValue *V = IRFile->getSymbolGV(Sym.getRawDataRefImpl()); - addDefinedDataSymbol(Buffer.c_str(), V); + addDefinedDataSymbol(Buffer, V); } -void LTOModule::addDefinedDataSymbol(const char *Name, const GlobalValue *v) { +void LTOModule::addDefinedDataSymbol(StringRef Name, const GlobalValue *v) { // Add to list of defined symbols. addDefinedSymbol(Name, v, false); @@ -410,19 +411,20 @@ void LTOModule::addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym) { { raw_svector_ostream OS(Buffer); Sym.printName(OS); + Buffer.c_str(); } const Function *F = cast(IRFile->getSymbolGV(Sym.getRawDataRefImpl())); - addDefinedFunctionSymbol(Buffer.c_str(), F); + addDefinedFunctionSymbol(Buffer, F); } -void LTOModule::addDefinedFunctionSymbol(const char *Name, const Function *F) { +void LTOModule::addDefinedFunctionSymbol(StringRef Name, const Function *F) { // add to list of defined symbols addDefinedSymbol(Name, F, true); } -void LTOModule::addDefinedSymbol(const char *Name, const GlobalValue *def, +void LTOModule::addDefinedSymbol(StringRef Name, const GlobalValue *def, bool isFunction) { // set alignment part log2() can have rounding errors uint32_t align = def->getAlignment(); @@ -471,8 +473,8 @@ void LTOModule::addDefinedSymbol(const char *Name, const GlobalValue *def, // fill information structure NameAndAttributes info; StringRef NameRef = Iter->first(); - info.name = NameRef.data(); - assert(info.name[NameRef.size()] == '\0'); + info.name = NameRef; + assert(NameRef.data()[NameRef.size()] == '\0'); info.attributes = attr; info.isFunction = isFunction; info.symbol = def; @@ -483,7 +485,7 @@ void LTOModule::addDefinedSymbol(const char *Name, const GlobalValue *def, /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the /// defined list. -void LTOModule::addAsmGlobalSymbol(const char *name, +void LTOModule::addAsmGlobalSymbol(StringRef name, lto_symbol_attributes scope) { auto IterBool = _defines.insert(name); @@ -491,7 +493,7 @@ void LTOModule::addAsmGlobalSymbol(const char *name, if (!IterBool.second) return; - NameAndAttributes &info = _undefines[IterBool.first->first().data()]; + NameAndAttributes &info = _undefines[IterBool.first->first()]; if (info.symbol == nullptr) { // FIXME: This is trying to take care of module ASM like this: @@ -503,7 +505,7 @@ void LTOModule::addAsmGlobalSymbol(const char *name, // much. // fill information structure - info.name = IterBool.first->first().data(); + info.name = IterBool.first->first(); info.attributes = LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope; info.isFunction = false; @@ -525,10 +527,10 @@ void LTOModule::addAsmGlobalSymbol(const char *name, /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the /// undefined list. -void LTOModule::addAsmGlobalSymbolUndef(const char *name) { +void LTOModule::addAsmGlobalSymbolUndef(StringRef name) { auto IterBool = _undefines.insert(std::make_pair(name, NameAndAttributes())); - _asm_undefines.push_back(IterBool.first->first().data()); + _asm_undefines.push_back(IterBool.first->first()); // we already have the symbol if (!IterBool.second) @@ -537,7 +539,7 @@ void LTOModule::addAsmGlobalSymbolUndef(const char *name) { uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED; attr |= LTO_SYMBOL_SCOPE_DEFAULT; NameAndAttributes &info = IterBool.first->second; - info.name = IterBool.first->first().data(); + info.name = IterBool.first->first(); info.attributes = attr; info.isFunction = false; info.symbol = nullptr; @@ -550,6 +552,7 @@ void LTOModule::addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym, { raw_svector_ostream OS(name); Sym.printName(OS); + name.c_str(); } auto IterBool = _undefines.insert(std::make_pair(name, NameAndAttributes())); @@ -560,7 +563,7 @@ void LTOModule::addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym, NameAndAttributes &info = IterBool.first->second; - info.name = IterBool.first->first().data(); + info.name = IterBool.first->first(); const GlobalValue *decl = IRFile->getSymbolGV(Sym.getRawDataRefImpl()); @@ -587,8 +590,9 @@ void LTOModule::parseSymbols() { { raw_svector_ostream OS(Buffer); Sym.printName(OS); + Buffer.c_str(); } - const char *Name = Buffer.c_str(); + StringRef Name(Buffer); if (IsUndefined) addAsmGlobalSymbolUndef(Name); diff --git a/tools/lto/lto.cpp b/tools/lto/lto.cpp index 7109cf4d909..64014e5a8e5 100644 --- a/tools/lto/lto.cpp +++ b/tools/lto/lto.cpp @@ -170,7 +170,7 @@ const char* lto_get_error_message() { } bool lto_module_is_object_file(const char* path) { - return LTOModule::isBitcodeFile(path); + return LTOModule::isBitcodeFile(StringRef(path)); } bool lto_module_is_object_file_for_target(const char* path, @@ -178,7 +178,8 @@ bool lto_module_is_object_file_for_target(const char* path, ErrorOr> Buffer = MemoryBuffer::getFile(path); if (!Buffer) return false; - return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix); + return LTOModule::isBitcodeForTarget(Buffer->get(), + StringRef(target_triplet_prefix)); } bool lto_module_has_objc_category(const void *mem, size_t length) { @@ -200,14 +201,15 @@ lto_module_is_object_file_in_memory_for_target(const void* mem, std::unique_ptr buffer(LTOModule::makeBuffer(mem, length)); if (!buffer) return false; - return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix); + return LTOModule::isBitcodeForTarget(buffer.get(), + StringRef(target_triplet_prefix)); } lto_module_t lto_module_create(const char* path) { lto_initialize(); llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); ErrorOr> M = - LTOModule::createFromFile(*LTOContext, path, Options); + LTOModule::createFromFile(*LTOContext, StringRef(path), Options); if (!M) return nullptr; return wrap(M->release()); @@ -216,8 +218,8 @@ lto_module_t lto_module_create(const char* path) { lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) { lto_initialize(); llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); - ErrorOr> M = - LTOModule::createFromOpenFile(*LTOContext, fd, path, size, Options); + ErrorOr> M = LTOModule::createFromOpenFile( + *LTOContext, fd, StringRef(path), size, Options); if (!M) return nullptr; return wrap(M->release()); @@ -230,7 +232,7 @@ lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path, lto_initialize(); llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); ErrorOr> M = LTOModule::createFromOpenFileSlice( - *LTOContext, fd, path, map_size, offset, Options); + *LTOContext, fd, StringRef(path), map_size, offset, Options); if (!M) return nullptr; return wrap(M->release()); @@ -251,8 +253,8 @@ lto_module_t lto_module_create_from_memory_with_path(const void* mem, const char *path) { lto_initialize(); llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); - ErrorOr> M = - LTOModule::createFromBuffer(*LTOContext, mem, length, Options, path); + ErrorOr> M = LTOModule::createFromBuffer( + *LTOContext, mem, length, Options, StringRef(path)); if (!M) return nullptr; return wrap(M->release()); @@ -267,9 +269,8 @@ lto_module_t lto_module_create_in_local_context(const void *mem, size_t length, std::unique_ptr Context = llvm::make_unique(); Context->setDiagnosticHandler(diagnosticHandler, nullptr, true); - ErrorOr> M = - LTOModule::createInLocalContext(std::move(Context), mem, length, Options, - path); + ErrorOr> M = LTOModule::createInLocalContext( + std::move(Context), mem, length, Options, StringRef(path)); if (!M) return nullptr; return wrap(M->release()); @@ -282,7 +283,7 @@ lto_module_t lto_module_create_in_codegen_context(const void *mem, lto_initialize(); llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); ErrorOr> M = LTOModule::createFromBuffer( - unwrap(cg)->getContext(), mem, length, Options, path); + unwrap(cg)->getContext(), mem, length, Options, StringRef(path)); return wrap(M->release()); } @@ -293,7 +294,7 @@ const char* lto_module_get_target_triple(lto_module_t mod) { } void lto_module_set_target_triple(lto_module_t mod, const char *triple) { - return unwrap(mod)->setTargetTriple(triple); + return unwrap(mod)->setTargetTriple(StringRef(triple)); } unsigned int lto_module_get_num_symbols(lto_module_t mod) { @@ -301,7 +302,7 @@ unsigned int lto_module_get_num_symbols(lto_module_t mod) { } const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) { - return unwrap(mod)->getSymbolName(index); + return unwrap(mod)->getSymbolName(index).data(); } lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod, @@ -310,7 +311,7 @@ lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod, } const char* lto_module_get_linkeropts(lto_module_t mod) { - return unwrap(mod)->getLinkerOpts(); + return unwrap(mod)->getLinkerOpts().data(); } void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,