[PATCH] Add clang_Location_isFromMainFile() to libclang

Stefanus Du Toit stefanus.du.toit at intel.com
Wed Aug 7 10:55:20 PDT 2013


Hi doug.gregor,

This exposes SourceManager::isFromMainFile() through the libclang interface.

CODE_OWNERS doesn't seem to indicate an owner for libclang, so I'm pointing this at Doug, but please let me know if there's someone more important.

I also couldn't find an obvious place to put a test for this. :(

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

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

Index: include/clang-c/Index.h
===================================================================
--- include/clang-c/Index.h
+++ include/clang-c/Index.h
@@ -414,6 +414,12 @@
 CINDEX_LINKAGE int clang_Location_isInSystemHeader(CXSourceLocation location);
 
 /**
+ * \brief Returns non-zero if the given source location is in the main file of
+ * the corresponding translation unit.
+ */
+CINDEX_LINKAGE int clang_Location_isFromMainFile(CXSourceLocation location);
+
+/**
  * \brief Retrieve a NULL (invalid) source range.
  */
 CINDEX_LINKAGE CXSourceRange clang_getNullRange(void);
Index: tools/libclang/CXSourceLocation.cpp
===================================================================
--- tools/libclang/CXSourceLocation.cpp
+++ tools/libclang/CXSourceLocation.cpp
@@ -209,6 +209,17 @@
   return SM.isInSystemHeader(Loc);
 }
 
+int clang_Location_isFromMainFile(CXSourceLocation location) {
+  const SourceLocation Loc =
+    SourceLocation::getFromRawEncoding(location.int_data);
+  if (Loc.isInvalid())
+    return 0;
+
+  const SourceManager &SM =
+    *static_cast<const SourceManager*>(location.ptr_data[0]);
+  return SM.isFromMainFile(Loc);
+}
+
 void clang_getExpansionLocation(CXSourceLocation location,
                                 CXFile *file,
                                 unsigned *line,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1313.1.patch
Type: text/x-patch
Size: 1320 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130807/d2367fcf/attachment.bin>


More information about the cfe-commits mailing list