From 8376ab2ecb36d0ed5237d34fa158b2a0a70461e2 Mon Sep 17 00:00:00 2001 From: Marcos Paulo de Souza Date: Fri, 14 Jun 2024 13:23:23 -0300 Subject: [PATCH] SymbolExternalizer: Rename externalized operand of sizeof Signed-off-by: Marcos Paulo de Souza --- libcextract/SymbolExternalizer.cpp | 29 +++++++++++++++++++++++++++++ testsuite/ccp/ext-obj-5.c | 17 +++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 testsuite/ccp/ext-obj-5.c diff --git a/libcextract/SymbolExternalizer.cpp b/libcextract/SymbolExternalizer.cpp index f7f4f70..c5339be 100644 --- a/libcextract/SymbolExternalizer.cpp +++ b/libcextract/SymbolExternalizer.cpp @@ -513,6 +513,35 @@ bool SymbolExternalizer::FunctionUpdater::Update_References_To_Symbol(Stmt *stmt expr->setDecl(NewSymbolDecl); replaced = true; } + } else if (DeclStmt::classof(stmt)) { + /* To handle symbol renames on situations like the sizeof below + * + * static char x[4]; + * + * void f(void) { + * char y[sizeof(x)]; + * } + */ + DeclStmt *dstmt = (DeclStmt *) stmt; + if (VarDecl *ddecl = dyn_cast(dstmt->getSingleDecl())) { + auto vec_of_ranges = Get_Range_Of_Identifier_In_SrcRange(ddecl->getSourceRange(), + OldSymbolName.c_str()); + + if (vec_of_ranges.size() > 0) { + /* Prepare the text modification. */ + std::string new_name; + if (Wrap) { + new_name = NewSymbolDecl->getName().str(); + } else { + new_name = "(*" + NewSymbolDecl->getName().str() + ")"; + } + + for (auto irange : vec_of_ranges) { + /* Issue a text modification. */ + SE.Replace_Text(irange, new_name, 100); + } + } + } } /* Repeat the process to child statements. */ diff --git a/testsuite/ccp/ext-obj-5.c b/testsuite/ccp/ext-obj-5.c new file mode 100644 index 0000000..6abd0dd --- /dev/null +++ b/testsuite/ccp/ext-obj-5.c @@ -0,0 +1,17 @@ +/* { dg-options "-DCE_EXTRACT_FUNCTIONS=pu_f -DCE_EXPORT_SYMBOLS=ee_o" }*/ + +#include + +static char ee_o[] = "123"; + +void pu_f(void) +{ + char s[sizeof(ee_o)]; + char t[sizeof(ee_o)][sizeof(ee_o)]; + static_assert(sizeof(s) == 4); + static_assert(sizeof(t) == 16); + ee_o; +} + +/* { dg-final { scan-tree-dump "static char \(\*klpe_ee_o\)\[4\];" } } */ +/* { dg-final { scan-tree-dump "char s\[sizeof\(\(\*klpe_ee_o\)\)\];" } } */