Skip to content

Add AddNull argument to CreateGlobalString. #93036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2024

Conversation

tsymalla
Copy link
Contributor

@tsymalla tsymalla commented May 22, 2024

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.

There's currently no way to control whether a null terminator should be
appended to the string created in `CreateGlobalString` /
`CreateGlobalString`, 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 `ConstantDataArray::getString`, and passes it
down to this internal method.
@tsymalla tsymalla self-assigned this May 22, 2024
@tsymalla tsymalla requested a review from nikic May 22, 2024 14:25
@llvmbot
Copy link
Member

llvmbot commented May 22, 2024

@llvm/pr-subscribers-llvm-ir

Author: Thomas Symalla (tsymalla)

Changes

There's currently no way to control whether a null terminator should be appended to the string created in CreateGlobalString / CreateGlobalString, 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.


Full diff: https://github.com/llvm/llvm-project/pull/93036.diff

2 Files Affected:

  • (modified) llvm/include/llvm/IR/IRBuilder.h (+4-3)
  • (modified) llvm/lib/IR/IRBuilder.cpp (+2-2)
diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
index 0d8746344a44b..40a9cf507248a 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -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) {
@@ -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,
diff --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp
index 0d6760ed08411..b32799355d692 100644
--- a/llvm/lib/IR/IRBuilder.cpp
+++ b/llvm/lib/IR/IRBuilder.cpp
@@ -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(

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a side note, CreateGlobalStringPtr should be removed in favor of CreateGlobalString, as they are identical since the opaque pointer migration.

@tsymalla tsymalla merged commit cb6a623 into llvm:main May 22, 2024
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants