Skip to content

Commit

Permalink
Add AddNull argument to CreateGlobalString. (#93036)
Browse files Browse the repository at this point in the history
There's currently no way to control whether a null terminator should be
appended to the string created in `CreateGlobalString` /
`CreateGlobalStringPtr`, since the methods don't expose an additional
argument.
This change adds an additional argument to the methods that has the same
default value, `true`, as in `ConstantDataArray::getString`, and passes
it down to this internal method.
  • Loading branch information
tsymalla committed May 22, 2024
1 parent 2bde13c commit cb6a623
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions llvm/include/llvm/IR/IRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ class IRBuilderBase {
/// block.
GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
unsigned AddressSpace = 0,
Module *M = nullptr);
Module *M = nullptr, bool AddNull = true);

/// Get a constant value representing either true or false.
ConstantInt *getInt1(bool V) {
Expand Down Expand Up @@ -1992,8 +1992,9 @@ class IRBuilderBase {
/// block.
Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
unsigned AddressSpace = 0,
Module *M = nullptr) {
GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace, M);
Module *M = nullptr, bool AddNull = true) {
GlobalVariable *GV =
CreateGlobalString(Str, Name, AddressSpace, M, AddNull);
Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
Constant *Indices[] = {Zero, Zero};
return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV,
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/IR/IRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ using namespace llvm;
GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef Str,
const Twine &Name,
unsigned AddressSpace,
Module *M) {
Constant *StrConstant = ConstantDataArray::getString(Context, Str);
Module *M, bool AddNull) {
Constant *StrConstant = ConstantDataArray::getString(Context, Str, AddNull);
if (!M)
M = BB->getParent()->getParent();
auto *GV = new GlobalVariable(
Expand Down

0 comments on commit cb6a623

Please sign in to comment.