Skip to content

Commit

Permalink
[clang-transformer] Allow stencils to read from system headers. (#66480)
Browse files Browse the repository at this point in the history
We were previously checking that stencil input ranges were writable. It
suffices for them to be readable.
  • Loading branch information
legrosbuffle committed Sep 18, 2023
1 parent 0f952cf commit 75b76c4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
6 changes: 6 additions & 0 deletions clang/include/clang/Tooling/Transformer/SourceCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ StringRef getExtendedText(const T &Node, tok::TokenKind Next,
llvm::Error validateEditRange(const CharSourceRange &Range,
const SourceManager &SM);

/// Determines whether \p Range is one that can be read from. If
/// `AllowSystemHeaders` is false, a range that falls within a system header
/// fails validation.
llvm::Error validateRange(const CharSourceRange &Range, const SourceManager &SM,
bool AllowSystemHeaders);

/// Attempts to resolve the given range to one that can be edited by a rewrite;
/// generally, one that starts and ends within a particular file. If a value is
/// returned, it satisfies \c validateEditRange.
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Tooling/Transformer/SourceCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ CharSourceRange clang::tooling::maybeExtendRange(CharSourceRange Range,
return CharSourceRange::getTokenRange(Range.getBegin(), Tok.getLocation());
}

static llvm::Error validateRange(const CharSourceRange &Range,
const SourceManager &SM,
bool AllowSystemHeaders) {
llvm::Error clang::tooling::validateRange(const CharSourceRange &Range,
const SourceManager &SM,
bool AllowSystemHeaders) {
if (Range.isInvalid())
return llvm::make_error<StringError>(errc::invalid_argument,
"Invalid range");
Expand Down
9 changes: 5 additions & 4 deletions clang/lib/Tooling/Transformer/Stencil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ class SelectorStencil : public StencilInterface {
// Validate the original range to attempt to get a meaningful error
// message. If it's valid, then something else is the cause and we just
// return the generic failure message.
if (auto Err =
tooling::validateEditRange(*RawRange, *Match.SourceManager))
if (auto Err = tooling::validateRange(*RawRange, *Match.SourceManager,
/*AllowSystemHeaders=*/true))
return handleErrors(std::move(Err), [](std::unique_ptr<StringError> E) {
assert(E->convertToErrorCode() ==
llvm::make_error_code(errc::invalid_argument) &&
Expand All @@ -245,8 +245,9 @@ class SelectorStencil : public StencilInterface {
"selected range could not be resolved to a valid source range");
}
// Validate `Range`, because `makeFileCharRange` accepts some ranges that
// `validateEditRange` rejects.
if (auto Err = tooling::validateEditRange(Range, *Match.SourceManager))
// `validateRange` rejects.
if (auto Err = tooling::validateRange(Range, *Match.SourceManager,
/*AllowSystemHeaders=*/true))
return joinErrors(
llvm::createStringError(errc::invalid_argument,
"selected range is not valid for editing"),
Expand Down

0 comments on commit 75b76c4

Please sign in to comment.