SourceCode.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031
  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/Refactoring/SourceCode.h"
  13. #include "clang/Lex/Lexer.h"
  14. using namespace clang;
  15. StringRef clang::tooling::getText(CharSourceRange Range,
  16. const ASTContext &Context) {
  17. return Lexer::getSourceText(Range, Context.getSourceManager(),
  18. Context.getLangOpts());
  19. }
  20. CharSourceRange clang::tooling::maybeExtendRange(CharSourceRange Range,
  21. tok::TokenKind Next,
  22. ASTContext &Context) {
  23. Optional<Token> Tok = Lexer::findNextToken(
  24. Range.getEnd(), Context.getSourceManager(), Context.getLangOpts());
  25. if (!Tok || !Tok->is(Next))
  26. return Range;
  27. return CharSourceRange::getTokenRange(Range.getBegin(), Tok->getLocation());
  28. }