Skip to content

Commit 025c78f

Browse files
committed
[LLVM-C] Use Length-Providing Value Name Getters and Setters
Summary: - Provide LLVMGetValueName2 and LLVMSetValueName2 that return and take the length of the provided C string respectively - Deprecate LLVMGetValueName and LLVMSetValueName Reviewers: whitequark, deadalnix Reviewed By: whitequark Subscribers: llvm-commits, harlanhaskins Differential Revision: https://reviews.llvm.org/D46890 llvm-svn: 332810
1 parent c0b268f commit 025c78f

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

llvm/include/llvm-c/Core.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,14 +1496,14 @@ LLVMValueKind LLVMGetValueKind(LLVMValueRef Val);
14961496
*
14971497
* @see llvm::Value::getName()
14981498
*/
1499-
const char *LLVMGetValueName(LLVMValueRef Val);
1499+
const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length);
15001500

15011501
/**
15021502
* Set the string name of a value.
15031503
*
15041504
* @see llvm::Value::setName()
15051505
*/
1506-
void LLVMSetValueName(LLVMValueRef Val, const char *Name);
1506+
void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen);
15071507

15081508
/**
15091509
* Dump a representation of a value to stderr.
@@ -1555,6 +1555,11 @@ LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DECLARE_VALUE_CAST)
15551555
LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val);
15561556
LLVMValueRef LLVMIsAMDString(LLVMValueRef Val);
15571557

1558+
/** Deprecated: Use LLVMGetValueName2 instead. */
1559+
const char *LLVMGetValueName(LLVMValueRef Val);
1560+
/** Deprecated: Use LLVMSetValueName2 instead. */
1561+
void LLVMSetValueName(LLVMValueRef Val, const char *Name);
1562+
15581563
/**
15591564
* @}
15601565
*/

llvm/lib/IR/Core.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,16 @@ LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
796796
}
797797
}
798798

799+
const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
800+
auto *V = unwrap(Val);
801+
*Length = V->getName().size();
802+
return V->getName().data();
803+
}
804+
805+
void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
806+
unwrap(Val)->setName(StringRef(Name, NameLen));
807+
}
808+
799809
const char *LLVMGetValueName(LLVMValueRef Val) {
800810
return unwrap(Val)->getName().data();
801811
}

llvm/tools/llvm-c-test/echo.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) {
174174
LLVMValueRef SrcNext = nullptr;
175175
LLVMValueRef DstNext = nullptr;
176176
while (true) {
177-
const char *Name = LLVMGetValueName(SrcCur);
178-
LLVMSetValueName(DstCur, Name);
177+
size_t NameLen;
178+
const char *Name = LLVMGetValueName2(SrcCur, &NameLen);
179+
LLVMSetValueName2(DstCur, Name, NameLen);
179180

180181
VMap[SrcCur] = DstCur;
181182

@@ -232,7 +233,8 @@ static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
232233

233234
// Maybe it is a symbol
234235
if (LLVMIsAGlobalValue(Cst)) {
235-
const char *Name = LLVMGetValueName(Cst);
236+
size_t NameLen;
237+
const char *Name = LLVMGetValueName2(Cst, &NameLen);
236238

237239
// Try function
238240
if (LLVMIsAFunction(Cst)) {
@@ -402,7 +404,8 @@ struct FunCloner {
402404
if (!LLVMIsAInstruction(Src))
403405
report_fatal_error("Expected an instruction");
404406

405-
const char *Name = LLVMGetValueName(Src);
407+
size_t NameLen;
408+
const char *Name = LLVMGetValueName2(Src, &NameLen);
406409

407410
// Check if this is something we already computed.
408411
{
@@ -734,7 +737,8 @@ struct FunCloner {
734737
report_fatal_error("Basic block is not a basic block");
735738

736739
const char *Name = LLVMGetBasicBlockName(Src);
737-
const char *VName = LLVMGetValueName(V);
740+
size_t NameLen;
741+
const char *VName = LLVMGetValueName2(V, &NameLen);
738742
if (Name != VName)
739743
report_fatal_error("Basic block name mismatch");
740744

@@ -830,7 +834,8 @@ static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
830834
}
831835

832836
while (true) {
833-
const char *Name = LLVMGetValueName(Cur);
837+
size_t NameLen;
838+
const char *Name = LLVMGetValueName2(Cur, &NameLen);
834839
if (LLVMGetNamedGlobal(M, Name))
835840
report_fatal_error("GlobalVariable already cloned");
836841
LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
@@ -863,7 +868,8 @@ static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
863868
Cur = Begin;
864869
Next = nullptr;
865870
while (true) {
866-
const char *Name = LLVMGetValueName(Cur);
871+
size_t NameLen;
872+
const char *Name = LLVMGetValueName2(Cur, &NameLen);
867873
if (LLVMGetNamedFunction(M, Name))
868874
report_fatal_error("Function already cloned");
869875
auto Ty = LLVMGetElementType(TypeCloner(M).Clone(Cur));
@@ -909,7 +915,8 @@ static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
909915
}
910916

911917
while (true) {
912-
const char *Name = LLVMGetValueName(Cur);
918+
size_t NameLen;
919+
const char *Name = LLVMGetValueName2(Cur, &NameLen);
913920
LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
914921
if (!G)
915922
report_fatal_error("GlobalVariable must have been declared already");
@@ -952,13 +959,16 @@ static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
952959
Cur = Begin;
953960
Next = nullptr;
954961
while (true) {
955-
const char *Name = LLVMGetValueName(Cur);
962+
size_t NameLen;
963+
const char *Name = LLVMGetValueName2(Cur, &NameLen);
956964
LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
957965
if (!Fun)
958966
report_fatal_error("Function must have been declared already");
959967

960968
if (LLVMHasPersonalityFn(Cur)) {
961-
const char *FName = LLVMGetValueName(LLVMGetPersonalityFn(Cur));
969+
size_t FNameLen;
970+
const char *FName = LLVMGetValueName2(LLVMGetPersonalityFn(Cur),
971+
&FNameLen);
962972
LLVMValueRef P = LLVMGetNamedFunction(M, FName);
963973
if (!P)
964974
report_fatal_error("Could not find personality function");

0 commit comments

Comments
 (0)