This revision was automatically updated to reflect the committed changes. Closed by commit rL301648: [libclang] Expose some target information via the C API. (authored by emilio).
Changed prior to commit: https://reviews.llvm.org/D32389?vs=96834&id=97109#toc Repository: rL LLVM https://reviews.llvm.org/D32389 Files: cfe/trunk/include/clang-c/Index.h cfe/trunk/test/Index/target-info.c cfe/trunk/tools/c-index-test/c-index-test.c cfe/trunk/tools/libclang/CIndex.cpp cfe/trunk/tools/libclang/CXTranslationUnit.h cfe/trunk/tools/libclang/libclang.exports
Index: cfe/trunk/tools/libclang/CIndex.cpp =================================================================== --- cfe/trunk/tools/libclang/CIndex.cpp +++ cfe/trunk/tools/libclang/CIndex.cpp @@ -26,6 +26,7 @@ #include "clang/Basic/Diagnostic.h" #include "clang/Basic/DiagnosticCategories.h" #include "clang/Basic/DiagnosticIDs.h" +#include "clang/Basic/TargetInfo.h" #include "clang/Basic/Version.h" #include "clang/Frontend/ASTUnit.h" #include "clang/Frontend/CompilerInstance.h" @@ -4018,6 +4019,50 @@ return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU); } +CXTargetInfo clang_getTranslationUnitTargetInfo(CXTranslationUnit CTUnit) { + if (isNotUsableTU(CTUnit)) { + LOG_BAD_TU(CTUnit); + return nullptr; + } + + CXTargetInfoImpl* impl = new CXTargetInfoImpl(); + impl->TranslationUnit = CTUnit; + return impl; +} + +CXString clang_TargetInfo_getTriple(CXTargetInfo TargetInfo) { + if (!TargetInfo) + return cxstring::createEmpty(); + + CXTranslationUnit CTUnit = TargetInfo->TranslationUnit; + assert(!isNotUsableTU(CTUnit) && + "Unexpected unusable translation unit in TargetInfo"); + + ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); + std::string Triple = + CXXUnit->getASTContext().getTargetInfo().getTriple().normalize(); + return cxstring::createDup(Triple); +} + +int clang_TargetInfo_getPointerWidth(CXTargetInfo TargetInfo) { + if (!TargetInfo) + return -1; + + CXTranslationUnit CTUnit = TargetInfo->TranslationUnit; + assert(!isNotUsableTU(CTUnit) && + "Unexpected unusable translation unit in TargetInfo"); + + ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); + return CXXUnit->getASTContext().getTargetInfo().getMaxPointerWidth(); +} + +void clang_TargetInfo_dispose(CXTargetInfo TargetInfo) { + if (!TargetInfo) + return; + + delete TargetInfo; +} + //===----------------------------------------------------------------------===// // CXFile Operations. //===----------------------------------------------------------------------===// Index: cfe/trunk/tools/libclang/CXTranslationUnit.h =================================================================== --- cfe/trunk/tools/libclang/CXTranslationUnit.h +++ cfe/trunk/tools/libclang/CXTranslationUnit.h @@ -35,6 +35,10 @@ clang::index::CommentToXMLConverter *CommentToXML; }; +struct CXTargetInfoImpl { + CXTranslationUnit TranslationUnit; +}; + namespace clang { namespace cxtu { Index: cfe/trunk/tools/libclang/libclang.exports =================================================================== --- cfe/trunk/tools/libclang/libclang.exports +++ cfe/trunk/tools/libclang/libclang.exports @@ -79,6 +79,9 @@ clang_TParamCommandComment_isParamPositionValid clang_TParamCommandComment_getDepth clang_TParamCommandComment_getIndex +clang_TargetInfo_dispose +clang_TargetInfo_getPointerWidth +clang_TargetInfo_getTriple clang_Type_getAlignOf clang_Type_getClassType clang_Type_getSizeOf @@ -250,6 +253,7 @@ clang_getTokenSpelling clang_getTranslationUnitCursor clang_getTranslationUnitSpelling +clang_getTranslationUnitTargetInfo clang_getTypeDeclaration clang_getTypeKindSpelling clang_getTypeSpelling Index: cfe/trunk/tools/c-index-test/c-index-test.c =================================================================== --- cfe/trunk/tools/c-index-test/c-index-test.c +++ cfe/trunk/tools/c-index-test/c-index-test.c @@ -1560,6 +1560,51 @@ } /******************************************************************************/ +/* Target information testing. */ +/******************************************************************************/ + +static int print_target_info(int argc, const char **argv) { + CXIndex Idx; + CXTranslationUnit TU; + CXTargetInfo TargetInfo; + CXString Triple; + const char *FileName; + enum CXErrorCode Err; + int PointerWidth; + + if (argc == 0) { + fprintf(stderr, "No filename specified\n"); + return 1; + } + + FileName = argv[1]; + + Idx = clang_createIndex(0, 1); + Err = clang_parseTranslationUnit2(Idx, FileName, argv, argc, NULL, 0, + getDefaultParsingOptions(), &TU); + if (Err != CXError_Success) { + fprintf(stderr, "Couldn't parse translation unit!\n"); + describeLibclangFailure(Err); + clang_disposeIndex(Idx); + return 1; + } + + TargetInfo = clang_getTranslationUnitTargetInfo(TU); + + Triple = clang_TargetInfo_getTriple(TargetInfo); + printf("TargetTriple: %s\n", clang_getCString(Triple)); + clang_disposeString(Triple); + + PointerWidth = clang_TargetInfo_getPointerWidth(TargetInfo); + printf("PointerWidth: %d\n", PointerWidth); + + clang_TargetInfo_dispose(TargetInfo); + clang_disposeTranslationUnit(TU); + clang_disposeIndex(Idx); + return 0; +} + +/******************************************************************************/ /* Loading ASTs/source. */ /******************************************************************************/ @@ -4301,11 +4346,12 @@ " c-index-test -test-print-type {<args>}*\n" " c-index-test -test-print-type-size {<args>}*\n" " c-index-test -test-print-bitwidth {<args>}*\n" + " c-index-test -test-print-target-info {<args>}*\n" " c-index-test -test-print-type-declaration {<args>}*\n" " c-index-test -print-usr [<CursorKind> {<args>}]*\n" - " c-index-test -print-usr-file <file>\n" - " c-index-test -write-pch <file> <compiler arguments>\n"); + " c-index-test -print-usr-file <file>\n"); fprintf(stderr, + " c-index-test -write-pch <file> <compiler arguments>\n" " c-index-test -compilation-db [lookup <filename>] database\n"); fprintf(stderr, " c-index-test -print-build-session-timestamp\n"); @@ -4411,6 +4457,8 @@ return perform_test_load_tu(argv[2], "all", NULL, PrintMangledName, NULL); else if (argc > 2 && strcmp(argv[1], "-test-print-manglings") == 0) return perform_test_load_tu(argv[2], "all", NULL, PrintManglings, NULL); + else if (argc > 2 && strcmp(argv[1], "-test-print-target-info") == 0) + return print_target_info(argc - 2, argv + 2); else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) { if (argc > 2) return print_usrs(argv + 2, argv + argc); Index: cfe/trunk/include/clang-c/Index.h =================================================================== --- cfe/trunk/include/clang-c/Index.h +++ cfe/trunk/include/clang-c/Index.h @@ -32,7 +32,7 @@ * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable. */ #define CINDEX_VERSION_MAJOR 0 -#define CINDEX_VERSION_MINOR 37 +#define CINDEX_VERSION_MINOR 38 #define CINDEX_VERSION_ENCODE(major, minor) ( \ ((major) * 10000) \ @@ -81,6 +81,12 @@ typedef void *CXIndex; /** + * \brief An opaque type representing target information for a given translation + * unit. + */ +typedef struct CXTargetInfoImpl *CXTargetInfo; + +/** * \brief A single translation unit, which resides in an index. */ typedef struct CXTranslationUnitImpl *CXTranslationUnit; @@ -1553,6 +1559,36 @@ CINDEX_LINKAGE void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage); /** + * \brief Get target information for this translation unit. + * + * The CXTargetInfo object cannot outlive the CXTranslationUnit object. + */ +CINDEX_LINKAGE CXTargetInfo +clang_getTranslationUnitTargetInfo(CXTranslationUnit CTUnit); + +/** + * \brief Destroy the CXTargetInfo object. + */ +CINDEX_LINKAGE void +clang_TargetInfo_dispose(CXTargetInfo Info); + +/** + * \brief Get the normalized target triple as a string. + * + * Returns the empty string in case of any error. + */ +CINDEX_LINKAGE CXString +clang_TargetInfo_getTriple(CXTargetInfo Info); + +/** + * \brief Get the pointer width of the target in bits. + * + * Returns -1 in case of error. + */ +CINDEX_LINKAGE int +clang_TargetInfo_getPointerWidth(CXTargetInfo Info); + +/** * @} */ Index: cfe/trunk/test/Index/target-info.c =================================================================== --- cfe/trunk/test/Index/target-info.c +++ cfe/trunk/test/Index/target-info.c @@ -0,0 +1,6 @@ +// RUN: c-index-test -test-print-target-info %s --target=i386-unknown-linux-gnu | FileCheck %s +// RUN: c-index-test -test-print-target-info %s --target=x86_64-unknown-linux-gnu | FileCheck --check-prefix=CHECK-1 %s +// CHECK: TargetTriple: i386-unknown-linux-gnu +// CHECK: PointerWidth: 32 +// CHECK-1: TargetTriple: x86_64-unknown-linux-gnu +// CHECK-1: PointerWidth: 64
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits