Issue 71554
Summary [Flang] Feature request/Discussion: Preprocessor/Prescanner callbacks
Labels flang
Assignees
Reporter tarcisiofischer
    Hello!

I am working on a project called [Codevis](https://invent.kde.org/sdk/codevis), which is able to read C and C++ source code and extract a database representation from it, which is used (among other things) to be visualized in a graph-like structure in a Qt-based application. I am currently running some experiments to read Fortran code on Codevis, and I am using flang to do that.

I am already able to access the Fortran AST and walk around it and extract what we call the "logical representation" of the code. As for a first try, been quite easy, and I must say I really like how things are done in flang :)

The other representation on Codevis, which is the "physical representation" is not related to the data structures, but in relation to the files and inclusions. Basically, for each file, I need to get all `INCLUDE` and persist that file `a.f` depends on `b.f` given that include.
I have been able to hack the code to make it work, by adding a callback inside the Fortran  preprocessor. More specifically, around here:
https://github.com/llvm/llvm-project/blob/75d6795e420274346b14aca8b6bd49bfe6030eeb/flang/lib/Parser/prescan.cpp#L896

My current hack is something like this:
```
  const SourceFile *included{
      allSources_.Open(path, error, std::move(prependPath))};
  if (!included) {
    Say(provenance, "INCLUDE: %s"_err_en_US, error.str());
  } else if (included->bytes() > 0) {
    ProvenanceRange includeLineRange{
        provenance, static_cast<std::size_t>(p - nextLine_)};
    ProvenanceRange fileRange{
        allSources_.AddIncludedFile(*included, includeLineRange)};
 Prescanner{*this}.set_encoding(included->encoding()).Prescan(fileRange);

 if (onFortranInclude) {
 (*onFortranInclude)(included->path());
    }
  }
```

Which is, of course, not ideal, but it works for a first experiment.
I would like to start a conversation in the direction of making ways of intercepting the preprocessor and the prescanner in such a way that I'd have access to those things without having to hack it.

Question 1: Would this be something that LLVM/Flang community would be interested in?
Question 2: Would you be open to start a conversation regarding how to proper design the solution, perhaps having clang's `PPCallback`s as an example to be followed?

If you need further information please let me know.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to