TokenRewriter.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "clang/Rewrite/Core/TokenRewriter.h"
  15. #include "clang/Basic/SourceManager.h"
  16. #include "clang/Lex/Lexer.h"
  17. #include "clang/Lex/ScratchBuffer.h"
  18. using namespace clang;
  19. TokenRewriter::TokenRewriter(FileID FID, SourceManager &SM,
  20. const LangOptions &LangOpts) {
  21. ScratchBuf.reset(new ScratchBuffer(SM));
  22. // Create a lexer to lex all the tokens of the main file in raw mode.
  23. const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
  24. Lexer RawLex(FID, FromFile, SM, LangOpts);
  25. // Return all comments and whitespace as tokens.
  26. RawLex.SetKeepWhitespaceMode(true);
  27. // Lex the file, populating our datastructures.
  28. Token RawTok;
  29. RawLex.LexFromRawLexer(RawTok);
  30. while (RawTok.isNot(tok::eof)) {
  31. #if 0
  32. if (Tok.is(tok::raw_identifier)) {
  33. // Look up the identifier info for the token. This should use
  34. // IdentifierTable directly instead of PP.
  35. PP.LookUpIdentifierInfo(Tok);
  36. }
  37. #endif
  38. AddToken(RawTok, TokenList.end());
  39. RawLex.LexFromRawLexer(RawTok);
  40. }
  41. }
  42. TokenRewriter::~TokenRewriter() {
  43. }
  44. /// RemapIterator - Convert from token_iterator (a const iterator) to
  45. /// TokenRefTy (a non-const iterator).
  46. TokenRewriter::TokenRefTy TokenRewriter::RemapIterator(token_iterator I) {
  47. if (I == token_end()) return TokenList.end();
  48. // FIXME: This is horrible, we should use our own list or something to avoid
  49. // this.
  50. std::map<SourceLocation, TokenRefTy>::iterator MapIt =
  51. TokenAtLoc.find(I->getLocation());
  52. assert(MapIt != TokenAtLoc.end() && "iterator not in rewriter?");
  53. return MapIt->second;
  54. }
  55. /// AddToken - Add the specified token into the Rewriter before the other
  56. /// position.
  57. TokenRewriter::TokenRefTy
  58. TokenRewriter::AddToken(const Token &T, TokenRefTy Where) {
  59. Where = TokenList.insert(Where, T);
  60. bool InsertSuccess = TokenAtLoc.insert(std::make_pair(T.getLocation(),
  61. Where)).second;
  62. assert(InsertSuccess && "Token location already in rewriter!");
  63. (void)InsertSuccess;
  64. return Where;
  65. }
  66. TokenRewriter::token_iterator
  67. TokenRewriter::AddTokenBefore(token_iterator I, const char *Val) {
  68. unsigned Len = strlen(Val);
  69. // Plop the string into the scratch buffer, then create a token for this
  70. // string.
  71. Token Tok;
  72. Tok.startToken();
  73. const char *Spelling;
  74. Tok.setLocation(ScratchBuf->getToken(Val, Len, Spelling));
  75. Tok.setLength(Len);
  76. // TODO: Form a whole lexer around this and relex the token! For now, just
  77. // set kind to tok::unknown.
  78. Tok.setKind(tok::unknown);
  79. return AddToken(Tok, RemapIterator(I));
  80. }