blob: e17e80133b11d6cdecee48d229bdcfdf11c5e671 [file] [log] [blame]
Chris Lattnercff9cc92008-10-12 05:44:03 +00001//===--- 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 Lattner99bd46c2008-10-12 06:09:52 +000017#include "clang/Lex/ScratchBuffer.h"
Chris Lattnercff9cc92008-10-12 05:44:03 +000018#include "clang/Basic/SourceManager.h"
19using namespace clang;
20
Chris Lattner2b2453a2009-01-17 06:22:33 +000021TokenRewriter::TokenRewriter(FileID FID, SourceManager &SM,
Chris Lattnercff9cc92008-10-12 05:44:03 +000022 const LangOptions &LangOpts) {
Chris Lattner99bd46c2008-10-12 06:09:52 +000023 ScratchBuf.reset(new ScratchBuffer(SM));
Chris Lattnercff9cc92008-10-12 05:44:03 +000024
Chris Lattnercff9cc92008-10-12 05:44:03 +000025 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner025c3a62009-01-17 07:35:14 +000026 Lexer RawLex(FID, SM, LangOpts);
Chris Lattnercff9cc92008-10-12 05:44:03 +000027
28 // Return all comments and whitespace as tokens.
29 RawLex.SetKeepWhitespaceMode(true);
30
31 // Lex the file, populating our datastructures.
32 Token RawTok;
33 RawLex.LexFromRawLexer(RawTok);
34 while (RawTok.isNot(tok::eof)) {
Chris Lattner99bd46c2008-10-12 06:09:52 +000035#if 0
36 if (Tok.is(tok::identifier)) {
37 // Look up the identifier info for the token. This should use
38 // IdentifierTable directly instead of PP.
39 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
40 }
41#endif
42
Chris Lattnercff9cc92008-10-12 05:44:03 +000043 AddToken(RawTok, TokenList.end());
44 RawLex.LexFromRawLexer(RawTok);
45 }
Chris Lattnercff9cc92008-10-12 05:44:03 +000046}
47
Chris Lattner99bd46c2008-10-12 06:09:52 +000048TokenRewriter::~TokenRewriter() {
49}
50
51
52/// RemapIterator - Convert from token_iterator (a const iterator) to
53/// TokenRefTy (a non-const iterator).
54TokenRewriter::TokenRefTy TokenRewriter::RemapIterator(token_iterator I) {
55 if (I == token_end()) return TokenList.end();
56
57 // FIXME: This is horrible, we should use our own list or something to avoid
58 // this.
59 std::map<SourceLocation, TokenRefTy>::iterator MapIt =
60 TokenAtLoc.find(I->getLocation());
61 assert(MapIt != TokenAtLoc.end() && "iterator not in rewriter?");
62 return MapIt->second;
63}
64
65
Chris Lattnercff9cc92008-10-12 05:44:03 +000066/// AddToken - Add the specified token into the Rewriter before the other
67/// position.
Chris Lattner99bd46c2008-10-12 06:09:52 +000068TokenRewriter::TokenRefTy
69TokenRewriter::AddToken(const Token &T, TokenRefTy Where) {
Chris Lattnercff9cc92008-10-12 05:44:03 +000070 Where = TokenList.insert(Where, T);
71
72 bool InsertSuccess = TokenAtLoc.insert(std::make_pair(T.getLocation(),
73 Where)).second;
74 assert(InsertSuccess && "Token location already in rewriter!");
75 InsertSuccess = InsertSuccess;
Chris Lattner99bd46c2008-10-12 06:09:52 +000076 return Where;
Chris Lattnercff9cc92008-10-12 05:44:03 +000077}
Chris Lattner99bd46c2008-10-12 06:09:52 +000078
79
80TokenRewriter::token_iterator
Chris Lattner47246be2009-01-26 19:29:26 +000081TokenRewriter::AddTokenBefore(token_iterator I, const char *Val) {
Chris Lattner99bd46c2008-10-12 06:09:52 +000082 unsigned Len = strlen(Val);
83
84 // Plop the string into the scratch buffer, then create a token for this
85 // string.
86 Token Tok;
87 Tok.startToken();
Chris Lattner47246be2009-01-26 19:29:26 +000088 const char *Spelling;
89 Tok.setLocation(ScratchBuf->getToken(Val, Len, Spelling));
Chris Lattner99bd46c2008-10-12 06:09:52 +000090 Tok.setLength(Len);
91
92 // TODO: Form a whole lexer around this and relex the token! For now, just
93 // set kind to tok::unknown.
94 Tok.setKind(tok::unknown);
95
96 return AddToken(Tok, RemapIterator(I));
97}
98