Skip to content

Commit 60a4163

Browse files
committedMar 13, 2019
[LibTooling] Add retrieval of extended AST-node source to FixIt library
Summary: Introduces variants of `getText` and `getSourceRange` that extract the source text of an AST node potentially with a trailing token. Some of the new functions manipulate `CharSourceRange`s, rather than `SourceRange`s, because they document and dynamically enforce their type. So, this revision also updates the corresponding existing FixIt functions to manipulate `CharSourceRange`s. This change is not strictly necessary, but seems like the correct choice, to keep the API self-consistent. This revision is the first in a series intended to improve the abstractions available to users for writing source-to-source transformations. A full discussion of the end goal can be found on the cfe-dev list with subject "[RFC] Easier source-to-source transformations with clang tooling". Reviewers: ilya-biryukov Reviewed By: ilya-biryukov Subscribers: kimgr, riccibruno, JonasToth, jdoerfert, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D58556 llvm-svn: 356095
1 parent 0253620 commit 60a4163

File tree

3 files changed

+97
-15
lines changed

3 files changed

+97
-15
lines changed
 

‎clang/include/clang/Tooling/FixIt.h

+56-11
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,81 @@
2020
#define LLVM_CLANG_TOOLING_FIXIT_H
2121

2222
#include "clang/AST/ASTContext.h"
23+
#include "clang/Basic/TokenKinds.h"
2324

2425
namespace clang {
2526
namespace tooling {
2627
namespace fixit {
2728

2829
namespace internal {
29-
StringRef getText(SourceRange Range, const ASTContext &Context);
30+
StringRef getText(CharSourceRange Range, const ASTContext &Context);
3031

31-
/// Returns the SourceRange of a SourceRange. This identity function is
32-
/// used by the following template abstractions.
33-
inline SourceRange getSourceRange(const SourceRange &Range) { return Range; }
32+
/// Returns the token CharSourceRange corresponding to \p Range.
33+
inline CharSourceRange getSourceRange(const SourceRange &Range) {
34+
return CharSourceRange::getTokenRange(Range);
35+
}
3436

35-
/// Returns the SourceRange of the token at Location \p Loc.
36-
inline SourceRange getSourceRange(const SourceLocation &Loc) {
37-
return SourceRange(Loc);
37+
/// Returns the CharSourceRange of the token at Location \p Loc.
38+
inline CharSourceRange getSourceRange(const SourceLocation &Loc) {
39+
return CharSourceRange::getTokenRange(Loc, Loc);
3840
}
3941

40-
/// Returns the SourceRange of an given Node. \p Node is typically a
42+
/// Returns the CharSourceRange of an given Node. \p Node is typically a
4143
/// 'Stmt', 'Expr' or a 'Decl'.
42-
template <typename T> SourceRange getSourceRange(const T &Node) {
43-
return Node.getSourceRange();
44+
template <typename T> CharSourceRange getSourceRange(const T &Node) {
45+
return CharSourceRange::getTokenRange(Node.getSourceRange());
4446
}
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.
50+
CharSourceRange maybeExtendRange(CharSourceRange Range, tok::TokenKind Next,
51+
ASTContext &Context);
4552
} // end namespace internal
4653

47-
// Returns a textual representation of \p Node.
54+
/// Returns a textual representation of \p Node.
4855
template <typename T>
4956
StringRef getText(const T &Node, const ASTContext &Context) {
5057
return internal::getText(internal::getSourceRange(Node), Context);
5158
}
5259

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>
64+
CharSourceRange getExtendedRange(const T &Node, tok::TokenKind Next,
65+
ASTContext &Context) {
66+
return internal::maybeExtendRange(internal::getSourceRange(Node), Next,
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+
5398
// Returns a FixItHint to remove \p Node.
5499
// TODO: Add support for related syntactical elements (i.e. comments, ...).
55100
template <typename T> FixItHint createRemoval(const T &Node) {

‎clang/lib/Tooling/FixIt.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ namespace tooling {
1818
namespace fixit {
1919

2020
namespace internal {
21-
StringRef getText(SourceRange Range, const ASTContext &Context) {
22-
return Lexer::getSourceText(CharSourceRange::getTokenRange(Range),
23-
Context.getSourceManager(),
21+
StringRef getText(CharSourceRange Range, const ASTContext &Context) {
22+
return Lexer::getSourceText(Range, Context.getSourceManager(),
2423
Context.getLangOpts());
2524
}
26-
} // end namespace internal
25+
26+
CharSourceRange maybeExtendRange(CharSourceRange Range, tok::TokenKind Next,
27+
ASTContext &Context) {
28+
Optional<Token> Tok = Lexer::findNextToken(
29+
Range.getEnd(), Context.getSourceManager(), Context.getLangOpts());
30+
if (!Tok || !Tok->is(Next))
31+
return Range;
32+
return CharSourceRange::getTokenRange(Range.getBegin(), Tok->getLocation());
33+
}
34+
} // namespace internal
2735

2836
} // end namespace fixit
2937
} // end namespace tooling

‎clang/unittests/Tooling/FixItTest.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using namespace clang;
1414

1515
using tooling::fixit::getText;
16+
using tooling::fixit::getExtendedText;
1617
using tooling::fixit::createRemoval;
1718
using tooling::fixit::createReplacement;
1819

@@ -77,6 +78,34 @@ TEST(FixItTest, getTextWithMacro) {
7778
"void foo(int x, int y) { FOO(x,y) }");
7879
}
7980

81+
TEST(FixItTest, getExtendedText) {
82+
CallsVisitor Visitor;
83+
84+
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
85+
EXPECT_EQ("foo(x, y);",
86+
getExtendedText(*CE, tok::TokenKind::semi, *Context));
87+
88+
Expr *P0 = CE->getArg(0);
89+
Expr *P1 = CE->getArg(1);
90+
EXPECT_EQ("x", getExtendedText(*P0, tok::TokenKind::semi, *Context));
91+
EXPECT_EQ("x,", getExtendedText(*P0, tok::TokenKind::comma, *Context));
92+
EXPECT_EQ("y", getExtendedText(*P1, tok::TokenKind::semi, *Context));
93+
};
94+
Visitor.runOver("void foo(int x, int y) { foo(x, y); }");
95+
Visitor.runOver("void foo(int x, int y) { if (true) foo(x, y); }");
96+
Visitor.runOver("int foo(int x, int y) { if (true) return 3 + foo(x, y); }");
97+
Visitor.runOver("void foo(int x, int y) { for (foo(x, y);;) ++x; }");
98+
Visitor.runOver(
99+
"bool foo(int x, int y) { for (;foo(x, y);) x = 1; return true; }");
100+
101+
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
102+
EXPECT_EQ("foo()", getExtendedText(*CE, tok::TokenKind::semi, *Context));
103+
};
104+
Visitor.runOver("bool foo() { if (foo()) return true; return false; }");
105+
Visitor.runOver("void foo() { int x; for (;; foo()) ++x; }");
106+
Visitor.runOver("int foo() { return foo() + 3; }");
107+
}
108+
80109
TEST(FixItTest, createRemoval) {
81110
CallsVisitor Visitor;
82111

0 commit comments

Comments
 (0)
Please sign in to comment.