From d0ae77d7a624c34acd49ee9998938904d7a38e69 Mon Sep 17 00:00:00 2001 From: Marcos Paulo de Souza Date: Fri, 20 Sep 2024 10:19:25 -0300 Subject: [PATCH 1/2] SymbolExternalizer: Add ! to TOKEN_VECTOR Otherwise it doesn't recognize the following expression: if (unlikely(!check_prev_ino(leaf, key, slot, prev_key))) Closes: #129 Signed-off-by: Marcos Paulo de Souza --- libcextract/SymbolExternalizer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libcextract/SymbolExternalizer.cpp b/libcextract/SymbolExternalizer.cpp index 02a495f..9ce5249 100644 --- a/libcextract/SymbolExternalizer.cpp +++ b/libcextract/SymbolExternalizer.cpp @@ -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 From a4bc997e4acfecb8eaa932611fd98e4c0937b05e Mon Sep 17 00:00:00 2001 From: Marcos Paulo de Souza Date: Fri, 20 Sep 2024 11:42:12 -0300 Subject: [PATCH 2/2] SymbolExternalizer: Make sure we pass a valid location when searching identifiers Signed-off-by: Marcos Paulo de Souza --- libcextract/SymbolExternalizer.cpp | 59 ++++++++++++++++-------------- testsuite/small/macro-21.c | 22 +++++++++++ testsuite/small/macro-22.c | 22 +++++++++++ 3 files changed, 76 insertions(+), 27 deletions(-) create mode 100644 testsuite/small/macro-21.c create mode 100644 testsuite/small/macro-22.c diff --git a/libcextract/SymbolExternalizer.cpp b/libcextract/SymbolExternalizer.cpp index 9ce5249..4aa8203 100644 --- a/libcextract/SymbolExternalizer.cpp +++ b/libcextract/SymbolExternalizer.cpp @@ -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 { public: @@ -157,8 +180,9 @@ class ExternalizerVisitor: public RecursiveASTVisitor } } 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; @@ -241,33 +265,12 @@ class ExternalizerVisitor: public RecursiveASTVisitor 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; @@ -286,7 +289,9 @@ template 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; diff --git a/testsuite/small/macro-21.c b/testsuite/small/macro-21.c new file mode 100644 index 0000000..281f32e --- /dev/null +++ b/testsuite/small/macro-21.c @@ -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" } } */ diff --git a/testsuite/small/macro-22.c b/testsuite/small/macro-22.c new file mode 100644 index 0000000..f7c7248 --- /dev/null +++ b/testsuite/small/macro-22.c @@ -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" } } */