Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Range ids #131

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 33 additions & 28 deletions libcextract/SymbolExternalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

/* Tokenize away the function-like macro stuff or expression, we only want
the identifier. */
#define TOKEN_VECTOR " ().,;+-*/^|&{}[]<>^&|\r\n\t"
#define TOKEN_VECTOR " ().,;+-*/^|&{}[]<>^&|!\r\n\t"

/* Return the ranges for all identifiers on the ids vector */
template <typename T>
Expand Down 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" } } */
Loading