Skip to content

Commit 75b76c4

Browse files
authored
[clang-transformer] Allow stencils to read from system headers. (#66480)
We were previously checking that stencil input ranges were writable. It suffices for them to be readable.
1 parent 0f952cf commit 75b76c4

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

clang/include/clang/Tooling/Transformer/SourceCode.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ StringRef getExtendedText(const T &Node, tok::TokenKind Next,
9191
llvm::Error validateEditRange(const CharSourceRange &Range,
9292
const SourceManager &SM);
9393

94+
/// Determines whether \p Range is one that can be read from. If
95+
/// `AllowSystemHeaders` is false, a range that falls within a system header
96+
/// fails validation.
97+
llvm::Error validateRange(const CharSourceRange &Range, const SourceManager &SM,
98+
bool AllowSystemHeaders);
99+
94100
/// Attempts to resolve the given range to one that can be edited by a rewrite;
95101
/// generally, one that starts and ends within a particular file. If a value is
96102
/// returned, it satisfies \c validateEditRange.

clang/lib/Tooling/Transformer/SourceCode.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ CharSourceRange clang::tooling::maybeExtendRange(CharSourceRange Range,
5050
return CharSourceRange::getTokenRange(Range.getBegin(), Tok.getLocation());
5151
}
5252

53-
static llvm::Error validateRange(const CharSourceRange &Range,
54-
const SourceManager &SM,
55-
bool AllowSystemHeaders) {
53+
llvm::Error clang::tooling::validateRange(const CharSourceRange &Range,
54+
const SourceManager &SM,
55+
bool AllowSystemHeaders) {
5656
if (Range.isInvalid())
5757
return llvm::make_error<StringError>(errc::invalid_argument,
5858
"Invalid range");

clang/lib/Tooling/Transformer/Stencil.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ class SelectorStencil : public StencilInterface {
229229
// Validate the original range to attempt to get a meaningful error
230230
// message. If it's valid, then something else is the cause and we just
231231
// return the generic failure message.
232-
if (auto Err =
233-
tooling::validateEditRange(*RawRange, *Match.SourceManager))
232+
if (auto Err = tooling::validateRange(*RawRange, *Match.SourceManager,
233+
/*AllowSystemHeaders=*/true))
234234
return handleErrors(std::move(Err), [](std::unique_ptr<StringError> E) {
235235
assert(E->convertToErrorCode() ==
236236
llvm::make_error_code(errc::invalid_argument) &&
@@ -245,8 +245,9 @@ class SelectorStencil : public StencilInterface {
245245
"selected range could not be resolved to a valid source range");
246246
}
247247
// Validate `Range`, because `makeFileCharRange` accepts some ranges that
248-
// `validateEditRange` rejects.
249-
if (auto Err = tooling::validateEditRange(Range, *Match.SourceManager))
248+
// `validateRange` rejects.
249+
if (auto Err = tooling::validateRange(Range, *Match.SourceManager,
250+
/*AllowSystemHeaders=*/true))
250251
return joinErrors(
251252
llvm::createStringError(errc::invalid_argument,
252253
"selected range is not valid for editing"),

0 commit comments

Comments
 (0)