Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 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 | |
Eli Friedman | b09f6e1 | 2009-05-19 04:14:29 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/Utils.h" |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 16 | #include "clang/Rewrite/Rewriter.h" |
| 17 | #include "clang/Lex/Preprocessor.h" |
| 18 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 20 | #include "llvm/System/Path.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/OwningPtr.h" |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 22 | #include <cstdio> |
| 23 | |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 26 | /// isSameToken - Return true if the two specified tokens start have the same |
| 27 | /// content. |
| 28 | static bool isSameToken(Token &RawTok, Token &PPTok) { |
Chris Lattner | 8ea78e6 | 2008-05-31 22:01:01 +0000 | [diff] [blame] | 29 | // If two tokens have the same kind and the same identifier info, they are |
| 30 | // obviously the same. |
| 31 | if (PPTok.getKind() == RawTok.getKind() && |
| 32 | PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo()) |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 33 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 8ea78e6 | 2008-05-31 22:01:01 +0000 | [diff] [blame] | 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. |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 38 | if (PPTok.getIdentifierInfo() && |
| 39 | PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo()) |
| 40 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 42 | return false; |
| 43 | } |
| 44 | |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 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 | unsigned &CurTok, bool ReturnComment) { |
| 50 | assert(CurTok < RawTokens.size() && "Overran eof!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 52 | // If the client doesn't want comments and we have one, skip it. |
| 53 | if (!ReturnComment && RawTokens[CurTok].is(tok::comment)) |
| 54 | ++CurTok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 56 | return RawTokens[CurTok++]; |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 59 | |
| 60 | /// LexRawTokensFromMainFile - Lets all the raw tokens from the main file into |
| 61 | /// the specified vector. |
| 62 | static void LexRawTokensFromMainFile(Preprocessor &PP, |
| 63 | std::vector<Token> &RawTokens) { |
| 64 | SourceManager &SM = PP.getSourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 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. |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 68 | const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID()); |
| 69 | Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions()); |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 70 | |
| 71 | // Switch on comment lexing because we really do want them. |
| 72 | RawLex.SetCommentRetentionState(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 74 | Token RawTok; |
| 75 | do { |
Chris Lattner | 590f0cc | 2008-10-12 01:15:46 +0000 | [diff] [blame] | 76 | RawLex.LexFromRawLexer(RawTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 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. |
| 81 | if (RawTok.is(tok::identifier) && !RawTok.getIdentifierInfo()) |
| 82 | RawTok.setIdentifierInfo(PP.LookUpIdentifierInfo(RawTok)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 84 | RawTokens.push_back(RawTok); |
| 85 | } while (RawTok.isNot(tok::eof)); |
| 86 | } |
| 87 | |
| 88 | |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 89 | /// RewriteMacrosInInput - Implement -rewrite-macros mode. |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 90 | void clang::RewriteMacrosInInput(Preprocessor &PP, llvm::raw_ostream *OS) { |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 91 | SourceManager &SM = PP.getSourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 93 | Rewriter Rewrite; |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 94 | Rewrite.setSourceMgr(SM, PP.getLangOptions()); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 95 | RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID()); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 97 | std::vector<Token> RawTokens; |
| 98 | LexRawTokensFromMainFile(PP, RawTokens); |
| 99 | unsigned CurRawTok = 0; |
| 100 | Token RawTok = GetNextRawTok(RawTokens, CurRawTok, false); |
| 101 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 103 | // Get the first preprocessing token. |
| 104 | PP.EnterMainSourceFile(); |
| 105 | Token PPTok; |
| 106 | PP.Lex(PPTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 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. |
Ted Kremenek | a9bc2d9 | 2008-08-14 03:45:07 +0000 | [diff] [blame] | 110 | // a header). If we see tokens that are in the preprocessed file but not the |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 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. |
| 114 | while (RawTok.isNot(tok::eof) || PPTok.isNot(tok::eof)) { |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 115 | SourceLocation PPLoc = SM.getInstantiationLoc(PPTok.getLocation()); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 116 | |
| 117 | // If PPTok is from a different source file, ignore it. |
| 118 | if (!SM.isFromMainFile(PPLoc)) { |
| 119 | PP.Lex(PPTok); |
| 120 | continue; |
| 121 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 123 | // If the raw file hits a preprocessor directive, they will be extra tokens |
Chris Lattner | a59e050 | 2008-07-25 16:37:06 +0000 | [diff] [blame] | 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. |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 127 | if (RawTok.is(tok::hash) && RawTok.isAtStartOfLine()) { |
Chris Lattner | a59e050 | 2008-07-25 16:37:06 +0000 | [diff] [blame] | 128 | // If this is a #warning directive or #pragma mark (GNU extensions), |
| 129 | // comment the line out. |
| 130 | if (RawTokens[CurRawTok].is(tok::identifier)) { |
| 131 | const IdentifierInfo *II = RawTokens[CurRawTok].getIdentifierInfo(); |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 132 | if (II->getName() == "warning") { |
Chris Lattner | a59e050 | 2008-07-25 16:37:06 +0000 | [diff] [blame] | 133 | // Comment out #warning. |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 134 | RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//"); |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 135 | } else if (II->getName() == "pragma" && |
Chris Lattner | a59e050 | 2008-07-25 16:37:06 +0000 | [diff] [blame] | 136 | RawTokens[CurRawTok+1].is(tok::identifier) && |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 137 | (RawTokens[CurRawTok+1].getIdentifierInfo()->getName() == |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 138 | "mark")) { |
Chris Lattner | a59e050 | 2008-07-25 16:37:06 +0000 | [diff] [blame] | 139 | // Comment out #pragma mark. |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 140 | RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//"); |
Chris Lattner | a59e050 | 2008-07-25 16:37:06 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Chris Lattner | a59e050 | 2008-07-25 16:37:06 +0000 | [diff] [blame] | 144 | // Otherwise, if this is a #include or some other directive, just leave it |
| 145 | // in the file by skipping over the line. |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 146 | RawTok = GetNextRawTok(RawTokens, CurRawTok, false); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 147 | while (!RawTok.isAtStartOfLine() && RawTok.isNot(tok::eof)) |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 148 | RawTok = GetNextRawTok(RawTokens, CurRawTok, false); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 149 | continue; |
| 150 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 152 | // Okay, both tokens are from the same file. Get their offsets from the |
| 153 | // start of the file. |
Chris Lattner | 52c2908 | 2009-01-27 06:27:13 +0000 | [diff] [blame] | 154 | unsigned PPOffs = SM.getFileOffset(PPLoc); |
| 155 | unsigned RawOffs = SM.getFileOffset(RawTok.getLocation()); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 156 | |
| 157 | // If the offsets are the same and the token kind is the same, ignore them. |
| 158 | if (PPOffs == RawOffs && isSameToken(RawTok, PPTok)) { |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 159 | RawTok = GetNextRawTok(RawTokens, CurRawTok, false); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 160 | PP.Lex(PPTok); |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | // If the PP token is farther along than the raw token, something was |
| 165 | // deleted. Comment out the raw token. |
| 166 | if (RawOffs <= PPOffs) { |
| 167 | // Comment out a whole run of tokens instead of bracketing each one with |
Chris Lattner | 1a78735 | 2008-06-03 06:10:17 +0000 | [diff] [blame] | 168 | // comments. Add a leading space if RawTok didn't have one. |
| 169 | bool HasSpace = RawTok.hasLeadingSpace(); |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 170 | RB.InsertTextAfter(RawOffs, " /*"+HasSpace); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 171 | unsigned EndPos; |
| 172 | |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 173 | do { |
| 174 | EndPos = RawOffs+RawTok.getLength(); |
| 175 | |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 176 | RawTok = GetNextRawTok(RawTokens, CurRawTok, true); |
Chris Lattner | 52c2908 | 2009-01-27 06:27:13 +0000 | [diff] [blame] | 177 | RawOffs = SM.getFileOffset(RawTok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 179 | if (RawTok.is(tok::comment)) { |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 180 | // Skip past the comment. |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 181 | RawTok = GetNextRawTok(RawTokens, CurRawTok, false); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 182 | break; |
| 183 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 185 | } while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() && |
| 186 | (PPOffs != RawOffs || !isSameToken(RawTok, PPTok))); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 187 | |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 188 | RB.InsertTextBefore(EndPos, "*/"); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 189 | continue; |
| 190 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 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 | unsigned InsertPos = PPOffs; |
| 196 | std::string Expansion; |
| 197 | while (PPOffs < RawOffs) { |
| 198 | Expansion += ' ' + PP.getSpelling(PPTok); |
| 199 | PP.Lex(PPTok); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 200 | PPLoc = SM.getInstantiationLoc(PPTok.getLocation()); |
Chris Lattner | 52c2908 | 2009-01-27 06:27:13 +0000 | [diff] [blame] | 201 | PPOffs = SM.getFileOffset(PPLoc); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 202 | } |
| 203 | Expansion += ' '; |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 204 | RB.InsertTextBefore(InsertPos, Expansion); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 205 | } |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 206 | |
| 207 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 208 | // we are done. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | if (const RewriteBuffer *RewriteBuf = |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 210 | Rewrite.getRewriteBufferFor(SM.getMainFileID())) { |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 211 | //printf("Changed:\n"); |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 212 | *OS << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 213 | } else { |
| 214 | fprintf(stderr, "No changes\n"); |
| 215 | } |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 216 | OS->flush(); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 217 | } |