1 //===--- FixIt.h - FixIt Hint utilities -------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements functions to ease source rewriting from AST-nodes.
12 // Example swapping A and B expressions:
15 // tooling::fixit::createReplacement(*A, *B);
16 // tooling::fixit::createReplacement(*B, *A);
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_CLANG_TOOLING_FIXIT_H
21 #define LLVM_CLANG_TOOLING_FIXIT_H
23 #include "clang/AST/ASTContext.h"
30 StringRef getText(SourceRange Range, const ASTContext &Context);
32 /// \brief Returns the SourceRange of a SourceRange. This identity function is
33 /// used by the following template abstractions.
34 inline SourceRange getSourceRange(const SourceRange &Range) { return Range; }
36 /// \brief Returns the SourceRange of the token at Location \p Loc.
37 inline SourceRange getSourceRange(const SourceLocation &Loc) {
38 return SourceRange(Loc);
41 /// \brief Returns the SourceRange of an given Node. \p Node is typically a
42 /// 'Stmt', 'Expr' or a 'Decl'.
43 template <typename T> SourceRange getSourceRange(const T &Node) {
44 return Node.getSourceRange();
46 } // end namespace internal
48 // \brief Returns a textual representation of \p Node.
50 StringRef getText(const T &Node, const ASTContext &Context) {
51 return internal::getText(internal::getSourceRange(Node), Context);
54 // \brief Returns a FixItHint to remove \p Node.
55 // TODO: Add support for related syntactical elements (i.e. comments, ...).
56 template <typename T> FixItHint createRemoval(const T &Node) {
57 return FixItHint::CreateRemoval(internal::getSourceRange(Node));
60 // \brief Returns a FixItHint to replace \p Destination by \p Source.
61 template <typename D, typename S>
62 FixItHint createReplacement(const D &Destination, const S &Source,
63 const ASTContext &Context) {
64 return FixItHint::CreateReplacement(internal::getSourceRange(Destination),
65 getText(Source, Context));
68 } // end namespace fixit
69 } // end namespace tooling
70 } // end namespace clang
72 #endif // LLVM_CLANG_TOOLING_FIXINT_H