clang: lib/Frontend/Rewrite/RewriteMacros.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
18#include "llvm/ADT/RewriteBuffer.h"
19#include
20
21using namespace clang;
22using llvm::RewriteBuffer;
23
24
25
27
28
31 return true;
32
33
34
35
38 return true;
39
40 return false;
41}
42
43
44
45
47 unsigned &CurTok, bool ReturnComment) {
48 assert(CurTok < RawTokens.size() && "Overran eof!");
49
50
51 if (!ReturnComment && RawTokens[CurTok].is(tok::comment))
52 ++CurTok;
53
54 return RawTokens[CurTok++];
55}
56
57
58
59
61 std::vector &RawTokens) {
63
64
65
66 llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(SM.getMainFileID());
68
69
71
73 do {
75
76
77
78
79 if (RawTok.is(tok::raw_identifier))
81
82 RawTokens.push_back(RawTok);
83 } while (RawTok.isNot(tok::eof));
84}
85
86
87
90
93 RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID());
94
95 std::vector RawTokens;
97 unsigned CurRawTok = 0;
99
100
101
104 PP.Lex(PPTok);
105
106
107
108
109
110
111
112 while (RawTok.isNot(tok::eof) || PPTok.isNot(tok::eof)) {
114
115
116 if (.isWrittenInMainFile(PPLoc)) {
117 PP.Lex(PPTok);
118 continue;
119 }
120
121
122
123
124
126
127
128 if (RawTokens[CurRawTok].is(tok::identifier)) {
129 const IdentifierInfo *II = RawTokens[CurRawTok].getIdentifierInfo();
130 if (II->getName() == "warning") {
131
132 RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
133 } else if (II->getName() == "pragma" &&
134 RawTokens[CurRawTok+1].is(tok::identifier) &&
135 (RawTokens[CurRawTok+1].getIdentifierInfo()->getName() ==
136 "mark")) {
137
138 RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
139 }
140 }
141
142
143
144 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
146 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
147 continue;
148 }
149
150
151
152 unsigned PPOffs = SM.getFileOffset(PPLoc);
153 unsigned RawOffs = SM.getFileOffset(RawTok.getLocation());
154
155
156 if (PPOffs == RawOffs && isSameToken(RawTok, PPTok)) {
157 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
158 PP.Lex(PPTok);
159 continue;
160 }
161
162
163
164 if (RawOffs <= PPOffs) {
165
166
168 RB.InsertTextAfter(RawOffs, &" /*"[HasSpace]);
169 unsigned EndPos;
170
171 do {
172 EndPos = RawOffs+RawTok.getLength();
173
174 RawTok = GetNextRawTok(RawTokens, CurRawTok, true);
175 RawOffs = SM.getFileOffset(RawTok.getLocation());
176
177 if (RawTok.is(tok::comment)) {
178
179 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
180 break;
181 }
182
183 } while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() &&
184 (PPOffs != RawOffs || (RawTok, PPTok)));
185
186 RB.InsertTextBefore(EndPos, "*/");
187 continue;
188 }
189
190
191
192
193 unsigned InsertPos = PPOffs;
194 std::string Expansion;
195 while (PPOffs < RawOffs) {
196 Expansion += ' ' + PP.getSpelling(PPTok);
197 PP.Lex(PPTok);
199 PPOffs = SM.getFileOffset(PPLoc);
200 }
201 Expansion += ' ';
202 RB.InsertTextBefore(InsertPos, Expansion);
203 }
204
205
206
207 if (const RewriteBuffer *RewriteBuf =
208 Rewrite.getRewriteBufferFor(SM.getMainFileID())) {
209
210 *OS << std::string(RewriteBuf->begin(), RewriteBuf->end());
211 } else {
212 fprintf(stderr, "No changes\n");
213 }
214 OS->flush();
215}
bool is(tok::TokenKind Kind) const
Defines the clang::Preprocessor interface.
static bool isSameToken(Token &RawTok, Token &PPTok)
isSameToken - Return true if the two specified tokens start have the same content.
Definition RewriteMacros.cpp:26
static const Token & GetNextRawTok(const std::vector< Token > &RawTokens, unsigned &CurTok, bool ReturnComment)
GetNextRawTok - Return the next raw token in the stream, skipping over comments if ReturnComment is f...
Definition RewriteMacros.cpp:46
static void LexRawTokensFromMainFile(Preprocessor &PP, std::vector< Token > &RawTokens)
LexRawTokensFromMainFile - Lets all the raw tokens from the main file into the specified vector.
Definition RewriteMacros.cpp:60
Defines the SourceManager interface.
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
Lexer - This provides a simple interface that turns a text buffer into a stream of tokens.
bool LexFromRawLexer(Token &Result)
LexFromRawLexer - Lex a token from a designated raw lexer (one with no associated preprocessor object...
void SetCommentRetentionState(bool Mode)
SetCommentRetentionMode - Change the comment retention mode of the lexer to the specified mode.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
IdentifierInfo * LookUpIdentifierInfo(Token &Identifier) const
Given a tok::raw_identifier token, look up the identifier information for the token and install it in...
void Lex(Token &Result)
Lex the next token for this preprocessor.
void EnterMainSourceFile()
Enter the specified FileID as the main source file, which implicitly adds the builtin defines etc.
SourceManager & getSourceManager() const
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the 'spelling' of the token at the given location; does not go up to the spelling location or ...
const LangOptions & getLangOpts() const
Rewriter - This is the main interface to the rewrite buffers.
Encodes a location in the source.
This class handles loading and caching of source files into memory.
Token - This structure provides full information about a lexed token.
IdentifierInfo * getIdentifierInfo() const
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
unsigned getLength() const
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
tok::TokenKind getKind() const
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
bool hasLeadingSpace() const
Return true if this token has whitespace before it.
bool isNot(tok::TokenKind K) const
The JSON file list parser is used to communicate input to InstallAPI.
@ Rewrite
We are substituting template parameters for (typically) other template parameters in order to rewrite...
void RewriteMacrosInInput(Preprocessor &PP, raw_ostream *OS)
RewriteMacrosInInput - Implement -rewrite-macros mode.
Definition RewriteMacros.cpp:88
__LIBC_ATTRS FILE * stderr