[PATCH] D32389: [libclang] Expose some target information via the C API.
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); + + Tri
[PATCH] D32389: [libclang] Expose some target information via the C API.
arphaman accepted this revision. arphaman added a comment. This revision is now accepted and ready to land. Thanks, LGTM Repository: rL LLVM https://reviews.llvm.org/D32389 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D32389: [libclang] Expose some target information via the C API.
emilio updated this revision to Diff 96834. emilio added a comment. Updated per review comments, thanks :) 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/CXTranslationUnit.h clang/tools/libclang/libclang.exports Index: clang/tools/libclang/libclang.exports === --- clang/tools/libclang/libclang.exports +++ clang/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: clang/tools/libclang/CXTranslationUnit.h === --- clang/tools/libclang/CXTranslationUnit.h +++ clang/tools/libclang/CXTranslationUnit.h @@ -35,6 +35,10 @@ clang::index::CommentToXMLConverter *CommentToXML; }; +struct CXTargetInfoImpl { + CXTranslationUnit TranslationUnit; +}; + namespace clang { namespace cxtu { 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" @@ -4015,6 +4016,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: 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,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
[PATCH] D32389: [libclang] Expose some target information via the C API.
arphaman added a comment. It looks good, I have a couple of comments: Comment at: clang/include/clang-c/Index.h:1573 +CINDEX_LINKAGE void +clang_TargetInfo_dispose(CXTargetInfo info); + Please capitalize `Info` here and in the other parameter lists. Comment at: clang/include/clang-c/Index.h:1584 +/** + * \brief Get the pointer width of the target. + * You could mention that the function returns the width in bits. Comment at: clang/test/Index/target-info.c:4 +// 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 NIT: Please put both RUN lines at the top of the file. Comment at: clang/tools/libclang/CIndex.cpp:4035 + CXTranslationUnit CTUnit = TargetInfo->TranslationUnit; + assert(!isNotUsableTU(CTUnit)); + Please add a message to the `assert` (e.g. `&& "message"`). Comment at: clang/tools/libclang/CIndex.cpp:4048 + CXTranslationUnit CTUnit = TargetInfo->TranslationUnit; + assert(!isNotUsableTU(CTUnit)); + Please add an assertion message here as well. Repository: rL LLVM https://reviews.llvm.org/D32389 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D32389: [libclang] Expose some target information via the C API.
emilio updated this revision to Diff 96652. emilio added a comment. Updated per comments, I used `clang_TargetInfo_dispose` following recent APIs instead of `clang_disposeTargetInfo`, let me know if I should change that. Also, didn't add a new header for `CXTargetInfo`, since it's rather small, but can definitely do if needed. 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/CXTranslationUnit.h clang/tools/libclang/libclang.exports Index: clang/tools/libclang/libclang.exports === --- clang/tools/libclang/libclang.exports +++ clang/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: clang/tools/libclang/CXTranslationUnit.h === --- clang/tools/libclang/CXTranslationUnit.h +++ clang/tools/libclang/CXTranslationUnit.h @@ -35,6 +35,10 @@ clang::index::CommentToXMLConverter *CommentToXML; }; +struct CXTargetInfoImpl { + CXTranslationUnit TranslationUnit; +}; + namespace clang { namespace cxtu { 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" @@ -4015,6 +4016,48 @@ 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)); + + 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)); + + ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); + return CXXUnit->getASTContext().getTargetInfo().getMaxPointerWidth(); +} + +void clang_TargetInfo_dispose(CXTargetInfo TargetInfo) { + if (!TargetInfo) +return; + + delete TargetInfo; +} + //===--===// // CXFile Operations. //===--===// 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,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(Tripl
[PATCH] D32389: [libclang] Expose some target information via the C API.
emilio added a comment. Sounds good to me, will do :) Repository: rL LLVM https://reviews.llvm.org/D32389 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D32389: [libclang] Expose some target information via the C API.
arphaman added a comment. Maybe it would be better to introduce a `CXTargetInfo` type, and change the API to be: clang_getTranslationUnitTargetInfo clang_TargetInfo_getTriple clang_TargetInfo_getPointerWidth ? This way the `TargetInfo` functions will be cleanly separated, so we can extend the API easier in the future. Repository: rL LLVM https://reviews.llvm.org/D32389 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D32389: [libclang] Expose some target information via the C API.
emilio added a comment. I'd appreciate if anyone could point me to an appropriate reviewer for this. Thanks in advance! :) Repository: rL LLVM https://reviews.llvm.org/D32389 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D32389: [libclang] Expose some target information via the C API.
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 {}*\n" " c-index-test -test-print-type-size {}*\n" " c-index-test -test-print-bitwidth {}*\n" +" c-index-test -test-print-target-info {}*\n" " c-index-test -test-print-type-declaration {}*\n" " c-index-test -print-usr [ {}]*\n" -" c-index-test -print-usr-file \n" -" c-index-test -write-pch \n"); +" c-index-test -print-usr-file \n"); fprintf(stderr, +" c-index-test -write-pch \n" " c-index-test -compilation-db [lookup ] 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) r