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

via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 18 01:07:34 PDT 2023


Author: Clement Courbet
Date: 2023-09-18T10:07:30+02:00
New Revision: 75b76c4b477e98b5e74f986f180000acc04b4ada

URL: https://github.com/llvm/llvm-project/commit/75b76c4b477e98b5e74f986f180000acc04b4ada
DIFF: https://github.com/llvm/llvm-project/commit/75b76c4b477e98b5e74f986f180000acc04b4ada.diff

LOG: [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.

Added: 
    

Modified: 
    clang/include/clang/Tooling/Transformer/SourceCode.h
    clang/lib/Tooling/Transformer/SourceCode.cpp
    clang/lib/Tooling/Transformer/Stencil.cpp

Removed: 
    


################################################################################
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