Chris Lattner | cff9cc9 | 2008-10-12 05:44:03 +0000 | [diff] [blame] | 1 | //===--- TokenRewriter.cpp - Token-based code rewriting interface ---------===// |
| 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 file implements the TokenRewriter class, which is used for code |
| 11 | // transformations. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Rewrite/TokenRewriter.h" |
| 16 | #include "clang/Lex/Lexer.h" |
Chris Lattner | 99bd46c | 2008-10-12 06:09:52 +0000 | [diff] [blame] | 17 | #include "clang/Lex/ScratchBuffer.h" |
Chris Lattner | cff9cc9 | 2008-10-12 05:44:03 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
| 19 | using namespace clang; |
| 20 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 21 | TokenRewriter::TokenRewriter(FileID FID, SourceManager &SM, |
Chris Lattner | cff9cc9 | 2008-10-12 05:44:03 +0000 | [diff] [blame] | 22 | const LangOptions &LangOpts) { |
Chris Lattner | 99bd46c | 2008-10-12 06:09:52 +0000 | [diff] [blame] | 23 | ScratchBuf.reset(new ScratchBuffer(SM)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 24 | |
Chris Lattner | cff9cc9 | 2008-10-12 05:44:03 +0000 | [diff] [blame] | 25 | // Create a lexer to lex all the tokens of the main file in raw mode. |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 26 | const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID); |
| 27 | Lexer RawLex(FID, FromFile, SM, LangOpts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 28 | |
Chris Lattner | cff9cc9 | 2008-10-12 05:44:03 +0000 | [diff] [blame] | 29 | // Return all comments and whitespace as tokens. |
| 30 | RawLex.SetKeepWhitespaceMode(true); |
| 31 | |
| 32 | // Lex the file, populating our datastructures. |
| 33 | Token RawTok; |
| 34 | RawLex.LexFromRawLexer(RawTok); |
| 35 | while (RawTok.isNot(tok::eof)) { |
Chris Lattner | 99bd46c | 2008-10-12 06:09:52 +0000 | [diff] [blame] | 36 | #if 0 |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 37 | if (Tok.is(tok::raw_identifier)) { |
Chris Lattner | 99bd46c | 2008-10-12 06:09:52 +0000 | [diff] [blame] | 38 | // Look up the identifier info for the token. This should use |
| 39 | // IdentifierTable directly instead of PP. |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 40 | PP.LookUpIdentifierInfo(Tok); |
Chris Lattner | 99bd46c | 2008-10-12 06:09:52 +0000 | [diff] [blame] | 41 | } |
| 42 | #endif |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
Chris Lattner | cff9cc9 | 2008-10-12 05:44:03 +0000 | [diff] [blame] | 44 | AddToken(RawTok, TokenList.end()); |
| 45 | RawLex.LexFromRawLexer(RawTok); |
| 46 | } |
Chris Lattner | cff9cc9 | 2008-10-12 05:44:03 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Chris Lattner | 99bd46c | 2008-10-12 06:09:52 +0000 | [diff] [blame] | 49 | TokenRewriter::~TokenRewriter() { |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /// RemapIterator - Convert from token_iterator (a const iterator) to |
| 54 | /// TokenRefTy (a non-const iterator). |
| 55 | TokenRewriter::TokenRefTy TokenRewriter::RemapIterator(token_iterator I) { |
| 56 | if (I == token_end()) return TokenList.end(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 99bd46c | 2008-10-12 06:09:52 +0000 | [diff] [blame] | 58 | // FIXME: This is horrible, we should use our own list or something to avoid |
| 59 | // this. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | std::map<SourceLocation, TokenRefTy>::iterator MapIt = |
Chris Lattner | 99bd46c | 2008-10-12 06:09:52 +0000 | [diff] [blame] | 61 | TokenAtLoc.find(I->getLocation()); |
| 62 | assert(MapIt != TokenAtLoc.end() && "iterator not in rewriter?"); |
| 63 | return MapIt->second; |
| 64 | } |
| 65 | |
| 66 | |
Chris Lattner | cff9cc9 | 2008-10-12 05:44:03 +0000 | [diff] [blame] | 67 | /// AddToken - Add the specified token into the Rewriter before the other |
| 68 | /// position. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | TokenRewriter::TokenRefTy |
Chris Lattner | 99bd46c | 2008-10-12 06:09:52 +0000 | [diff] [blame] | 70 | TokenRewriter::AddToken(const Token &T, TokenRefTy Where) { |
Chris Lattner | cff9cc9 | 2008-10-12 05:44:03 +0000 | [diff] [blame] | 71 | Where = TokenList.insert(Where, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Chris Lattner | cff9cc9 | 2008-10-12 05:44:03 +0000 | [diff] [blame] | 73 | bool InsertSuccess = TokenAtLoc.insert(std::make_pair(T.getLocation(), |
| 74 | Where)).second; |
| 75 | assert(InsertSuccess && "Token location already in rewriter!"); |
Jakob Stoklund Olesen | b7ec67a | 2011-01-06 01:37:28 +0000 | [diff] [blame] | 76 | (void)InsertSuccess; |
Chris Lattner | 99bd46c | 2008-10-12 06:09:52 +0000 | [diff] [blame] | 77 | return Where; |
Chris Lattner | cff9cc9 | 2008-10-12 05:44:03 +0000 | [diff] [blame] | 78 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 99bd46c | 2008-10-12 06:09:52 +0000 | [diff] [blame] | 80 | |
| 81 | TokenRewriter::token_iterator |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 82 | TokenRewriter::AddTokenBefore(token_iterator I, const char *Val) { |
Chris Lattner | 99bd46c | 2008-10-12 06:09:52 +0000 | [diff] [blame] | 83 | unsigned Len = strlen(Val); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 99bd46c | 2008-10-12 06:09:52 +0000 | [diff] [blame] | 85 | // Plop the string into the scratch buffer, then create a token for this |
| 86 | // string. |
| 87 | Token Tok; |
| 88 | Tok.startToken(); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 89 | const char *Spelling; |
| 90 | Tok.setLocation(ScratchBuf->getToken(Val, Len, Spelling)); |
Chris Lattner | 99bd46c | 2008-10-12 06:09:52 +0000 | [diff] [blame] | 91 | Tok.setLength(Len); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 99bd46c | 2008-10-12 06:09:52 +0000 | [diff] [blame] | 93 | // TODO: Form a whole lexer around this and relex the token! For now, just |
| 94 | // set kind to tok::unknown. |
| 95 | Tok.setKind(tok::unknown); |
| 96 | |
| 97 | return AddToken(Tok, RemapIterator(I)); |
| 98 | } |
| 99 | |