clang 10.0.0git

SourceCode.h
Go to the documentation of this file.
1 //===--- SourceCode.h - 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
13 #ifndef LLVM_CLANG_TOOLING_TRANSFORMER_SOURCE_CODE_H
14 #define LLVM_CLANG_TOOLING_TRANSFORMER_SOURCE_CODE_H
15
16 #include "clang/AST/ASTContext.h"
17 #include "clang/Basic/SourceLocation.h"
18 #include "clang/Basic/TokenKinds.h"
19
20 namespace clang {
21 namespace tooling {
22
23 /// Extends \p Range to include the token \p Next, if it immediately follows the
24 /// end of the range. Otherwise, returns \p Range unchanged.
25 CharSourceRange maybeExtendRange(CharSourceRange Range, tok::TokenKind Next,
26 ASTContext &Context);
27
28 /// Returns the source range spanning the node, extended to include \p Next, if
29 /// it immediately follows \p Node. Otherwise, returns the normal range of \p
30 /// Node. See comments on `getExtendedText()` for examples.
31 template <typename T>
32 CharSourceRange getExtendedRange(const T &Node, tok::TokenKind Next,
33 ASTContext &Context) {
34 return maybeExtendRange(CharSourceRange::getTokenRange(Node.getSourceRange()),
35 Next, Context);
36 }
37
38 /// Returns the source-code text in the specified range.
39 StringRef getText(CharSourceRange Range, const ASTContext &Context);
40
41 /// Returns the source-code text corresponding to \p Node.
42 template <typename T>
43 StringRef getText(const T &Node, const ASTContext &Context) {
44 return getText(CharSourceRange::getTokenRange(Node.getSourceRange()),
45 Context);
46 }
47
48 /// Returns the source text of the node, extended to include \p Next, if it
49 /// immediately follows the node. Otherwise, returns the text of just \p Node.
50 ///
51 /// For example, given statements S1 and S2 below:
52 /// \code
53 /// {
54 /// // S1:
55 /// if (!x) return foo();
56 /// // S2:
57 /// if (!x) { return 3; }
58 /// }
59 /// \endcode
60 /// then
61 /// \code
62 /// getText(S1, Context) = "if (!x) return foo()"
63 /// getExtendedText(S1, tok::TokenKind::semi, Context)
64 /// = "if (!x) return foo();"
65 /// getExtendedText(*S1.getThen(), tok::TokenKind::semi, Context)
66 /// = "return foo();"
67 /// getExtendedText(*S2.getThen(), tok::TokenKind::semi, Context)
68 /// = getText(S2, Context) = "{ return 3; }"
69 /// \endcode
70 template <typename T>
71 StringRef getExtendedText(const T &Node, tok::TokenKind Next,
72 ASTContext &Context) {
73 return getText(getExtendedRange(Node, Next, Context), Context);
74 }
75
76 // Attempts to resolve the given range to one that can be edited by a rewrite;
77 // generally, one that starts and ends within a particular file. It supports
78 // a limited set of cases involving source locations in macro expansions.
79 llvm::Optional<CharSourceRange>
80 getRangeForEdit(const CharSourceRange &EditRange, const SourceManager &SM,
81 const LangOptions &LangOpts);
82
83 inline llvm::Optional<CharSourceRange>
84 getRangeForEdit(const CharSourceRange &EditRange, const ASTContext &Context) {
85 return getRangeForEdit(EditRange, Context.getSourceManager(),
86 Context.getLangOpts());
87 }
88 } // namespace tooling
89 } // namespace clang
90 #endif // LLVM_CLANG_TOOLING_TRANSFORMER_SOURCE_CODE_H
ASTContext.h
Defines the clang::ASTContext interface.
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
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::tooling::Range
A source range independent of the SourceManager.
Definition: Replacement.h:44
clang::CharSourceRange
Represents a character-granular source range.
Definition: SourceLocation.h:250
SM
const SourceManager & SM
Definition: Format.cpp:1685
clang::tooling::getExtendedText
StringRef getExtendedText(const T &Node, tok::TokenKind Next, ASTContext &Context)
Returns the source text of the node, extended to include Next, if it immediately follows the node...
Definition: SourceCode.h:71
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::tok::TokenKind
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:24
clang::tooling::getExtendedRange
CharSourceRange getExtendedRange(const T &Node, tok::TokenKind Next, ASTContext &Context)
Returns the source range spanning the node, extended to include Next, if it immediately follows Node...
Definition: SourceCode.h:32
Node
ast_type_traits::DynTypedNode Node
Definition: ASTMatchFinder.cpp:60
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
TokenKinds.h
Defines the clang::TokenKind enum and support functions.
clang::ASTContext::getSourceManager
SourceManager & getSourceManager()
Definition: ASTContext.h:679
SourceLocation.h
Defines the clang::SourceLocation class and associated facilities.
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