你可以做
CXSourceRange range = clang_getCursorExtent(clang_getTranslationUnitCursor(TU));
获得覆盖整个翻译单元的范围。请参阅clang_getTranslationUnitCursor(...)和clang_getCursorExtent(...)。
例如,这个程序对作为参数给出的文件进行标记:
#include <clang-c/Index.h>
#include <iostream>
int main (int argc, char *argv[])
{
if (argc != 2) {
std::cerr << "Usage: tokenize <file>" << std::endl;
return 1;
}
CXIndex index = clang_createIndex(0, 0);
CXTranslationUnit TU = clang_parseTranslationUnit(index, 0, argv, argc, 0, 0, CXTranslationUnit_None);
CXSourceRange range = clang_getCursorExtent(clang_getTranslationUnitCursor(TU));
// Tokenize.
CXToken *tokens;
unsigned int numTokens;
clang_tokenize(TU, range, &tokens, &numTokens);
for (int i = 0; i < numTokens; ++i) {
CXToken token = tokens[i];
switch (clang_getTokenKind(token)) {
case CXToken_Punctuation:
std::cout << "CXToken_Punctuation" << std::endl;
break;
case CXToken_Keyword:
std::cout << "CXToken_Keyword" << std::endl;
break;
case CXToken_Identifier:
std::cout << "CXToken_Identifier" << std::endl;
break;
case CXToken_Literal:
std::cout << "CXToken_Literal" << std::endl;
break;
case CXToken_Comment:
std::cout << "CXToken_Comment" << std::endl;
break;
default:
std::cout << "Unknown" << std::endl;
}
}
clang_disposeTokens(TU, tokens, numTokens);
clang_disposeTranslationUnit(TU);
clang_disposeIndex(index);
return 0;
}