-
Notifications
You must be signed in to change notification settings - Fork 4
/
NacroExpanders.cpp
277 lines (254 loc) · 9.22 KB
/
NacroExpanders.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
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
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/STLExtras.h"
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/MacroArgs.h"
#include "clang/Lex/PPCallbacks.h"
#include "NacroExpanders.h"
#include <iterator>
#include <vector>
using namespace clang;
using llvm::ArrayRef;
using llvm::Error;
using llvm::SmallVector;
using llvm::Twine;
static
DefMacroDirective* CreateMacroDirective(Preprocessor& PP, IdentifierInfo* Name,
SourceLocation BeginLoc,
ArrayRef<IdentifierInfo*> Args,
ArrayRef<Token> Body,
bool isVaradic = false) {
auto* MI = PP.AllocateMacroInfo(BeginLoc);
MI->setIsFunctionLike();
if(isVaradic) MI->setIsGNUVarargs();
for(auto Tok : Body) {
MI->AddTokenToBody(Tok);
}
MI->setParameterList(Args, PP.getPreprocessorAllocator());
return PP.appendDefMacroDirective(Name, MI);
}
Error NacroRuleExpander::ReplacementProtecting() {
using namespace llvm;
DenseMap<IdentifierInfo*, typename NacroRule::Replacement> IdentMap;
for(auto& R : Rule->replacements()) {
if(R.Identifier && !R.VarArgs) {
IdentMap.insert({R.Identifier, R});
}
}
if(IdentMap.empty()) return Error::success();
for(auto TI = Rule->token_begin(); TI != Rule->token_end();) {
auto Tok = *TI;
auto TokLoc = Tok.getLocation();
if(Tok.is(tok::identifier)) {
auto* II = Tok.getIdentifierInfo();
assert(II);
if(IdentMap.count(II)) {
auto& R = IdentMap[II];
using RTy = typename NacroRule::ReplacementTy;
switch(R.Type) {
case RTy::Expr: {
// Don't add parens if it's gonna be stringify later
if(TI != Rule->token_begin()) {
auto PrevTI = TI;
--PrevTI;
if(PrevTI->is(tok::hash)) {
++TI;
continue;
}
}
++TI;
Token LParen, RParen;
RParen.startToken();
RParen.setKind(tok::r_paren);
RParen.setLength(1);
RParen.setLocation(TokLoc.getLocWithOffset(1));
TI = Rule->insert_token(TI, RParen);
--TI;
LParen.startToken();
LParen.setKind(tok::l_paren);
LParen.setLength(1);
LParen.setLocation(TokLoc.getLocWithOffset(-1));
TI = Rule->insert_token(TI, LParen);
// skip expr and r_paren
for(int i = 0; i < 3; ++i) ++TI;
break;
}
case RTy::Stmt: {
++TI;
Token Semi;
Semi.startToken();
Semi.setKind(tok::semi);
Semi.setLength(1);
Semi.setLocation(TokLoc.getLocWithOffset(1));
TI = Rule->insert_token(TI, Semi);
++TI;
break;
}
default: ++TI;
}
continue;
}
}
++TI;
}
return Error::success();
}
namespace {
struct LoopExpandingPPCallbacks : public PPCallbacks {
LoopExpandingPPCallbacks(NacroRule* R, Preprocessor& PP)
: Rule(R), PP(PP) {
assert(Rule->hasVAArgs() && "No loops to expand from arguments");
llvm::transform(Rule->replacements(), std::back_inserter(UnexpArgsII),
[](NacroRule::Replacement& R) {
return R.Identifier;
});
}
void ExpandsLoop(const NacroRule::Loop& LoopInfo,
ArrayRef<Token> LoopBody,
ArrayRef<std::vector<Token>> ExpVAArgs,
SmallVectorImpl<Token>& OutputBuffer) {
auto* IndVarII = LoopInfo.InductionVar;
Token PrevTok, Tok;
for(const auto& Arg : ExpVAArgs) {
assert(Arg.size() > 0);
PrevTok.startToken();
PrevTok.setKind(tok::eof);
for(int I = 0, E = LoopBody.size(); I < E; ++I) {
if(I > 0) PrevTok = LoopBody[I - 1];
Tok = LoopBody[I];
if(Tok.is(tok::identifier) &&
Tok.getIdentifierInfo() == IndVarII) {
if(PrevTok.is(tok::hash)){
// Need to stringify
auto BeginLoc = PrevTok.getLocation(),
EndLoc = Tok.getLocation();
auto StrTok = MacroArgs::StringifyArgument(Arg.data(), PP, false,
BeginLoc, EndLoc);
// FIXME: Here is one of the most dirty workarounds in this
// project: Stringify can not be naturally dropped in here because
// we manually expand the VA arguments before stringify got
// applied on actual parameters. So the only way is to stringify
// ourself while we're manually expanding the VA arguments. But
// TokenLexer, which is the consumer of macro tokens, require
// every tokens in a macro comes from real text (i.e. isFileID
// needs to be true). But StrTok is born in scratch buffer. The
// workaround here is simply pick a random location in the macro
// and set it as StrTok's location. Which will 100% hinder the error
// message and debug experiences.
StrTok.setLocation(Tok.getLocation());
StrTok.setFlag(Token::StringifiedInMacro);
OutputBuffer.push_back(StrTok);
} else {
for(auto ArgTok : Arg) {
if(ArgTok.isNot(tok::eof)) {
ArgTok.setLocation(Tok.getLocation());
OutputBuffer.push_back(ArgTok);
}
}
}
} else {
// we will stringify by ourself
if(Tok.is(tok::hash)) continue;
OutputBuffer.push_back(Tok);
}
}
}
}
void MacroExpands(const Token& MacroNameToken,
const MacroDefinition& MD,
SourceRange Range,
const MacroArgs* ConstArgs) override {
// FIXME: Is this safe?
auto* Args = const_cast<MacroArgs*>(ConstArgs);
auto* MacroII = MacroNameToken.getIdentifierInfo();
assert(MacroII);
if(MacroII != Rule->getName()) return;
// Number of un-expanded arguments
assert(Args->getNumMacroArguments() == Rule->replacements_size());
// If there is a VAArgs, it must be the last (formal) argument
auto VAArgsIdx = Rule->replacements_size() - 1;
auto& VAReplacement = Rule->getReplacement(VAArgsIdx);
assert(VAReplacement.VarArgs);
const auto& RawExpVAArgs
= Args->getPreExpArgument(VAArgsIdx, PP);
SmallVector<std::vector<Token>, 4> ExpVAArgs;
std::vector<Token> VABuffer;
for(const auto& Tok : RawExpVAArgs) {
if(!Tok.isOneOf(tok::eof, tok::comma)) {
VABuffer.push_back(Tok);
} else {
// MacroArgs::StringifyArgument require
// input actual paramater list to be ended
// by tok::eof
Token EofTok;
EofTok.startToken();
EofTok.setKind(tok::eof);
VABuffer.push_back(EofTok);
ExpVAArgs.push_back(VABuffer);
VABuffer.clear();
}
}
// Create a new MacroInfo for this iteration number
SmallVector<Token, 16> ExpTokens;
auto LPI = Rule->loop_begin();
for(auto TokIdx = 0; TokIdx < Rule->token_size(); ++TokIdx) {
auto Tok = Rule->getToken(TokIdx);
if(Tok.is(tok::annot_pragma_loop_hint)) {
assert(LPI != Rule->loop_end() &&
"No loop in this rule or loop out-of-bound?");
const auto& LP = *(LPI++);
assert(VAReplacement.Identifier == LP.IterRange &&
"Iterating on non VAArgs variable");
// Extract loop body
SmallVector<Token, 8> LoopBody;
Tok = Rule->getToken(++TokIdx);
while(Tok.isNot(tok::annot_pragma_loop_hint)) {
LoopBody.push_back(Tok);
Tok = Rule->getToken(++TokIdx);
}
ExpandsLoop(LP, LoopBody, ExpVAArgs, ExpTokens);
} else {
ExpTokens.push_back(Tok);
}
}
auto* MI = MD.getMacroInfo();
llvm::for_each(ExpTokens, [&MI](const Token& Tok) {
MI->AddTokenToBody(Tok);
});
// Create an empty macro for next expansion
CreateMacroDirective(PP, MacroII,
Rule->getBeginLoc(),
UnexpArgsII, {}, true);
}
private:
NacroRule* Rule;
Preprocessor& PP;
SmallVector<IdentifierInfo*, 4> UnexpArgsII;
};
} // end anonymous namespace
Error NacroRuleExpander::Expand() {
if(auto E = ReplacementProtecting())
return E;
SmallVector<IdentifierInfo*, 2> ReplacementsII;
llvm::transform(Rule->replacements(), std::back_inserter(ReplacementsII),
[](NacroRule::Replacement& R) {
return R.Identifier;
});
if(!Rule->needsPPHooks()) {
// export as a normal macro function
CreateMacroDirective(PP, Rule->getName(), Rule->getBeginLoc(),
ReplacementsII,
ArrayRef<Token>(Rule->token_begin(),
Rule->token_end()));
}
if(!Rule->loop_empty()) {
// Create a placeholder macro first
CreateMacroDirective(PP, Rule->getName(), Rule->getBeginLoc(),
ReplacementsII, {}, true);
PP.addPPCallbacks(
std::make_unique<LoopExpandingPPCallbacks>(Rule, PP));
}
return Error::success();
}