Skip to content

Commit

Permalink
Handle static function declaration that starts with a macro expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
rinon committed Oct 15, 2024
1 parent 4c5c1cc commit c811095
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions tests/static_addr_taken/include/static_fns.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#define LOCAL static

typedef void (*fn_ptr_ty)(void);

static void inline_noop(void) {
Expand Down
10 changes: 7 additions & 3 deletions tests/static_addr_taken/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@ static void duplicate_noop(void) {
printf("called %s in main binary\n", __func__);
}

LOCAL void macro_attr_noop(void) {
printf("called %s in main binary\n", __func__);
}

static void identical_name(void) {
static int x = 3;
printf("%s in main binary read x = %d\n", __func__, x);
}

static fn_ptr_ty ptrs[3] IA2_SHARED_DATA = {
inline_noop, duplicate_noop, identical_name
static fn_ptr_ty ptrs[] IA2_SHARED_DATA = {
inline_noop, duplicate_noop, identical_name, macro_attr_noop,
};

fn_ptr_ty *get_ptrs_in_main(void) {
return ptrs;
}

Test(static_addr_taken, call_ptrs_in_main) {
for (int i = 0; i < 3; i++) {
for (int i = 0; i < sizeof(ptrs) / sizeof(ptrs[0]); i++) {
ptrs[i]();
}
}
Expand Down
6 changes: 4 additions & 2 deletions tools/rewriter/SourceRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,10 +671,12 @@ class FnPtrExpr : public RefactoringCallback {
// internal_addr_taken_fns map. To make the rewriter idempotent we should
// check for an existing used attribute.
if (new_fn) {
auto decl_start = fn_decl->getBeginLoc();
if (!decl_start.isFileID()) {
auto static_fn_range = fn_decl->getSourceRange();
auto expansion_range = sm.getExpansionRange(static_fn_range);
if (!expansion_range.getBegin().isFileID()) {
llvm::errs() << "Error: non-file loc for function " << fn_name << '\n';
} else {
auto decl_start = expansion_range.getBegin();
Filename decl_filename = get_filename(decl_start, sm);
Replacement old_used_attr(sm, decl_start, 0,
llvm::StringRef("__attribute__((used)) "));
Expand Down

0 comments on commit c811095

Please sign in to comment.