<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">Hi Kevin,<br>
      Thanks for the info. Unfortunately, I'm in the same boat as you,
      deadlines to meet etc, no time to delve into the depths of
      libclang. Actually I tried to compile it with the preview version
      of visual studio 2015 and failed miserably, lots of errors without
      time to investigate. (I want a 64-bit version of libclang.dll, not
      the 32-bit version that's supplied pre-compiled on windows).<br>
      <br>
      I've partly hacked together something that does most of what I
      need using the token functions, but it's certainly not a permanent
      solution, and not even a full solution to the problem I described.
      I may hack on it some more to remove extra guff in the values, but
      that's about as far as I'll probably be able to take it without
      writing a macro parser.<br>
      <br>
      <font face="Courier New, Courier, monospace" size="-2">void
        translateMacro( CXCursor cursor, Generator& model )<br>
        {<br>
            std::string name;<br>
            std::string value;<br>
            CXToken* tokens    = nullptr;<br>
            unsigned numTokens = 0;<br>
            CXTranslationUnit transUnit =
        clang_Cursor_getTranslationUnit(cursor);<br>
            CXSourceRange     srcRange  = clang_getCursorExtent(cursor);<br>
            clang_tokenize( transUnit, srcRange, &tokens,
        &numTokens );<br>
            for( unsigned n=0; n<numTokens; n++ )<br>
            {<br>
                CXStringWrapper tokenText = clang_getTokenSpelling(
        transUnit, tokens[n] );<br>
                if( n == 0 ) {<br>
                    value.clear();<br>
                    name = tokenText.getStr();<br>
                    if( name[0] == '_' )<br>
                        break;<br>
                }<br>
                else {<br>
                    CXTokenKind tokenKind = clang_getTokenKind(
        tokens[n] );<br>
                    if( tokenKind != CXToken_Comment )<br>
                    {<br>
                        const char* text = tokenText.getStr();<br>
                        if( text && (text[0]!='#') &&
        strcmp(text,"typedef") )<br>
                            value += text;<br>
                    }<br>
                }<br>
            }<br>
            if( value.length() )<br>
            {<br>
                Constant c;<br>
                c.setName( name );<br>
                c.setValue( value );<br>
                model.addConstant( c );<br>
            }<br>
            clang_disposeTokens( transUnit, tokens, numTokens );<br>
        }<br>
      </font><br>
      Anyway, it seems there is a clear need for better
      preprocessing/macro functionality in libclang, so perhaps someone
      can add it to the C-API feature wish-list for future development?<br>
      <br>
      Best regards,<br>
      Mark.<br>
      <br>
      <br>
      <br>
      On 2014-11-28 07:08, Kevin Funk wrote:<br>
    </div>
    <blockquote cite="mid:1968874.zzxKDLJB75@kerberos" type="cite">
      <pre wrap="">On Thursday 27 November 2014 18:47:17 Mark wrote:
</pre>
      <blockquote type="cite">
        <pre wrap="">Hello all,
I'm new to libclang and using the Clang C-interface (version 3.6.0,
222169) to parse some C header files.

My goal is to extract all the #define constants from the .h files.

For example, for the following #defines:

#define FEE 12
#define FIE 34
#define FOE (FEE+FIE)
#define FUM FIE

I would like to get both the names and the resulting raw values as follows:

FEE = 12
FIE = 34
FOE = 46
FUM = 34

I've got as far as handling /CXCursor_MacroDefinition, /and getting the
macro name from the cursor spelling. But after that, it's not clear to
me how I might obtain further information about the defined macro values
themselves.
</pre>
      </blockquote>
      <pre wrap="">
Yep. That's where we are as well. Getting the definitions + their uses is 
easy.

Context: I'm the one working on (lib)Clang integration in KDevelop.

</pre>
      <blockquote type="cite">
        <pre wrap="">Is this possible using the libclang C interface?
If so, could someone please provide an example, or suggest an approach
that that might work?
</pre>
      </blockquote>
      <pre wrap="">
I was looking into this several months ago [1], too, and didn't find a 
solution so far. I'd love to get some API in libclang for achieving this.

The open-source woboq code browser (code.woboq.org) has this feature, but uses 
C++-API of Clang; so it's definitely possible, but I still didn't find the 
time to map this implementation to libclang.

Example usage:
  <a class="moz-txt-link-freetext" href="http://code.woboq.org/qt5/qtbase/src/corelib/kernel/qobject.h.html#108">http://code.woboq.org/qt5/qtbase/src/corelib/kernel/qobject.h.html#108</a>,
  hover over 'Q_OBJECT'.

The code dealing with Clang's C++ API is here:
  <a class="moz-txt-link-freetext" href="https://github.com/woboq/woboq_codebrowser/blob/master/generator/preprocessorcallback.cpp">https://github.com/woboq/woboq_codebrowser/blob/master/generator/preprocessorcallback.cpp</a>

Note: If you're interested in getting this into libclang I'd be glad to help 
out!

[1] <a class="moz-txt-link-freetext" href="http://lists.cs.uiuc.edu/pipermail/cfe-dev/2014-August/038543.html">http://lists.cs.uiuc.edu/pipermail/cfe-dev/2014-August/038543.html</a>

</pre>
      <blockquote type="cite">
        <pre wrap="">
Thank you,

Mark.
</pre>
      </blockquote>
      <pre wrap="">
</pre>
    </blockquote>
    <br>
  </body>
</html>