Author: Chris B Date: 2025-09-07T16:05:37-05:00 New Revision: 799d3466fa97e24082cb036e71a7a92f72597b4e
URL: https://github.com/llvm/llvm-project/commit/799d3466fa97e24082cb036e71a7a92f72597b4e DIFF: https://github.com/llvm/llvm-project/commit/799d3466fa97e24082cb036e71a7a92f72597b4e.diff LOG: [NFC] Change const char* to StringRef (#154179) 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. Added: Modified: clang/lib/CodeGen/CGCUDANV.cpp clang/lib/CodeGen/CGExpr.cpp clang/lib/CodeGen/CGObjCGNU.cpp clang/lib/CodeGen/CodeGenModule.cpp clang/lib/CodeGen/CodeGenModule.h Removed: ################################################################################ diff --git a/clang/lib/CodeGen/CGCUDANV.cpp b/clang/lib/CodeGen/CGCUDANV.cpp index 5090a0559eab2..cb16fe1b36c68 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 26fba751e6f9d..e8456a44f8367 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -3513,11 +3513,10 @@ 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()); + auto C = CGM.GetAddrOfConstantCString(std::string(FnName), GVName); return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl); } } 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 f1ddaa875f3e4..c0cfc24f02877 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -6926,8 +6926,8 @@ CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { /// GetAddrOfConstantCString - Returns a pointer to a character array containing /// the literal and a terminating '\0' character. /// The result has pointer to array type. -ConstantAddress CodeGenModule::GetAddrOfConstantCString( - const std::string &Str, const char *GlobalName) { +ConstantAddress CodeGenModule::GetAddrOfConstantCString(const std::string &Str, + StringRef GlobalName) { StringRef StrWithNull(Str.c_str(), Str.size() + 1); CharUnits Alignment = getContext().getAlignOfGlobalVarInChars( getContext().CharTy, /*VD=*/nullptr); @@ -6947,9 +6947,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 b4b3a17662045..f62350fd8d378 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -1186,9 +1186,8 @@ class CodeGenModule : public CodeGenTypeCache { /// /// \param GlobalName If provided, the name to use for the global (if one is /// created). - ConstantAddress - GetAddrOfConstantCString(const std::string &Str, - const char *GlobalName = nullptr); + ConstantAddress GetAddrOfConstantCString(const std::string &Str, + StringRef GlobalName = ".str"); /// Returns a pointer to a constant global variable for the given file-scope /// compound literal expression. _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits