-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathpreprocessorcallback.cpp
More file actions
351 lines (294 loc) · 12.5 KB
/
preprocessorcallback.cpp
File metadata and controls
351 lines (294 loc) · 12.5 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/****************************************************************************
* Copyright (C) 2012-2016 Woboq GmbH
* Olivier Goffart <contact at woboq.com>
* https://woboq.com/codebrowser.html
*
* This file is part of the Woboq Code Browser.
*
* Commercial License Usage:
* Licensees holding valid commercial licenses provided by Woboq may use
* this file in accordance with the terms contained in a written agreement
* between the licensee and Woboq.
* For further information see https://woboq.com/codebrowser.html
*
* Alternatively, this work may be used under a Creative Commons
* Attribution-NonCommercial-ShareAlike 3.0 (CC-BY-NC-SA 3.0) License.
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US
* This license does not allow you to use the code browser to assist the
* development of your commercial software. If you intent to do so, consider
* purchasing a commercial licence.
****************************************************************************/
#include "preprocessorcallback.h"
#include "annotator.h"
#include "stringbuilder.h"
#include <clang/Basic/FileManager.h>
#include <clang/Basic/Version.h>
#include <clang/Lex/MacroInfo.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/Lex/Token.h>
#include <llvm/ADT/Twine.h>
void PreprocessorCallback::MacroExpands(const clang::Token &MacroNameTok, MyMacroDefinition MD,
clang::SourceRange Range, const clang::MacroArgs *)
{
if (disabled)
return;
auto *MI = MD.getMacroInfo();
clang::SourceLocation loc = MacroNameTok.getLocation();
if (!loc.isValid() || !loc.isFileID())
return;
clang::SourceManager &sm = annotator.getSourceMgr();
clang::FileID FID = sm.getFileID(loc);
if (!annotator.shouldProcess(FID))
return;
const char *begin = sm.getCharacterData(Range.getBegin());
int len = sm.getCharacterData(Range.getEnd()) - begin;
len += clang::Lexer::MeasureTokenLength(Range.getEnd(), sm, PP.getLangOpts());
std::string copy(begin, len);
begin = copy.c_str();
clang::Lexer lex(loc, PP.getLangOpts(), begin, begin, begin + len);
std::vector<clang::Token> tokens;
std::string expansion;
// Lousely based on code from clang::html::HighlightMacros
// Lex all the tokens in raw mode, to avoid entering #includes or expanding
// macros.
clang::Token tok;
do {
lex.LexFromRawLexer(tok);
// If this is a # at the start of a line, discard it from the token stream.
// We don't want the re-preprocess step to see #defines, #includes or other
// preprocessor directives.
if (tok.is(clang::tok::hash) && tok.isAtStartOfLine())
continue;
// If this is a ## token, change its kind to unknown so that repreprocessing
// it will not produce an error.
if (tok.is(clang::tok::hashhash))
tok.setKind(clang::tok::unknown);
// If this raw token is an identifier, the raw lexer won't have looked up
// the corresponding identifier info for it. Do this now so that it will be
// macro expanded when we re-preprocess it.
if (tok.is(clang::tok::raw_identifier))
PP.LookUpIdentifierInfo(tok);
tokens.push_back(tok);
} while (!tok.is(clang::tok::eof));
// Temporarily change the diagnostics object so that we ignore any generated
// diagnostics from this pass.
clang::DiagnosticsEngine TmpDiags(PP.getDiagnostics().getDiagnosticIDs(),
#if CLANG_VERSION_MAJOR >= 21
PP.getDiagnostics().getDiagnosticOptions(),
#else
&PP.getDiagnostics().getDiagnosticOptions(),
#endif
new clang::IgnoringDiagConsumer);
disabled = true;
clang::DiagnosticsEngine *OldDiags = &PP.getDiagnostics();
PP.setDiagnostics(TmpDiags);
// We don't want pragmas either. Although we filtered out #pragma, removing
// _Pragma and __pragma is much harder.
bool pragmasPreviouslyEnabled = PP.getPragmasEnabled();
PP.setPragmasEnabled(false);
seenPragma = false;
PP.EnterTokenStream(tokens, /*DisableMacroExpansion=*/false, /*IsReinject=*/false);
PP.Lex(tok);
while (tok.isNot(clang::tok::eof)) {
if (seenPragma) {
// skip pragma
while (tok.isNot(clang::tok::eof) && tok.isNot(clang::tok::eod))
PP.Lex(tok);
seenPragma = false;
PP.Lex(tok);
continue;
}
// If the tokens were already space separated, or if they must be to avoid
// them being implicitly pasted, add a space between them.
if (tok.hasLeadingSpace())
expansion += ' ';
// ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok)) //FIXME
// Escape any special characters in the token text.
expansion += PP.getSpelling(tok);
if (expansion.size() >= 30 * 1000) {
// Don't let the macro expansion grow too large.
expansion += "...";
while (tok.isNot(clang::tok::eof))
PP.LexUnexpandedToken(tok);
break;
}
PP.Lex(tok);
}
PP.setDiagnostics(*OldDiags);
PP.setPragmasEnabled(pragmasPreviouslyEnabled);
disabled = false;
std::string ref = llvm::Twine("_M/", MacroNameTok.getIdentifierInfo()->getName()).str();
clang::SourceLocation defLoc = MI->getDefinitionLoc();
clang::FileID defFID = sm.getFileID(defLoc);
llvm::SmallString<128> expansionBuffer;
std::string link;
std::string dataProj;
if (defFID != FID) {
link = annotator.pathTo(FID, defFID, &dataProj);
if (link.empty()) {
std::string tag = "class=\"macro\" title=\""
% Generator::escapeAttr(expansion, expansionBuffer) % "\" data-ref=\"" % ref % "\"";
annotator.generator(FID).addTag("span", tag, sm.getFileOffset(loc),
MacroNameTok.getLength());
return;
}
if (!dataProj.empty()) {
dataProj = " data-proj=\"" % dataProj % "\"";
}
}
if (sm.getMainFileID() != defFID) {
annotator.registerMacro(ref, MacroNameTok.getLocation(), Annotator::Use_Call);
}
std::string tag = "class=\"macro\" href=\"" % link % "#"
% llvm::Twine(sm.getExpansionLineNumber(defLoc)).str() % "\" title=\""
% Generator::escapeAttr(expansion, expansionBuffer) % "\" data-ref=\"" % ref % "\""
% dataProj;
annotator.generator(FID).addTag("a", tag, sm.getFileOffset(loc), MacroNameTok.getLength());
}
void PreprocessorCallback::MacroDefined(const clang::Token &MacroNameTok,
const clang::MacroDirective *MD)
{
clang::SourceLocation loc = MacroNameTok.getLocation();
if (!loc.isValid() || !loc.isFileID())
return;
clang::SourceManager &sm = annotator.getSourceMgr();
clang::FileID FID = sm.getFileID(loc);
if (!annotator.shouldProcess(FID))
return;
std::string ref = llvm::Twine("_M/", MacroNameTok.getIdentifierInfo()->getName()).str();
if (sm.getMainFileID() != FID) {
annotator.registerMacro(ref, MacroNameTok.getLocation(), Annotator::Declaration);
}
annotator.generator(FID).addTag("dfn",
"class=\"macro\" id=\"" % ref % "\" data-ref=\"" % ref % "\"",
sm.getFileOffset(loc), MacroNameTok.getLength());
}
void PreprocessorCallback::MacroUndefined(const clang::Token &MacroNameTok,
PreprocessorCallback::MyMacroDefinition MD,
const clang::MacroDirective *)
{
clang::SourceLocation loc = MacroNameTok.getLocation();
if (!loc.isValid() || !loc.isFileID())
return;
clang::SourceManager &sm = annotator.getSourceMgr();
clang::FileID FID = sm.getFileID(loc);
if (!annotator.shouldProcess(FID))
return;
std::string ref = llvm::Twine("_M/", MacroNameTok.getIdentifierInfo()->getName()).str();
std::string link;
std::string dataProj;
clang::SourceLocation defLoc;
clang::FileID defFID;
if (MD) {
auto *MI = MD.getMacroInfo();
if (MI) {
defLoc = MI->getDefinitionLoc();
defFID = sm.getFileID(defLoc);
}
}
if (defFID.isInvalid() || defFID != FID) {
if (!defFID.isInvalid()) {
link = annotator.pathTo(FID, defFID, &dataProj);
}
if (link.empty()) {
std::string tag = "class=\"macro\" data-ref=\"" % ref % "\"";
annotator.generator(FID).addTag("span", tag, sm.getFileOffset(loc),
MacroNameTok.getLength());
return;
}
if (!dataProj.empty()) {
dataProj = " data-proj=\"" % dataProj % "\"";
}
}
if (sm.getMainFileID() != defFID) {
annotator.registerMacro(ref, MacroNameTok.getLocation(), Annotator::Use_Write);
}
std::string tag = "class=\"macro\" href=\"" % link % "#"
% llvm::Twine(sm.getExpansionLineNumber(defLoc)).str() % "\" data-ref=\"" % ref % "\""
% dataProj;
annotator.generator(FID).addTag("a", tag, sm.getFileOffset(loc), MacroNameTok.getLength());
}
void PreprocessorCallback::InclusionDirective(
clang::SourceLocation HashLoc, const clang::Token &IncludeTok, llvm::StringRef FileName,
bool IsAngled, clang::CharSourceRange FilenameRange, clang::OptionalFileEntryRef File,
llvm::StringRef SearchPath, llvm::StringRef RelativePath, const clang::Module *Imported,
#if CLANG_VERSION_MAJOR >= 19
bool ModuleImported,
#endif
clang::SrcMgr::CharacteristicKind)
{
if (!HashLoc.isValid() || !HashLoc.isFileID() || !File)
return;
clang::SourceManager &sm = annotator.getSourceMgr();
clang::FileID FID = sm.getFileID(HashLoc);
if (!annotator.shouldProcess(FID))
return;
std::string link = annotator.pathTo(FID, File->getName());
if (link.empty())
return;
auto B = sm.getFileOffset(FilenameRange.getBegin());
auto E = sm.getFileOffset(FilenameRange.getEnd());
annotator.generator(FID).addTag("a", "href=\"" % link % "\"", B, E - B);
}
void PreprocessorCallback::Defined(const clang::Token &MacroNameTok, MyMacroDefinition MD,
clang::SourceRange Range)
{
clang::SourceLocation loc = MacroNameTok.getLocation();
if (!loc.isValid() || !loc.isFileID())
return;
clang::SourceManager &sm = annotator.getSourceMgr();
clang::FileID FID = sm.getFileID(loc);
if (!annotator.shouldProcess(FID))
return;
std::string ref = llvm::Twine("_M/", MacroNameTok.getIdentifierInfo()->getName()).str();
std::string link;
std::string dataProj;
clang::SourceLocation defLoc;
clang::FileID defFID;
if (MD) {
auto *MI = MD.getMacroInfo();
if (MI) {
defLoc = MI->getDefinitionLoc();
defFID = sm.getFileID(defLoc);
}
}
if (defFID.isInvalid() || defFID != FID) {
if (!defFID.isInvalid()) {
link = annotator.pathTo(FID, defFID, &dataProj);
}
if (link.empty()) {
std::string tag = "class=\"macro\" data-ref=\"" % ref % "\"";
annotator.generator(FID).addTag("span", tag, sm.getFileOffset(loc),
MacroNameTok.getLength());
return;
}
if (!dataProj.empty()) {
dataProj = " data-proj=\"" % dataProj % "\"";
}
}
if (sm.getMainFileID() != defFID) {
annotator.registerMacro(ref, MacroNameTok.getLocation(), Annotator::Use_Address);
}
std::string tag = "class=\"macro\" href=\"" % link % "#"
% llvm::Twine(sm.getExpansionLineNumber(defLoc)).str() % "\" data-ref=\"" % ref % "\""
% dataProj;
annotator.generator(FID).addTag("a", tag, sm.getFileOffset(loc), MacroNameTok.getLength());
}
void PreprocessorCallback::HandlePPCond(clang::SourceLocation Loc, clang::SourceLocation IfLoc)
{
if (!Loc.isValid() || !Loc.isFileID())
return;
clang::SourceManager &SM = annotator.getSourceMgr();
clang::FileID FID = SM.getFileID(Loc);
if (!annotator.shouldProcess(FID))
return;
while (ElifMapping.count(IfLoc)) {
IfLoc = Loc;
}
if (SM.getFileID(IfLoc) != FID) {
return;
}
annotator.generator(FID).addTag(
"span", ("data-ppcond=\"" + clang::Twine(SM.getExpansionLineNumber(IfLoc)) + "\"").str(),
SM.getFileOffset(Loc), clang::Lexer::MeasureTokenLength(Loc, SM, PP.getLangOpts()));
}