ymandel created this revision.
ymandel added reviewers: ilya-biryukov, ioeric.
Herald added a project: clang.

Introduces a utility library in Refactoring/ to collect routines related to
source-code manipulation.  In this change, we move "extended-range" functions
from the FixIt library (in clangTooling) to this new library.

We need to use this functionality in Refactoring/ and cannot access it if it
resides in Tooling/, because that would cause clangToolingRefactor to depend on
clangTooling, which would be a circular dependency.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D60269

Files:
  clang/include/clang/Tooling/FixIt.h
  clang/include/clang/Tooling/Refactoring/SourceCode.h
  clang/lib/Tooling/FixIt.cpp
  clang/lib/Tooling/Refactoring/SourceCode.cpp

Index: clang/lib/Tooling/Refactoring/SourceCode.cpp
===================================================================
--- clang/lib/Tooling/Refactoring/SourceCode.cpp
+++ clang/lib/Tooling/Refactoring/SourceCode.cpp
@@ -1,4 +1,4 @@
-//===--- FixIt.cpp - FixIt Hint utilities -----------------------*- C++ -*-===//
+//===--- SourceCode.cpp - Source code manipulation routines -----*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,33 +6,26 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// This file contains implementations of utitilies to ease source code rewriting
-// by providing helper functions related to FixItHint.
+//  This file provides functions that simplify extraction of source code.
 //
 //===----------------------------------------------------------------------===//
-#include "clang/Tooling/FixIt.h"
+#include "clang/Tooling/Refactoring/SourceCode.h"
 #include "clang/Lex/Lexer.h"
 
-namespace clang {
-namespace tooling {
-namespace fixit {
+using namespace clang;
 
-namespace internal {
-StringRef getText(CharSourceRange Range, const ASTContext &Context) {
+StringRef clang::tooling::getText(CharSourceRange Range,
+                                  const ASTContext &Context) {
   return Lexer::getSourceText(Range, Context.getSourceManager(),
                               Context.getLangOpts());
 }
 
-CharSourceRange maybeExtendRange(CharSourceRange Range, tok::TokenKind Next,
-                                 ASTContext &Context) {
+CharSourceRange clang::tooling::maybeExtendRange(CharSourceRange Range,
+                                                 tok::TokenKind Next,
+                                                 ASTContext &Context) {
   Optional<Token> Tok = Lexer::findNextToken(
       Range.getEnd(), Context.getSourceManager(), Context.getLangOpts());
   if (!Tok || !Tok->is(Next))
     return Range;
   return CharSourceRange::getTokenRange(Range.getBegin(), Tok->getLocation());
 }
-} // namespace internal
-
-} // end namespace fixit
-} // end namespace tooling
-} // end namespace clang
Index: clang/lib/Tooling/FixIt.cpp
===================================================================
--- clang/lib/Tooling/FixIt.cpp
+++ clang/lib/Tooling/FixIt.cpp
@@ -22,15 +22,6 @@
   return Lexer::getSourceText(Range, Context.getSourceManager(),
                               Context.getLangOpts());
 }
-
-CharSourceRange maybeExtendRange(CharSourceRange Range, tok::TokenKind Next,
-                                 ASTContext &Context) {
-  Optional<Token> Tok = Lexer::findNextToken(
-      Range.getEnd(), Context.getSourceManager(), Context.getLangOpts());
-  if (!Tok || !Tok->is(Next))
-    return Range;
-  return CharSourceRange::getTokenRange(Range.getBegin(), Tok->getLocation());
-}
 } // namespace internal
 
 } // end namespace fixit
