From dee38f6cd230a9da1e5829b61a942e2a0faef431 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Wed, 23 Dec 2015 08:14:43 -0800 Subject: [PATCH] Generate functions to "bind" parameterized macros. --- Analyze.cpp | 51 +++++++++++++++++++++++++++++++++++++++++---------- Execute.cpp | 2 +- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/Analyze.cpp b/Analyze.cpp index 36ec86d..42728bf 100644 --- a/Analyze.cpp +++ b/Analyze.cpp @@ -306,19 +306,50 @@ static CXChildVisitResult CYChildVisit(CXCursor cursor, CXCursor parent, CXClien CXCursor cursors[tokens.size()]; clang_annotateTokens(unit, tokens, tokens.size(), cursors); - CYCXPosition<> start(clang_getRangeStart(range)); - CYCXString first(unit, tokens[1]); - if (first == "(") { - CYCXPosition<> paren(unit, tokens[1]); - if (start.offset_ + strlen(spelling) == paren.offset_) - _assert(false); // XXX: support parameterized macros + CYLocalPool local; + CYList parameters; + unsigned offset(1); + + if (tokens.size() != 1) { + CYCXPosition<> start(clang_getRangeStart(range)); + CYCXString first(unit, tokens[offset]); + if (first == "(") { + CYCXPosition<> paren(unit, tokens[offset]); + if (start.offset_ + strlen(spelling) == paren.offset_) { + for (;;) { + _assert(++offset != tokens.size()); + CYCXString token(unit, tokens[offset]); + parameters->*$P($B($I(token.Pool($pool)))); + _assert(++offset != tokens.size()); + CYCXString comma(unit, tokens[offset]); + if (comma == ")") + break; + _assert(comma == ","); + } + ++offset; + } + } } - for (unsigned i(1); i != tokens.size(); ++i) { + std::ostringstream body; + for (unsigned i(offset); i != tokens.size(); ++i) { CYCXString token(unit, tokens[i]); - if (i != 1) - value << " "; - value << token; + if (i != offset) + body << " "; + body << token; + } + + if (!parameters) + value << body.str(); + else { + CYOptions options; + CYOutput out(*value.rdbuf(), options); + out << '(' << "function" << '('; + out << parameters; + out << ')' << '{'; + out << "return" << ' '; + value << body.str(); + out << ';' << '}' << ')'; } } catch (const CYException &error) { CYPool pool; diff --git a/Execute.cpp b/Execute.cpp index 1c6b7ba..523dcca 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -2169,7 +2169,7 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) { JSObjectRef System(JSObjectMake(context, NULL, NULL)); CYSetProperty(context, cy, CYJSString("System"), System); - CYSetProperty(context, all, CYJSString("require"), &require_callAsFunction, kJSPropertyAttributeDontEnum); + CYSetProperty(context, global, CYJSString("require"), &require_callAsFunction, kJSPropertyAttributeDontEnum); CYSetProperty(context, global, CYJSString("system"), System); CYSetProperty(context, System, CYJSString("args"), CYJSNull(context)); -- 2.45.2