Question

Look at the LLVM Demo, the offical way to create a constant string is:

Constant* consStr = ConstantArray::get(mod->getContext(), "hello", true);

However, it doesn't work! I aways get compile error about:

test.cc:860:78: error: no matching function for call to llvm::ConstantArray::get(llvm::LLVMContext&, char*&, bool)’
/remote/vgrnd66/wli/Tools/llvm-3.1/include/llvm/Constants.h:354:20: note: candidate is: static llvm::Constant* llvm::ConstantArray::get(llvm::ArrayType*, llvm::ArrayRef<llvm::Constant*>)

Look at the llvm source code, there is no member function to support

llvm::ConstantArray::get(llvm::LLVMContext&, char*&, bool)

I am using llvm3.1.

Is there anything wrong with my code or this constructor has been removed in new source?

Here is the LLVM source code difference.

LLVM2.8

class ConstantArray : public Constant {

  // ConstantArray accessors
  static Constant *get(const ArrayType *T, const std::vector<Constant*> &V);
  static Constant *get(const ArrayType *T, Constant *const *Vals,
                       unsigned NumVals);

  /// This method constructs a ConstantArray and initializes it with a text
  /// string. The default behavior (AddNull==true) causes a null terminator to
  /// be placed at the end of the array. This effectively increases the length
  /// of the array by one (you've been warned).  However, in some situations
  /// this is not desired so if AddNull==false then the string is copied without
  /// null termination.
  static Constant *get(LLVMContext &Context, StringRef Initializer,
                       bool AddNull = true);
}

LLVM3.1

class ConstantArray : public Constant {
public:
  // ConstantArray accessors
  static Constant *get(ArrayType *T, ArrayRef<Constant*> V);

}

Apparently, there are 3 constructor in 2.8 but only one constructor in 3.1 for ConstantArray. Now I don't know how to create a constant string... :(

Any help is appreicated!

Thanks!

Was it helpful?

Solution

OK. I found it has been moved to ConstantDataArray. It seems the LLVM demo cgi is out-of-date:).

class ConstantDataArray : public ConstantDataSequential {
  /// getString - This method constructs a CDS and initializes it with a text
  /// string. The default behavior (AddNull==true) causes a null terminator to
  /// be placed at the end of the array (increasing the length of the string by
  /// one more than the StringRef would normally indicate.  Pass AddNull=false
  /// to disable this behavior.
  static Constant *getString(LLVMContext &Context, StringRef Initializer,
                             bool AddNull = true);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top