clang 10.0.0git

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 //===----------------------------------------------------------------------===//
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 }
clang::CharSourceRange::getTokenRange
static CharSourceRange getTokenRange(SourceRange R)
Definition: SourceLocation.h:258
clang::tooling::getText
StringRef getText(CharSourceRange Range, const ASTContext &Context)
Returns the source-code text in the specified range.
Definition: SourceCode.cpp:17
SourceCode.h
clang::CharSourceRange::getBegin
SourceLocation getBegin() const
Definition: SourceLocation.h:280
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
llvm::Optional
Definition: LLVM.h:37
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:53
clang::format::Tok
const FormatToken & Tok
Definition: BreakableToken.h:239
clang::tooling::Range
A source range independent of the SourceManager.
Definition: Replacement.h:44
clang::Lexer::getSourceText
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
clang::CharSourceRange
Represents a character-granular source range.
Definition: SourceLocation.h:250
clang::SourceManager::isInSystemHeader
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
Definition: SourceManager.h:1493
clang::CharSourceRange::isInvalid
bool isInvalid() const
Definition: SourceLocation.h:289
Lexer.h
SM
const SourceManager & SM
Definition: Format.cpp:1685
clang::tooling::maybeExtendRange
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
clang::Lexer::findNextToken
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
clang::tok::TokenKind
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:24
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::tooling::getRangeForEdit
llvm::Optional< CharSourceRange > getRangeForEdit(const CharSourceRange &EditRange, const SourceManager &SM, const LangOptions &LangOpts)
Definition: SourceCode.cpp:34
clang::Lexer::makeFileCharRange
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
clang::SourceLocation::isMacroID
bool isMacroID() const
Definition: SourceLocation.h:99
clang::CharSourceRange::getEnd
SourceLocation getEnd() const
Definition: SourceLocation.h:281
clang::ASTContext::getSourceManager
SourceManager & getSourceManager()
Definition: ASTContext.h:679
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:724
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:596
clang::SourceManager::getDecomposedLoc
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
Definition: SourceManager.h:1238