[lld] r322423 - [WebAssembly] Use ArrayRef over raw pointers

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 12 16:22:00 PST 2018


Author: sbc
Date: Fri Jan 12 16:22:00 2018
New Revision: 322423

URL: http://llvm.org/viewvc/llvm-project?rev=322423&view=rev
Log:
[WebAssembly] Use ArrayRef over raw pointers

Differential Revision: https://reviews.llvm.org/D42013

Modified:
    lld/trunk/wasm/InputChunks.cpp
    lld/trunk/wasm/InputChunks.h
    lld/trunk/wasm/Writer.cpp

Modified: lld/trunk/wasm/InputChunks.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputChunks.cpp?rev=322423&r1=322422&r2=322423&view=diff
==============================================================================
--- lld/trunk/wasm/InputChunks.cpp (original)
+++ lld/trunk/wasm/InputChunks.cpp Fri Jan 12 16:22:00 2018
@@ -86,7 +86,7 @@ static void applyRelocations(uint8_t *Bu
 }
 
 void InputChunk::writeTo(uint8_t *SectionStart) const {
-  memcpy(SectionStart + getOutputOffset(), getData(), getSize());
+  memcpy(SectionStart + getOutputOffset(), data().data(), data().size());
   applyRelocations(SectionStart, OutRelocations);
 }
 

Modified: lld/trunk/wasm/InputChunks.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputChunks.h?rev=322423&r1=322422&r2=322423&view=diff
==============================================================================
--- lld/trunk/wasm/InputChunks.h (original)
+++ lld/trunk/wasm/InputChunks.h Fri Jan 12 16:22:00 2018
@@ -56,7 +56,7 @@ protected:
   InputChunk(const ObjFile *F) : File(F) {}
   virtual ~InputChunk() = default;
   void calcRelocations();
-  virtual const uint8_t *getData() const = 0;
+  virtual ArrayRef<uint8_t> data() const = 0;
   virtual uint32_t getInputSectionOffset() const = 0;
 
   std::vector<WasmRelocation> Relocations;
@@ -88,9 +88,6 @@ public:
     OutputSegmentOffset = Offset;
   }
 
-  const uint8_t *getData() const override {
-    return Segment.Data.Content.data();
-  }
   uint32_t getSize() const override { return Segment.Data.Content.size(); }
   uint32_t getAlignment() const { return Segment.Data.Alignment; }
   uint32_t startVA() const { return Segment.Data.Offset.Value.Int32; }
@@ -101,6 +98,7 @@ public:
   int32_t OutputSegmentOffset = 0;
 
 protected:
+  ArrayRef<uint8_t> data() const override { return Segment.Data.Content; }
   uint32_t getInputSectionOffset() const override {
     return Segment.SectionOffset;
   }
@@ -118,9 +116,6 @@ public:
       : InputChunk(F), Signature(S), WrittenToNameSec(false), Function(Func) {}
 
   uint32_t getSize() const override { return Function->Size; }
-  const uint8_t *getData() const override {
-    return File->CodeSection->Content.data() + getInputSectionOffset();
-  }
   StringRef getComdat() const override { return Function->Comdat; }
   uint32_t getOutputIndex() const { return OutputIndex.getValue(); };
   bool hasOutputIndex() const { return OutputIndex.hasValue(); };
@@ -131,25 +126,28 @@ public:
   unsigned WrittenToNameSec : 1;
 
 protected:
+  ArrayRef<uint8_t> data() const override {
+    return File->CodeSection->Content.slice(getInputSectionOffset(), getSize());
+  }
   uint32_t getInputSectionOffset() const override {
     return Function->CodeSectionOffset;
   }
+
   const WasmFunction *Function;
   llvm::Optional<uint32_t> OutputIndex;
 };
 
 class SyntheticFunction : public InputFunction {
 public:
-  SyntheticFunction(const WasmSignature &S, StringRef Body)
+  SyntheticFunction(const WasmSignature &S, ArrayRef<uint8_t> Body)
       : InputFunction(S, nullptr, nullptr), Body(Body) {}
 
   uint32_t getSize() const override { return Body.size(); }
-  const uint8_t *getData() const override {
-    return reinterpret_cast<const uint8_t *>(Body.data());
-  }
 
 protected:
-  StringRef Body;
+  ArrayRef<uint8_t> data() const override { return Body; }
+
+  ArrayRef<uint8_t> Body;
 };
 
 } // namespace wasm

Modified: lld/trunk/wasm/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Writer.cpp?rev=322423&r1=322422&r2=322423&view=diff
==============================================================================
--- lld/trunk/wasm/Writer.cpp (original)
+++ lld/trunk/wasm/Writer.cpp Fri Jan 12 16:22:00 2018
@@ -770,8 +770,10 @@ void Writer::createCtorFunction() {
   writeUleb128(OS, FunctionBody.size(), "function size");
   OS.flush();
   CtorFunctionBody += FunctionBody;
-  CtorFunction =
-      llvm::make_unique<SyntheticFunction>(Signature, CtorFunctionBody);
+  ArrayRef<uint8_t> BodyArray(
+      reinterpret_cast<const uint8_t *>(CtorFunctionBody.data()),
+      CtorFunctionBody.size());
+  CtorFunction = llvm::make_unique<SyntheticFunction>(Signature, BodyArray);
   DefinedFunctions.emplace_back(CtorFunction.get());
 }
 




More information about the llvm-commits mailing list