Skip to content

Commit 799d346

Browse files
authored
[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.
1 parent c4d927c commit 799d346

File tree

5 files changed

+8
-13
lines changed

5 files changed

+8
-13
lines changed

clang/lib/CodeGen/CGCUDANV.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class CGNVCUDARuntime : public CGCUDARuntime {
9494
/// where the C code specifies const char*.
9595
llvm::Constant *makeConstantString(const std::string &Str,
9696
const std::string &Name = "") {
97-
return CGM.GetAddrOfConstantCString(Str, Name.c_str()).getPointer();
97+
return CGM.GetAddrOfConstantCString(Str, Name).getPointer();
9898
}
9999

100100
/// Helper function which generates an initialized constant array from Str,

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3513,11 +3513,10 @@ LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
35133513
CGM.getCXXABI().getMangleContext().getBlockId(BD, true);
35143514
if (Discriminator)
35153515
Name += "_" + Twine(Discriminator + 1).str();
3516-
auto C = CGM.GetAddrOfConstantCString(Name, GVName.c_str());
3516+
auto C = CGM.GetAddrOfConstantCString(Name, GVName);
35173517
return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl);
35183518
} else {
3519-
auto C =
3520-
CGM.GetAddrOfConstantCString(std::string(FnName), GVName.c_str());
3519+
auto C = CGM.GetAddrOfConstantCString(std::string(FnName), GVName);
35213520
return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl);
35223521
}
35233522
}

clang/lib/CodeGen/CGObjCGNU.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class CGObjCGNU : public CGObjCRuntime {
195195
/// Helper function that generates a constant string and returns a pointer to
196196
/// the start of the string. The result of this function can be used anywhere
197197
/// where the C code specifies const char*.
198-
llvm::Constant *MakeConstantString(StringRef Str, const char *Name = "") {
198+
llvm::Constant *MakeConstantString(StringRef Str, StringRef Name = "") {
199199
ConstantAddress Array =
200200
CGM.GetAddrOfConstantCString(std::string(Str), Name);
201201
return Array.getPointer();

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6926,8 +6926,8 @@ CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
69266926
/// GetAddrOfConstantCString - Returns a pointer to a character array containing
69276927
/// the literal and a terminating '\0' character.
69286928
/// The result has pointer to array type.
6929-
ConstantAddress CodeGenModule::GetAddrOfConstantCString(
6930-
const std::string &Str, const char *GlobalName) {
6929+
ConstantAddress CodeGenModule::GetAddrOfConstantCString(const std::string &Str,
6930+
StringRef GlobalName) {
69316931
StringRef StrWithNull(Str.c_str(), Str.size() + 1);
69326932
CharUnits Alignment = getContext().getAlignOfGlobalVarInChars(
69336933
getContext().CharTy, /*VD=*/nullptr);
@@ -6947,9 +6947,6 @@ ConstantAddress CodeGenModule::GetAddrOfConstantCString(
69476947
}
69486948
}
69496949

6950-
// Get the default prefix if a name wasn't specified.
6951-
if (!GlobalName)
6952-
GlobalName = ".str";
69536950
// Create a global variable for this.
69546951
auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this,
69556952
GlobalName, Alignment);

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,9 +1186,8 @@ class CodeGenModule : public CodeGenTypeCache {
11861186
///
11871187
/// \param GlobalName If provided, the name to use for the global (if one is
11881188
/// created).
1189-
ConstantAddress
1190-
GetAddrOfConstantCString(const std::string &Str,
1191-
const char *GlobalName = nullptr);
1189+
ConstantAddress GetAddrOfConstantCString(const std::string &Str,
1190+
StringRef GlobalName = ".str");
11921191

11931192
/// Returns a pointer to a constant global variable for the given file-scope
11941193
/// compound literal expression.

0 commit comments

Comments
 (0)