[cfe-commits] [PATCH] clang_Cursor_getArgument works with CallExprs

Matthias Kleine matthias_kleine at gmx.de
Tue Oct 16 10:16:44 PDT 2012


Until now, the two functions clang_Cursor_getArgument and clang_Cursor_getNumArguments would work only for function/method declarations. Yet, their names indicate that they can be used for anything that has arguments, e.g., CallExprs.

As I currently need a way to access cursors for the arguments of a CallExpr, I extended the existing functions to also work for CallExprs.

Please note that I'm not sure whether I'm correctly creating the result cursor from the argument-expressions. Currently I use NULL as the parent-declaration. I'm not sure whether this correct or whether one would rather have to compute the parent-declaration a'la getDeclFromExpr.

http://llvm-reviews.chandlerc.com/D63

Files:
  include/clang-c/Index.h
  tools/libclang/CXCursor.cpp

Index: include/clang-c/Index.h
===================================================================
--- include/clang-c/Index.h
+++ include/clang-c/Index.h
@@ -2657,14 +2657,15 @@
  * \brief Retrieve the number of non-variadic arguments associated with a given
  * cursor.
  *
- * If a cursor that is not a function or method is passed in, -1 is returned.
+ * If a cursor that is not a function, method, or call is passed in, -1 is
+ * returned.
  */
 CINDEX_LINKAGE int clang_Cursor_getNumArguments(CXCursor C);
 
 /**
- * \brief Retrieve the argument cursor of a function or method.
+ * \brief Retrieve the argument cursor of a function, method, or call.
  *
- * If a cursor that is not a function or method is passed in or the index
+ * If a cursor that is not a function, method, or call is passed in or the index
  * exceeds the number of arguments, an invalid cursor is returned.
  */
 CINDEX_LINKAGE CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i);
Index: tools/libclang/CXCursor.cpp
===================================================================
--- tools/libclang/CXCursor.cpp
+++ tools/libclang/CXCursor.cpp
@@ -928,6 +928,13 @@
       return FD->param_size();
   }
 
+  if (clang_isExpression(C.kind)) {
+    Expr *E = cxcursor::getCursorExpr(C);
+    if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
+      return CE->getNumArgs();
+    }
+  }
+
   return -1;
 }
 
@@ -945,6 +952,18 @@
     }
   }
 
+  if (clang_isExpression(C.kind)) {
+    Expr *E = cxcursor::getCursorExpr(C);
+    if (CallExpr *CE = dyn_cast<CallExpr>(E))
+    {
+      if (i < CE->getNumArgs()) {
+        return cxcursor::MakeCXCursor(CE->getArg(i),
+                                      NULL,
+                                      cxcursor::getCursorTU(C));
+      }
+    }
+  }
+
   return clang_getNullCursor();
 }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63.1.patch
Type: text/x-patch
Size: 1822 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20121016/872a1338/attachment.bin>


More information about the cfe-commits mailing list