-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClangDoc.cpp
182 lines (144 loc) · 4.66 KB
/
ClangDoc.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
// ClangDoc.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "MyASTConsumer.h"
// This function isn't referenced outside its translation unit, but it
// can't use the "static" keyword because its address is used for
// GetMainExecutable (since some platforms don't support taking the
// address of main, and some platforms can't implement GetMainExecutable
// without being given the address of a function in the main executable).
llvm::sys::Path GetExecutablePath(const char *Argv0) {
// This just needs to be some symbol in the binary; C++ doesn't
// allow taking the address of ::main however.
void *MainAddr = (void*) (intptr_t) GetExecutablePath;
return llvm::sys::Path::GetMainExecutable(Argv0, MainAddr);
}
static int Execute(llvm::Module *Mod, char * const *envp) {
llvm::InitializeNativeTarget();
std::string Error;
llvm::OwningPtr<llvm::ExecutionEngine> EE(
llvm::ExecutionEngine::createJIT(Mod, &Error));
if (!EE) {
llvm::errs() << "unable to make execution engine: " << Error << "\n";
return 255;
}
llvm::Function *EntryFn = Mod->getFunction("main");
if (!EntryFn) {
llvm::errs() << "'main' function not found in module.\n";
return 255;
}
// FIXME: Support passing arguments.
std::vector<std::string> Args;
Args.push_back(Mod->getModuleIdentifier());
return EE->runFunctionAsMain(EntryFn, Args, envp);
}
int main(int argc, const char **argv, char * const *envp)
{
// Path
void *MainAddr = (void*) (intptr_t) GetExecutablePath;
llvm::sys::Path Path = GetExecutablePath(argv[0]);
// DiagnosticOptions
DiagnosticOptions diagnosticOptions;
// DiagnosticClient
TextDiagnosticPrinter *DiagClient = new TextDiagnosticPrinter(llvm::outs(), diagnosticOptions);
// Diagnostic
llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
Diagnostic diagnostic(DiagID, DiagClient);
//DiagnosticOptions DiagOpts;
//llvm::IntrusiveRefCntPtr<Diagnostic> diagnostic = CompilerInstance::createDiagnostics(DiagOpts, argc, argv);
// LangOptions
LangOptions langOptions;
langOptions.CPlusPlus = 1;
langOptions.CPlusPlus0x = 1;
langOptions.Microsoft = 1;
langOptions.Bool = 1;
langOptions.Exceptions = 1;
langOptions.CXXExceptions = 1;
langOptions.EmitAllDecls = 1;
// FileManager
FileSystemOptions fileSystemOptions;
FileManager fileManager(fileSystemOptions);
// SourceManager
SourceManager sourceManager(diagnostic, fileManager);
// HeadderSearch
HeaderSearch headerSearch(fileManager);
// TargetOptions
TargetOptions targetOptions;
targetOptions.Triple = llvm::sys::getHostTriple();
// TargetInfo
TargetInfo *pTargetInfo =
TargetInfo::CreateTargetInfo(
diagnostic,
targetOptions);
// HeaderSearchOptions
HeaderSearchOptions headerSearchOptions;
ApplyHeaderSearchOptions(
headerSearch,
headerSearchOptions,
langOptions,
pTargetInfo->getTriple());
// Preprocessor
Preprocessor preprocessor(
diagnostic,
langOptions,
*pTargetInfo,
sourceManager,
headerSearch);
// PreprocessorOptions
PreprocessorOptions preprocessorOptions;
preprocessorOptions.DetailedRecord = true;
preprocessor.createPreprocessingRecord();
// FrontendOptions
FrontendOptions frontendOptions;
// InitializePreprocessor
InitializePreprocessor(
preprocessor,
preprocessorOptions,
headerSearchOptions,
frontendOptions);
//preprocessor.SetCommentRetentionState(true, true);
// Tutorial
const FileEntry *pFile = fileManager.getFile(
"test.cpp",
true);
sourceManager.createMainFileID(pFile);
/*preprocessor.EnterMainSourceFile();
Token Tok;
do {
preprocessor.Lex(Tok); // read one token
//if (context.diags.hasErrorOccurred()) // stop lexing/pp on error
// break;
preprocessor.DumpToken(Tok); // outputs to cerr
std::cerr << std::endl;
} while (Tok.isNot(tok::eof));*/
const TargetInfo &targetInfo = *pTargetInfo;
IdentifierTable identifierTable(langOptions);
SelectorTable selectorTable;
Builtin::Context builtinContext(targetInfo);
ASTContext astContext(
langOptions,
sourceManager,
targetInfo,
identifierTable,
selectorTable,
builtinContext,
0 /* size_reserve*/);
// ASTConsumer astConsumer;
MyASTConsumer astConsumer;
astConsumer.sourceManager = &sourceManager;
astConsumer.html = new ClangDocHTML;
astConsumer.html->astConsumer = &astConsumer;
preprocessor.addPPCallbacks(astConsumer.html);
preprocessor.AddCommentHandler(astConsumer.html);
Sema sema(
preprocessor,
astContext,
astConsumer);
sema.Initialize();
//MySemanticAnalisys mySema( preprocessor, astContext, astConsumer);
//Parser parser( preprocessor, sema);
//parser.ParseTranslationUnit();
astConsumer.preprocessor = &sema.PP;
ParseAST(preprocessor, &astConsumer, astContext);
return 0;
}