1 //===--- SourceCode.cpp - Source code manipulation routines -----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file provides functions that simplify extraction of source code.
11 //===----------------------------------------------------------------------===//
12 #include "clang/Tooling/Transformer/SourceCode.h"
13 #include "clang/Lex/Lexer.h"
15 using namespace clang;
17 StringRef clang::tooling::getText(CharSourceRange Range,
18 const ASTContext &Context) {
19 return Lexer::getSourceText(Range, Context.getSourceManager(),
20 Context.getLangOpts());
23 CharSourceRange clang::tooling::maybeExtendRange(CharSourceRange Range,
25 ASTContext &Context) {
26 Optional<Token> Tok = Lexer::findNextToken(
27 Range.getEnd(), Context.getSourceManager(), Context.getLangOpts());
28 if (!Tok || !Tok->is(Next))
30 return CharSourceRange::getTokenRange(Range.getBegin(), Tok->getLocation());
33 llvm::Optional<CharSourceRange>
34 clang::tooling::getRangeForEdit(const CharSourceRange &EditRange,
35 const SourceManager &SM,
36 const LangOptions &LangOpts) {
37 // FIXME: makeFileCharRange() has the disadvantage of stripping off "identity"
38 // macros. For example, if we're looking to rewrite the int literal 3 to 6,
39 // and we have the following definition:
40 // #define DO_NOTHING(x) x
43 // will be rewritten to
45 // rather than the arguably better
47 // Decide whether the current behavior is desirable and modify if not.
48 CharSourceRange Range = Lexer::makeFileCharRange(EditRange, SM, LangOpts);
49 if (Range.isInvalid())
52 if (Range.getBegin().isMacroID() || Range.getEnd().isMacroID())
54 if (SM.isInSystemHeader(Range.getBegin()) ||
55 SM.isInSystemHeader(Range.getEnd()))
58 std::pair<FileID, unsigned> BeginInfo = SM.getDecomposedLoc(Range.getBegin());
59 std::pair<FileID, unsigned> EndInfo = SM.getDecomposedLoc(Range.getEnd());
60 if (BeginInfo.first != EndInfo.first ||
61 BeginInfo.second > EndInfo.second)