blob: 0effbb18b8ac297dea940fdf09353c6210173f38 [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));
Mike Stump1eb44332009-09-09 15:08:12 +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);
Mike Stump1eb44332009-09-09 15:08:12 +000027
Chris Lattnercff9cc92008-10-12 05:44:03 +000028 // 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
Mike Stump1eb44332009-09-09 15:08:12 +000042
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();
Mike Stump1eb44332009-09-09 15:08:12 +000056
Chris Lattner99bd46c2008-10-12 06:09:52 +000057 // FIXME: This is horrible, we should use our own list or something to avoid
58 // this.
Mike Stump1eb44332009-09-09 15:08:12 +000059 std::map<SourceLocation, TokenRefTy>::iterator MapIt =
Chris Lattner99bd46c2008-10-12 06:09:52 +000060 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.
Mike Stump1eb44332009-09-09 15:08:12 +000068TokenRewriter::TokenRefTy
Chris Lattner99bd46c2008-10-12 06:09:52 +000069TokenRewriter::AddToken(const Token &T, TokenRefTy Where) {
Chris Lattnercff9cc92008-10-12 05:44:03 +000070 Where = TokenList.insert(Where, T);
Mike Stump1eb44332009-09-09 15:08:12 +000071
Chris Lattnercff9cc92008-10-12 05:44:03 +000072 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}
Mike Stump1eb44332009-09-09 15:08:12 +000078
Chris Lattner99bd46c2008-10-12 06:09:52 +000079
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);
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chris Lattner99bd46c2008-10-12 06:09:52 +000084 // 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);
Mike Stump1eb44332009-09-09 15:08:12 +000091
Chris Lattner99bd46c2008-10-12 06:09:52 +000092 // 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