cppparser icon indicating copy to clipboard operation
cppparser copied to clipboard

segfault on a very large source file

Open mmomtchev opened this issue 2 years ago • 2 comments

I get a segfault when trying to analyze this file: https://github.com/mmomtchev/node-magickwand/raw/84e1cdff82aae838233202e76b9ca01815e9ebec/Magick%2B%2B.cxx

I am using a very basic walking of the outermost members:

#include "cppparser/pub/cppparser.h"
#include <stdio.h>
#include <boost/filesystem.hpp>

namespace fs = boost::filesystem;

int main() {
  CppParser  parser;
  const auto testFilePath = fs::path(__FILE__).parent_path() / "Magick++.cxx";
  const auto ast          = parser.parseFile(testFilePath.string());

  const auto& members = ast->members();

  for (const auto &m : members) {
    if (m->objType_ == CppObjType::kFunction)
      printf("%s\n", ((CppFunctionEPtr)m)->name_.c_str());
  }
}

The output is:

Error: Unexpected 'SWIG_AddCast', while in context=ctxGeneral(1), found at line#383
SWIGINTERNINLINE int SWIG_AddCast
                     ^
Segmentation fault

mmomtchev avatar Jul 26 '23 17:07 mmomtchev

I am trying to use the library to write a simple splitter that extracts all declarations in a single .h file and then splits the code definitions in multiple source files in order to bring down this huge codebase that has been generated by SWIG down to manageable levels.

mmomtchev avatar Jul 26 '23 17:07 mmomtchev

I don't see a seg fault with the latest code but I do get a parsing error. The file uses macros to its extreme. The goal of cppparser is to allow parsing of all valid C++ files, but as of now handling of macros like this, which will need to do limited preprocessing before parsing, is not the priority.

satya-das avatar Oct 17 '24 14:10 satya-das

One question: is it possible to add a custom text replacement rule which acts like a macro expansion before the lexer?

I mean, if I need to have "#define AAA 100" for all the codes, I would like to run this text replacement rule on every "AAA" token.


int a = AAA;

is actually


int a = 100;

asmwarrior avatar Feb 03 '25 14:02 asmwarrior

Yes, SWIG has C preprocessor macro support.

mmomtchev avatar Feb 03 '25 16:02 mmomtchev

Yes, SWIG has C preprocessor macro support.

Maybe the text replacement rule can be implemented in the lexer level. I'm not sure whether the flex tool support this.

asmwarrior avatar Feb 04 '25 00:02 asmwarrior

One question: is it possible to add a custom text replacement rule which acts like a macro expansion before the lexer?

I had a plan to support a limited preprocessing before parsing to create an AST. But I never came around to do that.

satya-das avatar Feb 04 '25 07:02 satya-das