Path: blob/main/contrib/llvm-project/clang/lib/Rewrite/TokenRewriter.cpp
35233 views
//===- TokenRewriter.cpp - Token-based code rewriting interface -----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the TokenRewriter class, which is used for code9// transformations.10//11//===----------------------------------------------------------------------===//1213#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>2223using namespace clang;2425TokenRewriter::TokenRewriter(FileID FID, SourceManager &SM,26const LangOptions &LangOpts) {27ScratchBuf.reset(new ScratchBuffer(SM));2829// Create a lexer to lex all the tokens of the main file in raw mode.30llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(FID);31Lexer RawLex(FID, FromFile, SM, LangOpts);3233// Return all comments and whitespace as tokens.34RawLex.SetKeepWhitespaceMode(true);3536// Lex the file, populating our datastructures.37Token RawTok;38RawLex.LexFromRawLexer(RawTok);39while (RawTok.isNot(tok::eof)) {40#if 041if (Tok.is(tok::raw_identifier)) {42// Look up the identifier info for the token. This should use43// IdentifierTable directly instead of PP.44PP.LookUpIdentifierInfo(Tok);45}46#endif4748AddToken(RawTok, TokenList.end());49RawLex.LexFromRawLexer(RawTok);50}51}5253TokenRewriter::~TokenRewriter() = default;5455/// RemapIterator - Convert from token_iterator (a const iterator) to56/// TokenRefTy (a non-const iterator).57TokenRewriter::TokenRefTy TokenRewriter::RemapIterator(token_iterator I) {58if (I == token_end()) return TokenList.end();5960// FIXME: This is horrible, we should use our own list or something to avoid61// this.62std::map<SourceLocation, TokenRefTy>::iterator MapIt =63TokenAtLoc.find(I->getLocation());64assert(MapIt != TokenAtLoc.end() && "iterator not in rewriter?");65return MapIt->second;66}6768/// AddToken - Add the specified token into the Rewriter before the other69/// position.70TokenRewriter::TokenRefTy71TokenRewriter::AddToken(const Token &T, TokenRefTy Where) {72Where = TokenList.insert(Where, T);7374bool InsertSuccess = TokenAtLoc.insert(std::make_pair(T.getLocation(),75Where)).second;76assert(InsertSuccess && "Token location already in rewriter!");77(void)InsertSuccess;78return Where;79}8081TokenRewriter::token_iterator82TokenRewriter::AddTokenBefore(token_iterator I, const char *Val) {83unsigned Len = strlen(Val);8485// Plop the string into the scratch buffer, then create a token for this86// string.87Token Tok;88Tok.startToken();89const char *Spelling;90Tok.setLocation(ScratchBuf->getToken(Val, Len, Spelling));91Tok.setLength(Len);9293// TODO: Form a whole lexer around this and relex the token! For now, just94// set kind to tok::unknown.95Tok.setKind(tok::unknown);9697return AddToken(Tok, RemapIterator(I));98}99100101