Clang Project

clang_source_code/include/clang/Rewrite/Core/TokenRewriter.h
1//===- TokenRewriter.h - Token-based Rewriter -------------------*- C++ -*-===//
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 defines the TokenRewriter class, which is used for code
10//  transformations.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H
15#define LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H
16
17#include "clang/Basic/SourceLocation.h"
18#include "clang/Lex/Token.h"
19#include <cassert>
20#include <list>
21#include <map>
22#include <memory>
23
24namespace clang {
25
26class LangOptions;
27class ScratchBuffer;
28class SourceManager;
29
30  class TokenRewriter {
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<TokenTokenList;
34
35    /// TokenRefTy - This is the type used to refer to a token in the TokenList.
36    using TokenRefTy = std::list<Token>::iterator;
37
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
41    /// backwards.
42    std::map<SourceLocationTokenRefTyTokenAtLoc;
43
44    /// ScratchBuf - This is the buffer that we create scratch tokens from.
45    std::unique_ptr<ScratchBufferScratchBuf;
46
47  public:
48    /// TokenRewriter - This creates a TokenRewriter for the file with the
49    /// specified FileID.
50    TokenRewriter(FileID FIDSourceManager &SMconst LangOptions &LO);
51
52    TokenRewriter(const TokenRewriter &) = delete;
53    TokenRewriter &operator=(const TokenRewriter &) = delete;
54    ~TokenRewriter();
55
56    using token_iterator = std::list<Token>::const_iterator;
57
58    token_iterator token_begin() const { return TokenList.begin(); }
59    token_iterator token_end() const { return TokenList.end(); }
60
61    token_iterator AddTokenBefore(token_iterator Iconst char *Val);
62
63    token_iterator AddTokenAfter(token_iterator Iconst char *Val) {
64       (0) . __assert_fail ("I != token_end() && \"Cannot insert after token_end()!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Rewrite/Core/TokenRewriter.h", 64, __PRETTY_FUNCTION__))" file_link="../../../../../include/assert.h.html#88" macro="true">assert(I != token_end() && "Cannot insert after token_end()!");
65      return AddTokenBefore(++IVal);
66    }
67
68  private:
69    /// RemapIterator - Convert from token_iterator (a const iterator) to
70    /// TokenRefTy (a non-const iterator).
71    TokenRefTy RemapIterator(token_iterator I);
72
73    /// AddToken - Add the specified token into the Rewriter before the other
74    /// position.
75    TokenRefTy AddToken(const Token &TTokenRefTy Where);
76  };
77
78// namespace clang
79
80#endif // LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H
81
clang::TokenRewriter::TokenList
clang::TokenRewriter::TokenAtLoc
clang::TokenRewriter::ScratchBuf
clang::TokenRewriter::token_begin
clang::TokenRewriter::token_end
clang::TokenRewriter::AddTokenBefore
clang::TokenRewriter::AddTokenAfter
clang::TokenRewriter::RemapIterator
clang::TokenRewriter::AddToken