Skip to content

Conversation

@Kerrigan29a
Copy link

I've used the GCC Pragmas manual page as reference.

@umegaya
Copy link
Collaborator

umegaya commented Feb 26, 2015

I think its better to show your "actual" use case.

but from the information which currently available, its better to add _Pragma as pre-defined macro function, like following. (it is only work for current master HEAD. does not work your PR branch)

it is effective because it never add processing (processPragmaOp) to all result from preprocessLine. and also flexible because user can override behavior if default behavior is not work for his use-case.

// pragma.c
_Pragma("GCC dependency \"parse.y\"")

int main(int argc, char *argv[]) {
    return 0;
}
-- test.lua
local lcpp = require 'lcpp'

local result = lcpp.compile(io.open('pragma.c'):read('*a'), {
    _Pragma = function (v) -- this can be added to lcpp.lua as default predefined macro
        return v:gsub("_Pragma%s*(%b())", function (match)
            return "#pragma "..match:sub(3, -3)
        end):gsub("\\\\", "\\"):gsub("\\\"", "\"")
    end
})
print("result=[[\n"..result.."\n]]")
luajit test.lua
result=[[
#pragma GCC dependency "parse.y"

int main(int argc, char *argv[]) {
return 0;
}
]]

@Kerrigan29a
Copy link
Author

The problem of this approximation (predefined macro) is addressed in the GCC page:

[...]
C99 introduces the _Pragma operator. This feature addresses a major problem with ‘#pragma’: being a directive, it cannot be produced as the result of macro expansion. _Pragma is an operator, much like sizeof or defined, and can be embedded in a macro.
[...]

With _Pragma you can write code like this:

#ifdef DEBUG
#define DO_PRAGMA(x) _Pragma (#x)
#else
#define DO_PRAGMA(x)
#endif
DO_PRAGMA (CUSTOM_COMPILER debug_msg "random message")

but if you substitute with a predefined macro _Pragma with #pragma this code will fail because inside a define you cannot use a preprocessor directive. This substitution must be done AFTER preprocessing-time to avoid this restriction, that's why in C99 is defined as an operator.

Please check the first commit of the pull request (0465952), because it's completely different from the _Pragma enhancement. Maybe I should have pulled this other enhancement in a different request.

Thanks for your time

@umegaya
Copy link
Collaborator

umegaya commented Feb 27, 2015

if you give function as lcpp's predefined macros, it just works as string converter and does not care about whether converting result contains preprocessor directive or not. see following result (with current master HEAD). this is not what you want to do?

dokyougemusu-no-MacBook-Pro:lcpp iyatomi$ cat test.lua 
local lcpp = require 'lcpp'

local src = [[
#ifdef DEBUG
#define DO_PRAGMA(x) _Pragma (#x)
#else
#define DO_PRAGMA(x)
#endif
DO_PRAGMA (CUSTOM_COMPILER debug_msg "random message")
]]

local function process_pragma(v)
    return v:gsub("_Pragma%s*(%b())", function (match)
        return "#pragma "..match:sub(3, -3)
    end):gsub("\\\\", "\\"):gsub("\\\"", "\"")
end

local result1 = lcpp.compile(src, {
    _Pragma = process_pragma
})
local result2 = lcpp.compile(src, {
    _Pragma = process_pragma,
    DEBUG = true,
})
print("result1=[[\n"..result1.."\n]]")
print("result2=[[\n"..result2.."\n]]")

dokyougemusu-no-MacBook-Pro:lcpp iyatomi$ luajit test.lua 
result1=[[






]]
result2=[[





#pragma CUSTOM_COMPILER debug_msg "random message"
]]

umegaya added a commit to umegaya/lcpp that referenced this pull request Mar 2, 2015
refs m-schmoock#13 fix bug that #x (stringify operator) 's apply order is wrong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants