Skip to content

Macro parameters with type conversion are processed incorrectly #23

Description

@ayourtch

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions