Skip to content

Commit

Permalink
SymbolExternalizer: Make sure we pass a valid location when searching…
Browse files Browse the repository at this point in the history
… identifiers

Signed-off-by: Marcos Paulo de Souza <[email protected]>
  • Loading branch information
marcosps authored and giulianobelinassi committed Sep 20, 2024
1 parent bb815fa commit bfc113f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 27 deletions.
59 changes: 32 additions & 27 deletions libcextract/SymbolExternalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ using namespace llvm;
/* IntervalTree. */
using namespace Intervals;

static SourceRange Get_Range_For_Rewriter(const ASTUnit *ast, const SourceRange &range)
{
const SourceManager &sm = ast->getSourceManager();

/* Get a more precise source range of declaration. */
SourceLocation start = range.getBegin();

/* Some declarations start with macro expansion, which the Rewriter
class simple rejects. Get one which it will accept. */
if (!start.isFileID()) {
start = sm.getExpansionLoc(start);
}

SourceLocation end = Lexer::getLocForEndOfToken(
range.getEnd(),
0,
sm,
ast->getLangOpts());

SourceRange new_range(start, end);
return new_range;
}

class ExternalizerVisitor: public RecursiveASTVisitor<ExternalizerVisitor>
{
public:
Expand Down Expand Up @@ -157,8 +180,9 @@ class ExternalizerVisitor: public RecursiveASTVisitor<ExternalizerVisitor>
}
} else if (type == ExternalizationType::RENAME) {
/* Get SourceRange where the function identifier is. */
auto ids = Get_Range_Of_Identifier(decl->getSourceRange(),
decl->getName());
SourceRange range = Get_Range_For_Rewriter(SE.AST, decl->getSourceRange());

auto ids = Get_Range_Of_Identifier(range, decl->getName());
assert(ids.size() > 0 && "Decl name do not match required identifier?");

SourceRange id_range = ids[0].second;
Expand Down Expand Up @@ -241,33 +265,12 @@ class ExternalizerVisitor: public RecursiveASTVisitor<ExternalizerVisitor>
SymbolExternalizer &SE;
};

static SourceRange Get_Range_For_Rewriter(const ASTUnit *ast, const SourceRange &range)
{
const SourceManager &sm = ast->getSourceManager();

/* Get a more precise source range of declaration. */
SourceLocation start = range.getBegin();

/* Some declarations start with macro expansion, which the Rewriter
class simple rejects. Get one which it will accept. */
if (!start.isFileID()) {
start = sm.getExpansionLoc(start);
}

SourceLocation end = Lexer::getLocForEndOfToken(
range.getEnd(),
0,
sm,
ast->getLangOpts());

SourceRange new_range(start, end);
return new_range;
}

bool SymbolExternalizer::Drop_Static(FunctionDecl *decl)
{
if (decl->isStatic()) {
auto ids = Get_Range_Of_Identifier(decl->getSourceRange(), StringRef("static"));
SourceRange range = Get_Range_For_Rewriter(AST, decl->getSourceRange());

auto ids = Get_Range_Of_Identifier(range, StringRef("static"));
assert(ids.size() > 0 && "static decl without static keyword?");

SourceRange static_range = ids[0].second;
Expand All @@ -286,7 +289,9 @@ template <typename DECL>
bool SymbolExternalizer::Drop_Static_Add_Extern(DECL *decl)
{
if (decl->getStorageClass() == StorageClass::SC_Static) {
auto ids = Get_Range_Of_Identifier(decl->getSourceRange(), StringRef("static"));
SourceRange range = Get_Range_For_Rewriter(AST, decl->getSourceRange());

auto ids = Get_Range_Of_Identifier(range, StringRef("static"));
assert(ids.size() > 0 && "static decl without static keyword?");

SourceRange static_range = ids[0].second;
Expand Down
22 changes: 22 additions & 0 deletions testsuite/small/macro-21.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* { dg-options "-DCE_EXTRACT_FUNCTIONS=f" }*/

// Same as macro-22.c but without rename symbols

typedef __builtin_va_list __gnuc_va_list;
extern int vprintf (const char *__restrict __format, __gnuc_va_list __arg);
typedef __builtin_va_list va_list;

#define va_start(ap, param) __builtin_va_start(ap, param)
#define va_end(ap) __builtin_va_end(ap)
#define __printf(a, b) __attribute__((__format__(printf, a, b)))

__printf(1, 2)
static void f(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vprintf (fmt, args);
va_end(args);
}

/* { dg-final { scan-tree-dump "static void f" } } */
22 changes: 22 additions & 0 deletions testsuite/small/macro-22.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* { dg-options "-DCE_EXTRACT_FUNCTIONS=f -DCE_RENAME_SYMBOLS" }*/

// Same as macro-21.c but with rename symbols
//
typedef __builtin_va_list __gnuc_va_list;
extern int vprintf (const char *__restrict __format, __gnuc_va_list __arg);
typedef __builtin_va_list va_list;

#define va_start(ap, param) __builtin_va_start(ap, param)
#define va_end(ap) __builtin_va_end(ap)
#define __printf(a, b) __attribute__((__format__(printf, a, b)))

__printf(1, 2)
static void f(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vprintf (fmt, args);
va_end(args);
}

/* { dg-final { scan-tree-dump-not "static" } } */

0 comments on commit bfc113f

Please sign in to comment.