satya-das / cppparser

A library to parse C/C++ source as AST
Other
278 stars 36 forks source link

Constructor initializers are parsed incorrectly #14

Closed agladilin closed 2 years ago

agladilin commented 2 years ago

The code below is parsed incorrectly

    class Base {
    public:
        Base(std::vector<int> columns) {}
    };

    class Derived {
    public:
        Derived() :  Base(std::vector<int>{1, 2, 3}) {}
    };

The std::vector<int>{1, 2, 3} is parsed as a function call.

(gdb) p *memInit.second
{
      <CppObj> = {_vptr.CppObj = 0x5555556028c0 <vtable for CppExpr+16>, objType_ = CppObjType::kExpression, accessType_ = CppAccessType::kUnknown, owner_ = 0x0}, 
      static kObjectType = CppObjType::kExpression, 
      expr1_ = {
            {atom = 0x5555556760d0, expr = 0x5555556760d0, lambda = 0x5555556760d0, varType = 0x5555556760d0}, 
            type = CppExprAtom::kExpr
      }, 
      expr2_ = {
            {atom = 0x555555676050, expr = 0x555555676050, lambda = 0x555555676050, varType = 0x555555676050}, 
            type = CppExprAtom::kExpr
      }, 
      expr3_ = {
            {atom = 0x0, expr = 0x0, lambda = 0x0, varType = 0x0}, 
            type = CppExprAtom::kExpr
      }, 
      oper_ = kFunctionCall, 
      flags_ = 0
}

Therefore, once we use a CppWriter, the result is the following:

        Derived()
            : Base(std::vector<int>(1, 2, 3))
        {
        }
satya-das commented 2 years ago

Its a bug, I will look into it and fix it soon. Thanks for reporting and giving a detailed analysis.

satya-das commented 2 years ago

This should now be fixed with commit 97458486. Please use the latest CppParser.

agladilin commented 2 years ago

It works now. Thanks a lot!