blob: 494defdedaa945f2e6f8ece2978d631eba4607fa [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
Ted Kremenek305c6132012-09-01 05:09:24 +000015#include "clang/Rewrite/Core/TokenRewriter.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "clang/Basic/SourceManager.h"
Chris Lattnercff9cc92008-10-12 05:44:03 +000017#include "clang/Lex/Lexer.h"
Chris Lattner99bd46c2008-10-12 06:09:52 +000018#include "clang/Lex/ScratchBuffer.h"
Chris Lattnercff9cc92008-10-12 05:44:03 +000019using 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 Lattner6e290142009-11-30 04:18:44 +000026 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
27 Lexer RawLex(FID, FromFile, SM, LangOpts);
Mike Stump1eb44332009-09-09 15:08:12 +000028
Chris Lattnercff9cc92008-10-12 05:44:03 +000029 // 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 Lattner99bd46c2008-10-12 06:09:52 +000036#if 0
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +000037 if (Tok.is(tok::raw_identifier)) {
Chris Lattner99bd46c2008-10-12 06:09:52 +000038 // Look up the identifier info for the token. This should use
39 // IdentifierTable directly instead of PP.
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +000040 PP.LookUpIdentifierInfo(Tok);
Chris Lattner99bd46c2008-10-12 06:09:52 +000041 }
42#endif
Mike Stump1eb44332009-09-09 15:08:12 +000043
Chris Lattnercff9cc92008-10-12 05:44:03 +000044 AddToken(RawTok, TokenList.end());
45 RawLex.LexFromRawLexer(RawTok);
46 }
Chris Lattnercff9cc92008-10-12 05:44:03 +000047}
48
Chris Lattner99bd46c2008-10-12 06:09:52 +000049TokenRewriter::~TokenRewriter() {
50}
51
52
53/// RemapIterator - Convert from token_iterator (a const iterator) to
54/// TokenRefTy (a non-const iterator).
55TokenRewriter::TokenRefTy TokenRewriter::RemapIterator(token_iterator I) {
56 if (I == token_end()) return TokenList.end();
Mike Stump1eb44332009-09-09 15:08:12 +000057
Chris Lattner99bd46c2008-10-12 06:09:52 +000058 // FIXME: This is horrible, we should use our own list or something to avoid
59 // this.
Mike Stump1eb44332009-09-09 15:08:12 +000060 std::map<SourceLocation, TokenRefTy>::iterator MapIt =
Chris Lattner99bd46c2008-10-12 06:09:52 +000061 TokenAtLoc.find(I->getLocation());
62 assert(MapIt != TokenAtLoc.end() && "iterator not in rewriter?");
63 return MapIt->second;
64}
65
66
Chris Lattnercff9cc92008-10-12 05:44:03 +000067/// AddToken - Add the specified token into the Rewriter before the other
68/// position.
Mike Stump1eb44332009-09-09 15:08:12 +000069TokenRewriter::TokenRefTy
Chris Lattner99bd46c2008-10-12 06:09:52 +000070TokenRewriter::AddToken(const Token &T, TokenRefTy Where) {
Chris Lattnercff9cc92008-10-12 05:44:03 +000071 Where = TokenList.insert(Where, T);
Mike Stump1eb44332009-09-09 15:08:12 +000072
Chris Lattnercff9cc92008-10-12 05:44:03 +000073 bool InsertSuccess = TokenAtLoc.insert(std::make_pair(T.getLocation(),
74 Where)).second;
75 assert(InsertSuccess && "Token location already in rewriter!");
Jakob Stoklund Olesenb7ec67a2011-01-06 01:37:28 +000076 (void)InsertSuccess;
Chris Lattner99bd46c2008-10-12 06:09:52 +000077 return Where;
Chris Lattnercff9cc92008-10-12 05:44:03 +000078}
Mike Stump1eb44332009-09-09 15:08:12 +000079
Chris Lattner99bd46c2008-10-12 06:09:52 +000080
81TokenRewriter::token_iterator
Chris Lattner47246be2009-01-26 19:29:26 +000082TokenRewriter::AddTokenBefore(token_iterator I, const char *Val) {
Chris Lattner99bd46c2008-10-12 06:09:52 +000083 unsigned Len = strlen(Val);
Mike Stump1eb44332009-09-09 15:08:12 +000084
Chris Lattner99bd46c2008-10-12 06:09:52 +000085 // Plop the string into the scratch buffer, then create a token for this
86 // string.
87 Token Tok;
88 Tok.startToken();
Chris Lattner47246be2009-01-26 19:29:26 +000089 const char *Spelling;
90 Tok.setLocation(ScratchBuf->getToken(Val, Len, Spelling));
Chris Lattner99bd46c2008-10-12 06:09:52 +000091 Tok.setLength(Len);
Mike Stump1eb44332009-09-09 15:08:12 +000092
Chris Lattner99bd46c2008-10-12 06:09:52 +000093 // 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