--- a/tools/clang/include/clang-c/Index.h +++ a/tools/clang/include/clang-c/Index.h @@ -404,6 +404,21 @@ const char *file_name); /** + * \brief Retrieve the buffer associated with the given file. + * + * \param tu the translation unit + * + * \param file the file for which to retrieve the buffer. + * + * \param size [out] if non-NULL, will be set to the size of the buffer. + * + * \returns a pointer to the buffer in memory that holds the contents of + * \p file, or a NULL pointer when the file is not loaded. + */ +CINDEX_LINKAGE const char *clang_getFileContents(CXTranslationUnit tu, + CXFile file, size_t *size); + +/** * \brief Returns non-zero if the \c file1 and \c file2 point to the same file, * or they are both NULL. */ --- a/tools/clang/tools/libclang/CIndex.cpp +++ a/tools/clang/tools/libclang/CIndex.cpp @@ -4162,6 +4162,27 @@ return const_cast(FMgr.getFile(file_name)); } +const char *clang_getFileContents(CXTranslationUnit TU, CXFile file, + size_t *size) { + if (isNotUsableTU(TU)) { + LOG_BAD_TU(TU); + return nullptr; + } + + const SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager(); + FileID fid = SM.translateFile(static_cast(file)); + bool Invalid = true; + llvm::MemoryBuffer *buf = SM.getBuffer(fid, &Invalid); + if (Invalid) { + if (size) + *size = 0; + return nullptr; + } + if (size) + *size = buf->getBufferSize(); + return buf->getBufferStart(); +} + unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU, CXFile file) { if (isNotUsableTU(TU)) { --- a/tools/clang/tools/libclang/libclang.exports +++ b/tools/clang/tools/libclang/libclang.exports @@ -216,6 +216,7 @@ clang_getExceptionSpecificationType clang_getFieldDeclBitWidth clang_getExpansionLocation clang_getFile +clang_getFileContents clang_getFileLocation clang_getFileName clang_getFileTime