clang  9.0.0svn
FixIt.h
Go to the documentation of this file.
1 //===--- FixIt.h - FixIt Hint utilities -------------------------*- 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 implements functions to ease source rewriting from AST-nodes.
10 //
11 // Example swapping A and B expressions:
12 //
13 // Expr *A, *B;
14 // tooling::fixit::createReplacement(*A, *B);
15 // tooling::fixit::createReplacement(*B, *A);
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_CLANG_TOOLING_FIXIT_H
20 #define LLVM_CLANG_TOOLING_FIXIT_H
21 
22 #include "clang/AST/ASTContext.h"
23 #include "clang/Basic/TokenKinds.h"
24 
25 namespace clang {
26 namespace tooling {
27 namespace fixit {
28 
29 namespace internal {
30 StringRef getText(CharSourceRange Range, const ASTContext &Context);
31 
32 /// Returns the token CharSourceRange corresponding to \p Range.
34  return CharSourceRange::getTokenRange(Range);
35 }
36 
37 /// Returns the CharSourceRange of the token at Location \p Loc.
39  return CharSourceRange::getTokenRange(Loc, Loc);
40 }
41 
42 /// Returns the CharSourceRange of an given Node. \p Node is typically a
43 /// 'Stmt', 'Expr' or a 'Decl'.
44 template <typename T> CharSourceRange getSourceRange(const T &Node) {
45  return CharSourceRange::getTokenRange(Node.getSourceRange());
46 }
47 
48 /// Extends \p Range to include the token \p Next, if it immediately follows the
49 /// end of the range. Otherwise, returns \p Range unchanged.
51  ASTContext &Context);
52 } // end namespace internal
53 
54 /// Returns a textual representation of \p Node.
55 template <typename T>
56 StringRef getText(const T &Node, const ASTContext &Context) {
57  return internal::getText(internal::getSourceRange(Node), Context);
58 }
59 
60 /// Returns the source range spanning the node, extended to include \p Next, if
61 /// it immediately follows \p Node. Otherwise, returns the normal range of \p
62 /// Node. See comments on `getExtendedText()` for examples.
63 template <typename T>
65  ASTContext &Context) {
67  Context);
68 }
69 
70 /// Returns the source text of the node, extended to include \p Next, if it
71 /// immediately follows the node. Otherwise, returns the text of just \p Node.
72 ///
73 /// For example, given statements S1 and S2 below:
74 /// \code
75 /// {
76 /// // S1:
77 /// if (!x) return foo();
78 /// // S2:
79 /// if (!x) { return 3; }
80 /// }
81 /// \endcode
82 /// then
83 /// \code
84 /// getText(S1, Context) = "if (!x) return foo()"
85 /// getExtendedText(S1, tok::TokenKind::semi, Context)
86 /// = "if (!x) return foo();"
87 /// getExtendedText(*S1.getThen(), tok::TokenKind::semi, Context)
88 /// = "return foo();"
89 /// getExtendedText(*S2.getThen(), tok::TokenKind::semi, Context)
90 /// = getText(S2, Context) = "{ return 3; }"
91 /// \endcode
92 template <typename T>
93 StringRef getExtendedText(const T &Node, tok::TokenKind Next,
94  ASTContext &Context) {
95  return internal::getText(getExtendedRange(Node, Next, Context), Context);
96 }
97 
98 // Returns a FixItHint to remove \p Node.
99 // TODO: Add support for related syntactical elements (i.e. comments, ...).
100 template <typename T> FixItHint createRemoval(const T &Node) {
102 }
103 
104 // Returns a FixItHint to replace \p Destination by \p Source.
105 template <typename D, typename S>
106 FixItHint createReplacement(const D &Destination, const S &Source,
107  const ASTContext &Context) {
109  getText(Source, Context));
110 }
111 
112 // Returns a FixItHint to replace \p Destination by \p Source.
113 template <typename D>
114 FixItHint createReplacement(const D &Destination, StringRef Source) {
116  Source);
117 }
118 
119 } // end namespace fixit
120 } // end namespace tooling
121 } // end namespace clang
122 
123 #endif // LLVM_CLANG_TOOLING_FIXINT_H
CharSourceRange getExtendedRange(const T &Node, tok::TokenKind Next, ASTContext &Context)
Returns the source range spanning the node, extended to include Next, if it immediately follows Node...
Definition: FixIt.h:64
Defines the clang::ASTContext interface.
static CharSourceRange getTokenRange(SourceRange R)
StringRef getText(CharSourceRange Range, const ASTContext &Context)
Definition: FixIt.cpp:21
FixItHint createRemoval(const T &Node)
Definition: FixIt.h:100
FixItHint createReplacement(const D &Destination, const S &Source, const ASTContext &Context)
Definition: FixIt.h:106
CharSourceRange getSourceRange(const SourceRange &Range)
Returns the token CharSourceRange corresponding to Range.
Definition: FixIt.h:33
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
A source range independent of the SourceManager.
Definition: Replacement.h:44
Represents a character-granular source range.
StringRef getExtendedText(const T &Node, tok::TokenKind Next, ASTContext &Context)
Returns the source text of the node, extended to include Next, if it immediately follows the node...
Definition: FixIt.h:93
Encodes a location in the source.
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:24
ast_type_traits::DynTypedNode Node
Dataflow Directional Tag Classes.
static FixItHint CreateRemoval(CharSourceRange RemoveRange)
Create a code modification hint that removes the given source range.
Definition: Diagnostic.h:117
Defines the clang::TokenKind enum and support functions.
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string...
Definition: Diagnostic.h:128
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition: Diagnostic.h:65
A trivial tuple used to represent a source range.
CharSourceRange maybeExtendRange(CharSourceRange Range, tok::TokenKind Next, ASTContext &Context)
Extends Range to include the token Next, if it immediately follows the end of the range...
Definition: FixIt.cpp:26