Easy way to get function prototypes?

Hey guys, friend and I were discussing imaginary and real languages, and the question that arose was that one of us wanted to create headers for another language (maybe D that already has the tool ), which would be simple and very good way to do it?

One of us said to scan C files and headers and ignore function bodies, and only count the curly braces inside to find out when the function is finished. The counter of this was typedefs, defines (which brackets, but defines, was considered as a trivial problem) and patterns + specialization.

Another solution was to read the binaries, not the actual exe, but the object files used by the linker. An example of this was the format and complexity. None of us knew anything about any object format, so we could not evaluate (we thought about gcc and VS C ++).

What do you guys think? which is easier? this should be supported by reasonable logic and fact.

If someone can reference a useful project, one that analyzes C files / headers and outputs it, or one that reads in the elf data and displays information in an example project, will be useful. I tried a search on Google, but I did not know how this would be caused. I found libelf, but at this point I could not compile it. I can soon.

+3
source share
6 answers

clang C/++ , .

, clang, . (liblex, libparse, libsema). , , , (typedefs, ..).

clang AST XML, , test.cpp:

void foo() {}

int main()
{
    foo();
}

clang++ -Xclang -ast-print-xml -fsyntax-only test.cpp, test.xml, ( ):

<?xml version="1.0"?>
<CLANG_XML>
  <TranslationUnit>
    <Function id="_1D" file="f2" line="1" col="6" context="_2"
              name="foo" type="_12" function_type="_1E" num_args="0">
    </Function>
    <Function id="_1F" file="f2" line="3" col="5" context="_2"
              name="main" type="_21" function_type="_22" num_args="0">
    </Function>
  </TranslationUnit>
  <ReferenceSection>
    <Types>
      <FunctionType result_type="_12" id="_1E"/>
      <FundamentalType kind="int" id="_21"/>
      <FundamentalType kind="void" id="_12"/>
      <FunctionType result_type="_21" id="_22"/>
      <PointerType type="_12" id="_10"/>
    </Types>
    <Files>
      <File id="f2" name="test.cpp"/>
    </Files>
  </ReferenceSection>
</CLANG_XML>

, , , C-, mangling.

+6

ctags /

+2

, swig

+2

, , : . , , , , .

, ++ ( !), .

. , ++ .

+1

- (IDL) , . IDL , .

Windows MIDL, .

+1

Doxygen . , .

+1

Source: https://habr.com/ru/post/1773364/


All Articles