1 //===- TokenRewriter.h - Token-based Rewriter -------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the TokenRewriter class, which is used for code
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H
15 #define LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H
17 #include "clang/Basic/SourceLocation.h"
18 #include "clang/Lex/Token.h"
31 /// TokenList - This is the list of raw tokens that make up this file. Each
32 /// of these tokens has a unique SourceLocation, which is a FileID.
33 std::list<Token> TokenList;
35 /// TokenRefTy - This is the type used to refer to a token in the TokenList.
36 using TokenRefTy = std::list<Token>::iterator;
38 /// TokenAtLoc - This map indicates which token exists at a specific
39 /// SourceLocation. Since each token has a unique SourceLocation, this is a
40 /// one to one map. The token can return its own location directly, to map
42 std::map<SourceLocation, TokenRefTy> TokenAtLoc;
44 /// ScratchBuf - This is the buffer that we create scratch tokens from.
45 std::unique_ptr<ScratchBuffer> ScratchBuf;
48 /// TokenRewriter - This creates a TokenRewriter for the file with the
50 TokenRewriter(FileID FID, SourceManager &SM, const LangOptions &LO);
52 TokenRewriter(const TokenRewriter &) = delete;
53 TokenRewriter &operator=(const TokenRewriter &) = delete;
56 using token_iterator = std::list<Token>::const_iterator;
58 token_iterator token_begin() const { return TokenList.begin(); }
59 token_iterator token_end() const { return TokenList.end(); }
61 token_iterator AddTokenBefore(token_iterator I, const char *Val);
63 token_iterator AddTokenAfter(token_iterator I, const char *Val) {
64 assert(I != token_end() && "Cannot insert after token_end()!");
65 return AddTokenBefore(++I, Val);
69 /// RemapIterator - Convert from token_iterator (a const iterator) to
70 /// TokenRefTy (a non-const iterator).
71 TokenRefTy RemapIterator(token_iterator I);
73 /// AddToken - Add the specified token into the Rewriter before the other
75 TokenRefTy AddToken(const Token &T, TokenRefTy Where);
80 #endif // LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H