11 #include "llvm/Support/Error.h" 13 using namespace clang;
16 using ProcessTokensFn = llvm::function_ref<void(llvm::ArrayRef<syntax::Token>,
19 void enumerateTokenSpans(
const syntax::Tree *Root, ProcessTokensFn Callback) {
21 Enumerator(ProcessTokensFn Callback)
22 : SpanBegin(nullptr), SpanEnd(nullptr), SpanIsOriginal(
false),
29 Callback(llvm::makeArrayRef(SpanBegin, SpanEnd), SpanIsOriginal);
34 if (
auto *T = dyn_cast<syntax::Tree>(N)) {
35 for (
auto *
C = T->firstChild();
C !=
nullptr;
C =
C->nextSibling())
40 auto *L = cast<syntax::Leaf>(N);
41 if (SpanEnd == L->token() && SpanIsOriginal == L->isOriginal()) {
48 Callback(llvm::makeArrayRef(SpanBegin, SpanEnd), SpanIsOriginal);
50 SpanBegin = L->token();
51 SpanEnd = SpanBegin + 1;
52 SpanIsOriginal = L->isOriginal();
55 const syntax::Token *SpanBegin;
56 const syntax::Token *SpanEnd;
58 ProcessTokensFn Callback;
61 return Enumerator(Callback).run(Root);
64 syntax::FileRange rangeOfExpanded(
const syntax::Arena &A,
66 auto &Buffer = A.tokenBuffer();
67 auto &
SM = A.sourceManager();
70 assert(Buffer.expandedTokens().begin() <= Expanded.begin());
71 assert(Expanded.end() < Buffer.expandedTokens().end());
75 return syntax::FileRange(
78 auto Spelled = Buffer.spelledForExpanded(Expanded);
79 assert(Spelled &&
"could not find spelled tokens for expanded");
92 std::string Replacement;
94 if (ReplacedRange.empty() && Replacement.empty())
97 SM, rangeOfExpanded(A, ReplacedRange).toCharRange(
SM), Replacement)));
101 const syntax::Token *NextOriginal = Buffer.expandedTokens().begin();
109 assert(NextOriginal <= Tokens.begin());
111 if (NextOriginal != Tokens.begin()) {
118 NextOriginal = Tokens.end();
123 NextOriginal, Buffer.expandedTokens().drop_back().end()));
A token coming directly from a file or from a macro invocation.
A root node for a translation unit. Parent is always null.
SourceLocation getExpansionLoc(SourceLocation Loc) const
Given a SourceLocation object Loc, return the expansion location referenced by the ID...
const SourceManager & sourceManager() const
tooling::Replacements computeReplacements(const Arena &A, const syntax::TranslationUnit &TU)
Computes textual replacements required to mimic the tree modifications made to the syntax tree...
const TokenBuffer & tokenBuffer() const
A memory arena for syntax trees.
ast_type_traits::DynTypedNode Node
Dataflow Directional Tag Classes.
static void emitReplacement(Sema &S, SourceLocation Loc, SourceRange Range, unsigned AbsKind, QualType ArgType)
FileRange range(const SourceManager &SM) const
Gets a range of this token.