Skip to content

Commit 1fabe6e

Browse files
committed
[libTooling] Change addInclude to use expansion locs.
This patch changes the default range used to anchor the include insertion to use an expansion loc. This ensures that the location is valid, when the user relies on the default range. Driveby: extend a FIXME for a problem that was emphasized by this change; fix some spellings. Differential Revision: https://reviews.llvm.org/D93703
1 parent 214387c commit 1fabe6e

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

clang/include/clang/Tooling/Transformer/RewriteRule.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,12 @@ ASTEdit addInclude(RangeSelector Target, StringRef Header,
213213
IncludeFormat Format = IncludeFormat::Quoted);
214214

215215
/// Adds an include directive for the given header to the file associated with
216-
/// `RootID`.
216+
/// `RootID`. If `RootID` matches inside a macro expansion, will add the
217+
/// directive to the file in which the macro was expanded (as opposed to the
218+
/// file in which the macro is defined).
217219
inline ASTEdit addInclude(StringRef Header,
218220
IncludeFormat Format = IncludeFormat::Quoted) {
219-
return addInclude(node(RootID), Header, Format);
221+
return addInclude(expansion(node(RootID)), Header, Format);
220222
}
221223

222224
// FIXME: If `Metadata` returns an `llvm::Expected<T>` the `AnyGenerator` will
@@ -312,8 +314,8 @@ inline RewriteRule makeRule(ast_matchers::internal::DynTypedMatcher M,
312314
/// \code
313315
/// auto R = makeRule(callExpr(callee(functionDecl(hasName("foo")))),
314316
/// changeTo(cat("bar()")));
315-
/// AddInclude(R, "path/to/bar_header.h");
316-
/// AddInclude(R, "vector", IncludeFormat::Angled);
317+
/// addInclude(R, "path/to/bar_header.h");
318+
/// addInclude(R, "vector", IncludeFormat::Angled);
317319
/// \endcode
318320
void addInclude(RewriteRule &Rule, llvm::StringRef Header,
319321
IncludeFormat Format = IncludeFormat::Quoted);

clang/lib/Tooling/Transformer/RewriteRule.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ translateEdits(const MatchResult &Result, ArrayRef<ASTEdit> ASTEdits) {
4242
llvm::Optional<CharSourceRange> EditRange =
4343
tooling::getRangeForEdit(*Range, *Result.Context);
4444
// FIXME: let user specify whether to treat this case as an error or ignore
45-
// it as is currently done.
45+
// it as is currently done. This behavior is problematic in that it hides
46+
// failures from bad ranges. Also, the behavior here differs from
47+
// `flatten`. Here, we abort (without error), whereas flatten, if it hits an
48+
// empty list, does not abort. As a result, `editList({A,B})` is not
49+
// equivalent to `flatten(edit(A), edit(B))`. The former will abort if `A`
50+
// produces a bad range, whereas the latter will simply ignore A.
4651
if (!EditRange)
4752
return SmallVector<Edit, 0>();
4853
auto Replacement = E.Replacement->eval(Result);

0 commit comments

Comments
 (0)