我正在学习使用clang,我正在努力解析代码,我尝试#include标准头文件。
这是我的代码:
#include <iostream>
#include <clang\Basic\DiagnosticOptions.h>
#include <clang\Frontend\TextDiagnosticPrinter.h>
#include <llvm\Support\raw_ostream.h>
#include <clang\Basic\LangOptions.h>
#include <clang\Basic\FileSystemOptions.h>
#include <clang\Basic\FileManager.h>
#include <clang\Basic\SourceManager.h>
#include <clang\Basic\TargetOptions.h>
#include <clang\Basic\TargetInfo.h>
#include <clang\Frontend\CompilerInstance.h>
#include <clang\Lex\HeaderSearch.h>
#include <clang\Lex\HeaderSearchOptions.h>
#include <clang\Lex\Preprocessor.h>
int main(void)
{
clang::DiagnosticOptions diagnosticOptions;
diagnosticOptions.VerifyDiagnostics = 1;
clang::TextDiagnosticPrinter *pTextDiagnosticPrinter = new clang::TextDiagnosticPrinter(llvm::outs(), &diagnosticOptions);
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> pDiagIDs(new clang::DiagnosticIDs);
clang::DiagnosticsEngine *pDiagnosticsEngine = new clang::DiagnosticsEngine(pDiagIDs, &diagnosticOptions);
pDiagnosticsEngine->setClient(pTextDiagnosticPrinter, true);
clang::LangOptions languageOptions;
clang::FileSystemOptions fileSystemOptions;
clang::FileManager fileManager(fileSystemOptions);
clang::SourceManager sourceManager(*pDiagnosticsEngine, fileManager);
std::shared_ptr<clang::TargetOptions> targetOptions = std::make_shared<clang::TargetOptions>();
(*targetOptions).Triple = llvm::sys::getDefaultTargetTriple();
clang::TargetInfo *pTargetInfo = clang::TargetInfo::CreateTargetInfo(*pDiagnosticsEngine, targetOptions);
clang::CompilerInstance compInst;
clang::IntrusiveRefCntPtr<clang::HeaderSearchOptions> hsOptions(new clang::HeaderSearchOptions);
hsOptions.operator*().UseStandardSystemIncludes = 0;
hsOptions.operator*().UseStandardCXXIncludes = 0;
hsOptions.operator*().UseBuiltinIncludes = 0;
hsOptions.operator*().AddPath("c:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.10240.0\\ucrt", clang::frontend::IncludeDirGroup::Angled, false, false);
clang::HeaderSearch headerSearch(hsOptions, sourceManager, *pDiagnosticsEngine, languageOptions, pTargetInfo);
clang::IntrusiveRefCntPtr<clang::PreprocessorOptions> ppOptions;
clang::Preprocessor preprocessor(ppOptions, *pDiagnosticsEngine, languageOptions, sourceManager, headerSearch, compInst);
const clang::FileEntry *pFile = fileManager.getFile("test.c");
clang::FileID mainFileId = sourceManager.createFileID(pFile, clang::SourceLocation(),
clang::SrcMgr::CharacteristicKind::C_User);
sourceManager.setMainFileID(mainFileId);
preprocessor.EnterMainSourceFile();
clang::Token token;
do
{
preprocessor.Lex(token);
if (pDiagnosticsEngine->hasErrorOccurred())
{
break;
}
preprocessor.DumpToken(token);
std::cerr << std::endl;
}
while (token.isNot(clang::tok::eof));
}
这是我要解析的代码:
#include <stdio.h>
int i = 4;
extern int j;
int main()
{
printf("Hello World\n");
return 0;
}
这是我得到的错误:
断言失败:TextDiag&amp;&amp; “外部来源的意外诊断 文件处理“,文件 C:\ SOFTWARE \ LLVM \工具\铛\ LIB \前端\ TextDiagnosticPrinter.cpp, 第153行
注意,如果我尝试包含与源文件位于同一文件夹中的文件,那么它可以正常工作。
我正在使用最新版本的llvm和clang(3.7.0)。我正在使用Windows 8.1。 Visual studio版本是2015社区版。我试图遵循本教程:https://github.com/loarabia/Clang-tutorial/wiki/TutorialOrig