Index: clang/include/clang/Tooling/Refactoring/SourceCode.h
===================================================================
--- /dev/null
+++ clang/include/clang/Tooling/Refactoring/SourceCode.h
@@ -0,0 +1,70 @@
+//===--- SourceCode.h - Source code manipulation routines -------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file provides functions that simplify extraction of source code.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLING_REFACTOR_SOURCE_CODE_H
+#define LLVM_CLANG_TOOLING_REFACTOR_SOURCE_CODE_H
+
+#include "clang/AST/ASTContext.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/TokenKinds.h"
+
+namespace clang {
+namespace tooling {
+
+/// Extends \p Range to include the token \p Next, if it immediately follows the
+/// end of the range. Otherwise, returns \p Range unchanged.
+CharSourceRange maybeExtendRange(CharSourceRange Range, tok::TokenKind Next,
+                                 ASTContext &Context);
+
+/// Returns the source range spanning the node, extended to include \p Next, if
+/// it immediately follows \p Node. Otherwise, returns the normal range of \p
+/// Node.  See comments on `getExtendedText()` for examples.
+template <typename T>
+CharSourceRange getExtendedRange(const T &Node, tok::TokenKind Next,
+                                 ASTContext &Context) {
+  return internal::maybeExtendRange(
+      CharSourceRange::getTokenRange(Node.getSourceRange()), Next, Context);
+}
+
+/// Returns the source-code text in the specified range.
+StringRef getText(CharSourceRange Range, const ASTContext &Context);
+
+/// Returns the source text of the node, extended to include \p Next, if it
+/// immediately follows the node. Otherwise, returns the text of just \p Node.
+///
+/// For example, given statements S1 and S2 below:
+/// \code
+///   {
+///     // S1:
+///     if (!x) return foo();
+///     // S2:
+///     if (!x) { return 3; }
+///   }
+/// \endcode
+/// then
+/// \code
+///   getText(S1, Context) = "if (!x) return foo()"
+///   getExtendedText(S1, tok::TokenKind::semi, Context)
+///     = "if (!x) return foo();"
+///   getExtendedText(*S1.getThen(), tok::TokenKind::semi, Context)
+///     = "return foo();"
+///   getExtendedText(*S2.getThen(), tok::TokenKind::semi, Context)
+///     = getText(S2, Context) = "{ return 3; }"
+/// \endcode
+template <typename T>
+StringRef getExtendedText(const T &Node, tok::TokenKind Next,
+                          ASTContext &Context) {
+  return getText(getExtendedRange(Node, Next, Context), Context);
+}
+} // namespace tooling
+} // namespace clang
+#endif // LLVM_CLANG_TOOLING_REFACTOR_SOURCE_CODE_H
Index: clang/include/clang/Tooling/FixIt.h
===================================================================
--- clang/include/clang/Tooling/FixIt.h
+++ clang/include/clang/Tooling/FixIt.h
@@ -20,7 +20,6 @@
 #define LLVM_CLANG_TOOLING_FIXIT_H
 
 #include "clang/AST/ASTContext.h"
-#include "clang/Basic/TokenKinds.h"
 
 namespace clang {
 namespace tooling {
@@ -44,11 +43,6 @@
 template <typename T> CharSourceRange getSourceRange(const T &Node) {
   return CharSourceRange::getTokenRange(Node.getSourceRange());
 }
-
-/// Extends \p Range to include the token \p Next, if it immediately follows the
-/// end of the range. Otherwise, returns \p Range unchanged.
-CharSourceRange maybeExtendRange(CharSourceRange Range, tok::TokenKind Next,
-                                 ASTContext &Context);
 } // end namespace internal
 
 /// Returns a textual representation of \p Node.
@@ -57,44 +51,6 @@
   return internal::getText(internal::getSourceRange(Node), Context);
 }
 
-/// Returns the source range spanning the node, extended to include \p Next, if
-/// it immediately follows \p Node. Otherwise, returns the normal range of \p
-/// Node.  See comments on `getExtendedText()` for examples.
-template <typename T>
-CharSourceRange getExtendedRange(const T &Node, tok::TokenKind Next,
-                                 ASTContext &Context) {
-  return internal::maybeExtendRange(internal::getSourceRange(Node), Next,
-                                    Context);
-}
-
-/// Returns the source text of the node, extended to include \p Next, if it
-/// immediately follows the node. Otherwise, returns the text of just \p Node.
-///
-/// For example, given statements S1 and S2 below:
-/// \code
-///   {
-///     // S1:
-///     if (!x) return foo();
-///     // S2:
-///     if (!x) { return 3; }
-///   }
-/// \endcode
-/// then
-/// \code
-///   getText(S1, Context) = "if (!x) return foo()"
-///   getExtendedText(S1, tok::TokenKind::semi, Context)
-///     = "if (!x) return foo();"
-///   getExtendedText(*S1.getThen(), tok::TokenKind::semi, Context)
-///     = "return foo();"
-///   getExtendedText(*S2.getThen(), tok::TokenKind::semi, Context)
-///     = getText(S2, Context) = "{ return 3; }"
-/// \endcode
-template <typename T>
-StringRef getExtendedText(const T &Node, tok::TokenKind Next,
-                          ASTContext &Context) {
-  return internal::getText(getExtendedRange(Node, Next, Context), Context);
-}
-
 // Returns a FixItHint to remove \p Node.
 // TODO: Add support for related syntactical elements (i.e. comments, ...).
 template <typename T> FixItHint createRemoval(const T &Node) {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to