clang
20.0.0git
include
clang
Tooling
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
24
namespace
clang
{
25
namespace
tooling {
26
namespace
fixit {
27
28
namespace
internal {
29
StringRef
getText
(
CharSourceRange
Range
,
const
ASTContext
&Context);
30
31
/// Returns the token CharSourceRange corresponding to \p Range.
32
inline
CharSourceRange
getSourceRange
(
const
SourceRange
&
Range
) {
33
return
CharSourceRange::getTokenRange
(
Range
);
34
}
35
36
/// Returns the CharSourceRange of the token at Location \p Loc.
37
inline
CharSourceRange
getSourceRange
(
const
SourceLocation
&
Loc
) {
38
return
CharSourceRange::getTokenRange
(
Loc
,
Loc
);
39
}
40
41
/// Returns the CharSourceRange of an given Node. \p Node is typically a
42
/// 'Stmt', 'Expr' or a 'Decl'.
43
template
<
typename
T>
CharSourceRange
getSourceRange
(
const
T
&
Node
) {
44
return
CharSourceRange::getTokenRange
(
Node
.getSourceRange());
45
}
46
}
// end namespace internal
47
48
/// 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
// 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
));
58
}
59
60
// 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));
66
}
67
68
// Returns a FixItHint to replace \p Destination by \p Source.
69
template
<
typename
D>
70
FixItHint
createReplacement
(
const
D
&Destination, StringRef Source) {
71
return
FixItHint::CreateReplacement
(
internal::getSourceRange
(Destination),
72
Source);
73
}
74
75
}
// end namespace fixit
76
}
// end namespace tooling
77
}
// end namespace clang
78
79
#endif
// LLVM_CLANG_TOOLING_FIXIT_H
ASTContext.h
Defines the clang::ASTContext interface.
Node
DynTypedNode Node
Definition:
ASTMatchFinder.cpp:70
D
const Decl * D
Definition:
CheckExprLifetime.cpp:200
Loc
SourceLocation Loc
Definition:
SemaObjC.cpp:759
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition:
ASTContext.h:187
clang::CharSourceRange
Represents a character-granular source range.
Definition:
SourceLocation.h:256
clang::CharSourceRange::getTokenRange
static CharSourceRange getTokenRange(SourceRange R)
Definition:
SourceLocation.h:264
clang::FixItHint
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition:
Diagnostic.h:71
clang::FixItHint::CreateReplacement
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:134
clang::FixItHint::CreateRemoval
static FixItHint CreateRemoval(CharSourceRange RemoveRange)
Create a code modification hint that removes the given source range.
Definition:
Diagnostic.h:123
clang::SourceLocation
Encodes a location in the source.
Definition:
SourceLocation.h:88
clang::SourceRange
A trivial tuple used to represent a source range.
Definition:
SourceLocation.h:213
clang::tooling::Range
A source range independent of the SourceManager.
Definition:
Replacement.h:44
clang::tooling::fixit::internal::getText
StringRef getText(CharSourceRange Range, const ASTContext &Context)
Definition:
FixIt.cpp:21
clang::tooling::fixit::internal::getSourceRange
CharSourceRange getSourceRange(const SourceRange &Range)
Returns the token CharSourceRange corresponding to Range.
Definition:
FixIt.h:32
clang::tooling::fixit::createReplacement
FixItHint createReplacement(const D &Destination, const S &Source, const ASTContext &Context)
Definition:
FixIt.h:62
clang::tooling::fixit::createRemoval
FixItHint createRemoval(const T &Node)
Definition:
FixIt.h:56
clang::tooling::fixit::getText
StringRef getText(const T &Node, const ASTContext &Context)
Returns a textual representation of Node.
Definition:
FixIt.h:50
clang
The JSON file list parser is used to communicate input to InstallAPI.
Definition:
CalledOnceCheck.h:17
clang::T
const FunctionProtoType * T
Definition:
RecursiveASTVisitor.h:1359
Generated on Tue Nov 19 2024 23:01:42 for clang by
1.9.6