 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
73.8% |
62 / 84 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
92.9% |
78 / 84 |
| |
|
Line Coverage: |
91.8% |
78 / 85 |
| |
 |
|
 |
1 : //===--- RewriteMacros.cpp - Rewrite macros into their expansions ---------===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // This code rewrites macro invocations into their expansions. This gives you
11 : // a macro expanded file that retains comments and #includes.
12 : //
13 : //===----------------------------------------------------------------------===//
14 :
15 : #include "clang/Frontend/Utils.h"
16 : #include "clang/Rewrite/Rewriter.h"
17 : #include "clang/Lex/Preprocessor.h"
18 : #include "clang/Basic/SourceManager.h"
19 : #include "llvm/Support/raw_ostream.h"
20 : #include "llvm/System/Path.h"
21 : #include "llvm/ADT/OwningPtr.h"
22 : #include <cstdio>
23 :
24 : using namespace clang;
25 :
26 : /// isSameToken - Return true if the two specified tokens start have the same
27 : /// content.
28 1: static bool isSameToken(Token &RawTok, Token &PPTok) {
29 : // If two tokens have the same kind and the same identifier info, they are
30 : // obviously the same.
0: branch 2 not taken
1: branch 3 taken
0: branch 6 not taken
0: branch 7 not taken
0: branch 8 not taken
1: branch 9 taken
31 1: if (PPTok.getKind() == RawTok.getKind() &&
32 : PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo())
33 0: return true;
34 :
35 : // Otherwise, if they are different but have the same identifier info, they
36 : // are also considered to be the same. This allows keywords and raw lexed
37 : // identifiers with the same name to be treated the same.
0: branch 1 not taken
1: branch 2 taken
0: branch 5 not taken
0: branch 6 not taken
0: branch 7 not taken
1: branch 8 taken
38 1: if (PPTok.getIdentifierInfo() &&
39 : PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo())
40 0: return true;
41 :
42 1: return false;
43 : }
44 :
45 :
46 : /// GetNextRawTok - Return the next raw token in the stream, skipping over
47 : /// comments if ReturnComment is false.
48 : static const Token &GetNextRawTok(const std::vector<Token> &RawTokens,
49 32: unsigned &CurTok, bool ReturnComment) {
32: branch 1 taken
0: branch 2 not taken
50 32: assert(CurTok < RawTokens.size() && "Overran eof!");
51 :
52 : // If the client doesn't want comments and we have one, skip it.
19: branch 0 taken
13: branch 1 taken
3: branch 4 taken
16: branch 5 taken
3: branch 6 taken
29: branch 7 taken
53 51: if (!ReturnComment && RawTokens[CurTok].is(tok::comment))
54 3: ++CurTok;
55 :
56 32: return RawTokens[CurTok++];
57 : }
58 :
59 :
60 : /// LexRawTokensFromMainFile - Lets all the raw tokens from the main file into
61 : /// the specified vector.
62 : static void LexRawTokensFromMainFile(Preprocessor &PP,
63 1: std::vector<Token> &RawTokens) {
64 1: SourceManager &SM = PP.getSourceManager();
65 :
66 : // Create a lexer to lex all the tokens of the main file in raw mode. Even
67 : // though it is in raw mode, it will not return comments.
68 1: const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
69 1: Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions());
70 :
71 : // Switch on comment lexing because we really do want them.
72 1: RawLex.SetCommentRetentionState(true);
73 :
74 1: Token RawTok;
34: branch 1 taken
1: branch 2 taken
75 35: do {
76 35: RawLex.LexFromRawLexer(RawTok);
77 :
78 : // If we have an identifier with no identifier info for our raw token, look
79 : // up the indentifier info. This is important for equality comparison of
80 : // identifier tokens.
13: branch 1 taken
22: branch 2 taken
13: branch 4 taken
0: branch 5 not taken
13: branch 6 taken
22: branch 7 taken
81 35: if (RawTok.is(tok::identifier) && !RawTok.getIdentifierInfo())
82 13: RawTok.setIdentifierInfo(PP.LookUpIdentifierInfo(RawTok));
83 :
84 35: RawTokens.push_back(RawTok);
85 1: } while (RawTok.isNot(tok::eof));
86 1: }
87 :
88 :
89 : /// RewriteMacrosInInput - Implement -rewrite-macros mode.
90 1: void clang::RewriteMacrosInInput(Preprocessor &PP, llvm::raw_ostream *OS) {
91 1: SourceManager &SM = PP.getSourceManager();
92 :
93 1: Rewriter Rewrite;
94 1: Rewrite.setSourceMgr(SM, PP.getLangOptions());
95 1: RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID());
96 :
97 1: std::vector<Token> RawTokens;
98 1: LexRawTokensFromMainFile(PP, RawTokens);
99 1: unsigned CurRawTok = 0;
100 1: Token RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
101 :
102 :
103 : // Get the first preprocessing token.
104 1: PP.EnterMainSourceFile();
105 1: Token PPTok;
106 1: PP.Lex(PPTok);
107 :
108 : // Preprocess the input file in parallel with raw lexing the main file. Ignore
109 : // all tokens that are preprocessed from a file other than the main file (e.g.
110 : // a header). If we see tokens that are in the preprocessed file but not the
111 : // lexed file, we have a macro expansion. If we see tokens in the lexed file
112 : // that aren't in the preprocessed view, we have macros that expand to no
113 : // tokens, or macro arguments etc.
1: branch 2 taken
12: branch 3 taken
0: branch 5 not taken
1: branch 6 taken
12: branch 7 taken
1: branch 8 taken
114 2: while (RawTok.isNot(tok::eof) || PPTok.isNot(tok::eof)) {
115 12: SourceLocation PPLoc = SM.getInstantiationLoc(PPTok.getLocation());
116 :
117 : // If PPTok is from a different source file, ignore it.
5: branch 1 taken
7: branch 2 taken
118 12: if (!SM.isFromMainFile(PPLoc)) {
119 5: PP.Lex(PPTok);
120 5: continue;
121 : }
122 :
123 : // If the raw file hits a preprocessor directive, they will be extra tokens
124 : // in the raw file that don't exist in the preprocsesed file. However, we
125 : // choose to preserve them in the output file and otherwise handle them
126 : // specially.
3: branch 1 taken
4: branch 2 taken
2: branch 4 taken
1: branch 5 taken
2: branch 6 taken
5: branch 7 taken
127 7: if (RawTok.is(tok::hash) && RawTok.isAtStartOfLine()) {
128 : // If this is a #warning directive or #pragma mark (GNU extensions),
129 : // comment the line out.
2: branch 2 taken
0: branch 3 not taken
130 2: if (RawTokens[CurRawTok].is(tok::identifier)) {
131 2: const IdentifierInfo *II = RawTokens[CurRawTok].getIdentifierInfo();
0: branch 3 not taken
2: branch 4 taken
132 2: if (II->getName() == "warning") {
133 : // Comment out #warning.
134 0: RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
1: branch 3 taken
1: branch 4 taken
1: branch 7 taken
0: branch 8 not taken
1: branch 14 taken
0: branch 15 not taken
1: branch 16 taken
1: branch 17 taken
135 2: } else if (II->getName() == "pragma" &&
136 : RawTokens[CurRawTok+1].is(tok::identifier) &&
137 : (RawTokens[CurRawTok+1].getIdentifierInfo()->getName() ==
138 : "mark")) {
139 : // Comment out #pragma mark.
140 1: RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
141 : }
142 : }
143 :
144 : // Otherwise, if this is a #include or some other directive, just leave it
145 : // in the file by skipping over the line.
146 2: RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
14: branch 1 taken
1: branch 2 taken
13: branch 4 taken
1: branch 5 taken
13: branch 6 taken
2: branch 7 taken
147 17: while (!RawTok.isAtStartOfLine() && RawTok.isNot(tok::eof))
148 13: RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
149 2: continue;
150 : }
151 :
152 : // Okay, both tokens are from the same file. Get their offsets from the
153 : // start of the file.
154 5: unsigned PPOffs = SM.getFileOffset(PPLoc);
155 5: unsigned RawOffs = SM.getFileOffset(RawTok.getLocation());
156 :
157 : // If the offsets are the same and the token kind is the same, ignore them.
1: branch 0 taken
4: branch 1 taken
0: branch 3 not taken
1: branch 4 taken
0: branch 5 not taken
5: branch 6 taken
158 5: if (PPOffs == RawOffs && isSameToken(RawTok, PPTok)) {
159 0: RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
160 0: PP.Lex(PPTok);
161 0: continue;
162 : }
163 :
164 : // If the PP token is farther along than the raw token, something was
165 : // deleted. Comment out the raw token.
4: branch 0 taken
1: branch 1 taken
166 5: if (RawOffs <= PPOffs) {
167 : // Comment out a whole run of tokens instead of bracketing each one with
168 : // comments. Add a leading space if RawTok didn't have one.
169 4: bool HasSpace = RawTok.hasLeadingSpace();
170 4: RB.InsertTextAfter(RawOffs, " /*"+HasSpace);
171 : unsigned EndPos;
172 :
9: branch 0 taken
1: branch 1 taken
9: branch 3 taken
0: branch 4 not taken
0: branch 5 not taken
9: branch 6 taken
0: branch 8 not taken
0: branch 9 not taken
9: branch 10 taken
1: branch 11 taken
173 10: do {
174 13: EndPos = RawOffs+RawTok.getLength();
175 :
176 13: RawTok = GetNextRawTok(RawTokens, CurRawTok, true);
177 13: RawOffs = SM.getFileOffset(RawTok.getLocation());
178 :
3: branch 1 taken
10: branch 2 taken
179 13: if (RawTok.is(tok::comment)) {
180 : // Skip past the comment.
181 3: RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
182 3: break;
183 : }
184 :
185 : } while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() &&
186 : (PPOffs != RawOffs || !isSameToken(RawTok, PPTok)));
187 :
188 4: RB.InsertTextBefore(EndPos, "*/");
189 4: continue;
190 : }
191 :
192 : // Otherwise, there was a replacement an expansion. Insert the new token
193 : // in the output buffer. Insert the whole run of new tokens at once to get
194 : // them in the right order.
195 1: unsigned InsertPos = PPOffs;
196 1: std::string Expansion;
1: branch 0 taken
1: branch 1 taken
197 3: while (PPOffs < RawOffs) {
198 1: Expansion += ' ' + PP.getSpelling(PPTok);
199 1: PP.Lex(PPTok);
200 1: PPLoc = SM.getInstantiationLoc(PPTok.getLocation());
201 1: PPOffs = SM.getFileOffset(PPLoc);
202 : }
203 1: Expansion += ' ';
204 1: RB.InsertTextBefore(InsertPos, Expansion);
205 : }
206 :
207 : // Get the buffer corresponding to MainFileID. If we haven't changed it, then
208 : // we are done.
1: branch 0 taken
0: branch 1 not taken
209 1: if (const RewriteBuffer *RewriteBuf =
210 1: Rewrite.getRewriteBufferFor(SM.getMainFileID())) {
211 : //printf("Changed:\n");
212 1: *OS << std::string(RewriteBuf->begin(), RewriteBuf->end());
213 : } else {
214 0: fprintf(stderr, "No changes\n");
215 : }
216 1: OS->flush();
217 1: }
Generated: 2010-02-10 01:31 by zcov