TokenRewriter.cpp 3.2 KB

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