forked from loarabia/Clang-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tutorial6.cpp
183 lines (155 loc) · 5.49 KB
/
tutorial6.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// 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 "llvm/Support/Casting.h"
#include "clang/Frontend/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/HeaderSearchOptions.h"
#include "clang/Frontend/Utils.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Frontend/PreprocessorOptions.h"
#include "clang/Frontend/FrontendOptions.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/Builtins.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Sema/Sema.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/Type.h"
#include "clang/AST/Decl.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Ownership.h"
#include "clang/AST/DeclGroup.h"
#include "clang/Parse/Parser.h"
#include "clang/Parse/ParseAST.h"
#include "clang/Frontend/CompilerInstance.h"
class MyASTConsumer : public clang::ASTConsumer
{
public:
MyASTConsumer() : clang::ASTConsumer() { }
virtual ~MyASTConsumer() { }
virtual bool HandleTopLevelDecl( clang::DeclGroupRef d)
{
static int count = 0;
clang::DeclGroupRef::iterator it;
for( it = d.begin(); it != d.end(); it++)
{
count++;
clang::VarDecl *vd = llvm::dyn_cast<clang::VarDecl>(*it);
if(!vd)
{
continue;
}
if( vd->isFileVarDecl() && !vd->hasExternalStorage() )
{
std::cerr << "Read top-level variable decl: '";
std::cerr << vd->getDeclName().getAsString() ;
std::cerr << std::endl;
}
}
return true;
}
};
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, pTextDiagnosticPrinter);
clang::LangOptions languageOptions;
clang::FileSystemOptions fileSystemOptions;
clang::FileManager fileManager(fileSystemOptions);
clang::SourceManager sourceManager(
*pDiagnosticsEngine,
fileManager);
clang::HeaderSearchOptions headerSearchOptions;
// <Warning!!> -- Platform Specific Code lives here
// This depends on A) that you're running linux and
// B) that you have the same GCC LIBs installed that
// I do.
// Search through Clang itself for something like this,
// go on, you won't find it. The reason why is Clang
// has its own versions of std* which are installed under
// /usr/local/lib/clang/<version>/include/
// See somewhere around Driver.cpp:77 to see Clang adding
// its version of the headers to its include path.
headerSearchOptions.AddPath("/usr/include/linux",
clang::frontend::Angled,
false,
false,
false);
headerSearchOptions.AddPath("/usr/include/c++/4.4/tr1",
clang::frontend::Angled,
false,
false,
false);
headerSearchOptions.AddPath("/usr/include/c++/4.4",
clang::frontend::Angled,
false,
false,
false);
// </Warning!!> -- End of Platform Specific Code
clang::TargetOptions targetOptions;
targetOptions.Triple = llvm::sys::getDefaultTargetTriple();
clang::TargetInfo *pTargetInfo =
clang::TargetInfo::CreateTargetInfo(
*pDiagnosticsEngine,
targetOptions);
clang::HeaderSearch headerSearch(fileManager,
*pDiagnosticsEngine,
languageOptions,
pTargetInfo);
clang::CompilerInstance compInst;
clang::Preprocessor preprocessor(
*pDiagnosticsEngine,
languageOptions,
pTargetInfo,
sourceManager,
headerSearch,
compInst);
clang::PreprocessorOptions preprocessorOptions;
clang::FrontendOptions frontendOptions;
clang::InitializePreprocessor(
preprocessor,
preprocessorOptions,
headerSearchOptions,
frontendOptions);
const clang::FileEntry *pFile = fileManager.getFile(
"input04.c");
sourceManager.createMainFileID(pFile);
const clang::TargetInfo &targetInfo = *pTargetInfo;
clang::IdentifierTable identifierTable(languageOptions);
clang::SelectorTable selectorTable;
clang::Builtin::Context builtinContext;
builtinContext.InitializeTarget(targetInfo);
clang::ASTContext astContext(
languageOptions,
sourceManager,
pTargetInfo,
identifierTable,
selectorTable,
builtinContext,
0 /* size_reserve*/);
MyASTConsumer astConsumer;
clang::Sema sema(
preprocessor,
astContext,
astConsumer);
pTextDiagnosticPrinter->BeginSourceFile(languageOptions, &preprocessor);
clang::ParseAST(preprocessor, &astConsumer, astContext);
pTextDiagnosticPrinter->EndSourceFile();
return 0;
}