Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Tooling/Transformer/SourceCode.h
Line
Count
Source
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
10
                                 ASTContext &Context) {
34
10
  return maybeExtendRange(CharSourceRange::getTokenRange(Node.getSourceRange()),
35
10
                          Next, Context);
36
10
}
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
49
StringRef getText(const T &Node, const ASTContext &Context) {
44
49
  return getText(CharSourceRange::getTokenRange(Node.getSourceRange()),
45
49
                 Context);
46
49
}
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
/// Determines whether \p Range is one that can be edited by a rewrite;
77
/// generally, one that starts and ends within a particular file.
78
llvm::Error validateEditRange(const CharSourceRange &Range,
79
                              const SourceManager &SM);
80
81
/// Attempts to resolve the given range to one that can be edited by a rewrite;
82
/// generally, one that starts and ends within a particular file. It supports a
83
/// limited set of cases involving source locations in macro expansions. If a
84
/// value is returned, it satisfies \c validateEditRange.
85
llvm::Optional<CharSourceRange>
86
getRangeForEdit(const CharSourceRange &EditRange, const SourceManager &SM,
87
                const LangOptions &LangOpts);
88
inline llvm::Optional<CharSourceRange>
89
49
getRangeForEdit(const CharSourceRange &EditRange, const ASTContext &Context) {
90
49
  return getRangeForEdit(EditRange, Context.getSourceManager(),
91
49
                         Context.getLangOpts());
92
49
}
93
} // namespace tooling
94
} // namespace clang
95
#endif // LLVM_CLANG_TOOLING_TRANSFORMER_SOURCE_CODE_H