blob: f288ac2402794868b52682729c10334ae218a406 [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
21TokenRewriter::TokenRewriter(unsigned FileID, SourceManager &SM,
22 const LangOptions &LangOpts) {
Chris Lattner99bd46c2008-10-12 06:09:52 +000023 ScratchBuf.reset(new ScratchBuffer(SM));
Chris Lattnercff9cc92008-10-12 05:44:03 +000024
25 std::pair<const char*,const char*> File = SM.getBufferData(FileID);
26
27 // Create a lexer to lex all the tokens of the main file in raw mode.
28 Lexer RawLex(SourceLocation::getFileLoc(FileID, 0),
29 LangOpts, File.first, File.second);
30
31 // Return all comments and whitespace as tokens.
32 RawLex.SetKeepWhitespaceMode(true);
33
34 // Lex the file, populating our datastructures.
35 Token RawTok;
36 RawLex.LexFromRawLexer(RawTok);
37 while (RawTok.isNot(tok::eof)) {
Chris Lattner99bd46c2008-10-12 06:09:52 +000038#if 0
39 if (Tok.is(tok::identifier)) {
40 // Look up the identifier info for the token. This should use
41 // IdentifierTable directly instead of PP.
42 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
43 }
44#endif
45
Chris Lattnercff9cc92008-10-12 05:44:03 +000046 AddToken(RawTok, TokenList.end());
47 RawLex.LexFromRawLexer(RawTok);
48 }
Chris Lattnercff9cc92008-10-12 05:44:03 +000049}
50
Chris Lattner99bd46c2008-10-12 06:09:52 +000051TokenRewriter::~TokenRewriter() {
52}
53
54
55/// RemapIterator - Convert from token_iterator (a const iterator) to
56/// TokenRefTy (a non-const iterator).
57TokenRewriter::TokenRefTy TokenRewriter::RemapIterator(token_iterator I) {
58 if (I == token_end()) return TokenList.end();
59
60 // FIXME: This is horrible, we should use our own list or something to avoid
61 // this.
62 std::map<SourceLocation, TokenRefTy>::iterator MapIt =
63 TokenAtLoc.find(I->getLocation());
64 assert(MapIt != TokenAtLoc.end() && "iterator not in rewriter?");
65 return MapIt->second;
66}
67
68
Chris Lattnercff9cc92008-10-12 05:44:03 +000069/// AddToken - Add the specified token into the Rewriter before the other
70/// position.
Chris Lattner99bd46c2008-10-12 06:09:52 +000071TokenRewriter::TokenRefTy
72TokenRewriter::AddToken(const Token &T, TokenRefTy Where) {
Chris Lattnercff9cc92008-10-12 05:44:03 +000073 Where = TokenList.insert(Where, T);
74
75 bool InsertSuccess = TokenAtLoc.insert(std::make_pair(T.getLocation(),
76 Where)).second;
77 assert(InsertSuccess && "Token location already in rewriter!");
78 InsertSuccess = InsertSuccess;
Chris Lattner99bd46c2008-10-12 06:09:52 +000079 return Where;
Chris Lattnercff9cc92008-10-12 05:44:03 +000080}
Chris Lattner99bd46c2008-10-12 06:09:52 +000081
82
83TokenRewriter::token_iterator
84TokenRewriter::AddTokenBefore(token_iterator I, const char *Val){
85 unsigned Len = strlen(Val);
86
87 // Plop the string into the scratch buffer, then create a token for this
88 // string.
89 Token Tok;
90 Tok.startToken();
91 Tok.setLocation(ScratchBuf->getToken(Val, Len));
92 Tok.setLength(Len);
93
94 // TODO: Form a whole lexer around this and relex the token! For now, just
95 // set kind to tok::unknown.
96 Tok.setKind(tok::unknown);
97
98 return AddToken(Tok, RemapIterator(I));
99}
100