xref: /freebsd/contrib/llvm-project/clang/include/clang/Tooling/FixIt.h (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
10b57cec5SDimitry Andric //===--- FixIt.h - FixIt Hint utilities -------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric //  This file implements functions to ease source rewriting from AST-nodes.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //  Example swapping A and B expressions:
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //    Expr *A, *B;
140b57cec5SDimitry Andric //    tooling::fixit::createReplacement(*A, *B);
150b57cec5SDimitry Andric //    tooling::fixit::createReplacement(*B, *A);
160b57cec5SDimitry Andric //
170b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric #ifndef LLVM_CLANG_TOOLING_FIXIT_H
200b57cec5SDimitry Andric #define LLVM_CLANG_TOOLING_FIXIT_H
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric #include "clang/AST/ASTContext.h"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric namespace clang {
250b57cec5SDimitry Andric namespace tooling {
260b57cec5SDimitry Andric namespace fixit {
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric namespace internal {
290b57cec5SDimitry Andric StringRef getText(CharSourceRange Range, const ASTContext &Context);
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric /// Returns the token CharSourceRange corresponding to \p Range.
getSourceRange(const SourceRange & Range)320b57cec5SDimitry Andric inline CharSourceRange getSourceRange(const SourceRange &Range) {
330b57cec5SDimitry Andric   return CharSourceRange::getTokenRange(Range);
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric /// Returns the CharSourceRange of the token at Location \p Loc.
getSourceRange(const SourceLocation & Loc)370b57cec5SDimitry Andric inline CharSourceRange getSourceRange(const SourceLocation &Loc) {
380b57cec5SDimitry Andric   return CharSourceRange::getTokenRange(Loc, Loc);
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric /// Returns the CharSourceRange of an given Node. \p Node is typically a
420b57cec5SDimitry Andric ///        'Stmt', 'Expr' or a 'Decl'.
getSourceRange(const T & Node)430b57cec5SDimitry Andric template <typename T> CharSourceRange getSourceRange(const T &Node) {
440b57cec5SDimitry Andric   return CharSourceRange::getTokenRange(Node.getSourceRange());
450b57cec5SDimitry Andric }
460b57cec5SDimitry Andric } // end namespace internal
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric /// Returns a textual representation of \p Node.
490b57cec5SDimitry Andric template <typename T>
getText(const T & Node,const ASTContext & Context)500b57cec5SDimitry Andric StringRef getText(const T &Node, const ASTContext &Context) {
510b57cec5SDimitry Andric   return internal::getText(internal::getSourceRange(Node), Context);
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric // Returns a FixItHint to remove \p Node.
550b57cec5SDimitry Andric // TODO: Add support for related syntactical elements (i.e. comments, ...).
createRemoval(const T & Node)560b57cec5SDimitry Andric template <typename T> FixItHint createRemoval(const T &Node) {
570b57cec5SDimitry Andric   return FixItHint::CreateRemoval(internal::getSourceRange(Node));
580b57cec5SDimitry Andric }
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric // Returns a FixItHint to replace \p Destination by \p Source.
610b57cec5SDimitry Andric template <typename D, typename S>
createReplacement(const D & Destination,const S & Source,const ASTContext & Context)620b57cec5SDimitry Andric FixItHint createReplacement(const D &Destination, const S &Source,
630b57cec5SDimitry Andric                                    const ASTContext &Context) {
640b57cec5SDimitry Andric   return FixItHint::CreateReplacement(internal::getSourceRange(Destination),
650b57cec5SDimitry Andric                                       getText(Source, Context));
660b57cec5SDimitry Andric }
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric // Returns a FixItHint to replace \p Destination by \p Source.
690b57cec5SDimitry Andric template <typename D>
createReplacement(const D & Destination,StringRef Source)700b57cec5SDimitry Andric FixItHint createReplacement(const D &Destination, StringRef Source) {
710b57cec5SDimitry Andric   return FixItHint::CreateReplacement(internal::getSourceRange(Destination),
720b57cec5SDimitry Andric                                       Source);
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric } // end namespace fixit
760b57cec5SDimitry Andric } // end namespace tooling
770b57cec5SDimitry Andric } // end namespace clang
780b57cec5SDimitry Andric 
79*04eeddc0SDimitry Andric #endif // LLVM_CLANG_TOOLING_FIXIT_H
80