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" |
| 20 | #include "llvm/System/Path.h" |
| 21 | #include <fstream> |
| 22 | using namespace clang; |
| 23 | |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 24 | /// isSameToken - Return true if the two specified tokens start have the same |
| 25 | /// content. |
| 26 | static bool isSameToken(Token &RawTok, Token &PPTok) { |
Chris Lattner | 8ea78e6 | 2008-05-31 22:01:01 +0000 | [diff] [blame] | 27 | // If two tokens have the same kind and the same identifier info, they are |
| 28 | // obviously the same. |
| 29 | if (PPTok.getKind() == RawTok.getKind() && |
| 30 | PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo()) |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 31 | return true; |
| 32 | |
Chris Lattner | 8ea78e6 | 2008-05-31 22:01:01 +0000 | [diff] [blame] | 33 | // Otherwise, if they are different but have the same identifier info, they |
| 34 | // are also considered to be the same. This allows keywords and raw lexed |
| 35 | // identifiers with the same name to be treated the same. |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 36 | if (PPTok.getIdentifierInfo() && |
| 37 | PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo()) |
| 38 | return true; |
| 39 | |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | static void GetNextRawTok(Lexer &RawLex, Token &RawTok, Preprocessor &PP) { |
| 44 | RawLex.LexRawToken(RawTok); |
| 45 | |
| 46 | // If we have an identifier with no identifier info for our raw token, look |
| 47 | // up the indentifier info. |
| 48 | if (RawTok.is(tok::identifier) && !RawTok.getIdentifierInfo()) |
| 49 | RawTok.setIdentifierInfo(PP.LookUpIdentifierInfo(RawTok)); |
| 50 | } |
| 51 | |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 52 | /// RewriteMacrosInInput - Implement -rewrite-macros mode. |
Chris Lattner | 0951052 | 2008-05-09 22:43:24 +0000 | [diff] [blame] | 53 | void clang::RewriteMacrosInInput(Preprocessor &PP,const std::string &InFileName, |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 54 | const std::string &OutFileName) { |
| 55 | SourceManager &SM = PP.getSourceManager(); |
| 56 | |
| 57 | Rewriter Rewrite; |
| 58 | Rewrite.setSourceMgr(SM); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 59 | RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID()); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 60 | |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 61 | const SourceManager &SourceMgr = PP.getSourceManager(); |
| 62 | std::pair<const char*, const char*> File = |
| 63 | SourceMgr.getBufferData(SM.getMainFileID()); |
| 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. |
| 67 | Lexer RawLex(SourceLocation::getFileLoc(SM.getMainFileID(), 0), |
| 68 | PP.getLangOptions(), File.first, File.second); |
| 69 | Token RawTok; |
| 70 | GetNextRawTok(RawLex, RawTok, PP); |
| 71 | |
| 72 | // Get the first preprocessing token. |
| 73 | PP.EnterMainSourceFile(); |
| 74 | Token PPTok; |
| 75 | PP.Lex(PPTok); |
| 76 | |
| 77 | // Preprocess the input file in parallel with raw lexing the main file. Ignore |
| 78 | // all tokens that are preprocessed from a file other than the main file (e.g. |
| 79 | // a header). If we see tokens that are in the preprocessed file bug not the |
| 80 | // lexed file, we have a macro expansion. If we see tokens in the lexed file |
| 81 | // that aren't in the preprocessed view, we have macros that expand to no |
| 82 | // tokens, or macro arguments etc. |
| 83 | while (RawTok.isNot(tok::eof) || PPTok.isNot(tok::eof)) { |
| 84 | SourceLocation PPLoc = SM.getLogicalLoc(PPTok.getLocation()); |
| 85 | |
| 86 | // If PPTok is from a different source file, ignore it. |
| 87 | if (!SM.isFromMainFile(PPLoc)) { |
| 88 | PP.Lex(PPTok); |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | // If the raw file hits a preprocessor directive, they will be extra tokens |
| 93 | // in the input file, but we don't want to treat them as such... just ignore |
| 94 | // them. |
| 95 | if (RawTok.is(tok::hash) && RawTok.isAtStartOfLine()) { |
| 96 | GetNextRawTok(RawLex, RawTok, PP); |
| 97 | while (!RawTok.isAtStartOfLine() && RawTok.isNot(tok::eof)) |
| 98 | GetNextRawTok(RawLex, RawTok, PP); |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | // Okay, both tokens are from the same file. Get their offsets from the |
| 103 | // start of the file. |
| 104 | unsigned PPOffs = SM.getFullFilePos(PPLoc); |
| 105 | unsigned RawOffs = SM.getFullFilePos(RawTok.getLocation()); |
| 106 | |
| 107 | // If the offsets are the same and the token kind is the same, ignore them. |
| 108 | if (PPOffs == RawOffs && isSameToken(RawTok, PPTok)) { |
| 109 | GetNextRawTok(RawLex, RawTok, PP); |
| 110 | PP.Lex(PPTok); |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | // If the PP token is farther along than the raw token, something was |
| 115 | // deleted. Comment out the raw token. |
| 116 | if (RawOffs <= PPOffs) { |
| 117 | // 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] | 118 | // comments. Add a leading space if RawTok didn't have one. |
| 119 | bool HasSpace = RawTok.hasLeadingSpace(); |
| 120 | RB.InsertTextAfter(RawOffs, " /*"+HasSpace, 2+!HasSpace); |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 121 | unsigned EndPos; |
| 122 | |
| 123 | // Switch on comment lexing. If we get a comment, we don't want to |
| 124 | // include it as part of our run of tokens, because we don't want to |
| 125 | // nest /* */ comments. |
| 126 | RawLex.SetCommentRetentionState(true); |
| 127 | |
| 128 | do { |
| 129 | EndPos = RawOffs+RawTok.getLength(); |
| 130 | |
| 131 | GetNextRawTok(RawLex, RawTok, PP); |
| 132 | RawOffs = SM.getFullFilePos(RawTok.getLocation()); |
| 133 | |
| 134 | if (RawTok.is(tok::comment)) { |
| 135 | RawLex.SetCommentRetentionState(false); |
| 136 | // Skip past the comment. |
| 137 | GetNextRawTok(RawLex, RawTok, PP); |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | } while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() && |
| 142 | (PPOffs != RawOffs || !isSameToken(RawTok, PPTok))); |
| 143 | |
| 144 | RawLex.SetCommentRetentionState(false); |
| 145 | |
| 146 | RB.InsertTextBefore(EndPos, "*/", 2); |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | // Otherwise, there was a replacement an expansion. Insert the new token |
| 151 | // in the output buffer. Insert the whole run of new tokens at once to get |
| 152 | // them in the right order. |
| 153 | unsigned InsertPos = PPOffs; |
| 154 | std::string Expansion; |
| 155 | while (PPOffs < RawOffs) { |
| 156 | Expansion += ' ' + PP.getSpelling(PPTok); |
| 157 | PP.Lex(PPTok); |
| 158 | PPLoc = SM.getLogicalLoc(PPTok.getLocation()); |
| 159 | PPOffs = SM.getFullFilePos(PPLoc); |
| 160 | } |
| 161 | Expansion += ' '; |
| 162 | RB.InsertTextBefore(InsertPos, &Expansion[0], Expansion.size()); |
| 163 | } |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 164 | |
| 165 | // Create the output file. |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 166 | std::ostream *OutFile; |
| 167 | if (OutFileName == "-") { |
| 168 | OutFile = llvm::cout.stream(); |
| 169 | } else if (!OutFileName.empty()) { |
| 170 | OutFile = new std::ofstream(OutFileName.c_str(), |
| 171 | std::ios_base::binary|std::ios_base::out); |
| 172 | } else if (InFileName == "-") { |
| 173 | OutFile = llvm::cout.stream(); |
| 174 | } else { |
| 175 | llvm::sys::Path Path(InFileName); |
| 176 | Path.eraseSuffix(); |
| 177 | Path.appendSuffix("cpp"); |
| 178 | OutFile = new std::ofstream(Path.toString().c_str(), |
| 179 | std::ios_base::binary|std::ios_base::out); |
| 180 | } |
| 181 | |
| 182 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 183 | // we are done. |
| 184 | if (const RewriteBuffer *RewriteBuf = |
Chris Lattner | ec92156 | 2008-05-10 00:02:33 +0000 | [diff] [blame] | 185 | Rewrite.getRewriteBufferFor(SM.getMainFileID())) { |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 186 | //printf("Changed:\n"); |
| 187 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 188 | } else { |
| 189 | fprintf(stderr, "No changes\n"); |
| 190 | } |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 191 | } |