Skip to content

Commit c7387c5

Browse files
committed
[NFC] Change const char* to StringRef
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 a2225bc commit c7387c5

File tree

5 files changed

+6
-9
lines changed

5 files changed

+6
-9
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3513,11 +3513,11 @@ 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 {
35193519
auto C =
3520-
CGM.GetAddrOfConstantCString(std::string(FnName), GVName.c_str());
3520+
CGM.GetAddrOfConstantCString(std::string(FnName), GVName);
35213521
return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl);
35223522
}
35233523
}

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: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6927,7 +6927,7 @@ CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
69276927
/// the literal and a terminating '\0' character.
69286928
/// The result has pointer to array type.
69296929
ConstantAddress CodeGenModule::GetAddrOfConstantCString(
6930-
const std::string &Str, const char *GlobalName) {
6930+
const std::string &Str, 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ class CodeGenModule : public CodeGenTypeCache {
11881188
/// created).
11891189
ConstantAddress
11901190
GetAddrOfConstantCString(const std::string &Str,
1191-
const char *GlobalName = nullptr);
1191+
StringRef GlobalName = ".str");
11921192

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

0 commit comments

Comments
 (0)