clang  6.0.0svn
FixIt.h
Go to the documentation of this file.
1 //===--- FixIt.h - FixIt Hint utilities -------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements functions to ease source rewriting from AST-nodes.
11 //
12 // Example swapping A and B expressions:
13 //
14 // Expr *A, *B;
15 // tooling::fixit::createReplacement(*A, *B);
16 // tooling::fixit::createReplacement(*B, *A);
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #ifndef LLVM_CLANG_TOOLING_FIXIT_H
21 #define LLVM_CLANG_TOOLING_FIXIT_H
22 
23 #include "clang/AST/ASTContext.h"
24 
25 namespace clang {
26 namespace tooling {
27 namespace fixit {
28 
29 namespace internal {
30 StringRef getText(SourceRange Range, const ASTContext &Context);
31 
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; }
35 
36 /// \brief Returns the SourceRange of the token at Location \p Loc.
38  return SourceRange(Loc);
39 }
40 
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();
45 }
46 } // end namespace internal
47 
48 // \brief Returns a textual representation of \p Node.
49 template <typename T>
50 StringRef getText(const T &Node, const ASTContext &Context) {
51  return internal::getText(internal::getSourceRange(Node), Context);
52 }
53 
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) {
58 }
59 
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) {
65  getText(Source, Context));
66 }
67 
68 // \brief Returns a FixItHint to replace \p Destination by \p Source.
69 template <typename D>
70 FixItHint createReplacement(const D &Destination, StringRef Source) {
72  Source);
73 }
74 
75 } // end namespace fixit
76 } // end namespace tooling
77 } // end namespace clang
78 
79 #endif // LLVM_CLANG_TOOLING_FIXINT_H
Defines the clang::ASTContext interface.
FixItHint createRemoval(const T &Node)
Definition: FixIt.h:56
FixItHint createReplacement(const D &Destination, const S &Source, const ASTContext &Context)
Definition: FixIt.h:62
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
StringRef getText(SourceRange Range, const ASTContext &Context)
Definition: FixIt.cpp:22
A source range independent of the SourceManager.
Definition: Replacement.h:42
const FunctionProtoType * T
Encodes a location in the source.
ast_type_traits::DynTypedNode Node
Dataflow Directional Tag Classes.
SourceRange getSourceRange(const SourceRange &Range)
Returns the SourceRange of a SourceRange.
Definition: FixIt.h:34
static FixItHint CreateRemoval(CharSourceRange RemoveRange)
Create a code modification hint that removes the given source range.
Definition: Diagnostic.h:116
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:127
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition: Diagnostic.h:64
A trivial tuple used to represent a source range.