xref: /llvm-project/llvm/lib/TableGen/Parser.cpp (revision 2ac3cd20cacdf37135eeb64ad2b7baefb9769e99)
1 //===- Parser.cpp - Top-Level TableGen Parser implementation --------------===//
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 #include "llvm/TableGen/Parser.h"
10 #include "TGParser.h"
11 #include "llvm/Support/MemoryBuffer.h"
12 #include "llvm/TableGen/Error.h"
13 #include "llvm/TableGen/Record.h"
14 
15 using namespace llvm;
16 
17 bool llvm::TableGenParseFile(std::unique_ptr<MemoryBuffer> Buffer,
18                              std::vector<std::string> IncludeDirs,
19                              TableGenParserFn ParserFn) {
20   RecordKeeper Records;
21   Records.saveInputFilename(Buffer->getBufferIdentifier().str());
22 
23   SrcMgr = SourceMgr();
24   SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
25   SrcMgr.setIncludeDirs(IncludeDirs);
26   TGParser Parser(SrcMgr, /*Macros=*/None, Records);
27   if (Parser.ParseFile())
28     return true;
29 
30   // Invoke the provided handler function.
31   if (ParserFn(Records))
32     return true;
33 
34   // After parsing, reset the tablegen data.
35   SrcMgr = SourceMgr();
36   return false;
37 }
38