Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:12

0001 //===--- FixIt.h - FixIt Hint utilities -------------------------*- 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 implements functions to ease source rewriting from AST-nodes.
0010 //
0011 //  Example swapping A and B expressions:
0012 //
0013 //    Expr *A, *B;
0014 //    tooling::fixit::createReplacement(*A, *B);
0015 //    tooling::fixit::createReplacement(*B, *A);
0016 //
0017 //===----------------------------------------------------------------------===//
0018 
0019 #ifndef LLVM_CLANG_TOOLING_FIXIT_H
0020 #define LLVM_CLANG_TOOLING_FIXIT_H
0021 
0022 #include "clang/AST/ASTContext.h"
0023 
0024 namespace clang {
0025 namespace tooling {
0026 namespace fixit {
0027 
0028 namespace internal {
0029 StringRef getText(CharSourceRange Range, const ASTContext &Context);
0030 
0031 /// Returns the token CharSourceRange corresponding to \p Range.
0032 inline CharSourceRange getSourceRange(const SourceRange &Range) {
0033   return CharSourceRange::getTokenRange(Range);
0034 }
0035 
0036 /// Returns the CharSourceRange of the token at Location \p Loc.
0037 inline CharSourceRange getSourceRange(const SourceLocation &Loc) {
0038   return CharSourceRange::getTokenRange(Loc, Loc);
0039 }
0040 
0041 /// Returns the CharSourceRange of an given Node. \p Node is typically a
0042 ///        'Stmt', 'Expr' or a 'Decl'.
0043 template <typename T> CharSourceRange getSourceRange(const T &Node) {
0044   return CharSourceRange::getTokenRange(Node.getSourceRange());
0045 }
0046 } // end namespace internal
0047 
0048 /// Returns a textual representation of \p Node.
0049 template <typename T>
0050 StringRef getText(const T &Node, const ASTContext &Context) {
0051   return internal::getText(internal::getSourceRange(Node), Context);
0052 }
0053 
0054 // Returns a FixItHint to remove \p Node.
0055 // TODO: Add support for related syntactical elements (i.e. comments, ...).
0056 template <typename T> FixItHint createRemoval(const T &Node) {
0057   return FixItHint::CreateRemoval(internal::getSourceRange(Node));
0058 }
0059 
0060 // Returns a FixItHint to replace \p Destination by \p Source.
0061 template <typename D, typename S>
0062 FixItHint createReplacement(const D &Destination, const S &Source,
0063                                    const ASTContext &Context) {
0064   return FixItHint::CreateReplacement(internal::getSourceRange(Destination),
0065                                       getText(Source, Context));
0066 }
0067 
0068 // Returns a FixItHint to replace \p Destination by \p Source.
0069 template <typename D>
0070 FixItHint createReplacement(const D &Destination, StringRef Source) {
0071   return FixItHint::CreateReplacement(internal::getSourceRange(Destination),
0072                                       Source);
0073 }
0074 
0075 } // end namespace fixit
0076 } // end namespace tooling
0077 } // end namespace clang
0078 
0079 #endif // LLVM_CLANG_TOOLING_FIXIT_H