]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/clang/lib/Tooling/Transformer/SourceCode.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / clang / lib / Tooling / Transformer / SourceCode.cpp
1 //===--- SourceCode.cpp - Source code manipulation routines -----*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file provides functions that simplify extraction of source code.
10 //
11 //===----------------------------------------------------------------------===//
12 #include "clang/Tooling/Transformer/SourceCode.h"
13 #include "clang/Lex/Lexer.h"
14
15 using namespace clang;
16
17 StringRef clang::tooling::getText(CharSourceRange Range,
18                                   const ASTContext &Context) {
19   return Lexer::getSourceText(Range, Context.getSourceManager(),
20                               Context.getLangOpts());
21 }
22
23 CharSourceRange clang::tooling::maybeExtendRange(CharSourceRange Range,
24                                                  tok::TokenKind Next,
25                                                  ASTContext &Context) {
26   Optional<Token> Tok = Lexer::findNextToken(
27       Range.getEnd(), Context.getSourceManager(), Context.getLangOpts());
28   if (!Tok || !Tok->is(Next))
29     return Range;
30   return CharSourceRange::getTokenRange(Range.getBegin(), Tok->getLocation());
31 }
32
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
41   // then
42   //    foo(DO_NOTHING(3))
43   // will be rewritten to
44   //    foo(6)
45   // rather than the arguably better
46   //    foo(DO_NOTHING(6))
47   // Decide whether the current behavior is desirable and modify if not.
48   CharSourceRange Range = Lexer::makeFileCharRange(EditRange, SM, LangOpts);
49   if (Range.isInvalid())
50     return None;
51
52   if (Range.getBegin().isMacroID() || Range.getEnd().isMacroID())
53     return None;
54   if (SM.isInSystemHeader(Range.getBegin()) ||
55       SM.isInSystemHeader(Range.getEnd()))
56     return None;
57
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)
62     return None;
63
64   return Range;
65 }