参考书:《Getting Started with LLVM Core Libraries》
词法分析器使用,参考上一小节。
一、makefile
使用上一节的makefile:
二、cpp源代码
extern "C" {
#include "clang-c/Index.h"
}
#include "llvm/Support/CommandLine.h"
#include <iostream>
using namespace llvm;
static cl::opt<std::string> FileName(cl::Positional ,cl::desc("Input file"),cl::Required);
int main(int argc, char** argv)
{
cl::ParseCommandLineOptions(argc, argv, "My tokenizer\n");
CXIndex index = clang_createIndex(0,0);
const char *args[] = {"-I/usr/include","-I." };
CXTranslationUnit translationUnit = clang_parseTranslationUnit(index, FileName.c_str(),args, 2, NULL, 0, CXTranslationUnit_None);
CXFile file = clang_getFile(translationUnit, FileName.c_str());
CXSourceLocation loc_start = clang_getLocationForOffset(translationUnit, file, 0);
CXSourceLocation loc_end = clang_getLocationForOffset(translationUnit, file, 62);
CXSourceRange range = clang_getRange(loc_start, loc_end);
unsigned numTokens = 0;
CXToken *tokens = NULL;
clang_tokenize (translationUnit, range, &tokens, &numTokens);
for (unsigned i = 0; i < numTokens; ++i) {
enum CXTokenKind kind = clang_getTokenKind(tokens[i]);
CXString name = clang_getTokenSpelling(translationUnit,tokens[i]);
switch (kind) {
case CXToken_Punctuation:
std::cout << "PUNCTUATION(" << clang_getCString(name) << ") ";
break;
case CXToken_Keyword:
std::cout << "KEYWORD(" << clang_getCString(name) << ") ";
break;
case CXToken_Identifier:
std::cout << "IDENTIFIER(" << clang_getCString(name) << ") ";
break;
case CXToken_Literal:
std::cout << "COMMENT(" << clang_getCString(name) << ") ";
break;
default:
std::cout << "UNKNOWN(" << clang_getCString(name) << ") ";
break;
}
clang_disposeString(name);
}
std::cout << std::endl;
clang_disposeTokens (translationUnit, tokens, numTokens);
clang_disposeTranslationUnit(translationUnit);
return 0;
}
clang_getLocationForOffset 参数要注意62代表下边hello.c 的字节数。
三、执行结果:
hello.c
#include <stdio.h>
int main() {
printf("hello, world!");
}
image.png
网友评论