Added _Pragma operator support AND function extraction from external defines#12
Added _Pragma operator support AND function extraction from external defines#12Kerrigan29a wants to merge 2 commits intom-schmoock:masterfrom Kerrigan29a:master
Conversation
|
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;
}
]] |
|
The problem of this approximation (predefined macro) is addressed in the GCC page:
With #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 Please check the first commit of the pull request (0465952), because it's completely different from the Thanks for your time |
|
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"
]] |
refs m-schmoock#13 fix bug that #x (stringify operator) 's apply order is wrong
I've used the GCC Pragmas manual page as reference.