mirror of https://github.com/microsoft/clang.git
[libclang] Expose some target information via the C API.
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). Differential Revision: https://reviews.llvm.org/D32389 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@301648 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
de4f6266fa
commit
1b98417fe9
|
@ -32,7 +32,7 @@
|
||||||
* compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
|
* compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
|
||||||
*/
|
*/
|
||||||
#define CINDEX_VERSION_MAJOR 0
|
#define CINDEX_VERSION_MAJOR 0
|
||||||
#define CINDEX_VERSION_MINOR 37
|
#define CINDEX_VERSION_MINOR 38
|
||||||
|
|
||||||
#define CINDEX_VERSION_ENCODE(major, minor) ( \
|
#define CINDEX_VERSION_ENCODE(major, minor) ( \
|
||||||
((major) * 10000) \
|
((major) * 10000) \
|
||||||
|
@ -80,6 +80,12 @@ extern "C" {
|
||||||
*/
|
*/
|
||||||
typedef void *CXIndex;
|
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.
|
* \brief A single translation unit, which resides in an index.
|
||||||
*/
|
*/
|
||||||
|
@ -1552,6 +1558,36 @@ CINDEX_LINKAGE CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU
|
||||||
|
|
||||||
CINDEX_LINKAGE void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage);
|
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);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -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
|
|
@ -1559,6 +1559,51 @@ static enum CXChildVisitResult PrintTypeDeclaration(CXCursor cursor, CXCursor p,
|
||||||
return CXChildVisit_Recurse;
|
return CXChildVisit_Recurse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/* 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. */
|
/* Loading ASTs/source. */
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
@ -4301,11 +4346,12 @@ static void print_usage(void) {
|
||||||
" c-index-test -test-print-type {<args>}*\n"
|
" c-index-test -test-print-type {<args>}*\n"
|
||||||
" c-index-test -test-print-type-size {<args>}*\n"
|
" c-index-test -test-print-type-size {<args>}*\n"
|
||||||
" c-index-test -test-print-bitwidth {<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 -test-print-type-declaration {<args>}*\n"
|
||||||
" c-index-test -print-usr [<CursorKind> {<args>}]*\n"
|
" c-index-test -print-usr [<CursorKind> {<args>}]*\n"
|
||||||
" c-index-test -print-usr-file <file>\n"
|
" c-index-test -print-usr-file <file>\n");
|
||||||
" c-index-test -write-pch <file> <compiler arguments>\n");
|
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
|
" c-index-test -write-pch <file> <compiler arguments>\n"
|
||||||
" c-index-test -compilation-db [lookup <filename>] database\n");
|
" c-index-test -compilation-db [lookup <filename>] database\n");
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
" c-index-test -print-build-session-timestamp\n");
|
" c-index-test -print-build-session-timestamp\n");
|
||||||
|
@ -4411,6 +4457,8 @@ int cindextest_main(int argc, const char **argv) {
|
||||||
return perform_test_load_tu(argv[2], "all", NULL, PrintMangledName, NULL);
|
return perform_test_load_tu(argv[2], "all", NULL, PrintMangledName, NULL);
|
||||||
else if (argc > 2 && strcmp(argv[1], "-test-print-manglings") == 0)
|
else if (argc > 2 && strcmp(argv[1], "-test-print-manglings") == 0)
|
||||||
return perform_test_load_tu(argv[2], "all", NULL, PrintManglings, NULL);
|
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) {
|
else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
|
||||||
if (argc > 2)
|
if (argc > 2)
|
||||||
return print_usrs(argv + 2, argv + argc);
|
return print_usrs(argv + 2, argv + argc);
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include "clang/Basic/Diagnostic.h"
|
#include "clang/Basic/Diagnostic.h"
|
||||||
#include "clang/Basic/DiagnosticCategories.h"
|
#include "clang/Basic/DiagnosticCategories.h"
|
||||||
#include "clang/Basic/DiagnosticIDs.h"
|
#include "clang/Basic/DiagnosticIDs.h"
|
||||||
|
#include "clang/Basic/TargetInfo.h"
|
||||||
#include "clang/Basic/Version.h"
|
#include "clang/Basic/Version.h"
|
||||||
#include "clang/Frontend/ASTUnit.h"
|
#include "clang/Frontend/ASTUnit.h"
|
||||||
#include "clang/Frontend/CompilerInstance.h"
|
#include "clang/Frontend/CompilerInstance.h"
|
||||||
|
@ -4018,6 +4019,50 @@ CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
|
||||||
return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU);
|
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.
|
// CXFile Operations.
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
|
@ -35,6 +35,10 @@ struct CXTranslationUnitImpl {
|
||||||
clang::index::CommentToXMLConverter *CommentToXML;
|
clang::index::CommentToXMLConverter *CommentToXML;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct CXTargetInfoImpl {
|
||||||
|
CXTranslationUnit TranslationUnit;
|
||||||
|
};
|
||||||
|
|
||||||
namespace clang {
|
namespace clang {
|
||||||
namespace cxtu {
|
namespace cxtu {
|
||||||
|
|
||||||
|
|
|
@ -79,6 +79,9 @@ clang_TParamCommandComment_getParamName
|
||||||
clang_TParamCommandComment_isParamPositionValid
|
clang_TParamCommandComment_isParamPositionValid
|
||||||
clang_TParamCommandComment_getDepth
|
clang_TParamCommandComment_getDepth
|
||||||
clang_TParamCommandComment_getIndex
|
clang_TParamCommandComment_getIndex
|
||||||
|
clang_TargetInfo_dispose
|
||||||
|
clang_TargetInfo_getPointerWidth
|
||||||
|
clang_TargetInfo_getTriple
|
||||||
clang_Type_getAlignOf
|
clang_Type_getAlignOf
|
||||||
clang_Type_getClassType
|
clang_Type_getClassType
|
||||||
clang_Type_getSizeOf
|
clang_Type_getSizeOf
|
||||||
|
@ -250,6 +253,7 @@ clang_getTokenLocation
|
||||||
clang_getTokenSpelling
|
clang_getTokenSpelling
|
||||||
clang_getTranslationUnitCursor
|
clang_getTranslationUnitCursor
|
||||||
clang_getTranslationUnitSpelling
|
clang_getTranslationUnitSpelling
|
||||||
|
clang_getTranslationUnitTargetInfo
|
||||||
clang_getTypeDeclaration
|
clang_getTypeDeclaration
|
||||||
clang_getTypeKindSpelling
|
clang_getTypeKindSpelling
|
||||||
clang_getTypeSpelling
|
clang_getTypeSpelling
|
||||||
|
|
Loading…
Reference in New Issue