diff options
author | Yitzhak Mandelbaum <yitzhakm@google.com> | 2019-04-05 14:05:03 +0000 |
---|---|---|
committer | Yitzhak Mandelbaum <yitzhakm@google.com> | 2019-04-05 14:05:03 +0000 |
commit | 84f2271acd818e50c598fef7ce9586e7ee0ac553 (patch) | |
tree | 0e5a3b4f6e9fc4fd25830d88fe1cb56fb7d68fb7 /clang/lib/Tooling/Refactoring/SourceCode.cpp | |
parent | 106429b4e4155a279114ab8cd6d6383872ef2835 (diff) | |
download | bcm5719-llvm-84f2271acd818e50c598fef7ce9586e7ee0ac553.tar.gz bcm5719-llvm-84f2271acd818e50c598fef7ce9586e7ee0ac553.zip |
[LibTooling] Add "SourceCode" library for functions relating to source-code manipulation.
Summary:
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.
Reviewers: ilya-biryukov, ioeric
Reviewed By: ilya-biryukov
Subscribers: mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D60269
llvm-svn: 357764
Diffstat (limited to 'clang/lib/Tooling/Refactoring/SourceCode.cpp')
-rw-r--r-- | clang/lib/Tooling/Refactoring/SourceCode.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/clang/lib/Tooling/Refactoring/SourceCode.cpp b/clang/lib/Tooling/Refactoring/SourceCode.cpp new file mode 100644 index 00000000000..3a97e178bbd --- /dev/null +++ b/clang/lib/Tooling/Refactoring/SourceCode.cpp @@ -0,0 +1,31 @@ +//===--- 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. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file provides functions that simplify extraction of source code. +// +//===----------------------------------------------------------------------===// +#include "clang/Tooling/Refactoring/SourceCode.h" +#include "clang/Lex/Lexer.h" + +using namespace clang; + +StringRef clang::tooling::getText(CharSourceRange Range, + const ASTContext &Context) { + return Lexer::getSourceText(Range, Context.getSourceManager(), + Context.getLangOpts()); +} + +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()); +} |