Paste: macros in libclang

Author: blei
Mode: patch
Date: Fri, 23 Mar 2012 18:26:00
Plain Text |
diff --git a/tools/bindgen.clay b/tools/bindgen.clay
index ee1171c..9310bb9 100644
--- a/tools/bindgen.clay
+++ b/tools/bindgen.clay
@@ -448,9 +448,41 @@ visitToplevel(cursor:CXCursor, parent:CXCursor, data:CXClientData) : Int
     case (CXCursor_FieldDecl) {
         return CXChildVisit_Continue;
     }
+    case (CXCursor_MacroDefinition) {
+        var name = str(clang_getCursorSpelling(cursor));
+        /*
+        printlnTo(stderr, "visiting macro with name ", name);
+        clang_visitChildren(cursor, CXCursorVisitor(visitMacro),
+            CXClientData());
+        */
+        var extent = clang_getCursorExtent(cursor);
+        var lineStart = 0u;
+        var lineEnd = 0u;
+        var columnStart = 0u;
+        var columnEnd = 0u;
+        var start = clang_getRangeStart(extent);
+        clang_getSpellingLocation(start, null(CXFile), &lineStart, &columnStart, null(UInt));
+        var end = clang_getRangeEnd(extent);
+        clang_getSpellingLocation(end, null(CXFile), &lineEnd, &columnEnd, null(UInt));
+        printlnTo(stderr, "macro with name ", name, " from ", lineStart, ":", columnStart,
+            " until ", lineEnd, ":", columnEnd);
+        return CXChildVisit_Continue;
+    }
     return CXChildVisit_Recurse;
 }
 
+visitMacro(cursor:CXCursor, parent:CXCursor, data:CXClientData) : Int
+{
+    var kind = clang_getCursorKind(cursor);
+    printlnTo(stderr, "found kind ", kind);
+    /*
+    switch (kind)
+    case (CXCursor_StructDecl) {
+    }
+    */
+    return CXChildVisit_Continue;
+}
+
 record Usage ();
 record MissingOutputFileName ();
 record MissingImportModuleName ();
@@ -583,7 +615,7 @@ main(argc, argv)
         clang_argc,
         null(Struct_CXUnsavedFile),
         0u,
-        0u,
+        CXTranslationUnit_DetailedPreprocessingRecord,
     );
     if (null?(unit)) {
         printlnTo(stderr, "no input files given");

Annotation: change to CIndex.cpp

Author: blei
Mode: c++
Date: Fri, 23 Mar 2012 20:10:45
Plain Text |
CINDEX_LINKAGE int clang_getMacroArgCount(CXCursor C) {
  if (C.kind == CXCursor_MacroDefinition) {
    CXTranslationUnit tu = getCursorTU(C);
    ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData);
    MacroInfo *info = CXXUnit->getPreprocessor().getMacroInfo(
            const_cast<IdentifierInfo *>(
                cxcursor::getCursorMacroDefinition(C)->getName()));
    return info->getNumArgs();
  } else {
      return -1;
  }
}


Also! don't forget to add the function to libclang.exports

New Annotation

Summary:
Author:
Mode:
Body: