m-schmoock / lcpp

A Lua C PreProcessor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Macro parameters with type conversion are processed incorrectly

ayourtch opened this issue · comments

Processing this file:

#define MACRO(a) a

MACRO(((int)0))
MACRO((int)1)
MACRO(2)

results in output:

((int)0)
(int)
2

i.e. if a macro argument is preceded by a parenthesized expression, this expression itself is taken as an argument.

The following diff takes care of the problem:

diff --git a/lcpp.lua b/lcpp.lua
index 99daf75..4450dcd 100755
--- a/lcpp.lua
+++ b/lcpp.lua
@@ -1238,20 +1238,25 @@ local function replaceArgs(argsstr, repl)
        argsstr = argsstr:sub(2,-2)
        -- print('argsstr:'..argsstr)
        local comma
+       local arg_acc = ''^M
        for k, v, start, end_ in tokenizer(argsstr, LCPP_TOKENIZE_MACRO_ARGS) do
                -- print("replaceArgs:" .. k .. "|" .. v)
                if k == "ARGS" or k == "PARENTHESE" or k == "STRING_LITERAL" or
                        k == "FUNCTIONAL" or k == "SINGLE_CHARACTER_ARGS" then
-                       table.insert(args, v)
+                       arg_acc = arg_acc..v^M
                        comma = false
                elseif k == "COMMA" then
                        if comma then
                                -- continued comma means empty parameter
                                table.insert(args, "")
+                       else^M
+                               table.insert(args, arg_acc)^M
+                               arg_acc = ''^M
                        end
                        comma = true
                end
        end
+       table.insert(args, arg_acc)^M
        local v = repl:gsub("%$(%d+)", function (m) return args[tonumber(m)] or "" end)
        -- print("replaceArgs:" .. repl .. "|" .. tostring(#args) .. "|" .. v)
        return v