From e753cbc6858ca9506d7c889dc1a55b845dbf958f Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Fri, 9 Mar 2012 05:27:43 +0000 Subject: [PATCH] Fix MSVC incompatibilities. llvm-svn: 152389 --- lld/CMakeLists.txt | 16 ++++++++------ lld/include/lld/Core/File.h | 4 ++-- lld/lib/Core/NativeWriter.cpp | 6 ++--- lld/lib/Core/YamlReader.cpp | 25 ++++++++++++++++----- lld/lib/Core/YamlWriter.cpp | 35 ++++++++++++++---------------- lld/test/multiple-def-error.objtxt | 5 +++-- 6 files changed, 52 insertions(+), 39 deletions(-) diff --git a/lld/CMakeLists.txt b/lld/CMakeLists.txt index 2c92ea693499..a09f9ecb58bf 100644 --- a/lld/CMakeLists.txt +++ b/lld/CMakeLists.txt @@ -64,13 +64,15 @@ endif() # lld now requires C++11 to build # # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") -include(CheckCXXCompilerFlag) -check_cxx_compiler_flag("-std=c++0x" SUPPORTS_CXX11_FLAG) -if( SUPPORTS_CXX11_FLAG ) - message(STATUS "Building with -std=c++0x") - set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS}") -else( SUPPORTS_CXX11_FLAG ) - message(WARNING "-std=c++0x not supported.") +if (NOT MSVC) + include(CheckCXXCompilerFlag) + check_cxx_compiler_flag("-std=c++0x" SUPPORTS_CXX11_FLAG) + if( SUPPORTS_CXX11_FLAG ) + message(STATUS "Building with -std=c++0x") + set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS}") + else( SUPPORTS_CXX11_FLAG ) + message(WARNING "-std=c++0x not supported.") + endif() endif() diff --git a/lld/include/lld/Core/File.h b/lld/include/lld/Core/File.h index 741244692b15..375b607f7fae 100644 --- a/lld/include/lld/Core/File.h +++ b/lld/include/lld/Core/File.h @@ -188,11 +188,11 @@ protected: class atom_collection_vector : public atom_collection { public: virtual atom_iterator begin() const { - return atom_iterator(*this, reinterpret_cast(&_atoms[0])); + return atom_iterator(*this, reinterpret_cast(_atoms.data())); } virtual atom_iterator end() const{ return atom_iterator(*this, reinterpret_cast - (&_atoms[_atoms.size()])); + (_atoms.data() + _atoms.size())); } virtual const T* deref(const void* it) const { return *reinterpret_cast(it); diff --git a/lld/lib/Core/NativeWriter.cpp b/lld/lib/Core/NativeWriter.cpp index 3cc49cc3edb3..e65573e38d49 100644 --- a/lld/lib/Core/NativeWriter.cpp +++ b/lld/lib/Core/NativeWriter.cpp @@ -335,8 +335,7 @@ private: } // first use of this library name uint32_t result = this->getNameOffset(name); - _sharedLibraryNames.push_back( - std::make_pair(name, result)); + _sharedLibraryNames.push_back(std::make_pair(name, result)); return result; } @@ -390,8 +389,7 @@ private: } // first use of this section name uint32_t result = this->getNameOffset(name); - _sectionNames.push_back( - std::make_pair(name, result)); + _sectionNames.push_back(std::make_pair(name, result)); return result; } diff --git a/lld/lib/Core/YamlReader.cpp b/lld/lib/Core/YamlReader.cpp index 31f999e3d241..98201f8b046e 100644 --- a/lld/lib/Core/YamlReader.cpp +++ b/lld/lib/Core/YamlReader.cpp @@ -22,6 +22,7 @@ #include "lld/Platform/Platform.h" +#include "llvm/ADT/APInt.h" #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/ArrayRef.h" @@ -934,7 +935,9 @@ llvm::error_code parseObjectText( llvm::MemoryBuffer *mb haveAtom = true; } else if (strcmp(entry->key, KeyValues::valueKeyword) == 0) { - llvm::StringRef(entry->value).getAsInteger(0, atomState._value); + llvm::APInt Val; + llvm::StringRef(entry->value).getAsInteger(0, Val); + atomState._value = Val.getZExtValue(); haveAtom = true; } else { @@ -953,8 +956,9 @@ llvm::error_code parseObjectText( llvm::MemoryBuffer *mb haveFixup = true; } else if (strcmp(entry->key, KeyValues::fixupsOffsetKeyword) == 0) { - llvm::StringRef(entry->value).getAsInteger(0, - atomState._ref._offsetInAtom); + llvm::APInt Val; + llvm::StringRef(entry->value).getAsInteger(0, Val); + atomState._ref._offsetInAtom = Val.getZExtValue(); haveFixup = true; } else if (strcmp(entry->key, KeyValues::fixupsTargetKeyword) == 0) { @@ -962,8 +966,19 @@ llvm::error_code parseObjectText( llvm::MemoryBuffer *mb haveFixup = true; } else if (strcmp(entry->key, KeyValues::fixupsAddendKeyword) == 0) { - llvm::StringRef(entry->value).getAsInteger(0, - atomState._ref._addend); + llvm::APInt Val; + // HACK: getAsInteger for APInt doesn't handle negative values + // the same as other getAsInteger functions. And getAsInteger + // doesn't work on all platforms for {,u}int64_t. So manually + // handle this until getAsInteger is fixed. + bool IsNeg = false; + llvm::StringRef Addend(entry->value); + if (Addend.find('-') == 0) { + IsNeg = true; + Addend = Addend.substr(1); + } + Addend.getAsInteger(0, Val); + atomState._ref._addend = Val.getSExtValue() * (IsNeg ? -1 : 1); haveFixup = true; } } diff --git a/lld/lib/Core/YamlWriter.cpp b/lld/lib/Core/YamlWriter.cpp index f8440667c9d9..2a9e05078ea7 100644 --- a/lld/lib/Core/YamlWriter.cpp +++ b/lld/lib/Core/YamlWriter.cpp @@ -19,10 +19,13 @@ #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/StringMap.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/DataTypes.h" +#include "llvm/Support/Format.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/raw_ostream.h" #include "llvm/Support/system_error.h" #include @@ -62,9 +65,10 @@ public: const Reference* ref = *rit; // create refname for any unnamed reference target if ( ref->target()->name().empty() ) { - char* buffer; - asprintf(&buffer, "L%03d", _unnamedCounter++); - _refNames[ref->target()] = buffer; + std::string Storage; + llvm::raw_string_ostream Buffer(Storage); + Buffer << llvm::format("L%03d", _unnamedCounter++); + _refNames[ref->target()] = Buffer.str(); } } } @@ -92,15 +96,16 @@ public: NameToAtom::iterator pos = _nameMap.find(atom.name()); if ( pos != _nameMap.end() ) { // Found name collision, give each a unique ref-name. - char* buffer; - asprintf(&buffer, "%s.%03d", atom.name().data(), ++_collisionCount); - _refNames[&atom] = buffer; + std::string Storage; + llvm::raw_string_ostream Buffer(Storage); + Buffer << atom.name() << llvm::format(".%03d", ++_collisionCount); + _refNames[&atom] = Buffer.str(); const Atom* prevAtom = pos->second; AtomToRefName::iterator pos2 = _refNames.find(prevAtom); if ( pos2 == _refNames.end() ) { // only create ref-name for previous if none already created - asprintf(&buffer, "%s.%03d", prevAtom->name().data(), ++_collisionCount); - _refNames[prevAtom] = buffer; + Buffer << prevAtom->name() << llvm::format(".%03d", ++_collisionCount); + _refNames[prevAtom] = Buffer.str(); } } else { @@ -113,21 +118,13 @@ public: return _refNames.count(atom); } - const char* refName(const Atom* atom) { + llvm::StringRef refName(const Atom* atom) { return _refNames.find(atom)->second; } private: - struct MyMappingInfo { - static llvm::StringRef getEmptyKey() { return llvm::StringRef(); } - static llvm::StringRef getTombstoneKey() { return llvm::StringRef(" ", 0); } - static unsigned getHashValue(llvm::StringRef const val) { - return llvm::HashString(val); } - static bool isEqual(llvm::StringRef const lhs, - llvm::StringRef const rhs) { return lhs.equals(rhs); } - }; - typedef llvm::DenseMap NameToAtom; - typedef llvm::DenseMap AtomToRefName; + typedef llvm::StringMap NameToAtom; + typedef llvm::DenseMap AtomToRefName; unsigned int _collisionCount; unsigned int _unnamedCounter; diff --git a/lld/test/multiple-def-error.objtxt b/lld/test/multiple-def-error.objtxt index 068e257cdd23..3017ad649458 100644 --- a/lld/test/multiple-def-error.objtxt +++ b/lld/test/multiple-def-error.objtxt @@ -1,9 +1,11 @@ -# RUN: lld-core %s 2>&1 | grep "duplicate symbol" +# RUN: lld-core %s 2>&1 | FileCheck %s # # Test that multiple definitions cause an error # +# CHECK: duplicate symbol + --- atoms: - name: _foo @@ -17,4 +19,3 @@ atoms: scope: global type: data ... -