clang  10.0.0svn
SourceCode.cpp
Go to the documentation of this file.
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 //===----------------------------------------------------------------------===//
13 #include "clang/Lex/Lexer.h"
14 
15 using namespace clang;
16 
18  const ASTContext &Context) {
19  return Lexer::getSourceText(Range, Context.getSourceManager(),
20  Context.getLangOpts());
21 }
22 
24  tok::TokenKind Next,
25  ASTContext &Context) {
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 
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 }
static CharSourceRange getTokenRange(SourceRange R)
StringRef getText(CharSourceRange Range, const ASTContext &Context)
Returns the source-code text in the specified range.
Definition: SourceCode.cpp:17
SourceLocation getBegin() const
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:160
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:49
const FormatToken & Tok
A source range independent of the SourceManager.
Definition: Replacement.h:44
static StringRef getSourceText(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts, bool *Invalid=nullptr)
Returns a string for the source that the range encompasses.
Definition: Lexer.cpp:939
Represents a character-granular source range.
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
const SourceManager & SM
Definition: Format.cpp:1667
CharSourceRange maybeExtendRange(CharSourceRange Range, tok::TokenKind Next, ASTContext &Context)
Extends Range to include the token Next, if it immediately follows the end of the range...
Definition: SourceCode.cpp:23
static Optional< Token > findNextToken(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Finds the token that comes right after the given location.
Definition: Lexer.cpp:1237
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:24
Dataflow Directional Tag Classes.
llvm::Optional< CharSourceRange > getRangeForEdit(const CharSourceRange &EditRange, const SourceManager &SM, const LangOptions &LangOpts)
Definition: SourceCode.cpp:34
static CharSourceRange makeFileCharRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Accepts a range and returns a character range with file locations.
Definition: Lexer.cpp:875
bool isMacroID() const
SourceLocation getEnd() const
SourceManager & getSourceManager()
Definition: ASTContext.h:678
const LangOptions & getLangOpts() const
Definition: ASTContext.h:723
This class handles loading and caching of source files into memory.
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.