Skip to content

Commit

Permalink
SymbolExternalizer: Use visitor to get the array size expression
Browse files Browse the repository at this point in the history
Extend current ccp test to catch a regression found.

Signed-off-by: Marcos Paulo de Souza <[email protected]>
  • Loading branch information
marcosps committed Jun 14, 2024
1 parent 8376ab2 commit df8d4ff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
45 changes: 16 additions & 29 deletions libcextract/SymbolExternalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ class TypeUpdaterVisitor : public RecursiveASTVisitor<TypeUpdaterVisitor>
return VISITOR_CONTINUE;
}

/* To handle symbol renames on situations like the sizeof below
*
* static char x[4];
*
* void f(void) {
* char y[sizeof(x)];
* }
*/
bool VisitArrayTypeLoc(const ArrayTypeLoc &type)
{
SymbolExternalizer::FunctionUpdater fu(SE, NewSymbolDecl, OldSymbolName, Wrap);
fu.Update_References_To_Symbol(type.getSizeExpr());

return VISITOR_CONTINUE;
}

private:

/** A reference to SymbolExternalizer. */
Expand Down Expand Up @@ -513,35 +529,6 @@ 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
5 changes: 2 additions & 3 deletions testsuite/ccp/ext-obj-5.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ static char ee_o[] = "123";

void pu_f(void)
{
char s[sizeof(ee_o)];
char t[sizeof(ee_o)][sizeof(ee_o)];
char s[sizeof(ee_o)], 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\)\)\];" } } */
/* { dg-final { scan-tree-dump "char s\[sizeof\(\(\*klpe_ee_o\)\)\], t\[sizeof\(\(\*klpe_ee_o\)\)\]\[sizeof\(\(\*klpe_ee_o\)\)\];" } } */

0 comments on commit df8d4ff

Please sign in to comment.