[clang] [clang-transformer] Allow stencils to read from system headers. (PR #66480)

Clement Courbet via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 15 05:45:14 PDT 2023


https://github.com/legrosbuffle updated https://github.com/llvm/llvm-project/pull/66480

>From b23472e55093ea1d48d11386cc4ced43f913c170 Mon Sep 17 00:00:00 2001
From: Clement Courbet <courbet at google.com>
Date: Fri, 15 Sep 2023 10:42:16 +0200
Subject: [PATCH] [clang-transformer] Allow stencils to read from system
 headers.

We were previously checking that stencil input ranges were writable. It suffices for them to be readable.
---
 clang/include/clang/Tooling/Transformer/SourceCode.h | 6 ++++++
 clang/lib/Tooling/Transformer/SourceCode.cpp         | 6 +++---
 clang/lib/Tooling/Transformer/Stencil.cpp            | 9 +++++----
 3 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/clang/include/clang/Tooling/Transformer/SourceCode.h b/clang/include/clang/Tooling/Transformer/SourceCode.h
index 44a4749db74c96c..004c614b0b9d93c 100644
--- a/clang/include/clang/Tooling/Transformer/SourceCode.h
+++ b/clang/include/clang/Tooling/Transformer/SourceCode.h
@@ -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.
diff --git a/clang/lib/Tooling/Transformer/SourceCode.cpp b/clang/lib/Tooling/Transformer/SourceCode.cpp
index 35edc261ef09670..30009537b5923ce 100644
--- a/clang/lib/Tooling/Transformer/SourceCode.cpp
+++ b/clang/lib/Tooling/Transformer/SourceCode.cpp
@@ -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");
diff --git a/clang/lib/Tooling/Transformer/Stencil.cpp b/clang/lib/Tooling/Transformer/Stencil.cpp
index f2c1b6f8520a8cb..d91c9e0a20cc1b0 100644
--- a/clang/lib/Tooling/Transformer/Stencil.cpp
+++ b/clang/lib/Tooling/Transformer/Stencil.cpp
@@ -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) &&
@@ -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"),



More information about the cfe-commits mailing list