llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang-codegen Author: Chris B (llvm-beanz) <details> <summary>Changes</summary> This API takes a const char* with a default nullptr value and immdiately passes it down to an API taking a StringRef. All of the places this is called from are either using compile time string literals, the default argument, or string objects that have known length. Discarding the length known from a calling API to just have to strlen it to call the next layer down that requires a StringRef is a bit silly, so this change updates CodeGenModule::GetAddrOfConstantCString to use StringRef instead of const char* for the GlobalName parameter. It might be worth also replacing the first parameter with an llvm ADT type that avoids allocation, but that change would have wider impact so we should consider it separately. --- Full diff: https://github.com/llvm/llvm-project/pull/154179.diff 6 Files Affected: - (modified) clang/lib/CodeGen/CGCUDANV.cpp (+1-1) - (modified) clang/lib/CodeGen/CGExpr.cpp (+2-2) - (modified) clang/lib/CodeGen/CGHLSLRuntime.cpp (+2-2) - (modified) clang/lib/CodeGen/CGObjCGNU.cpp (+1-1) - (modified) clang/lib/CodeGen/CodeGenModule.cpp (+1-4) - (modified) clang/lib/CodeGen/CodeGenModule.h (+1-1) ``````````diff diff --git a/clang/lib/CodeGen/CGCUDANV.cpp b/clang/lib/CodeGen/CGCUDANV.cpp index c7f4bf8a21354..73403c71e4052 100644 --- a/clang/lib/CodeGen/CGCUDANV.cpp +++ b/clang/lib/CodeGen/CGCUDANV.cpp @@ -94,7 +94,7 @@ class CGNVCUDARuntime : public CGCUDARuntime { /// where the C code specifies const char*. llvm::Constant *makeConstantString(const std::string &Str, const std::string &Name = "") { - return CGM.GetAddrOfConstantCString(Str, Name.c_str()).getPointer(); + return CGM.GetAddrOfConstantCString(Str, Name).getPointer(); } /// Helper function which generates an initialized constant array from Str, diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index d5df6dd3e303c..94bb5919a2160 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -3444,11 +3444,11 @@ LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) { CGM.getCXXABI().getMangleContext().getBlockId(BD, true); if (Discriminator) Name += "_" + Twine(Discriminator + 1).str(); - auto C = CGM.GetAddrOfConstantCString(Name, GVName.c_str()); + auto C = CGM.GetAddrOfConstantCString(Name, GVName); return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl); } else { auto C = - CGM.GetAddrOfConstantCString(std::string(FnName), GVName.c_str()); + CGM.GetAddrOfConstantCString(std::string(FnName), GVName); return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl); } } diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp index 9b3ef6aafd05f..64868c962f521 100644 --- a/clang/lib/CodeGen/CGHLSLRuntime.cpp +++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp @@ -593,7 +593,7 @@ static Value *buildNameForResource(llvm::StringRef BaseName, CodeGenModule &CGM) { std::string Str(BaseName); std::string GlobalName(Str + ".str"); - return CGM.GetAddrOfConstantCString(Str, GlobalName.c_str()).getPointer(); + return CGM.GetAddrOfConstantCString(Str, GlobalName).getPointer(); } void CGHLSLRuntime::initializeBufferFromBinding(const HLSLBufferDecl *BufDecl, @@ -632,7 +632,7 @@ void CGHLSLRuntime::initializeBufferFromBinding(const HLSLBufferDecl *BufDecl, std::string Str(BufDecl->getName()); std::string GlobalName(Str + ".str"); - Name = CGM.GetAddrOfConstantCString(Str, GlobalName.c_str()).getPointer(); + Name = CGM.GetAddrOfConstantCString(Str, GlobalName).getPointer(); // buffer with explicit binding if (RBA->hasRegisterSlot()) { diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp index e4147de8fc639..06643d4bdc211 100644 --- a/clang/lib/CodeGen/CGObjCGNU.cpp +++ b/clang/lib/CodeGen/CGObjCGNU.cpp @@ -195,7 +195,7 @@ class CGObjCGNU : public CGObjCRuntime { /// Helper function that generates a constant string and returns a pointer to /// the start of the string. The result of this function can be used anywhere /// where the C code specifies const char*. - llvm::Constant *MakeConstantString(StringRef Str, const char *Name = "") { + llvm::Constant *MakeConstantString(StringRef Str, StringRef Name = "") { ConstantAddress Array = CGM.GetAddrOfConstantCString(std::string(Str), Name); return Array.getPointer(); diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 414687640bf2d..f593ea1bb2356 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -6921,7 +6921,7 @@ CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { /// the literal and a terminating '\0' character. /// The result has pointer to array type. ConstantAddress CodeGenModule::GetAddrOfConstantCString( - const std::string &Str, const char *GlobalName) { + const std::string &Str, StringRef GlobalName) { StringRef StrWithNull(Str.c_str(), Str.size() + 1); CharUnits Alignment = getContext().getAlignOfGlobalVarInChars( getContext().CharTy, /*VD=*/nullptr); @@ -6941,9 +6941,6 @@ ConstantAddress CodeGenModule::GetAddrOfConstantCString( } } - // Get the default prefix if a name wasn't specified. - if (!GlobalName) - GlobalName = ".str"; // Create a global variable for this. auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this, GlobalName, Alignment); diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index 705d9a3cb9de3..92e4641a42af7 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -1187,7 +1187,7 @@ class CodeGenModule : public CodeGenTypeCache { /// created). ConstantAddress GetAddrOfConstantCString(const std::string &Str, - const char *GlobalName = nullptr); + StringRef GlobalName = ".str"); /// Returns a pointer to a constant global variable for the given file-scope /// compound literal expression. `````````` </details> https://github.com/llvm/llvm-project/pull/154179 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits