Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:59

0001 //===- TokenRewriter.h - Token-based Rewriter -------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 //  This file defines the TokenRewriter class, which is used for code
0010 //  transformations.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H
0015 #define LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H
0016 
0017 #include "clang/Basic/SourceLocation.h"
0018 #include "clang/Lex/Token.h"
0019 #include <cassert>
0020 #include <list>
0021 #include <map>
0022 #include <memory>
0023 
0024 namespace clang {
0025 
0026 class LangOptions;
0027 class ScratchBuffer;
0028 class SourceManager;
0029 
0030   class TokenRewriter {
0031     /// TokenList - This is the list of raw tokens that make up this file.  Each
0032     /// of these tokens has a unique SourceLocation, which is a FileID.
0033     std::list<Token> TokenList;
0034 
0035     /// TokenRefTy - This is the type used to refer to a token in the TokenList.
0036     using TokenRefTy = std::list<Token>::iterator;
0037 
0038     /// TokenAtLoc - This map indicates which token exists at a specific
0039     /// SourceLocation.  Since each token has a unique SourceLocation, this is a
0040     /// one to one map.  The token can return its own location directly, to map
0041     /// backwards.
0042     std::map<SourceLocation, TokenRefTy> TokenAtLoc;
0043 
0044     /// ScratchBuf - This is the buffer that we create scratch tokens from.
0045     std::unique_ptr<ScratchBuffer> ScratchBuf;
0046 
0047   public:
0048     /// TokenRewriter - This creates a TokenRewriter for the file with the
0049     /// specified FileID.
0050     TokenRewriter(FileID FID, SourceManager &SM, const LangOptions &LO);
0051 
0052     TokenRewriter(const TokenRewriter &) = delete;
0053     TokenRewriter &operator=(const TokenRewriter &) = delete;
0054     ~TokenRewriter();
0055 
0056     using token_iterator = std::list<Token>::const_iterator;
0057 
0058     token_iterator token_begin() const { return TokenList.begin(); }
0059     token_iterator token_end() const { return TokenList.end(); }
0060 
0061     token_iterator AddTokenBefore(token_iterator I, const char *Val);
0062 
0063     token_iterator AddTokenAfter(token_iterator I, const char *Val) {
0064       assert(I != token_end() && "Cannot insert after token_end()!");
0065       return AddTokenBefore(++I, Val);
0066     }
0067 
0068   private:
0069     /// RemapIterator - Convert from token_iterator (a const iterator) to
0070     /// TokenRefTy (a non-const iterator).
0071     TokenRefTy RemapIterator(token_iterator I);
0072 
0073     /// AddToken - Add the specified token into the Rewriter before the other
0074     /// position.
0075     TokenRefTy AddToken(const Token &T, TokenRefTy Where);
0076   };
0077 
0078 } // namespace clang
0079 
0080 #endif // LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H