11 #include "llvm/ADT/ArrayRef.h" 12 #include "llvm/ADT/STLExtras.h" 13 #include "llvm/Support/Casting.h" 15 using namespace clang;
19 : SourceMgr(SourceMgr), LangOpts(LangOpts), Tokens(
std::move(Tokens)) {}
25 std::pair<FileID, llvm::ArrayRef<syntax::Token>>
28 auto It = ExtraTokens.try_emplace(FID,
tokenize(FID, SourceMgr, LangOpts));
29 assert(It.second &&
"duplicate FileID");
30 return {FID, It.first->second};
34 assert(Tok !=
nullptr);
42 :
Parent(nullptr), NextSibling(nullptr), Kind(static_cast<unsigned>(Kind)),
47 void syntax::Tree::prependChildLowLevel(
Node *Child,
NodeRole Role) {
48 assert(Child->Parent ==
nullptr);
49 assert(Child->NextSibling ==
nullptr);
54 Child->NextSibling = this->FirstChild;
55 Child->Role =
static_cast<unsigned>(Role);
56 this->FirstChild = Child;
62 if (
auto *T = dyn_cast<syntax::Tree>(N)) {
63 for (
auto *C = T->firstChild(); C; C = C->nextSibling())
70 assert(!Tokens.empty());
72 for (
const auto &T : Tokens) {
86 static void dumpTree(llvm::raw_ostream &OS,
const syntax::Node *N,
93 OS << static_cast<int>(N->
role()) <<
": ";
95 if (
auto *L = llvm::dyn_cast<syntax::Leaf>(N)) {
101 auto *T = llvm::cast<syntax::Tree>(N);
102 OS << T->kind() <<
"\n";
104 for (
auto It = T->firstChild(); It !=
nullptr; It = It->nextSibling()) {
105 for (
bool Filled : IndentMask) {
111 if (!It->nextSibling()) {
113 IndentMask.push_back(
false);
116 IndentMask.push_back(
true);
118 dumpTree(OS, It, A, IndentMask);
119 IndentMask.pop_back();
126 llvm::raw_string_ostream OS(Str);
127 dumpTree(OS,
this, A, {});
128 return std::move(OS.str());
133 llvm::raw_string_ostream OS(Storage);
144 for (
auto *C = FirstChild; C; C = C->nextSibling()) {
std::pair< FileID, llvm::ArrayRef< syntax::Token > > lexBuffer(std::unique_ptr< llvm::MemoryBuffer > Buffer)
Add Buffer to the underlying source manager, tokenize it and store the resulting tokens.
std::string dump(const Arena &A) const
Dumps the structure of a subtree. For debugging and testing purposes.
A token coming directly from a file or from a macro invocation.
static bool classof(const Node *N)
static bool classof(const Node *N)
Node(NodeKind Kind)
Newly created nodes are detached from a tree, parent and sibling links are set when the node is added...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
const SourceManager & sourceManager() const
const TokenBuffer & tokenBuffer() const
A memory arena for syntax trees.
FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, int LoadedID=0, unsigned LoadedOffset=0)
Create a new FileID that represents the specified file being #included from the specified IncludePosi...
NodeKind
A kind of a syntax node, used for implementing casts.
NodeRole
A relation between a parent and child node. Used for implementing accessors.
Dataflow Directional Tag Classes.
Arena(SourceManager &SourceMgr, const LangOptions &LangOpts, TokenBuffer Tokens)
A leaf node points to a single token inside the expanded token stream.
std::vector< syntax::Token > tokenize(FileID FID, const SourceManager &SM, const LangOptions &LO)
Lex the text buffer, corresponding to FID, in raw mode and record the resulting spelled tokens...
Defines the clang::TokenKind enum and support functions.
Leaf(const syntax::Token *T)
std::string dumpTokens(const Arena &A) const
Dumps the tokens forming this subtree.
A list of tokens obtained by preprocessing a text buffer and operations to map between the expanded a...
This class handles loading and caching of source files into memory.
syntax::Node * findChild(NodeRole R)
Find the first node with a corresponding role.