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 | |
| 15 | #include "clang.h" |
| 16 | #include "clang/Rewrite/Rewriter.h" |
| 17 | #include "clang/Lex/Preprocessor.h" |
| 18 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Streams.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 21 | #include "llvm/System/Path.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 25 | /// isSameToken - Return true if the two specified tokens start have the same |
| 26 | /// content. |
| 27 | static bool isSameToken(Token &RawTok, Token &PPTok) { |
Chris Lattner | 8ea78e6 | 2008-05-31 22:01:01 +0000 | [diff] [blame] | 28 | // If two tokens have the same kind and the same identifier info, they are |
| 29 | // obviously the same. |
| 30 | if (PPTok.getKind() == RawTok.getKind() && |
| 31 | PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo()) |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 32 | return true; |
| 33 | |
Chris Lattner | 8ea78e6 | 2008-05-31 22:01:01 +0000 | [diff] [blame] | 34 | // Otherwise, if they are different but have the same identifier info, they |
| 35 | // are also considered to be the same. This allows keywords and raw lexed |
| 36 | // identifiers with the same name to be treated the same. |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 37 | if (PPTok.getIdentifierInfo() && |
| 38 | PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo()) |
| 39 | return true; |
| 40 | |
| 41 | return false; |
| 42 | } |
| 43 | |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 44 | |
| 45 | /// GetNextRawTok - Return the next raw token in the stream, skipping over |
| 46 | /// comments if ReturnComment is false. |
| 47 | static const Token &GetNextRawTok(const std::vector<Token> &RawTokens, |
| 48 | unsigned &CurTok, bool ReturnComment) { |
| 49 | assert(CurTok < RawTokens.size() && "Overran eof!"); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 51 | // If the client doesn't want comments and we have one, skip it. |
| 52 | if (!ReturnComment && RawTokens[CurTok].is(tok::comment)) |
| 53 | ++CurTok; |
| 54 | |
| 55 | return RawTokens[CurTok++]; |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 58 | |
| 59 | /// LexRawTokensFromMainFile - Lets all the raw tokens from the main file into |
| 60 | /// the specified vector. |
| 61 | static void LexRawTokensFromMainFile(Preprocessor &PP, |
| 62 | std::vector<Token> &RawTokens) { |
| 63 | SourceManager &SM = PP.getSourceManager(); |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 64 | |
| 65 | // Create a lexer to lex all the tokens of the main file in raw mode. Even |
| 66 | // though it is in raw mode, it will not return comments. |
Chris Lattner | 4448a01 | 2009-01-17 07:41:36 +0000 | [diff] [blame] | 67 | Lexer RawLex(SM.getMainFileID(), SM, PP.getLangOptions()); |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 68 | |
| 69 | // Switch on comment lexing because we really do want them. |
| 70 | RawLex.SetCommentRetentionState(true); |
| 71 | |
| 72 | Token RawTok; |
| 73 | do { |
Chris Lattner | 590f0cc | 2008-10-12 01:15:46 +0000 | [diff] [blame] | 74 | RawLex.LexFromRawLexer(RawTok); |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 75 | |
| 76 | // If we have an identifier with no identifier info for our raw token, look |
| 77 | // up the indentifier info. This is important for equality comparison of |
| 78 | // identifier tokens. |
| 79 | if (RawTok.is(tok::identifier) && !RawTok.getIdentifierInfo()) |
| 80 | RawTok.setIdentifierInfo(PP.LookUpIdentifierInfo(RawTok)); |
| 81 | |
| 82 | RawTokens.push_back(RawTok); |
| 83 | } while (RawTok.isNot(tok::eof)); |
| 84 | } |
| 85 | |
| 86 | |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 87 | /// RewriteMacrosInInput - Implement -rewrite-macros mode. |
Chris Lattner | 0951052 | 2008-05-09 22:43:24 +0000 | [diff] [blame] | 88 | void clang::RewriteMacrosInInput(Preprocessor &PP,const std::string &InFileName, |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 89 | const std::string &OutFileName) { |
| 90 | SourceManager &SM = PP.getSourceManager(); |
| 91 | |
| 92 | Rewriter Rewrite; |
| 93 | Rewrite.setSourceMgr(SM); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 94 | RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID()); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 96 | std::vector<Token> RawTokens; |
| 97 | LexRawTokensFromMainFile(PP, RawTokens); |
| 98 | unsigned CurRawTok = 0; |
| 99 | Token RawTok = GetNextRawTok(RawTokens, CurRawTok, false); |
| 100 | |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 101 | |
| 102 | // Get the first preprocessing token. |
| 103 | PP.EnterMainSourceFile(); |
| 104 | Token PPTok; |
| 105 | PP.Lex(PPTok); |
| 106 | |
| 107 | // Preprocess the input file in parallel with raw lexing the main file. Ignore |
| 108 | // 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] | 109 | // 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] | 110 | // lexed file, we have a macro expansion. If we see tokens in the lexed file |
| 111 | // that aren't in the preprocessed view, we have macros that expand to no |
| 112 | // tokens, or macro arguments etc. |
| 113 | while (RawTok.isNot(tok::eof) || PPTok.isNot(tok::eof)) { |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 114 | SourceLocation PPLoc = SM.getInstantiationLoc(PPTok.getLocation()); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 115 | |
| 116 | // If PPTok is from a different source file, ignore it. |
| 117 | if (!SM.isFromMainFile(PPLoc)) { |
| 118 | PP.Lex(PPTok); |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | // 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] | 123 | // in the raw file that don't exist in the preprocsesed file. However, we |
| 124 | // choose to preserve them in the output file and otherwise handle them |
| 125 | // specially. |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 126 | if (RawTok.is(tok::hash) && RawTok.isAtStartOfLine()) { |
Chris Lattner | a59e050 | 2008-07-25 16:37:06 +0000 | [diff] [blame] | 127 | // If this is a #warning directive or #pragma mark (GNU extensions), |
| 128 | // comment the line out. |
| 129 | if (RawTokens[CurRawTok].is(tok::identifier)) { |
| 130 | const IdentifierInfo *II = RawTokens[CurRawTok].getIdentifierInfo(); |
| 131 | if (!strcmp(II->getName(), "warning")) { |
| 132 | // Comment out #warning. |
Chris Lattner | 52c2908 | 2009-01-27 06:27:13 +0000 | [diff] [blame] | 133 | RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//", 2); |
Chris Lattner | a59e050 | 2008-07-25 16:37:06 +0000 | [diff] [blame] | 134 | } else if (!strcmp(II->getName(), "pragma") && |
| 135 | RawTokens[CurRawTok+1].is(tok::identifier) && |
| 136 | !strcmp(RawTokens[CurRawTok+1].getIdentifierInfo()->getName(), |
| 137 | "mark")){ |
| 138 | // Comment out #pragma mark. |
Chris Lattner | 52c2908 | 2009-01-27 06:27:13 +0000 | [diff] [blame] | 139 | RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//", 2); |
Chris Lattner | a59e050 | 2008-07-25 16:37:06 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | // Otherwise, if this is a #include or some other directive, just leave it |
| 144 | // in the file by skipping over the line. |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 145 | RawTok = GetNextRawTok(RawTokens, CurRawTok, false); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 146 | while (!RawTok.isAtStartOfLine() && RawTok.isNot(tok::eof)) |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 147 | RawTok = GetNextRawTok(RawTokens, CurRawTok, false); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 148 | continue; |
| 149 | } |
| 150 | |
| 151 | // Okay, both tokens are from the same file. Get their offsets from the |
| 152 | // start of the file. |
Chris Lattner | 52c2908 | 2009-01-27 06:27:13 +0000 | [diff] [blame] | 153 | unsigned PPOffs = SM.getFileOffset(PPLoc); |
| 154 | unsigned RawOffs = SM.getFileOffset(RawTok.getLocation()); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 155 | |
| 156 | // If the offsets are the same and the token kind is the same, ignore them. |
| 157 | if (PPOffs == RawOffs && isSameToken(RawTok, PPTok)) { |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 158 | RawTok = GetNextRawTok(RawTokens, CurRawTok, false); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 159 | PP.Lex(PPTok); |
| 160 | continue; |
| 161 | } |
| 162 | |
| 163 | // If the PP token is farther along than the raw token, something was |
| 164 | // deleted. Comment out the raw token. |
| 165 | if (RawOffs <= PPOffs) { |
| 166 | // 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] | 167 | // comments. Add a leading space if RawTok didn't have one. |
| 168 | bool HasSpace = RawTok.hasLeadingSpace(); |
| 169 | RB.InsertTextAfter(RawOffs, " /*"+HasSpace, 2+!HasSpace); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 170 | unsigned EndPos; |
| 171 | |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 172 | do { |
| 173 | EndPos = RawOffs+RawTok.getLength(); |
| 174 | |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 175 | RawTok = GetNextRawTok(RawTokens, CurRawTok, true); |
Chris Lattner | 52c2908 | 2009-01-27 06:27:13 +0000 | [diff] [blame] | 176 | RawOffs = SM.getFileOffset(RawTok.getLocation()); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 177 | |
| 178 | if (RawTok.is(tok::comment)) { |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 179 | // Skip past the comment. |
Chris Lattner | 0d76fcd | 2008-07-25 16:29:12 +0000 | [diff] [blame] | 180 | RawTok = GetNextRawTok(RawTokens, CurRawTok, false); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | |
| 184 | } while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() && |
| 185 | (PPOffs != RawOffs || !isSameToken(RawTok, PPTok))); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 186 | |
| 187 | RB.InsertTextBefore(EndPos, "*/", 2); |
| 188 | continue; |
| 189 | } |
| 190 | |
| 191 | // Otherwise, there was a replacement an expansion. Insert the new token |
| 192 | // in the output buffer. Insert the whole run of new tokens at once to get |
| 193 | // them in the right order. |
| 194 | unsigned InsertPos = PPOffs; |
| 195 | std::string Expansion; |
| 196 | while (PPOffs < RawOffs) { |
| 197 | Expansion += ' ' + PP.getSpelling(PPTok); |
| 198 | PP.Lex(PPTok); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 199 | PPLoc = SM.getInstantiationLoc(PPTok.getLocation()); |
Chris Lattner | 52c2908 | 2009-01-27 06:27:13 +0000 | [diff] [blame] | 200 | PPOffs = SM.getFileOffset(PPLoc); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 201 | } |
| 202 | Expansion += ' '; |
| 203 | RB.InsertTextBefore(InsertPos, &Expansion[0], Expansion.size()); |
| 204 | } |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 205 | |
| 206 | // Create the output file. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 207 | llvm::OwningPtr<llvm::raw_ostream> OwnedStream; |
| 208 | llvm::raw_ostream *OutFile; |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 209 | if (OutFileName == "-") { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 210 | OutFile = &llvm::outs(); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 211 | } else if (!OutFileName.empty()) { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 212 | std::string Err; |
Daniel Dunbar | 26fb272 | 2008-11-13 05:09:21 +0000 | [diff] [blame] | 213 | OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), false, Err); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 214 | OwnedStream.reset(OutFile); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 215 | } else if (InFileName == "-") { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 216 | OutFile = &llvm::outs(); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 217 | } else { |
| 218 | llvm::sys::Path Path(InFileName); |
| 219 | Path.eraseSuffix(); |
| 220 | Path.appendSuffix("cpp"); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 221 | std::string Err; |
Daniel Dunbar | 26fb272 | 2008-11-13 05:09:21 +0000 | [diff] [blame] | 222 | OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(), false, Err); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 223 | OwnedStream.reset(OutFile); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 227 | // we are done. |
| 228 | if (const RewriteBuffer *RewriteBuf = |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 229 | Rewrite.getRewriteBufferFor(SM.getMainFileID())) { |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 230 | //printf("Changed:\n"); |
| 231 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 232 | } else { |
| 233 | fprintf(stderr, "No changes\n"); |
| 234 | } |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 235 | OutFile->flush(); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 236 | } |