Skip to content

Commit

Permalink
SymbolExternalizer: Rename externalized operand of sizeof
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Paulo de Souza <[email protected]>
  • Loading branch information
marcosps authored and giulianobelinassi committed Jun 14, 2024
1 parent 0925bdb commit 8376ab2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
29 changes: 29 additions & 0 deletions libcextract/SymbolExternalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<VarDecl>(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. */
Expand Down
17 changes: 17 additions & 0 deletions testsuite/ccp/ext-obj-5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* { dg-options "-DCE_EXTRACT_FUNCTIONS=pu_f -DCE_EXPORT_SYMBOLS=ee_o" }*/

#include <assert.h>

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\)\)\];" } } */

0 comments on commit 8376ab2

Please sign in to comment.