-
Notifications
You must be signed in to change notification settings - Fork 122
/
tutorial3.cpp
110 lines (89 loc) · 3.59 KB
/
tutorial3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// This code is licensed under the New BSD license.
// See LICENSE.txt for more details.
#include <iostream>
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Host.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/FileSystemOptions.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Basic/FileManager.h"
#include "clang/Frontend/Utils.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Frontend/FrontendOptions.h"
#include "clang/Frontend/CompilerInstance.h"
int main()
{
clang::DiagnosticOptions diagnosticOptions;
clang::TextDiagnosticPrinter *pTextDiagnosticPrinter =
new clang::TextDiagnosticPrinter(
llvm::outs(),
&diagnosticOptions);
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> pDiagIDs;
clang::DiagnosticsEngine *pDiagnosticsEngine =
new clang::DiagnosticsEngine(pDiagIDs,
&diagnosticOptions,
pTextDiagnosticPrinter);
clang::LangOptions languageOptions;
clang::FileSystemOptions fileSystemOptions;
clang::FileManager fileManager(fileSystemOptions);
clang::SourceManager sourceManager(
*pDiagnosticsEngine,
fileManager);
const std::shared_ptr<clang::TargetOptions> targetOptions = std::make_shared<clang::TargetOptions>();
targetOptions->Triple = llvm::sys::getDefaultTargetTriple();
clang::TargetInfo *pTargetInfo =
clang::TargetInfo::CreateTargetInfo(
*pDiagnosticsEngine,
targetOptions);
llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions> hso(new clang::HeaderSearchOptions());
clang::HeaderSearch headerSearch(hso,
sourceManager,
*pDiagnosticsEngine,
languageOptions,
pTargetInfo);
clang::CompilerInstance compInst;
llvm::IntrusiveRefCntPtr<clang::PreprocessorOptions> pOpts(new clang::PreprocessorOptions);
clang::Preprocessor preprocessor(
pOpts,
*pDiagnosticsEngine,
languageOptions,
sourceManager,
headerSearch,
compInst);
preprocessor.Initialize(*pTargetInfo);
// disable predefined Macros so that you only see the tokens from your
// source file. Note, this has some nasty side-effects like also undefning
// your archictecture and things like that.
//preprocessorOptions.UsePredefines = false;
clang::FrontendOptions frontendOptions;
clang::InitializePreprocessor(
preprocessor,
*pOpts,
frontendOptions);
clang::ApplyHeaderSearchOptions( preprocessor.getHeaderSearchInfo(),
compInst.getHeaderSearchOpts(),
preprocessor.getLangOpts(),
preprocessor.getTargetInfo().getTriple());
// Note: Changed the file from tutorial2.
const clang::FileEntry *pFile = fileManager.getFile("testInclude.c");
sourceManager.setMainFileID( sourceManager.createFileID( pFile, clang::SourceLocation(), clang::SrcMgr::C_User));
preprocessor.EnterMainSourceFile();
pTextDiagnosticPrinter->BeginSourceFile(languageOptions, &preprocessor);
clang::Token token;
do {
preprocessor.Lex(token);
if( pDiagnosticsEngine->hasErrorOccurred())
{
break;
}
preprocessor.DumpToken(token);
std::cerr << std::endl;
} while( token.isNot(clang::tok::eof));
pTextDiagnosticPrinter->EndSourceFile();
return 0;
}