-
Notifications
You must be signed in to change notification settings - Fork 0
/
print-clang-ast.cc
169 lines (139 loc) · 5.12 KB
/
print-clang-ast.cc
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
// print-clang-ast.cc
// Entry point for print-clang-ast.exe program.
#include "clang-ast-visitor.h" // clangASTVisitorTest
#include "clang-ast.h" // ClangAST
#include "clang-util.h" // GlobalClangUtilInstance
#include "decl-implicit.h" // declareImplicitThings
#include "pca-command-line-options.h" // PCACommandLineOptions
#include "pca-unit-tests.h" // pca_unit_tests
#include "print-clang-ast-nodes.h" // printClangASTNodes
#include "printer-visitor.h" // printerVisitorTU
#include "rav-printer-visitor.h" // ravPrinterVisitorTU
#include "smbase/gdvalue.h" // gdv::GDValue
#include "smbase/map-util.h" // mapInsertAll
#include "smbase/sm-trace.h" // INIT_TRACE
#include "smbase/string-util.h" // stringVectorFromPointerArray
#include <exception> // std::exception
#include <string> // std::string
#include <vector> // std::vector
using std::cerr;
using std::clog;
using std::cout;
using std::string;
INIT_TRACE("print-clang-ast");
static int innerMain(int argc, char const **argv)
{
// When printing GDValues, for example as part of test failures, use
// indentation.
gdv::GDValue::s_defaultWriteOptions.m_enableIndentation = true;
// Command line options parser for the options that precede those
// intended for clang.
PCACommandLineOptions options;
int firstClangArg = 1;
string err = options.parseCommandLine(firstClangArg, argc, argv);
if (!err.empty()) {
cerr << err << "\n";
return 2;
}
if (options.m_printUsage) {
options.printUsage(argv[0]);
return 0;
}
if (options.m_runUnitTests) {
pca_unit_tests();
cout << "PCA unit tests passed\n";
return 0;
}
ClangAST ast;
if (!ast.parseCommandLine(
stringVectorFromPointerArray(argc - firstClangArg,
argv + firstClangArg))) {
return 2;
}
// Get the name of the primary source file.
clang::FrontendOptions const &feOpts =
ast.m_compilerInvocation->getFrontendOpts();
if (feOpts.Inputs.size() != 1) {
// Unfortunately, this message is never seen because
// createInvocation will choke first (and in the multiple input
// case, spew a huge error message).
cerr << "print-clang-ast: expected exactly 1 input file, not "
<< feOpts.Inputs.size()
<< "\n";
return 2;
}
string primarySourceFileName = feOpts.Inputs[0].getFile().str();
TRACE1("primarySourceFileName: " << primarySourceFileName);
// Scan the file for additional options.
err = options.parsePrimarySourceFile(primarySourceFileName);
if (!err.empty()) {
cerr << err << "\n";
return 2;
}
TRACE1("options: " << options.getAsArgumentsString());
if (!ast.parseSourceCode()) {
return 2;
}
// Set `ClangUtil::s_instance`, which I don't like using, but
// occasionally is needed to enable tracing in weird spots.
GlobalClangUtilInstance gcui(ast.getASTContext());
if (options.m_forceImplicit) {
declareImplicitThings(ast.getASTUnit(), true /*defineAlso*/);
}
if (options.m_dumpAST) {
dumpClangAST(cout, ast.getASTContext());
}
if (options.m_printAST_JSON) {
printClangAST_JSON(cout, ast.getASTContext());
}
if (options.m_printerVisitor) {
PrinterVisitor::Flags flags = PrinterVisitor::F_NONE;
if (options.m_printVisitContext) {
flags |= PrinterVisitor::F_PRINT_VISIT_CONTEXT;
}
if (options.m_printImplicitQualTypes) {
flags |= PrinterVisitor::F_PRINT_IMPLICIT_QUAL_TYPES;
}
if (options.m_omit_CTPSD_TAW) {
flags |= PrinterVisitor::F_OMIT_CTPSD_TAW;
}
if (options.m_printDefaultArgExprs) {
flags |= PrinterVisitor::F_PRINT_DEFAULT_ARG_EXPRS;
}
if (options.m_ravCompat) {
flags |= PrinterVisitor::F_RAV_COMPAT;
}
printerVisitorTU(cout,
ast.getASTContext(),
flags);
}
if (options.m_ravPrinterVisitor) {
ravPrinterVisitorTU(cout, ast.getASTContext());
}
if (options.m_printASTNodes) {
PrintClangASTNodesConfiguration config;
config.m_printNonPSFFileEntities = options.m_fullTU;
config.m_printAddresses = !options.m_suppressAddresses;
config.m_printQualifiers = !options.m_noASTFieldQualifiers;
if (int failedAssertions =
printClangASTNodes(cout, ast.getASTContext(), config)) {
cerr << "Failed assertions: " << failedAssertions << "\n";
return 2;
}
}
// Exercise the visitor. Do this last so we can use the above
// printing mechanisms to help debug any failures.
clangASTVisitorTest(ast.getASTContext());
return 0;
}
int main(int argc, char const **argv)
{
try {
return innerMain(argc, argv);
}
catch (std::exception &x) {
std::cerr << x.what() << "\n";
return 2;
}
}
// EOF