diff --git a/tests/static_addr_taken/include/static_fns.h b/tests/static_addr_taken/include/static_fns.h index 8c9263562..9f399664f 100644 --- a/tests/static_addr_taken/include/static_fns.h +++ b/tests/static_addr_taken/include/static_fns.h @@ -1,5 +1,7 @@ #pragma once +#define LOCAL static + typedef void (*fn_ptr_ty)(void); static void inline_noop(void) { diff --git a/tests/static_addr_taken/main.c b/tests/static_addr_taken/main.c index adc88b239..63efb39de 100644 --- a/tests/static_addr_taken/main.c +++ b/tests/static_addr_taken/main.c @@ -12,13 +12,17 @@ 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) { @@ -26,7 +30,7 @@ fn_ptr_ty *get_ptrs_in_main(void) { } 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](); } } diff --git a/tools/rewriter/SourceRewriter.cpp b/tools/rewriter/SourceRewriter.cpp index 87f6d60db..89bd8e5e3 100644 --- a/tools/rewriter/SourceRewriter.cpp +++ b/tools/rewriter/SourceRewriter.cpp @@ -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)) "));