emilio created this revision. emilio added a project: clang-c. This allows users to query the target triple and target pointer width, which would make me able to fix https://github.com/servo/rust-bindgen/issues/593 and other related bugs in an elegant way (without having to manually parse the target triple in the command line arguments).
Repository: rL LLVM https://reviews.llvm.org/D32389 Files: clang/include/clang-c/Index.h clang/test/Index/target-info.c clang/tools/c-index-test/c-index-test.c clang/tools/libclang/CIndex.cpp clang/tools/libclang/libclang.exports
Index: clang/tools/libclang/libclang.exports =================================================================== --- clang/tools/libclang/libclang.exports +++ clang/tools/libclang/libclang.exports @@ -250,6 +250,8 @@ clang_getTokenSpelling clang_getTranslationUnitCursor clang_getTranslationUnitSpelling +clang_getTranslationUnitTargetTriple +clang_getTranslationUnitTargetPointerWidth clang_getTypeDeclaration clang_getTypeKindSpelling clang_getTypeSpelling Index: clang/tools/libclang/CIndex.cpp =================================================================== --- clang/tools/libclang/CIndex.cpp +++ clang/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" @@ -4005,6 +4006,28 @@ return cxstring::createDup(CXXUnit->getOriginalSourceFileName()); } +CXString clang_getTranslationUnitTargetTriple(CXTranslationUnit CTUnit) { + if (isNotUsableTU(CTUnit)) { + LOG_BAD_TU(CTUnit); + return cxstring::createEmpty(); + } + + ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); + std::string Triple = + CXXUnit->getASTContext().getTargetInfo().getTriple().normalize(); + return cxstring::createDup(Triple); +} + +int clang_getTranslationUnitTargetPointerWidth(CXTranslationUnit CTUnit) { + if (isNotUsableTU(CTUnit)) { + LOG_BAD_TU(CTUnit); + return -1; + } + + ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); + return CXXUnit->getASTContext().getTargetInfo().getMaxPointerWidth(); +} + CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { if (isNotUsableTU(TU)) { LOG_BAD_TU(TU); Index: clang/tools/c-index-test/c-index-test.c =================================================================== --- clang/tools/c-index-test/c-index-test.c +++ clang/tools/c-index-test/c-index-test.c @@ -1560,6 +1560,47 @@ } /******************************************************************************/ +/* Target information testing. */ +/******************************************************************************/ + +static int print_target_info(int argc, const char **argv) { + CXIndex Idx; + CXTranslationUnit TU; + const char *FileName; + enum CXErrorCode Err; + CXString Target; + 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; + } + + Target = clang_getTranslationUnitTargetTriple(TU); + printf("TargetTriple: %s\n", clang_getCString(Target)); + clang_disposeString(Target); + + PointerWidth = clang_getTranslationUnitTargetPointerWidth(TU); + printf("PointerWidth: %d\n", PointerWidth); + + clang_disposeTranslationUnit(TU); + clang_disposeIndex(Idx); + return 0; +} + +/******************************************************************************/ /* Loading ASTs/source. */ /******************************************************************************/ @@ -4297,11 +4338,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"); @@ -4407,6 +4449,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: clang/test/Index/target-info.c =================================================================== --- /dev/null +++ clang/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 +// CHECK: TargetTriple: i386-unknown-linux-gnu +// CHECK: PointerWidth: 32 +// RUN: c-index-test -test-print-target-info %s --target=x86_64-unknown-linux-gnu | FileCheck --check-prefix=CHECK-1 %s +// CHECK-1: TargetTriple: x86_64-unknown-linux-gnu +// CHECK-1: PointerWidth: 64 Index: clang/include/clang-c/Index.h =================================================================== --- clang/include/clang-c/Index.h +++ clang/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) \ @@ -1043,6 +1043,23 @@ clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit); /** + * \brief Get the target this translation unit is being parsed as. + * + * Returns an empty string if the translation unit is not usable. + */ +CINDEX_LINKAGE CXString +clang_getTranslationUnitTargetTriple(CXTranslationUnit CTUnit); + +/** + * \brief Get the pointer width of the target this translation unit is being + * parsed as. + * + * Returns -1 if the translation unit is not usable. + */ +CINDEX_LINKAGE int +clang_getTranslationUnitTargetPointerWidth(CXTranslationUnit CTUnit); + +/** * \brief Return the CXTranslationUnit for a given source file and the provided * command line arguments one would pass to the compiler. *
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits