xref: /llvm-project/llvm/lib/TableGen/Parser.cpp (revision e865fa75308aa8dc103ed2ac559e678b5531fa30)
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 "RecordContext.h"
11 #include "TGParser.h"
12 #include "llvm/Support/MemoryBuffer.h"
13 #include "llvm/TableGen/Error.h"
14 #include "llvm/TableGen/Record.h"
15 
16 using namespace llvm;
17 
18 bool llvm::TableGenParseFile(std::unique_ptr<MemoryBuffer> Buffer,
19                              std::vector<std::string> IncludeDirs,
20                              TableGenParserFn ParserFn) {
21   RecordKeeper Records;
22   Records.saveInputFilename(Buffer->getBufferIdentifier().str());
23 
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   detail::resetTablegenRecordContext();
36   SrcMgr = SourceMgr();
37   return false;
38 }
39